-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBullet.elm
92 lines (57 loc) · 1.9 KB
/
Bullet.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module Bullet exposing (Bullet, tickBullets, bulletViews, initBullet)
import Mover exposing (Mover, tickMover, moverView)
-- Bullet is a module that represents the many bullets flying
-- around the game box. These can be fired by the player or at
-- the player. Bullets are tracked in a giant list and updated
-- via a List.filterMap.
import Time exposing (Time)
import Html exposing (Html)
import Svg exposing (g)
-- Bullet is a Mover that also has a "friendly" flag.
-- If a bullet is "friendly", it won't hit you.
type alias Bullet =
Mover { friendly : Bool }
initBullet : Bullet
initBullet =
{ x = 0
, y = 0
, d = 0
, s = 20
, ts = 20
, acc = 0
, turn = 0
, friendly = True
}
-- Take a list of bullets and "tick" them (make them move
-- forward, etc). If a bullet is out of bounds, we remove it
-- from the list of bullets.
tickBullets : Time -> List Bullet -> List Bullet
tickBullets diff bullets =
List.filterMap (tickBullet diff) bullets
-- Ticks a single bullet (moves it forward, etc). Returns
-- Nothing if we're out of bounds.
tickBullet : Time -> Bullet -> Maybe Bullet
tickBullet diff bullet =
let
b =
tickMover diff bullet -100 -100 1100 1100
in
if b |> isInBounds then
Just b
else
Nothing
-- Just checks if a bullet is in bounds. I should probably
-- not hardcode these values.
isInBounds : Bullet -> Bool
isInBounds b =
b.x >= -100 && b.y >= -100 && b.x < 1100 && b.y < 1100
-- Renders all bullets from a list of bullets.
-- `g` is an svg "group". We're grouping all bullets in the SVG output.
-- Really just a convenience so I can return one SVG node.
bulletViews : List Bullet -> Html msg
bulletViews bullets =
g [] (List.map bulletView bullets)
-- Renders one bullet.
bulletView : Bullet -> Html msg
bulletView bullet =
moverView bullet "./assets/bullet.svg" ( 10, 10 )