-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.z80
More file actions
765 lines (647 loc) · 12.1 KB
/
game.z80
File metadata and controls
765 lines (647 loc) · 12.1 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
module game
PHASE_FACE_STAND equ 0
PHASE_FACE_WALK equ 1
PHASE_BACK_STAND equ 2
PHASE_BACK_WALK equ 3
PHASE_SIDE_STAND equ 4
PHASE_SIDE_WALK_1 equ 5
PHASE_SIDE_WALK_2 equ 6
DIR_FACE equ 0b000
DIR_BACK equ 0b010
DIR_RIGHT equ 0b001
DIR_LEFT equ 0b011
hero_behaviour object_behaviour 0, 0, 0, map.draw_object
item_behaviour object_behaviour 0, item_turn, item_interact, draw_with_color
door_behaviour object_behaviour door_tick, door_turn, door_interact, draw_with_color
vdoor_behaviour object_behaviour door_tick, vdoor_turn, door_interact, map.draw_static_object
lift_behaviour object_behaviour door_tick, lift_turn, door_interact, draw_with_color
hero object 1, 1, 0, res.girl, 0, 0, res.girl, hero_behaviour
level_objects
dw hero
.32 dw 0
total_steps dw 0
adjacent_object dw 0
; Game object behaviours
interact:
ld hl, (adjacent_object)
ld a, h
or a
jp z, shoot
ld a, object_behaviour.interact
jp objects.call_behaviour ; call & ret
item_turn:
ld ix, hl
ld a, (ix + object.state + 1)
ld b, a
or a
ret z ; already picked up
ld a, (game.hero.x)
sub (ix + object.x)
inc a
cp 3
ret nc
ld a, (game.hero.y)
sub (ix + object.y)
inc a
cp 4
ret nc
ld (adjacent_object), hl
ret
item_menu_lines:
dw i18n.item_menu_examine
dw i18n.item_menu_take
dw i18n.exit
dw 0
item_menu dialog.dialog 0, 0, item_menu_lines, 5, 8
item_interact:
push hl
ld ix, hl
ld a, (ix + object.state) ; item id
call inventory.resolve_item
ld iy, hl
ld hl, (iy + inventory.inventory_item.name)
ld (item_menu), hl
ld hl, item_menu_lines
push ix
ld ix, item_menu
call dialog.open_dialog
pop ix
pop hl
.5 halt
or a
jr nz, .not_exam
push hl
ld hl, (iy + inventory.inventory_item.description)
ld ix, console.default_console
call console.clear
call console.println
pop hl
jr item_interact
.not_exam
cp 1
jr z, .add
call status.display_status
jr .done
.add
ld (ix + object.state + 1), 0
ld (hl), 0xff ; hide
ld a, (ix + object.state)
call inventory.add_item
ld hl, beeper.pickup_sound
call beeper.play_sfx
.done
ld ix, console.default_console
call console.clear
call status.display_status
ld a, 1
ld (dirty_screen), a
ret
;; Game object behaviours
lift_turn:
ld ix, hl
ld de, (ix) ; hero coord
ld a, (game.hero.y)
xor e
and 0x7f
jr nz, door_turn.inside
ld e, a
ld a, (game.hero.x)
cp d
jr nz, door_turn.inside
ld bc, (map.map_coord)
ld hl, (ix + object.state_2)
xor a
call activate_level
.3 halt
ld b, 3
1
push bc
halt
call move_hero_down
pop bc
djnz 1B
ret
door_turn:
ld ix, hl
.inside
ld a, (ix + object.state)
or a
jr nz, .check_closing
ld a, (game.hero.y) ; door.y - 1 <= y <= door.y + 1
sub (hl)
inc a
cp 3
ret nc
inc hl
ld a, (game.hero.x)
cp (hl)
ret nz
dec hl
ld (adjacent_object), hl
.check_closing
cp 0xff
ret nz
; -1, 0, 1
; keep open if
; -1 <= hero.y - door.y <= 1
ld a, (game.hero.y)
ld b, a
ld a, (hl)
and 0x7f ; the 7th bit is used for hiding
sub b
inc a
cp 3
jr nc, .start_closing
; keep open if
; -1 <= hero.x - door.x <= 1
inc hl
ld a, (game.hero.x)
sub (hl)
inc a
cp 3
ret c
.start_closing
ld (ix + object.state), 0x87
ld a, (ix)
and 0x7f
ld (ix), a ; unhide object
ret
vdoor_turn:
ld ix, hl
ld a, (ix + object.state)
or a
jr nz, .check_closing
ld a, (game.hero.y)
cp (hl)
ret nz
inc hl
; activate if -2 <= game.hero.x - door.x <= 1
ld a, (game.hero.x)
sub (hl)
add 2
cp 4
ret nc
dec hl
ld (adjacent_object), hl
ret
.check_closing
cp 0xff
ret nz
; inc hl
ld a, (hl)
and 0x7f
ld b, a
ld a, (game.hero.y)
cp b
jr nz, .start_closing
; keep closed if -2 <= hero.x - door.x <= 1
; i.e. 0 <= hero.x - door.x + 2 <= 3
ld a, (game.hero.x)
inc hl
sub (hl)
add 2
cp 4
ret c
.start_closing
ld (ix + object.state), 0x87
ld a, (ix)
and 0x7f
ld (ix), a ; unhide object
ret
; ld a, (hl)
; and 0x7f
; ld b, a
; ld a, (game.hero.x)
; ; close if not -1 <= door.x - game.hero.x <= 1
; and 0x7f
; sub (hl)
; inc a
; jr c, .start_closing
; inc hl
; ld a, (game.hero.y)
; cp
door_interact:
ld ix, hl
ld a, (ix + object.state)
or a
ret nz
ld (ix + object.state), 1
ret
door_tick:
ld ix, hl
ld a, (ix + object.state)
or a
ret z
cp 0xff
ret z
bit 7, a
jr z, .opening
.closing
and 0x0f
or a
jr nz, 1F
; ld hl, i18n.door_closed
; call console.print_localized
ld (ix + object.state), 0
ld a, 1
ld (dirty_screen), a
ret
1
bit 0, a
jr z, 2F
dec a
or 0x80
ld (ix + object.state), a
ret
2
dec a
or 0x80
ld (ix + object.state), a
and 0x7f
srl a
call objects.update_sprite
ret
.opening
cp 5
jr nz, 1F
; done opening, deactivate the door
; ld hl, i18n.door_open
; push ix
; call console.print_localized
; pop ix
ld a, (ix)
or 0x80
ld (ix), a ; hide object by moving it off-screen
ld (ix + object.state), 0xff
ld a, 1
ld (dirty_screen), a
ret
1
bit 0, a
jr z, 2F
inc a
ld (ix + object.state), a
ret
2
inc a
ld (ix + object.state), a
srl a
call objects.update_sprite
ret
; hl - door object
draw_with_color:
push hl
ld bc, object.state + 1
add hl, bc
ld a, (hl)
.3 dec hl
ld b, (hl)
inc hl
ld h, (hl)
ld l, b
call sprite.paint_sprite_monochrome
pop hl
jp map.draw_static_object
; sets Z if no object found
find_next_drawable_object:
ld hl, level_objects
ld b, 0xff ; best y
ld de, 0 ; best object
.loop
ld c, (hl)
inc hl
ld a, (hl)
or a
jr z, .end_loop
bit 7, a
jr z, .next_object
push de
ld e, c
ld d, a
ld a, (de)
cp b
jr nc, .next_object_pop
ld b, a ; new best y
pop af ; discard the previous object from the stack
ld ix, hl
inc hl
jr .loop
.next_object_pop
pop de
.next_object
inc hl
jr .loop
.end_loop
ld a, d
or a ; set Z if no object found
ret z
ex de, hl
ld a, (ix)
and 0x7f
ld (ix), a
ret
draw_objects:
ld hl, level_objects
.draw_loop
call find_next_drawable_object
jr z, .clean
ld a, object_behaviour.draw
call objects.call_behaviour
jr .draw_loop
.clean
ld hl, level_objects
.clean_loop
inc hl
ld a, (hl)
or a
ret z
or 0x80
ld (hl), a
inc hl
jr .clean_loop
stop_hero:
push af
ld a, (hero.state)
or a
jr z, .end
xor a
ld (hero.state), a
ld a, (hero.state+1) ; direction
cp DIR_FACE
jr nz, 1F
xor a
ld e, a
jr .update
1
cp DIR_BACK
jr nz, 2F
xor a
ld e, PHASE_BACK_STAND
jr .update
2
ld e, PHASE_SIDE_STAND
rra
.update
ld ix, hero
; ld (ix + object.mirror), 0
ld a, e
call objects.update_sprite
ld (ix + object.state), 0
.end
pop af
ret
macro post_turn
xor a
ld (adjacent_object+1), a
ld a, object_behaviour.turn
ld hl, level_objects
call objects.object_loop
endm
shoot:
ld a, (hero.state + 1)
cp DIR_RIGHT
jr nz, 1F
jp bullet.shoot_right
1
cp DIR_FACE
jr nz, 2F
jp bullet.shoot_down
2
cp DIR_BACK
jp nz, bullet.shoot_left
jp bullet.shoot_up
move_hero_right:
ld a, (hero.state + 1)
cp DIR_RIGHT
jr z, 1F
; init moving state
xor a
ld (hero.state), a
xor a
ld (hero.mirror), a
ld a, DIR_RIGHT
ld (hero.state + 1), a
ld a, 1
ld e, PHASE_SIDE_STAND
jr .update
1
ld a, (hero.state)
and 0xf
cp 1
jr nz, 2F
ld a, 2
ld e, PHASE_SIDE_WALK_1
jr .update
2
ld hl, beeper.step_sound
call beeper.play_sfx
call map.move_hero_right
call log_steps
ld a, 1
ld e, PHASE_SIDE_WALK_2
.update
ld ix, hero
ld (ix + object.state), a
ld a, e
call objects.update_sprite
post_turn
ret
move_hero_left:
ld a, (hero.state + 1)
cp DIR_LEFT
jr z, 1F
; init moving state
ld a, DIR_LEFT
ld (hero.state + 1), a
ld a, 1
ld (hero.mirror), a
ld e, PHASE_SIDE_STAND
jr .update
1
ld a, (hero.state)
and 0xf
cp 1
jr nz, 2F
ld a, 2
ld e, PHASE_SIDE_WALK_1
jr .update
2
ld hl, beeper.step_sound
call beeper.play_sfx
call map.move_hero_left
call log_steps
ld a, 1
ld e, PHASE_SIDE_WALK_2
.update
ld ix, hero
ld (ix + object.state), a
ld a, e
call objects.update_sprite
post_turn
ret
move_hero_down:
ld ix, hero
ld a, (hero.state + 1)
cp DIR_FACE
jr z, 1F
; init moving state
ld a, DIR_FACE
ld (hero.state + 1), a
xor a
ld (hero.mirror), a
inc a
ld de, PHASE_FACE_STAND
jr .update
1
ld a, (hero.state)
and 0xf
cp 1
jr nz, 2F
ld a, 2
ld de, PHASE_FACE_WALK
jr .update
2
ld hl, beeper.step_sound
call beeper.play_sfx
call map.move_hero_down
call log_steps
ld a, 1
ld de, 0x100 + PHASE_FACE_WALK
.update
ld (ix + object.state), a
ld (ix + object.mirror), d
ld a, e
call objects.update_sprite
post_turn
ret
move_hero_up:
ld ix, hero
ld a, (hero.state + 1)
cp DIR_BACK
jr z, 1F
; init moving state
ld a, DIR_BACK
ld (hero.state + 1), a
xor a
ld (hero.mirror), a
ld de, PHASE_BACK_STAND
inc a
jr .update
1
ld a, (hero.state)
and 0xf
cp 1
jr nz, 2F
ld a, 2
ld de, PHASE_BACK_WALK
jr .update
2
ld hl, beeper.step_sound
call beeper.play_sfx
call map.move_hero_up
call log_steps
ld a, 1
ld de, 0x100 + PHASE_BACK_WALK
.update
ld (ix + object.state), a
ld (ix + object.mirror), d
ld a, e
call objects.update_sprite
post_turn
ret
log_steps:
ret
ld hl, game.total_steps
inc (hl)
ld hl, i18n.step
push ix
ld ix, console.default_console
call console.println
pop ix
ret
; hl - level ref
; d - hero x, e - hero y
; b - map x, c - map y
; a - hero sprite
activate_level:
call map.fade_out
call map.recover_buffer
di
ld (game.hero), de
ld ix, game.hero
ld (ix + object.state + 1), a
call stop_hero
ld e, (hl)
inc hl
ld d, (hl)
inc hl
push de
push bc
ld de, CURRENT_MAP
call dzx0_standard
pop hl
call map.set_map_coord
pop hl
ld e, (hl)
inc hl
ld d, (hl)
inc hl
ex de, hl
call map.set_tiles
ex de, hl
ld bc, 0
push hl
.length
inc hl
bit 7,(hl)
jr z, 1F
inc c
inc c
inc hl
jr .length
1
pop hl
ld de, level_objects
ld a, c
or b
jr z, 2F
ldir
2
ex hl, de
ld (hl), low(hero)
inc hl
ld (hl), high(hero)
.2 inc hl
ld (hl), 0
call tiles.prepare_tiles
call map.redraw_map
.end
ei
ret
level_1
dw level_1_metadata
incbin "res/gen/levels/test_level.zx0"
level_1_metadata
dw res.tiles
dw .door_1
dw .door_2
dw .door_3
dw .vdoor_1
dw .keycard
dw .lift
dw 0
.door_1 object 8, 4, 0, res.door, 0x200, 0, res.door, door_behaviour
.door_2 object 14, 4, 0, res.door, 0x500, 0, res.door, door_behaviour
.door_3 object 14, 22, 0, res.door, 0x400, 0, res.door, door_behaviour
.vdoor_1 object 6, 20, 0, res.vdoor, 0x4700, 0, res.vdoor, vdoor_behaviour
.keycard object 6, 6, 0, res.keycard, 0x201, 0, 0, item_behaviour
.lift object 0, 32, 0, res.door, 0x4700, level_2, res.door, lift_behaviour
level_2
dw level_2_metadata
incbin "res/gen/levels/test_level_2.zx0"
level_2_metadata
dw res.tiles
dw .lift
dw 0
.lift object 0, 32, 0, 0, 0x47ff, level_1, res.door, lift_behaviour
endmodule