Browse code

Added darena, contributed by chirrolafupa, thank you!

Andrew Alderwick authored on 11/05/2021 07:50:35
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,550 @@
1
+( darena.usm )
2
+( an open-ended game of rocks and sand )
3
+( contributed by and cc0 sejo 12021 )
4
+
5
+%DEBUG  { .Console/byte DEO #0a .Console/char DEO }
6
+%DEBUG2 { .Console/short DEO2 #0a .Console/char DEO }
7
+
8
+( parameters )
9
+%nrocks { #1f }
10
+%nrocks-1 { #1e }
11
+%nrocks_mask { #1f }
12
+%minposx { #0f }
13
+%minposy { #0f }
14
+%maxposx { #f1 }
15
+%maxposy { #f1 }
16
+%anispeedmask_normal { #03 }
17
+%anispeedmask_slow { #07 }
18
+
19
+%c_color_normal { #33 }
20
+%c_color_flipx { #73 }
21
+%index_norock { #ff }
22
+
23
+( output macros )
24
+%out_screen_x { LDA #00 SWP .Screen/x DEO2 } ( ;addr )
25
+%out_screen_y { LDA #00 SWP .Screen/y DEO2 } ( ;addr )
26
+
27
+( helper macros )
28
+%get_bit_n { SFT #01 AND }
29
+%get_nibble_h { #04 SFT #0f AND }
30
+%get_nibble_l { #0f AND }
31
+
32
+%is_bit_n_set { get_bit_n #01 EQU }
33
+
34
+%set_animate { #01 ;c_state LDA ORA ;c_state STA }
35
+%rst_animate { #00 ;c_state STA }
36
+
37
+( devices )
38
+
39
+|00 @System     [ &vector $2 &wst      $1 &rst    $1 &pad   $4 &r      $2 &g     $2 &b      $2 ]
40
+|10 @Console    [ &pad    $8 &char     $1 &byte   $1 &short $2 &string $2 ]
41
+|20 @Screen     [ &vector $2 &width    $2 &height $2 &pad   $2 &x      $2 &y      $2 &addr  $2 &color $1 ]
42
+|80 @Controller [ &vector $2 &button   $1 &key    $1 ]
43
+
44
+( variables )
45
+
46
+|0000
47
+
48
+@c_pos [ &x $1 &y $1 ] ( character position )
49
+@c_speed [ &x $1 &y $1 ] ( character speed )
50
+@c_color [ $1 ] ( character color )
51
+@c_sprite [ $2 ] ( character sprite addr )
52
+@c_state [ $1 ] ( high_nibble: animation pointer, bit0: is_animated )
53
+
54
+@f_count [ $1 ] ( frame counter )
55
+@ani_speedmask [ $1 ] ( animation speed mask )
56
+
57
+@r_speed_x [ $f ]
58
+@r_speed_y [ $f ]
59
+
60
+@tog [ &x $1 &y $1 &state $1 ] ( toggle station state )
61
+
62
+( program )
63
+
64
+|0100 @reset ( -> )
65
+	#f396 .System/r DEO2
66
+	#e263 .System/g DEO2
67
+	#9030 .System/b DEO2
68
+
69
+	;on_frame .Screen/vector DEO2
70
+
71
+	( init character )
72
+	#50 ;c_pos/x STA
73
+	#10 ;c_pos/y STA
74
+	#00 ;c_speed/x STA
75
+	#00 ;c_speed/y STA
76
+	c_color_normal ;c_color STA
77
+	;s_monitx_stepfront0 ;c_sprite STA2
78
+	rst_animate
79
+
80
+	anispeedmask_normal ;ani_speedmask STA
81
+
82
+	( init toggler )
83
+	#27 ;tog/x STA
84
+	#27 ;tog/y STA
85
+	#00 ;tog/state STA
86
+
87
+
88
+	( init background )
89
+	;init_bg JSR2
90
+BRK
91
+
92
+
93
+@on_frame ( -> )
94
+	;f_count LDA #01 ADD DUP ;f_count STA ( increase frame counter )
95
+	;ani_speedmask LDA ( mask with animation speed mask )
96
+	AND #00 EQU ,update_frame JNZ ( jump to update if it's time )
97
+BRK
98
+
99
+@update_frame
100
+	( check keyboard )
101
+	;check_keys JSR2
102
+
103
+	( animate character sprite )
104
+	;animate_c JSR2
105
+
106
+	( clear sprites )
107
+	;clear JSR2
108
+
109
+	( update character vars )
110
+	;update_c/run JSR2
111
+
112
+	( update rocks + stand )
113
+	;update_r/run JSR2
114
+
115
+	( draw )
116
+	;draw JSR2
117
+
118
+BRK
119
+
120
+@clear
121
+	( clear rocks )
122
+	;s_clear .Screen/addr DEO2
123
+
124
+	nrocks #00
125
+	&rocks_loop
126
+		DUP ( get rocks_x[i] )
127
+		;rocks_x ROT #00 SWP ADD2 out_screen_x
128
+
129
+		DUP ( get rocks_y[i] )
130
+		;rocks_y ROT #00 SWP ADD2 out_screen_y
131
+
132
+		#30 .Screen/color DEO
133
+
134
+		#01 ADD
135
+		DUP2
136
+		NEQ ,&rocks_loop JNZ
137
+	POP2
138
+
139
+	( clear character )
140
+	;clear_c JSR2
141
+JMP2r
142
+
143
+@draw
144
+	( draw toggler )
145
+
146
+	;tog/x out_screen_x
147
+	;tog/x out_screen_y
148
+	;s_stand .Screen/addr DEO2
149
+	#23 .Screen/color DEO
150
+
151
+	( draw rocks )
152
+	;s_bola .Screen/addr DEO2
153
+
154
+	nrocks #00
155
+
156
+	&rocks_loop
157
+		DUP ( get rocks_x[i] )
158
+		;rocks_x ROT #00 SWP ADD2 out_screen_x
159
+
160
+		DUP ( get rocks_y[i] )
161
+		;rocks_y ROT #00 SWP ADD2 out_screen_y
162
+
163
+		( DUP ( get color bitwise ) )
164
+		( ;r_color LDA SWP get_bit_n #31 ADD .Screen/color DEO )
165
+
166
+		DUP
167
+		;r_color ROT #00 SWP ADD2 LDA #31 ADD .Screen/color DEO
168
+
169
+		#01 ADD
170
+
171
+		DUP2
172
+		NEQ ,&rocks_loop JNZ
173
+	POP2
174
+
175
+	( draw character )
176
+	;draw_c JSR2
177
+JMP2r
178
+
179
+@check_keys
180
+	#00 ;c_speed/x STA
181
+	#00 ;c_speed/y STA
182
+
183
+	.Controller/button DEI #07 is_bit_n_set ,&der JNZ
184
+	.Controller/button DEI #06 is_bit_n_set ,&izq JNZ
185
+	.Controller/button DEI #05 is_bit_n_set ,&aba JNZ
186
+	.Controller/button DEI #04 is_bit_n_set ,&arr JNZ
187
+
188
+	rst_animate
189
+
190
+	JMP2r
191
+
192
+	&der
193
+		#01 ;c_speed/x STA
194
+		set_animate
195
+		c_color_normal ;c_color STA
196
+		;s_monitx_stepside0 ;c_sprite STA2
197
+	JMP2r
198
+
199
+	&izq
200
+		#ff ;c_speed/x STA
201
+		set_animate
202
+		c_color_flipx ;c_color STA
203
+		;s_monitx_stepside0 ;c_sprite STA2
204
+	JMP2r
205
+
206
+	&aba
207
+		#01 ;c_speed/y STA
208
+		set_animate
209
+		c_color_normal ;c_color STA
210
+		;s_monitx_stepfront0 ;c_sprite STA2
211
+	JMP2r
212
+
213
+	&arr
214
+		#ff ;c_speed/y STA
215
+		set_animate
216
+		c_color_normal ;c_color STA
217
+		;s_monitx_stepback0 ;c_sprite STA2
218
+	JMP2r
219
+
220
+	&end
221
+JMP2r
222
+
223
+( sub-routines )
224
+
225
+( in: sourcex, source y, index, rangex, rangey )
226
+( puts in the stack the index of rock collisioned with )
227
+@collision_rocks
228
+	&range_y $1
229
+	&range_x $1
230
+	&src_i $1
231
+	&src_x  $1
232
+	&src_y $1
233
+
234
+	&rock_x $1
235
+	&rock_y $1
236
+
237
+	&run
238
+		,&range_y STR
239
+		,&range_x STR
240
+		,&src_i STR
241
+		,&src_y STR
242
+		,&src_x STR
243
+
244
+		( check collision with rocks )
245
+		( nrocks #00 )
246
+		,&src_i LDR nrocks_mask AND DUP #01 ADD nrocks_mask AND
247
+
248
+		&rocks_loop
249
+			DUP ( get rocks_x[i] )
250
+			;rocks_x ROT #00 SWP ADD2 LDA ,&rock_x STR
251
+
252
+			DUP ( get rocks_y[i] )
253
+			;rocks_y ROT #00 SWP ADD2 LDA ,&rock_y STR
254
+
255
+			,&src_x LDR ,&rock_x LDR ,&range_x LDR SUB GTH ( if sx > rx - 8  )
256
+			,&src_x LDR ,&rock_x LDR ,&range_x LDR ADD LTH ( if sx < rx + 8  )
257
+			,&src_y LDR ,&rock_y LDR ,&range_y LDR SUB GTH ( if sy > ry - 8  )
258
+			,&src_y LDR ,&rock_y LDR ,&range_y LDR ADD LTH ( if sy < ry + 8  )
259
+			ADD ADD ADD #04 EQU ,&found JNZ
260
+
261
+			#01 ADD nrocks_mask AND
262
+			DUP2
263
+			NEQ ,&rocks_loop JNZ
264
+		POP2
265
+		#ff
266
+		JMP2r
267
+	&found
268
+		SWP POP ( remove loop limit )
269
+		DUP ;&src_i LDA NEQ ,&end JNZ ( check if result is the same as index )
270
+		POP #ff
271
+		JMP2r
272
+
273
+	&end
274
+
275
+JMP2r
276
+
277
+@update_c ( update character position )
278
+	&new_x $1
279
+	&new_y $1
280
+
281
+	&rock_i $1
282
+	&rock_x $1
283
+	&rock_y $1
284
+
285
+
286
+	&run
287
+		;c_speed/x LDA ;c_pos/x LDA ADD
288
+		,&new_x STR
289
+		;c_speed/y LDA ;c_pos/y LDA ADD
290
+		,&new_y STR
291
+
292
+		anispeedmask_normal ;ani_speedmask STA
293
+
294
+	&check_x
295
+		( check collision with borders )
296
+		,&new_x LDR minposx EQU ;&noup_x JNZ2
297
+		,&new_x LDR maxposx EQU ;&noup_x JNZ2
298
+
299
+
300
+		( check collision with rocks )
301
+		,&new_x LDR ,&new_y LDR index_norock #09 #06
302
+		;collision_rocks/run JSR2
303
+
304
+		( if it is colliding with rock, check further )
305
+		DUP #ff NEQ ,&check_x_collision JNZ
306
+		POP
307
+		,&update_x JMP
308
+
309
+	&check_x_collision
310
+		( DUP DEBUG )
311
+		( slow down and save rock index )
312
+		anispeedmask_slow ;ani_speedmask STA
313
+		,&rock_i STR
314
+
315
+		( check if rock collides with others )
316
+		;rocks_x #00 ,&rock_i LDR ADD2 LDA ,&rock_x STR
317
+		;rocks_y #00 ,&rock_i LDR ADD2 LDA ,&rock_y STR
318
+
319
+		,&rock_x LDR ,&rock_y LDR ,&rock_i LDR #09 #06
320
+		;collision_rocks/run JSR2
321
+
322
+		( DUP DEBUG )
323
+
324
+		( if it is colliding, then skip adding x )
325
+		DUP #ff NEQ ,&check_y JNZ
326
+		POP
327
+
328
+
329
+		( if not, check for borders )
330
+		;&rock_x LDA minposx EQU ;&noup_x JNZ2
331
+		;&rock_x LDA maxposx EQU ;&noup_x JNZ2
332
+
333
+		( move rock with same speed as c )
334
+		;&rock_x LDA ;c_speed/x LDA ADD
335
+		;rocks_x #00 ;&rock_i LDA ADD2
336
+		STA
337
+
338
+
339
+	&update_x
340
+		;&new_x LDA ;c_pos/x STA
341
+
342
+	,&check_y JMP
343
+
344
+	&noup_x
345
+
346
+	&check_y
347
+		( check collision with borders )
348
+		;&new_y LDA minposy EQU ;&noup_y JNZ2
349
+		;&new_y LDA maxposy EQU ;&noup_y JNZ2
350
+
351
+		( check collision with rocks )
352
+		;&new_x LDA ;&new_y LDA index_norock #06 #09
353
+		;collision_rocks/run JSR2
354
+
355
+		( if it is colliding with rock, check further )
356
+		DUP #ff NEQ ,&check_y_collision JNZ
357
+		POP
358
+		,&update_y JMP
359
+
360
+	&check_y_collision
361
+		( DUP DEBUG )
362
+		anispeedmask_slow ;ani_speedmask STA
363
+		;&rock_i STA
364
+
365
+		( check if rock collides with others )
366
+		;rocks_x #00 ;&rock_i LDA ADD2 LDA ;&rock_x STA
367
+		;rocks_y #00 ;&rock_i LDA ADD2 LDA ;&rock_y STA
368
+
369
+		;&rock_x LDA ;&rock_y LDA ;&rock_i LDA #06 #09
370
+		;collision_rocks/run JSR2
371
+
372
+		( DUP DEBUG )
373
+
374
+		( if it is colliding, then skip adding y )
375
+		DUP #ff NEQ ,&noup_y JNZ
376
+		POP
377
+
378
+		( if not, check for borders )
379
+		;&rock_y LDA minposx EQU ;&noup_y JNZ2
380
+		;&rock_y LDA maxposx EQU ;&noup_y JNZ2
381
+
382
+		( if not colliding, then move rock with same speed as c )
383
+		;&rock_y LDA ;c_speed/y LDA ADD
384
+		;rocks_y #00 ;&rock_i LDA ADD2
385
+		STA
386
+
387
+
388
+	&update_y
389
+		;&new_y LDA ;c_pos/y STA
390
+	JMP2r
391
+
392
+	&noup_y
393
+JMP2r
394
+
395
+@update_r
396
+	&rock_i $1
397
+
398
+	&run
399
+
400
+		( check collision with rocks )
401
+		;tog/x LDA ;tog/y LDA index_norock #02 #02
402
+		;collision_rocks/run JSR2
403
+
404
+		( if it is colliding with rock, check if it needs to change state )
405
+		DUP #ff NEQ ,&change_state JNZ
406
+
407
+		( DUP DEBUG )
408
+
409
+		( if there's no collision, reset toggler )
410
+		POP
411
+		#00 ;tog/state STA
412
+		JMP2r
413
+
414
+	&change_state
415
+		( DUP DEBUG )
416
+		,&rock_i STR
417
+		;tog/state LDA ,&done JNZ ( don't toggle if state is active )
418
+
419
+		;r_color #00 ,&rock_i LDR ADD2 DUP2 STH2
420
+		LDA #01 EOR STH2r STA
421
+		#01 ;tog/state STA
422
+	&done
423
+
424
+JMP2r
425
+
426
+@animate_c
427
+	( is bit0 -animate- on? )
428
+	;c_state LDA DUP #00 get_bit_n #01 NEQ ,&s_no_animate JNZ
429
+
430
+	( increment and save animation pointer )
431
+	&s_animate
432
+		DUP
433
+		get_nibble_h #01 ADD #03 AND #40 SFT
434
+		SWP get_nibble_l ORA
435
+		;c_state STA
436
+	JMP2r
437
+
438
+	&s_no_animate
439
+		get_nibble_h #0f AND ;c_state STA
440
+JMP2r
441
+
442
+@draw_c ( draw character  )
443
+	#00 ;c_state LDA get_nibble_h #08 MUL
444
+	;c_sprite LDA2 ADD2 .Screen/addr DEO2
445
+	;c_pos/x out_screen_x
446
+	;c_pos/y out_screen_y
447
+	;c_color LDA .Screen/color DEO
448
+JMP2r
449
+
450
+@clear_c ( clear character )
451
+	;s_clear .Screen/addr DEO2
452
+	;c_pos/x out_screen_x
453
+	;c_pos/y out_screen_y
454
+	#30 .Screen/color DEO
455
+JMP2r
456
+
457
+@init_bg
458
+	( init bg )
459
+	;s_border .Screen/addr DEO2
460
+
461
+	.Screen/height DEI2 #0000 STH2
462
+	&vertical0loop
463
+		DUP2
464
+		STH2r
465
+		DUP2 .Screen/y DEO2
466
+
467
+
468
+		.Screen/width DEI2 #0000 STH2
469
+		&horizontal0loop
470
+			DUP2
471
+			STH2r
472
+			DUP2 .Screen/x DEO2
473
+
474
+			#23 .Screen/color DEO
475
+
476
+			#0008 ADD2 DUP2 STH2
477
+			GTH2 ,&horizontal0loop JNZ
478
+
479
+		STH2r POP2 POP2
480
+
481
+
482
+		#0008 ADD2 DUP2 STH2
483
+		GTH2 ,&vertical0loop JNZ
484
+	STH2r
485
+	POP2 POP2
486
+
487
+	( arena )
488
+
489
+	;s_clear .Screen/addr DEO2
490
+
491
+	 #00 maxposy  #00 minposy STH2
492
+	&vertical0loop_clear
493
+		DUP2
494
+		STH2r
495
+		DUP2 .Screen/y DEO2
496
+
497
+
498
+		 #00 maxposx  #00 minposx  STH2
499
+		&horizontal0loop_clear
500
+			DUP2
501
+			STH2r
502
+			DUP2 .Screen/x DEO2
503
+
504
+			#20 .Screen/color DEO
505
+
506
+			#0008 ADD2 DUP2 STH2
507
+			GTH2 ,&horizontal0loop_clear JNZ
508
+
509
+		STH2r POP2 POP2
510
+
511
+		#0008 ADD2 DUP2 STH2 GTH2 ,&vertical0loop_clear JNZ
512
+	STH2r
513
+	POP2 POP2
514
+
515
+JMP2r
516
+
517
+( rocks )
518
+@rocks_x [ 25 30 42 50  67 90 98 e8  20 43 43 57  5a 7f bc a5
519
+           e5 dd a2 20  b7 9b 38 e8  33 43 63 b7  aa cf bc    ]
520
+@rocks_y [ 60 48 34 56  23 65 65 65  ba e9 24 22  72 91 22 c5
521
+           25 30 42 50  67 90 98 e8  20 43 43 57  5a 7f bc    ]
522
+@r_color [ 00 01 01 00  00 00 01 01  01 01 00 00  01 01 00 00
523
+           01 00 01 00  00 01 00 01  01 01 01 01  00 00 00    ]
524
+
525
+( sprites )
526
+
527
+@s_clear          [ 0000 0000 0000 0000 ]
528
+@s_border         [ 3288 7e83 780d e013 ]
529
+@s_bola           [ 3c4e 9ffd f962 3c00 ]
530
+@s_stand          [ 0000 0000 0024 7eff ]
531
+@s_stand_original [ 0000 0000 0000 3c7e ]
532
+
533
+@s_monitx      [ 3c7e 5a7f 1b3c 5a18 ]
534
+@s_monitx_back [ 3c7e 7efe d83c 5a18 ]
535
+
536
+@s_monitx_stepfront0 [ 3c7e 5a7f 1b3c 5a18 ]
537
+@s_monitx_stepfront1 [ 3c7e 5a7f 1b3c 5a10 ]
538
+@s_monitx_stepfront2 [ 3c7e 5a7f 1b3c 5a18 ]
539
+@s_monitx_stepfront3 [ 3c7e 5a7f 1b3c 5a08 ]
540
+
541
+@s_monitx_stepback0 [ 3c7e 7efe d83c 5a18 ]
542
+@s_monitx_stepback1 [ 3c7e 7efe d83c 5a10 ]
543
+@s_monitx_stepback2 [ 3c7e 7efe d83c 5a18 ]
544
+@s_monitx_stepback3 [ 3c7e 7efe d83c 5a08 ]
545
+
546
+@s_monitx_stepside0 [ 1c3c 7afc d81c 1818 ]
547
+@s_monitx_stepside1 [ 1c3c 7afc d81c 1828 ]
548
+@s_monitx_stepside2 [ 1c3c 7afc d81c 3810 ]
549
+@s_monitx_stepside3 [ 1c3c 7afc d81c 1814 ]
550
+