-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.elm
277 lines (244 loc) · 8.58 KB
/
Game.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
{- This module contains functions for dealing with events in the game -}
module Game where
import List
import Time
import Keyboard
import Signal
import Char
import Random
import GameConstants exposing (..)
{---------------------- input -----------------------------}
--clock
delta : Signal Time.Time
delta =
Signal.map Time.inSeconds (Time.fps 35)
--check user input every gameclock cycle
input : Signal Input
input =
Signal.sampleOn delta <|
Signal.map5 Input
(Signal.map .y Keyboard.arrows)
(Signal.map .x Keyboard.arrows)
Keyboard.space
(Keyboard.isDown (Char.toCode 'R'))
(Keyboard.enter)
{---------------------- UPSTATE -----------------------------}
--Check if game is active (projectile fired) or passive (waiting for user input) and update things accordingly
upstate : Input -> GameState -> GameState
upstate input gs =
case (findLoser 0 gs.tanks) of
Just losingPlayer ->
if (Debug.watch "restart2: " input.restart) then
initState
else
gameLost gs losingPlayer
Nothing ->
let
pro = gs.proj
tnks = List.map (\t -> groundTank t gs.terrain) gs.tanks
trn = gs.turn
scr = gs.screen
p = input.power
a = (Debug.watch "input.angle:" input.angle)
s = input.space
e = input.enter
in
if (scr == Menu) then
{gs | screen = (if e then Inactive else Menu)}
else if input.restart then
{initState | screen = Menu,
seed = (snd (xPair gs.seed)),
terrain = (getTerrain gs.seed terrainMaps),
tanks = (Debug.watch "newTanks" (moveTanks gs.tanks (fst(xPair gs.seed))))}
else if (scr == Active) then
let
newp = updateProjectile pro
in
if (hitGround newp gs.terrain) then
case (hitTank newp tnks 0) of
Nothing ->
let newPlayer = (changeTurn trn)
newVec = (getActiveTank newPlayer tnks).initVelocity
newProj = {position = (getActiveTank newPlayer tnks).position,
velocity = newVec,
acceleration = gravity,
damage = dmg }
in
{gs | proj = newProj,
turn = newPlayer,
screen = Inactive}
Just n ->
let newPlayer = (changeTurn trn)
newProj = {position = ((getActiveTank newPlayer tnks).position),
velocity = ((getActiveTank newPlayer tnks).initVelocity),
acceleration = gravity,
damage = dmg}
newTnks = damageTank newp.damage gs.tanks n
in
{gs | proj = newProj,
turn = newPlayer,
screen = Inactive,
tanks = newTnks}
else
{gs | proj = newp, tanks = tnks}
else
let
newScreen = if s then Active else Inactive
newPos = (getActiveTank trn tnks).position
newVec = translateInput p a pro.velocity
newProj = {position=newPos, velocity=newVec, acceleration=gravity, damage=dmg}
newTanks = changeInitVel trn newVec tnks
in
{gs | proj = newProj,
screen = newScreen,
tanks = newTanks}
{---------------------- input helpers -----------------------------}
--turn the user's key presses into an initial velocity
translateInput : Int -> Int -> Vector2D -> Vector2D
translateInput pow ang initVec =
case (pow, ang) of
(0,0) -> initVec
_ ->
let
initMag = (sqrt ((initVec.x)^2 + (initVec.y)^2))
initDeg = (atan2 ( initVec.y) ( initVec.x))
newMag =
case pow of
1 -> initMag + 0.1
(-1) -> initMag - 0.1
_ -> initMag
newAng =
case ang of
-1 -> (min 3.13 (initDeg + 0.01))
1 -> (max 0.01 (initDeg - 0.01))
_ -> initDeg
newX = (newMag*(cos newAng))
newY = (newMag*(sin newAng))
in
{x = newX,
y = newY}
{---------------------- projectile helpers -----------------------------}
--move a projectile 1 step based on current velocity and acceleration
updateProjectile : Projectile -> Projectile
updateProjectile p =
{ p | position = (addVector2D p.position p.velocity),
velocity = (addVector2D p.velocity p.acceleration)}
addVector2D : Vector2D -> Vector2D -> Vector2D
addVector2D a b =
{x = (a.x + b.x),
y = (a.y + b.y)}
{---------------------- collision helpers -----------------------------}
hitGround : Projectile -> List (Float, Float) -> Bool
hitGround p grs =
case grs of
(x1,y1)::(x2,y2)::grs' ->
let
pX = p.position.x
pY = p.position.y
in
if pX >= x1 && pX < x2 then
let
newY = ((y2-y1)/(x2-x1))*(pX-x1) + y1
in
(pY <= newY)
else
hitGround p ((x2, y2)::grs')
_ -> True
--check if any tank was hit. NOTE: tanks cannot be w/in hitRadius of each other, since this will only catch 1 tank at a time
hitTank : Projectile -> List Tank -> Int -> Maybe Int
hitTank p ts i =
case ts of
[]-> Nothing
hd::tl->
if (abs(hd.position.x - p.position.x) < hitRadius) then
let thing = Debug.watch "health of tank:" hd.health in
Just i
else
hitTank p tl (i + 1)
--reduce the health of a tank
damageTank : Int -> List Tank -> Int -> List Tank
damageTank dam ts tnk =
case (tnk, ts) of
(0, hd::tl) -> {hd | health = (hd.health - dam)}::tl
(_, hd::tl) -> hd::(damageTank dam tl (tnk - 1))
(_, []) -> Debug.crash "damageTank: tank does not exist"
--see if any tank is dead
findLoser : Int -> List Tank -> Maybe Int
findLoser n tnks =
case tnks of
[] -> Nothing
hd::tnks' -> if (hd.health <= 0) then (Just n)
else (findLoser (n + 1) tnks')
--if any tank is dead, indicate to view that it should display the gameover screen
gameLost: GameState -> Loser -> GameState
gameLost gs l =
{gs | loser = Just l}
{---------------------- tank helpers -----------------------------}
--place tanks at ground level
groundTank : Tank -> List (Float, Float) -> Tank
groundTank t grs =
case grs of
(x1,y1)::(x2,y2)::grs' ->
let
tankX = t.position.x
tankY = t.position.y
in
if tankX >= x1 && tankX < x2 then
let
newY = ((y2-y1)/(x2-x1))*(tankX-x1) + y1 + 10
in
{t | position = {x=tankX, y=newY}}
else
groundTank t ((x2, y2)::grs')
_ -> Debug.crash "tank off map"
--get the tank associated with a player #
getActiveTank : Int -> List Tank -> Tank
getActiveTank n tnks =
case tnks of
[] -> Debug.crash "getActiveTanks: given empty list"
hd::tnks' -> if (n == 0) then hd
else getActiveTank (n - 1) tnks'
--apply new initial velocity to a tank
changeInitVel : Int -> Vector2D -> List Tank -> List Tank
changeInitVel n v tnks =
case tnks of
[] -> Debug.crash "changeInitVel: given empty list"
hd::tnks' -> if (n == 0) then ({hd | initVelocity = v})::tnks'
else hd::(changeInitVel (n - 1) v tnks')
--change x location of tanks to new position
moveTanks : List Tank -> (Float, Float) -> List Tank
moveTanks ts (x1, x2) =
List.map2 (\t -> \newX -> {t|position = {x = newX, y = 100}}) ts [x1, x2]
{---------------------- misc. helpers -----------------------------}
--switch active player
changeTurn : Int -> Int
changeTurn n =
case n of
1 -> 0
_ -> 1
--get 2 random x values. Used to randomize tank position.
xPair : Random.Seed -> ((Float, Float), Random.Seed)
xPair seed1 =
let
gen1 = Random.float -500 -10
(x1, seed2) = Random.generate gen1 seed1
gen2 = Random.float 10 500
(x2, _) = Random.generate gen2 seed2
in
((x1, (Debug.watch "x2gen:" x2)), seed2)
--pick a random terrain from the list of availible maps
getTerrain : Random.Seed -> List (List (Float, Float)) -> List (Float, Float)
getTerrain s mps =
let
g = Random.int 1 (List.length mps)
i = fst (Random.generate g s)
foo l index =
case (l, index) of
((x::xs), _) ->
if index == 1 then
x
else
foo xs (index-1)
([], _) -> [(-1000, 0), (1000, 0)]
in
foo mps i