-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.fs
362 lines (312 loc) · 7.87 KB
/
runner.fs
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
17 ficl-vocabulary runner-voc
also runner-voc definitions
0 DEFINE-SFX jump.wav
1 DEFINE-SFX stomp.wav
2 DEFINE-SFX coin.wav
3 DEFINE-SFX death.wav
: solid? ( tile# -- f ) 32 >= ;
: intersect? ( x0 y0 x1 y1 -- f )
ROT ( y1 y0 )
2DUP < IF SWAP THEN
- 0 8 WITHIN ( x0 x1 f )
-ROT ( f x0 x1 )
2DUP < IF SWAP THEN
- 0 8 WITHIN ( f f )
AND
;
0.1e FCONSTANT gravity
0 VALUE tick
0 VALUE offx
0 VALUE offy
0 VALUE coins
0 VALUE score
0 VALUE player-state
0 CONSTANT state-playing
1 CONSTANT state-dying
VARIABLE x
VARIABLE y
VARIABLE dx
VARIABLE dy
VARIABLE grounded?
VARIABLE jump-velocity
VARIABLE facing-left
\ --------------------------------------------------
\ entities
\ --------------------------------------------------
8 CELLS CONSTANT /entity
CREATE entities 256 /entity * ALLOT
0 VALUE #entities
1 CONSTANT type-enemy
2 CONSTANT type-coin
: active@ ( entity# -- f ) /entity * entities + @ ;
: active! ( f entity# -- ) /entity * entities + ! ;
: x@ ( entity# -- f ) /entity * entities + 1 CELLS+ F@ ;
: x! ( f entity# -- ) /entity * entities + 1 CELLS+ F! ;
: y@ ( entity# -- f ) /entity * entities + 2 CELLS+ F@ ;
: y! ( f entity# -- ) /entity * entities + 2 CELLS+ F! ;
: dx@ ( entity# -- f ) /entity * entities + 3 CELLS+ F@ ;
: dx! ( f entity# -- ) /entity * entities + 3 CELLS+ F! ;
: dy@ ( entity# -- f ) /entity * entities + 4 CELLS+ F@ ;
: dy! ( f entity# -- ) /entity * entities + 4 CELLS+ F! ;
: faceleft@ ( entity# -- f ) /entity * entities + 5 CELLS+ @ ;
: faceleft! ( f entity# -- ) /entity * entities + 5 CELLS+ ! ;
: frame@ ( entity# -- f ) /entity * entities + 6 CELLS+ @ ;
: frame! ( f entity# -- ) /entity * entities + 6 CELLS+ ! ;
: grounded@ ( entity# -- f ) /entity * entities + 7 CELLS+ @ ;
: grounded! ( f entity# -- ) /entity * entities + 7 CELLS+ ! ;
\ --------------------------------------------------
\ enemies
\ --------------------------------------------------
: kill-player ( -- )
3 sfx
state-dying TO player-state
dy F@ jump-velocity F@ F- dy F!
;
: walk-enemy ( entity# -- ) >R 0.3e R@ faceleft@ IF FNEGATE THEN R@ x@ F+ R> x! ;
: accumulate-gravity-enemy ( entity# -- ) { e } gravity e dy@ F0> IF 2e F* THEN e dy@ F+ e dy! ;
: fall-enemy ( entity# -- ) { e } e dy@ e y@ F+ e y! ;
: destination-down-enemy ( entity# -- tile )
{ e }
FALSE e grounded!
e x@ F>S 4 + 8 /
e y@ F>S 8 + 8 /
M@
;
: ?hit-floor-enemy ( entity# -- )
{ e }
e dy@ 0e F>= e destination-down-enemy solid? AND
IF
e y@ 8e F/ F>S 8 * S>F e y!
0e e dy!
TRUE e grounded!
THEN
;
: ?bounce-enemy ( entity# -- )
{ e }
e faceleft@ IF
e x@ F>S 8 / e y@ F>S 8 / M2@ 1 = IF FALSE e faceleft! THEN
ELSE
e x@ F>S 8 / 1+ e y@ F>S 8 / M2@ 1 = IF TRUE e faceleft! THEN
THEN
;
: ?collide-enemy ( entity# -- )
{ e }
x F@ F>S y F@ F>S
e x@ F>S e y@ F>S
intersect? IF
dy F@ 0e F> IF
FALSE e active!
-3.1e dy F!
score 100 + TO score
1 sfx
ELSE
kill-player
THEN
THEN
;
: ?collide-coin ( entity# -- )
{ e }
x F@ F>S y F@ F>S
e x@ F>S e y@ F>S
intersect? IF
FALSE e active!
coins 1+ TO coins
2 sfx
THEN
;
: update-entity ( entity# -- )
{ e }
e active@ CASE
type-enemy OF
e ?bounce-enemy
e walk-enemy
e accumulate-gravity-enemy
e fall-enemy
e ?hit-floor-enemy
e ?collide-enemy
e frame@ 1+ 4 8 * MOD e frame!
ENDOF
type-coin OF
e ?collide-coin
ENDOF
ENDCASE
;
: ?update-enemies ( -- ) #entities 0 ?DO I update-entity LOOP ;
: draw-enemies ( -- )
#entities 0 ?DO
I active@ IF
0 colorkey !
I faceleft@ NOT IF 1 ELSE 0 THEN flip !
I x@ F>S offx - I y@ F>S offy -
I active@ CASE
type-enemy OF I frame@ 8 / 24 + spr ENDOF
type-coin OF 20 spr ENDOF
ENDCASE
0 flip !
-1 colorkey !
THEN
LOOP
;
\ --------------------------------------------------
\ player movement
\ --------------------------------------------------
\ moving sideways
: walk ( -- )
0e dx F!
SCANCODE_A pressed? IF tick 1+ 32 MOD TO tick -1.3e dx F! TRUE facing-left ! THEN
SCANCODE_D pressed? IF tick 1+ 32 MOD TO tick 1.3e dx F! FALSE facing-left ! THEN
;
: destination-h ( -- )
x F@ F>S dx F@ F0> IF 7 ELSE 0 THEN + 8 /
y F@ F>S 7 + 8 /
m@
;
VARIABLE startx
: move ( -- ) x F@ startx F! dx F@ x F+! ;
: ?pushback ( -- ) destination-h solid? IF startx F@ x F! THEN ;
\ moving downwards (gravity doubles when falling, for feeling)
: accumulate-gravity ( -- ) gravity dy F@ F0> IF 2e F* THEN dy F+! ;
: fall ( -- ) dy F@ y F+! ;
: destination-down ( -- )
\ FALSE grounded? !
grounded? @ 1- 0 MAX grounded? !
x F@ F>S 4 + 8 /
y F@ F>S 8 + 8 /
m@
;
: ?hit-floor ( -- )
dy F@ 0e F>= destination-down solid? AND
IF
y F@ 8e F/ F>S 8 * S>F y F!
0e dy F!
12 grounded? !
THEN
;
\ moving upwards
: ?jump ( -- )
SCANCODE_SPACE just-pressed? grounded? @ AND
IF 0 grounded? ! dy F@ jump-velocity F@ F- dy F! 0 sfx THEN
;
: destination-up ( -- )
x F@ F>S 4 + 8 /
y F@ F>S 8 /
m@
;
: ?hit-ceiling ( -- )
dy F@ 0e F<= destination-up solid? AND
IF
y F@ 8e F+ 8e F/ F>S 8 * S>F y F!
0e dy F!
THEN
;
\ --------------------------------------------------
\ initialization
\ --------------------------------------------------
: init-player ( -- )
4e 8e F* x F!
8e 8e F* y F!
0e dx F!
0e dy F!
0 grounded? !
3.1e jump-velocity F!
0 TO coins
0 TO score
;
\ --------------------------------------------------
\ game stuff
\ --------------------------------------------------
: ?exit ( -- ) SCANCODE_Q pressed? IF retro-40 THEN ;
: <init> ( -- )
s" runner.spr" load-sprites
s" runner.map" load-map
0 TO #entities
state-playing TO player-state
init-player
;
: update-playing
?jump
walk
move
?pushback
accumulate-gravity
fall
?hit-floor
?hit-ceiling
?exit
?update-enemies
;
: update-dying
\ without updating the camera or reacting to input,
\ probably without moving the enemies as well:
\ move upwards,
\ THEN move downwards,
\ THEN when the player is offscreen, wait a bit and reset
accumulate-gravity
fall
dy F@ 10e F> IF <init> THEN
;
: <update> ( -- )
player-state CASE
state-playing OF update-playing ENDOF
state-dying OF update-dying ENDOF
ENDCASE
;
: spawn ( -- )
offy 8 / DUP 25 UNDER+ DO
offx 8 / DUP 33 UNDER+ DO
I J M2@ 2 = IF
#entities 256 < IF
type-enemy #entities active!
I 8 * S>F #entities x! J 8 * S>F #entities y!
0 I J M2!
#entities 1+ TO #entities
THEN
THEN
I J M2@ 20 = IF
#entities 256 < IF
type-coin #entities active!
I 8 * S>F #entities x! J 8 * S>F #entities y!
0 I J M2!
#entities 1+ TO #entities
THEN
THEN
LOOP
LOOP
;
: calculate-offsets ( -- )
\ determine horizontal offset
x F@ F>S W 2/ -
DUP 0< IF DROP 0 THEN
DUP W 8 * W - > IF DROP W 8 * W - THEN
TO offx
\ determine vertical offset ( you may want to place the line other than in the middle )
y F@ F>S H 2/ -
DUP 0< IF DROP 0 THEN
DUP H 8 * H - > IF DROP H 8 * H - THEN
TO offy
;
: draw-map ( -- ) offx 8 / offy 8 / 33 25 offx 8 MOD NEGATE offy 8 MOD NEGATE map* ;
: draw-player ( -- )
0 colorkey !
facing-left @ IF 1 ELSE 0 THEN flip !
x F@ F>S offx - y F@ F>S offy - tick 8 / 8 + spr
0 flip !
-1 colorkey !
0 0 at-xy s" COINS: " ?PUTS coins .
;
: <draw> ( -- )
0 cls
\ apply offset
player-state state-playing = IF calculate-offsets THEN
draw-map
spawn
draw-player
draw-enemies
;
\ --------------------------------------------------
\ install the software
\ --------------------------------------------------
PREVIOUS DEFINITIONS
ALSO runner-voc
INSTALL runner
PREVIOUS