-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShip.elm
80 lines (55 loc) · 1.61 KB
/
Ship.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
module Ship exposing (Ship, tickShip, shipView, initShip)
import Mover exposing (Mover, tickMover, moverView)
-- The Ship is the item that the user controls.
-- It's a little rocket ship that blasts around
-- the constrained game box and fires bullets wildly.
import Time exposing (Time)
import Html exposing (Html)
-- Ship is a Mover that also has hp and a weapon cooldown cooldown.
type alias Ship =
Mover
{ hp : Int
, cooldown : Int
, cooldownMax : Int
, firing : Bool
}
initShip : Ship
initShip =
{ x = 500
, y = 500
, d = 0
, s = 2.0
, ts =
10.0
-- Top speed
, acc = 0.0
, turn = 0.0
, hp = 100
, cooldown = 0
, cooldownMax = 6
, firing = False
}
-- Moves/turns the ship and cools down the weapon every tick.
tickShip : Time -> Ship -> Ship
tickShip diff ship =
tickMover diff { ship | cooldown = (weaponCooldown ship) } 20 20 980 980
-- Cooldown a weapon from cooldownMax down to cooled 0.
weaponCooldown : Ship -> Int
weaponCooldown { cooldown, cooldownMax, firing } =
if cooldown == 0 && firing then
cooldownMax
else
clamp 0 cooldownMax (cooldown - 1)
-- Renders the ship (aka rocket) with the provided SVGs.
-- If the ship is accelerating, shows the ship rocket engines
-- burning. 40x40 px within the 1000x1000 game box.
shipView : Ship -> Html msg
shipView ship =
let
rocketImg =
if ship.acc > 0 then
"./assets/rocket-4-burn.png"
else
"./assets/rocket-4.png"
in
moverView ship rocketImg ( 40, 40 )