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
|
module Game.Entities (Entities, mkEntities, mkPlayer, mkEffect, append, updateAll, render) where
import Data.Foldable (traverse_)
import Data.IORef
import qualified Game.Controller as C
import qualified Game.Sprites as S
import qualified SDL
data Dir = DirRight | DirLeft deriving (Eq)
data Type = TypePlayer | TypeEffect
toSpriteSet :: Dir -> Int
toSpriteSet DirRight = 0
toSpriteSet DirLeft = 1
frameDelay :: Int
frameDelay = 6
jumpFrame :: Int
jumpFrame = 3
gravityOff :: Int
gravityOff = -1
gravityUp :: Int
gravityUp = 0
gravityDown :: Int
gravityDown = 14
jumpLimit :: Int
jumpLimit = gravityDown + 7
gravityTable :: [Int]
gravityTable = [0, 6, 4, 4, 2, 2, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 4]
data Entities = Entities S.SpriteSheet [Entity]
data Spawn = DustEffectSpawn Int Int
data Entity = Entity
{ typ :: Type,
x :: Int,
y :: Int,
delay :: Int,
frame :: Int,
frameLimit :: Int,
jumping :: Bool,
gravity :: Int,
dir :: Dir,
sprite :: S.Sprite,
update :: Entity -> IO Entity,
isBlocked :: Int -> Int -> Bool,
destroy :: Bool,
spawns :: [Spawn]
}
mkEntities :: S.SpriteSheet -> Entities
mkEntities sprites = Entities sprites []
append :: Entity -> Entities -> Entities
append e (Entities sprites entities) = Entities sprites (entities ++ [e])
processSpawn :: S.SpriteSheet -> Spawn -> IO Entity
processSpawn sprites (DustEffectSpawn x y) = mkEffect sprites x y "dust"
updateAll :: Entities -> IO Entities
updateAll (Entities sprites entities) = do
updated <- traverse (\e -> e.update e) entities
new <- traverse (processSpawn sprites) (concatMap (\e -> e.spawns) updated)
pure $ Entities sprites $ map (\e -> e {spawns = []}) (filter (\e -> not e.destroy) updated) ++ new
render :: SDL.Renderer -> Entities -> IO ()
render renderer (Entities _ entities) = do
traverse_ renderOne entities
where
renderOne :: Entity -> IO ()
renderOne e =
S.render renderer e.sprite e.x e.y set e.frame
where
set = toSpriteSet e.dir
mkEffect :: S.SpriteSheet -> Int -> Int -> String -> IO Entity
mkEffect sprites x y name = do
s <- S.get sprites name
pure $
Entity
{ typ = TypeEffect,
x = x,
y = y,
delay = frameDelay,
frame = 0,
frameLimit = 3,
jumping = False,
gravity = gravityOff,
dir = DirRight,
sprite = s,
update = pure . updateEffect,
isBlocked = \_ _ -> False,
destroy = False,
spawns = []
}
updateEffect :: Entity -> Entity
updateEffect e
| e.delay > 0 = e {delay = e.delay - 1}
| e.frame + 1 < e.frameLimit = e {delay = e.frameLimit, frame = e.frame + 1}
| otherwise = e {destroy = True}
mkPlayer :: S.SpriteSheet -> Int -> Int -> IORef C.Controls -> (Int -> Int -> Bool) -> IO Entity
mkPlayer sprites x y controls isBlocked' = do
s <- S.get sprites "player"
pure $
Entity
{ typ = TypePlayer,
x = x,
y = y,
delay = 0,
frame = 0,
frameLimit = 3,
jumping = False,
gravity = gravityOff,
dir = DirRight,
sprite = s,
update = updatePlayer controls,
isBlocked = isBlocked',
destroy = False,
spawns = []
}
updateFrame :: Bool -> Entity -> Entity
updateFrame updated e
| isGravityOn = e
| e.delay > 0 = e {delay = e.delay - 1}
| e.frame < e.frameLimit = e {delay = frameDelay, frame = if updated then e.frame + 1 else 0}
| otherwise = e {delay = frameDelay, frame = 0}
where
isGravityOn = e.gravity > gravityOff
updateHorizontal :: Bool -> Bool -> Entity -> Entity
updateHorizontal left right e
-- prevent pressing both directions (kyboard)
| left && right = e
-- change direction first
| left && e.dir /= DirLeft = e {dir = DirLeft, delay = 0}
| right && e.dir /= DirRight = e {dir = DirRight, delay = 0}
| left && isGoingDown = if e.isBlocked (e.x - 1) (e.y + 23) then e else e {x = e.x - 1}
| left && not isGoingDown = if e.isBlocked (e.x - 1) (e.y + 23) && e.isBlocked (e.x - 1) (e.y + 17) then e else e {x = e.x - 1}
| right && isGoingDown = if e.isBlocked (e.x + 17) (e.y + 23) then e else e {x = e.x + 1}
| right && not isGoingDown = if e.isBlocked (e.x + 17) (e.y + 23) && e.isBlocked (e.x + 17) (e.y + 17) then e else e {x = e.x + 1}
| otherwise = e
where
isGoingDown = e.gravity == gravityOff || e.gravity >= gravityDown
updateVertical :: Bool -> Bool -> Entity -> Entity
updateVertical jump down e
| not jump
|| e.jumping
-- make jumping easier with "Coyote time"
|| (e.gravity /= gravityOff && (e.gravity < gravityDown || e.gravity > jumpLimit)) =
e
| not down = e {jumping = True, gravity = gravityUp, frame = jumpFrame, spawns = e.spawns ++ [DustEffectSpawn e.x (e.y + 8)]}
-- go down a 8 pixel tall platform; not ideal to have these values hardcoded here
-- but to be fair, the player height/width is hardcoded as well
| down
&& not (e.isBlocked (e.x + 2) (e.y + 24 + 8))
&& not (e.isBlocked (e.x + 12) (e.y + 24 + 8)) =
e {gravity = gravityDown, frame = jumpFrame, y = e.y + 1}
| otherwise = e
applyGravity :: Int -> Entity -> Entity
applyGravity v e
| v == 0 = e
-- hit the floor
| isGoingDown
&& (e.isBlocked (e.x + 2) (e.y + 24) || e.isBlocked (e.x + 12) (e.y + 24))
&& not (e.isBlocked (e.x + 2) (e.y + 23))
&& not (e.isBlocked (e.x + 12) (e.y + 23)) =
e {jumping = False, gravity = gravityOff, delay = 0}
| otherwise = applyGravity (v - 1) e {y = e.y + change}
where
isGoingDown = e.gravity >= gravityDown
change = if isGoingDown then 1 else -1
updateGravity :: Entity -> Entity
updateGravity e
| current > gravityOff = applyGravity (gravityTable !! current) e {gravity = new}
| not (e.isBlocked (e.x + 2) (e.y + 24) || e.isBlocked (e.x + 12) (e.y + 24)) = e {gravity = gravityDown, frame = jumpFrame}
| otherwise = e
where
current = e.gravity
new = if current > gravityOff && current < length gravityTable - 1 then current + 1 else current
updatePlayer :: IORef C.Controls -> Entity -> IO Entity
updatePlayer controls e = do
ctl <- readIORef controls
pure $
updateGravity $
updateVertical ctl.a ctl.down $
updateHorizontal ctl.left ctl.right $
-- left or right, but not both (keyboard)
updateFrame ((ctl.left || ctl.right) && (ctl.left /= ctl.right)) e
|