-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpong.elm
202 lines (150 loc) · 5.05 KB
/
pong.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
import Engine (..)
import Engine.Material.Material (MaterialProperty)
import Math.Vector3 (vec3, Vec3)
import Engine.Shader.GouraudShader (gouraudShader)
import Time (..)
import Signal (..)
import Keyboard
import Window
import Array (fromList)
import Graphics.Element (Element)
-- INPUT
type alias Input = {
space : Bool,
paddle1 : Int,
paddle2 : Int,
delta : Time
}
delta : Signal Time
delta = inSeconds <~ fps 60
input : Signal Input
input = sampleOn delta <| Input <~ Keyboard.space
~ map .y Keyboard.wasd
~ map .y Keyboard.arrows
~ delta
-- MODEL
(gameWidth, gameHeight) = (600, 400)
(halfWidth, halfHeight) = (300, 200)
type alias Object a = { a |
x : Float,
y : Float,
vx : Float,
vy : Float
}
type alias Ball = Object {}
type alias Player = Object { score : Int }
type State = Play | Pause
type alias Game = {
state : State,
ball : Ball,
player1 : Player,
player2 : Player
}
player : Float -> Player
player x = { x = x, y = 0, vx = 0, vy = 0, score = 0 }
defaultGame : Game
defaultGame = {
state = Pause,
ball = { x = 0, y = 0, vx = 200, vy = 200 },
player1 = player (20 - halfWidth),
player2 = player (halfWidth - 20) }
-- UPDATE
near : Float -> Float -> Float -> Bool
near n c m = m >= n - c && m <= n + c
within : Ball -> Player -> Bool
within ball player =
(ball.x |> near player.x 8) && (ball.y |> near player.y 20)
stepV : Float -> Bool -> Bool -> Float
stepV v lowerCollision upperCollision =
if | lowerCollision -> abs v
| upperCollision -> 0 - abs v
| otherwise -> v
stepObj : Time -> Object a -> Object a
stepObj t ({x,y,vx,vy} as obj) =
{ obj | x <- x + vx * t,
y <- y + vy * t }
stepBall : Time -> Ball -> Player -> Player -> Ball
stepBall t ({x,y,vx,vy} as ball) player1 player2 =
if not (ball.x |> near 0 halfWidth)
then { ball | x <- 0, y <- 0}
else
let vx' = stepV vx (ball `within` player1) (ball `within` player2)
vy' = stepV vy (y < 7 - halfHeight) (y > halfHeight - 7)
in
stepObj t { ball | vx <- vx', vy <- vy' }
stepPlyr : Time -> Int -> Int -> Player -> Player
stepPlyr t dir points player =
let player' = stepObj t { player | vy <- toFloat dir * 200 }
y' = clamp (22 - halfHeight) (halfHeight - 22) player'.y
score' = player.score + points
in
{ player' | y <- y', score <- score' }
stepGame : Input -> Game -> Game
stepGame {space, paddle1, paddle2, delta}
({state, ball, player1, player2} as game) =
let score1 = if ball.x > halfWidth then 1 else 0
score2 = if ball.x < -halfWidth then 1 else 0
state' = if | space -> Play
| score1 /= score2 -> Pause
| otherwise -> state
ball' = if state == Pause then ball
else stepBall delta ball player1 player2
player1' = stepPlyr delta paddle1 score1 player1
player2' = stepPlyr delta paddle2 score2 player2
in
{ game | state <- state',
ball <- ball',
player1 <- player1,
player2 <- player2'}
gameState : Signal Game
gameState = foldp stepGame defaultGame input
-- VIEW
pongGreen = vec3 (60 / 255) (100 / 255) (60 / 255)
white = vec3 1 1 1
blue = vec3 0 0 1
red = vec3 1 0 0
gouraudMaterial : Vec3 -> Material
gouraudMaterial color = {
material | fragmentShader <- gouraudShader,
emissive <- MaterialProperty color 1.0,
ambient <- MaterialProperty white 0.4,
diffuse <- MaterialProperty white 0.4,
specular <- MaterialProperty white 0.5 }
displayObj : Object a -> Renderable -> Renderable
displayObj object renderable =
{renderable | position <- vec3 object.x object.y 0,
material <- gouraudMaterial blue }
ballShape : Float -> Renderable
ballShape radius =
{ sphere | scale <- vec3 radius radius radius,
material <- gouraudMaterial red }
background : Vec3 -> Float -> Float -> Renderable
background color width height =
{ cube | position <- vec3 0 0 1,
scale <- vec3 width height 0.5,
material <- gouraudMaterial color }
paddleShape : Float -> Float -> Renderable
paddleShape width height =
{ cube | scale <- vec3 width height 1 }
display : (Int, Int) -> Game -> Element
display (w,h) {state, ball, player1, player2} =
let gameCamera = { camera |
position <- vec3 0 0 -550,
aspectRatio <- gameWidth / gameHeight }
gameLight = { light |
position <- vec3 -3 5 -4 }
gameDimensions = { width = gameWidth, height = gameHeight }
gameViewport = { viewport |
dimensions <- gameDimensions }
gameScene = { scene |
objects <- fromList [
background pongGreen gameWidth gameHeight,
displayObj ball (ballShape 15),
displayObj player1 (paddleShape 10 40),
displayObj player2 (paddleShape 10 40)
],
camera <- gameCamera,
light <- gameLight,
viewport <- gameViewport }
in render gameScene
main = map2 display Window.dimensions gameState