-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamePlayer.asm
468 lines (364 loc) · 13.9 KB
/
gamePlayer.asm
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
;==============================================================================
; Constants
PlayerMaxFrame = 24
PlayerXMinHigh = 0 ; 0*256 + 24 = 24 minX
PlayerXMinLow = 24
PlayerXMaxHigh = 1 ; 1*256 + 64 = 320 maxX
PlayerXMaxLow = 64
PlayerYMin = 30
PlayerYMax = 229
PlayerInputDelay = 3
PlayerFireDelay = 1
PlayerXMinScrollHigh = 0 ; 0*256 + 150 = 150 minX
PlayerXMinScrollLow = 170
PlayerXMaxScrollHigh = 0 ; 0*256 + 190 = 190 maxX
PlayerXMaxScrollLow = 170
;===============================================================================
; Variables
;playerActive byte 1
;playerFrame byte 6
;playerSpriteFrame byte 0
;playerSprite byte 0
;playerXHigh byte 0
;playerXLow byte 25
;playerY byte 55
;playerXChar byte 0
;playerXOffset byte 0
;playerYChar byte 0
;playerYOffset byte 0
;playerHorizontalSpeed byte 3
;playerVerticalSpeed byte 0
;playerHorizontalBombSpeed byte 0
;playerVerticalBombSpeed byte 0
;playerHorizontalBulletSpeed byte 1
;playerVerticalBulletSpeed byte 1
; down-sampled animation frames to reduce sprite size
playerSpriteFrameConv byte 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4
byte 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 0
; horizontal velocities by sprite frame
playerXmoveArray byte 0, 1, 1, 2, 2, 2, 3, 2, 2, 2, 1, 1
byte 0, 1, 1, 2, 2, 2, 3, 2, 2, 2, 1, 1
; vertical velocities by sprite frame
playerYmoveArray byte -3, -2, -2, -2, -1, -1, 0, 1, 1, 2, 2, 2
byte 3, 2, 2, 2, 1, 1, 0, -1, -1, -2, -2, -2
; bullet directions by sprite frame
playerXbulletArray byte 0, 0, 3, 3, 3, 6, 6, 6, 3, 3, 3, 0
byte 0, 0, 3, 3, 3, 6, 6, 6, 3, 3, 3, 0
; bullet directions by sprite frame
playerYbulletArray byte -6, -6, -3, -3, -3, 0, 0, 0, 3, 3, 3, 6
byte 6, 6, 3, 3, 3, 0, 0, 0, -3, -3, -3, -6
; horizontal velocities by sprite frame
playerXbombArray byte 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1
byte 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1
; vertical velocities by sprite frame
playerYbombArray byte -4, -4, -3, -2, -1, 0, 0, 1, 1, 2, 2, 2
byte 3, 2, 2, 2, 1, 1, 0, 0, -1, -2, -3, -4
;===============================================================================
; Macros/Subroutines
gamePlayerInit
; re-initialize the music
jsr libMusicInit
lda #True
sta playerActive
LIBSPRITE_STOPANIM_A playerOutlineSprite
LIBSPRITE_STOPANIM_A playerColorSprite
lda #6
sta playerFrame
lda #0
sta playerVerticalSpeed
lda #3
sta playerHorizontalSpeed
lda #0
sta playerXHigh
lda #25
sta playerXLow
lda #55
sta playerY
lda #1
sta playerXChar
lda #0
sta playerOutlineSprite
lda #1
sta playerColorSprite
LIBSPRITE_ENABLE_AV playerOutlineSprite, True
LIBSPRITE_ENABLE_AV playerColorSprite, True
ldx playerFrame
lda playerSpriteFrameConv,x
sta playerSpriteFrame
LIBSPRITE_SETFRAME_AAV playerOutlineSprite, playerSpriteFrame, PLAYEROUTLINERAM
LIBSPRITE_SETCOLOR_AV playerOutlineSprite, Black
LIBSPRITE_MULTICOLORENABLE_AV playerOutlineSprite, False
LIBSPRITE_SETFRAME_AAV playerColorSprite, playerSpriteFrame, PLAYERCOLORRAM
LIBSPRITE_SETCOLOR_AV playerColorSprite, Green
LIBSPRITE_MULTICOLORENABLE_AV playerColorSprite, True
lda #0
sta playerDelayCounter
sta playerFireCounter
rts
;==============================================================================
gamePlayerReset
jsr gamePlayerInit
;LIBSPRITE_SETPOSITION_AAAA playerSprite, playerXHigh, playerXLow, playerY
inc EXTCOL
jsr gamePlayerUpdatePosition
dec EXTCOL
rts
;===============================================================================
gamePlayerUpdate
jsr gamePlayerUpdateFiring
lda playerActive
beq @skipUpdate
inc EXTCOL
jsr gamePlayerUpdatePosition
inc EXTCOL
jsr gamePlayerUpdateCollisions
dec EXTCOL
dec EXTCOL
jmp @doneUpdate
@skipUpdate
lda #1
sta playerFrame
lda #0
sta playerVerticalSpeed
lda #0
sta playerHorizontalSpeed
jmp gPUPEndmove
@doneUpdate
rts
;==============================================================================
gamePlayerUpdateFiring
; do fire after the ship has been clamped to position
; so that the bullet lines up
LIBINPUT_GETFIREPRESSED
beq @firing
jmp gPUFNofire
@firing
lda playerActive
bne playerCharActive
; bring the player back to life
INCREMENT_FIRE_DELAY_AA playerFireCounter, gPUFNofire
jsr gamePlayerReset
jmp gPUFNofire
playerCharActive
; play firing sound and fire a bullet
INCREMENT_FIRE_DELAY_AA playerFireCounter, gPUFNofire
LIBSOUND_PLAY_VAA 1, soundExplosionHigh, soundExplosionLow
GAMEBULLETS_FIRE_AAAVAAAA playerXHigh, playerXLow, playerY, Black, playerHorizontalBulletSpeed, playerVerticalBulletSpeed, playerOutlineSprite, playerFrame
gPUFNofire
rts
;==============================================================================
gamePlayerUpdateCollisions
lda playerActive
beq @jumpNoCollision
jmp @continue
@jumpNoCollision
jmp @gPUCNoCollision
@continue
; if we have collided with someone else's bullet, always collision
GAMEBULLETS_COLLIDED_AAAV playerXChar, playerYChar, playerOutlineSprite
cmp #1
beq @gPUCCollision
; if we haven't hit someone else's bullet and we're in the background layer, no collision
lda playerYChar
cmp #13
bcc @gPUCNoCollision
; if we're above the vertical display area, no collision
lda playerY
cmp #40
bcc @gPUCNoCollision
; if we have hit a non-terrain background character, no collision
LIBSCREEN_BACKGROUND_CHECK playerXChar, playerYChar
cmp #0
beq @gPUCNoCollision
@gPUCcollision
jsr gamePlayerDestroyPlayer
@gPUCNoCollision
rts
gamePlayerDestroyPlayer
lda #False
sta playerActive
; run explosion animation
LIBSPRITE_SETFRAME_AVV playerColorSprite, #0, EXPLOSION1RAM
LIBSPRITE_SETCOLOR_AV playerColorSprite, Yellow
LIBSPRITE_PLAYANIM_AVVVV playerColorSprite, 0, 11, 3, False
LIBSPRITE_SETFRAME_AVV playerOutlineSprite, #0, DEBRISRAM
LIBSPRITE_SETCOLOR_AV playerOutlineSprite, Black
LIBSPRITE_PLAYANIM_AVVVV playerOutlineSprite, 0, 11, 3, False
; play explosion sound
LIBSOUND_PLAY_VAA 2, soundFiringHigh, soundFiringLow
rts
;===============================================================================
defm INCREMENT_INPUT_DELAY_AA ; /1 = Current delay counter
ldx /1
inx
cpx #PlayerInputDelay
bcc @endDelay
lda #0
sta /1
jmp @finished
@endDelay
stx /1
jmp /2
@finished
endm
;===============================================================================
defm INCREMENT_FIRE_DELAY_AA ; /1 = Current delay counter
ldx /1
inx
cpx #PlayerFireDelay
bcc @endDelay
lda #0
sta /1
jmp @finished
@endDelay
stx /1
jmp /2
@finished
endm
;===============================================================================
gamePlayerUpdatePosition
gPUPLeft
LIBINPUT_GETKEYPRESSED AKeyRow, AKeyBit
beq keyLeft
LIBINPUT_GETHELD GameportLeftMask
bne gPUPRight
keyLeft
INCREMENT_INPUT_DELAY_AA playerDelayCounter, gPUPEndmove
dec playerFrame
ldx playerFrame
cpx #0
bpl leftRotate
ldx #23
stx playerFrame
leftRotate
lda playerXmoveArray,x
sta playerHorizontalSpeed
lda playerYmoveArray,x
sta playerVerticalSpeed
lda playerSpriteFrameConv,x
sta playerSpriteFrame
LIBSPRITE_SETFRAME_AAV playerOutlineSprite, playerSpriteFrame, PLAYEROUTLINERAM
LIBSPRITE_SETFRAME_AAV playerColorSprite, playerSpriteFrame, PLAYERCOLORRAM
gPUPRight
LIBINPUT_GETKEYPRESSED DKeyRow, DKeyBit
beq keyRight
LIBINPUT_GETHELD GameportRightMask
bne gPUPUp
keyRight
INCREMENT_INPUT_DELAY_AA playerDelayCounter, gPUPEndmove
inc playerFrame
ldx playerFrame
cpx #24
bmi rightRotate
ldx #0
stx playerFrame
rightRotate
lda playerXmoveArray,x
sta playerHorizontalSpeed
lda playerYmoveArray,x
sta playerVerticalSpeed
lda playerSpriteFrameConv,x
sta playerSpriteFrame
LIBSPRITE_SETFRAME_AAV playerOutlineSprite, playerSpriteFrame, PLAYEROUTLINERAM
LIBSPRITE_SETFRAME_AAV playerColorSprite, playerSpriteFrame, PLAYERCOLORRAM
gPUPUp
; LIBINPUT_GETHELD GameportUpMask
; bne gPUPDown
gPUPDown
LIBINPUT_GETKEYPRESSED SKeyRow, SKeyBit
beq keyDown
LIBINPUT_GETHELD GameportDownMask
bne gPUPEndmove
keyDown
; create a bomb!
ldx playerFrame
lda playerXbombArray,x
sta playerHorizontalBombSpeed
lda playerYbombArray,x
sta playerVerticalBombSpeed
GAMEBOMB_DROPBOMB_AAAAAA playerFrame, playerY, playerXHigh, playerXLow, playerVerticalBombSpeed, playerHorizontalBombSpeed
gPUPEndmove
ldx playerFrame
lda playerXbulletArray,X
sta playerHorizontalBulletSpeed
lda playerYbulletArray,X
sta playerVerticalBulletSpeed
; apply horizontal velocity
cpx #13
bcs @leftMove
@rightMove
LIBMATH_ADD16BIT_AAVAAA playerXHigh, PlayerXLow, 0, playerHorizontalSpeed, playerXHigh, PlayerXLow
jmp @doneMove
@leftMove
LIBMATH_SUB16BIT_AAVAAA playerXHigh, PlayerXLow, 0, playerHorizontalSpeed, playerXHigh, PlayerXLow
@doneMove
; apply vertical velocity
LIBMATH_ADD8BIT_AAA PlayerY, playerVerticalSpeed, PlayerY
; clamp player x position and scroll the screen if needed
inc EXTCOL
jsr gamePlayerClampXandScroll
dec EXTCOL
; clamp the player y position
LIBMATH_MIN8BIT_AV playerY, PlayerYMax
LIBMATH_MAX8BIT_AV playerY, PlayerYMin
; set the sprite position
; can get away with 8 bits b/c the clamp area has <255 max X
LIBSPRITE_SETPOSITION_AAA playerOutlineSprite, PlayerXLow, PlayerY
LIBSPRITE_SETPOSITION_AAA playerColorSprite, PlayerXLow, PlayerY
; update the player char positions
LIBSCREEN_PIXELTOCHAR_AAVAVAAAA playerXHigh, playerXLow, 16, playerY, 40, playerXChar, playerXOffset, playerYChar, playerYOffset
;LIBSCREEN_DEBUG8BIT_VVA 80, 80, playerDelayCounter
rts
;===============================================================================
gamePlayerClampXandScroll
lda #0
sta screenScrolling
sta screenScrollingLeft
sta screenScrollingRight
; clamp the player x position ------------------------------
; can get away with 8 bits b/c the clamp area has <255 max X
LIBMATH_MAX8BIT_AV playerXLow, PLayerXMinScrollLow
LIBMATH_MIN8BIT_AV playerXLow, PLayerXMaxScrollLow
; scroll the screen ----------------------------------------
lda playerFrame
cmp #1
beq @jumpDone ; if zero velocity (pointing up)
cmp #13
beq @jumpDone ; if zero velocity (pointing down)
bcs gPCXSNegative
jmp gPCXSPositive
@jumpDone
jmp gPCXSDone
gPCXSNegative ; if negative velocity
; scroll the screen right if player X = min scrollX
; can get away with 8 bits b/c the clamp area has <255 max X
;lda playerXHigh
;cmp #PlayerXMinScrollHigh
;bne gPCXSDone
lda playerXLow
cmp #PlayerXMinScrollLow
bne gPCXSDone
lda #1
sta screenScrolling
LIBSCREEN_FOREGROUNDSCROLLRIGHTTWO
LIBSCREEN_BACKGROUNDSCROLLRIGHTONE
lda #1
sta ScreenScrollingRight
jmp gPCXSDone
gPCXSPositive ; if positive velocity
; scroll the screen left if player X = max scrollX
; can get away with 8 bits b/c the clamp area has <255 max X
;lda playerXHigh
;cmp #PLayerXMaxScrollHigh
;bne gPCXSDone
lda playerXLow
cmp #PLayerXMaxScrollLow
bcc gPCXSDone
lda #1
sta screenScrolling
LIBSCREEN_FOREGROUNDSCROLLLEFTTWO
LIBSCREEN_BACKGROUNDSCROLLLEFTONE
lda #1
sta ScreenScrollingLeft
gPCXSDone
rts