aboutsummaryrefslogtreecommitdiff
path: root/game/main.asm
blob: 253c812599cf6a149e5baacf78510f8e77ec7269 (plain)
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
.org 0

.equ INT_VECTOR 0xff00

    call init

;
; Menu entry point
;
menu:
    call clear_screen

menu_loop:
    halt

    call stars_update
    call menu_draw

    ; read controller 1
    ld b, 0xf0
    port b, b

    ; start
    bit b, 7
    bnz
    jmp menu_loop

;
; Gameplay entry point
;
play:
    call init_game

game_loop:
    halt

    call entities_erase
    call stars_update
    call entities_update
    call entities_draw
    call update_hud

    ; read controller 1
    ld b, 0xf0
    port b, b

    ; select to exit
    bit b, 6
    bnz
    jmp game_loop

    ; see if the hiscore needs updating

    ; 6 digits to compare
    ld y, 6
    push y

    ld x, <score
    ld a, >score
    ld y, <hiscore
    ld b, >hiscore
update_hiscore_loop:
    cmpi
    bc
    jmp update_no_hiscore
    bnz
    jmp update_new_hiscore

    push y
    ld y, [sp + 1]
    dec y
    ld [sp + 1], y
    pop y
    bno
    jmp update_hiscore_loop

update_new_hiscore:
    ; new hiscore!
    ld x, <score
    ld a, >score
    ld y, <hiscore
    ld b, >hiscore
    ldi
    ldi
    ldi
    ldi
    ldi
    ldi

update_no_hiscore:
    pop y
    jmp menu

;
; Setup the game after the menu
;
init_game:
    call entities_init

    ; set player initial coordinates
    ld x, <player_coord
    ld a, >player_coord

    ld y, 56
    ld [a : x], y
    inc x
    bo
    inc a
    ld y, 104
    ld [a : x], y

    ; clear score
    ld y, <score
    ld b, >score
    ; this is all zeroes that we use
    ; to erase things on screen etc
    ld x, 0
    ld a, 0xa0
    ldi
    ldi
    ldi
    ldi
    ldi
    ldi

    halt

    call clear_screen
    call entities_draw

    ret

;
; Update the HUD (score, lives, etc)
;
update_hud:
    ; erase

    ; settings mode
    ld y, 128
    ld b, 0xb0
    port b, y

    ; setup
    inc b

    ; addr: bg data
    ld y, 0
    port b, y
    ld y, 0xa0
    port b, y

    ; x
    inc x
    bo
    inc a
    ld y, 100
    port b, y

    ; y
    inc x
    bo
    inc a
    ld y, 4
    port b, y

    ; w
    ld y, 25
    port b, y
    ; h
    ld y, 8
    port b, y

    ; blit
    dec b
    ; write, no transparent
    ld y, 1
    port b, y

    ; 6 digits
    ld x, 6
    push x

    ; addr of the BCD number
    ld x, <score
    ld a, >score
    ; where we want the text
    ld b, 100
    ld y, 4

    call put_bcd_number

    pop x
    ret

;
; Add points to the score
;
; in: y points (1 to 10)
add_score:
    ; update score
    ld x, <score
    ld a, >score
    add x, 5
    bo
    inc a

    ld b, 6
add_score_loop:
    push b

    ld b, [a : x]
    add b, y
    ld [a : x], b
    cmp b, 10
    bnc
    jmp add_score_carry

    pop b
    ret

add_score_carry:
    sub b, 10
    ld [a : x], b

    ld y, 1

    dec x
    bo
    dec a

    pop b
    dec b
    bnz
    jmp add_score_loop
    ret

;
; Helper to clear the screen
;
clear_screen:
    ; erase the screen
    ld b, 0
    ld a, 0xbf
    ld x, 0
    ld y, 0x40
fill_loop:
    ld [a : x], b
    inc x
    bno
    jmp fill_loop
    inc a
    dec y
    bnz
    jmp fill_loop
    ret

;
; Draws the menu
;
menu_draw:
    ; hiscore
    ld x, <hiscore_text
    ld a, >hiscore_text
    ld b, 44
    ld y, 10
    call put_text

    ; 6 digits
    ld x, 6
    push x

    ; addr of the BCD number
    ld x, <hiscore
    ld a, >hiscore
    ; where we want the text
    ld b, 60
    ld y, 10

    call put_bcd_number

    pop x

    ; settings mode
    ld y, 128
    ld b, 0xb0
    port b, y

    ; setup
    inc b

    ; addr: title sprite
    ld y, <title
    port b, y
    ld y, >title
    port b, y

    ; x
    ld y, 26
    port b, y

    ; y
    ld y, 40
    port b, y

    ld y, 76
    port b, y
    ld y, 21
    port b, y

    ; blit
    dec b
    ; write, transparent
    ld y, 3
    port b, y

    ld x, <press_start_text
    ld a, >press_start_text
    ld b, 40
    ld y, 84
    call put_text

    ; settings mode
    ld y, 128
    ld b, 0xb0
    port b, y

    ; setup
    inc b

    ; addr: usebox logo sprite
    ld y, <usebox
    port b, y
    ld y, >usebox
    port b, y

    ; x
    ld y, 44
    port b, y

    ; y
    ld y, 118
    port b, y

    ld y, 40
    port b, y
    ld y, 8
    port b, y

    ; blit
    dec b
    ; write, transparent
    ld y, 3
    port b, y
    ret

;
; Init the TR8 (setup an int handler)
;
init:
    ; setup an int handler so we
    ; can use the frame int to sync
    ld a, >INT_VECTOR
    ld x, <INT_VECTOR
    ld b, <int_handler
    ld [a : x], b
    inc x
    ld b, >int_handler
    ld [a : x], b

    ; enable interrupts
    cif
    ret

int_handler:
    iret

.include "text.asm"
.include "random.asm"
.include "starfield.asm"
.include "entities.asm"
.include "player.asm"
.include "bullet.asm"

; in unpacked BCD format
hiscore:
    .db 0, 1, 0, 0, 0, 0
score:
    .db 0, 0, 0, 0, 0, 0

;
; entity data
;
entities:
    ; type
    .db ET_PLAYER
    ; x, y
player_coord:
    .db 0, 0
    ; frame
    .db 0
    .dw player_sprite
    .dw player_update

    ; 16 entities
    .ds 128, 0

    ; end of list
    .db ET_END

; sprite data
player_sprite:
    .incpng "assets/player.png"
pbullet_sprite:
    .incpng "assets/pbullet.png"

; starfield data
stars:
    ; video addr, speed, color
    .dw 0xc361
    .db 1, 15
    .dw 0xc204
    .db 1, 6
    .dw 0xcdf9
    .db 1, 15
    .dw 0xdb87
    .db 1, 3
    .dw 0xd242
    .db 1, 15
    .dw 0xdeea
    .db 1, 6
    .dw 0xe7af
    .db 1, 15
    .dw 0xed68
    .db 1, 3
    .dw 0xf812
    .db 1, 15
    .dw 0xfbca
    .db 1, 13
    .dw 0xc361
    .db 2, 8
    .dw 0xc204
    .db 2, 9
    .dw 0xcdf9
    .db 3, 7
    .dw 0xdb87
    .db 3, 4
    .dw 0xd242
    .db 2, 8
    .dw 0xdeea
    .db 3, 9
    .dw 0xe7af
    .db 3, 6
    .dw 0xed68
    .db 2, 9
    .dw 0xf812
    .db 3, 14
    .dw 0xfbca
    .db 2, 3
    ; end of list
    .dw 0xffff

; menu data
title:
    .incpng "assets/title.png"
font:
    .incpng "assets/font.png"
usebox:
    .incpng "assets/usebox.png"

; texts
press_start_text:
    .str "PRESS START!"
    .db 0
hiscore_text:
    .str "HI:"
    .db 0