-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurvy.elm
189 lines (164 loc) · 4.64 KB
/
curvy.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
import Player exposing (..)
import Text exposing (..)
import Outcome exposing (..)
import Color exposing (..)
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Keyboard
import Time exposing (..)
import Window
import Debug
-- MODEL
type alias Game =
{ players : List Player
, state : GameState
, rounds : Int
, currentRound : Int
}
type GameState = WaitingRound Float | PlayingRound | RoundOver Float Outcome | GameOver
type alias Keys = { x: Int, y: Int}
player1 : Player
player1 =
{ initPlayer |
name <- "Player1"
, color <- Color.blue
}
player2 : Player
player2 =
{ initPlayer |
name <- "Player2"
, position <- { x = 100
, y = 100
}
, color <- Color.red
}
game : Game
game =
{ players = [player1, player2]
, state = WaitingRound 0.0
, rounds = 3
, currentRound = 0
}
-- UPDATE
update : (Float, Keys, Keys) -> Game -> Game
update (dt, keys1, keys2) game =
case game.state of
PlayingRound ->
case outcome game.players of
Playing ->
game
|> updatePlayers dt keys1
WonBy player ->
game
|> nextRound
|> transition (RoundOver dt (WonBy player))
Draw ->
game
|> nextRound
|> transition (RoundOver dt Draw)
WaitingRound t ->
if t < 100 then
game
|> updatePlayers dt keys1
|> transition (WaitingRound (t+dt))
else
game
|> preparePlayers
|> transition PlayingRound
RoundOver t s ->
if game.currentRound > game.rounds then
game
|> transition GameOver
else
if t < 100 then
game
|> updatePlayers dt keys1
|> transition (RoundOver (t+dt) s)
else
game
|> resetPlayers
|> transition (WaitingRound dt)
GameOver ->
game
updatePlayers : Float -> Keys -> Game -> Game
updatePlayers dt keys game =
{ game |
players <- List.map (updatePlayer dt game.players keys) game.players
|> calculateScores
}
resetPlayers : Game -> Game
resetPlayers game =
{ game |
players <- List.map resetPlayer game.players
}
preparePlayers : Game -> Game
preparePlayers game =
{ game |
players <- List.map readyPlayer game.players
}
transition : GameState -> Game -> Game
transition state game =
{ game |
state <- state
}
nextRound : Game -> Game
nextRound game =
{ game |
currentRound <- game.currentRound + 1
}
-- VIEW
view : (Int, Int) -> Game -> Element
view (w',h') game =
collage w' h' <|
case game.state of
PlayingRound -> drawPlayingRound game w' h'
WaitingRound t -> drawWaitingRound game t w' h'
RoundOver t s -> drawRoundOver game t s w' h'
GameOver -> drawGameOver game w' h'
drawPlayingRound : Game -> Int -> Int -> List Form
drawPlayingRound game w' h' =
List.map drawPlayer game.players
drawWaitingRound : Game -> Float -> Int -> Int -> List Form
drawWaitingRound game t w' h' =
[ rect (toFloat w') (toFloat h')
|> filled black
|> alpha (0.4 - (t/250))
,
"Round " ++ (toString game.currentRound) ++ " starts in"
|> fromString
|> text
|> move (0, toFloat (h')/2.1)
,
toString ( floor ((100 - t)/30))
|> fromString
|> text
|> move (0, toFloat (h')/2.3)
] ++ List.map drawPlayer game.players
drawRoundOver : Game -> Float -> Outcome -> Int -> Int -> List Form
drawRoundOver game t s w' h' =
[ rect (toFloat w') (toFloat h')
|> filled black
|> alpha (0.0 + (t/250))
,
outcomeToString s
|> fromString
|> text
|> move (0, toFloat (h')/2.1)
] ++ List.map drawPlayer game.players
drawGameOver : Game -> Int -> Int -> List Form
drawGameOver game w' h' =
["GameOver"
|> fromString
|> text
|> move (0, toFloat (h')/2.1)
] ++ List.map drawPlayer game.players
-- SIGNALS
main : Signal Element
main =
Signal.map2 view Window.dimensions (Signal.foldp update game input)
input : Signal (Float, Keys, Keys)
input =
let
delta = Signal.map (\t -> t/20) (fps 60)
in
Signal.sampleOn delta (Signal.map3 (,,) delta Keyboard.arrows Keyboard.wasd)