-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.asm
505 lines (363 loc) · 10.3 KB
/
sprites.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
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
!zone playerSprite
.OffsetX !byte 2 ; Byte
.OffsetY !byte 2 ; Byte
!zone dirSprite
.OffsetX !byte 1 ; Byte
.OffsetY !byte 1 ; Byte
!zone raySprite
.OffsetX !byte 2 ; Byte
.OffsetY !byte 2 ; Byte
!zone backSprite
.Heading !word 0 ; Back sprite 0
!word 0 ; Back sprite 1
!word 0 ; Back sprite 2
!word 0 ; Back sprite 3
!word 12 ; Back sprite 4
!word 24 ; Back sprite 5
!word 0 ; Back sprite 6
!word 0 ; Back sprite 7
.Azimuth !byte 70
; ********************************************************************************
; INITIALIZE SPRITES
; ********************************************************************************
initSprites
lda #128 ; Set sprite start memory block
sta B
ldy #0
.loop sta SPRITE_PTR,Y
inc B
lda B
iny
cpy #6
bcc .loop
; Set sprite expand mode
lda #0
sta SPRITE_EXPAND_X
sta SPRITE_EXPAND_Y
; Set sprite color mode
lda #%00111000 ; Sprites 0 to 2 are hires
sta SPRITE_MCM ; Sprites 3 to 5 are multicolor
; Set sprite color
lda #YELLOW
sta SPRITE_0_COLOR
sta SPRITE_1_COLOR
sta SPRITE_2_COLOR
sta SPRITE_3_COLOR
sta SPRITE_4_COLOR
sta SPRITE_5_COLOR
; Set multicolor sprites
lda #WHITE
sta SPRITE_MCOLOR_0
lda #LIGHT_GRAY
sta SPRITE_MCOLOR_1
; Set sprite priority
lda #%00111000 ; Sprites 0 to 2 are in front of the text
sta SPRITE_PRIORITY ; Sprites 3 to 5 are behind the text
; Set height for the background sprite
lda backSprite.Azimuth
sta SPRITE_3_Y_POS
sta SPRITE_4_Y_POS
sta SPRITE_5_Y_POS
rts
!zone getSpriteX
; ********************************************************************************
; GET SPRITE X COORDINATE
;
; Description: Calculate sprite screen X position in pixels.
;
; Inputs: position Real Stored in X and A registers!
; Position on the screen in FIXED POINT format.
; /X register contains the high byte of the position,
; i.e. integer part that represents the screen cell.
; /A register contains the low byte of the posion,
; i.e. the remainder part that represents
; the position inside the cell.
;
; offset Byte Offset from the left side stored in Y register
; /Y register
;
; Outputs: result Word Stored in X and A registers! (also in DX)
; The sprite x position in PIXELS
; /A register will contain the lo-byte
; of the x position
; /X register will containt he hi-byte
; of the x position
;
; Pseudocode:
; result = position * 8 + SPRITE_MGN_LEFT - offset
; ********************************************************************************
getSpriteX
sty B ; Store offset into B register
stx DL ; Store position hi-byte into DL
ldx #0 ; Store 0 into DH
stx DH
asl ; Catch Carry if negative
rol DL ; A register contains lo-byte of the position
rol DH
asl
rol DL
rol DH
asl
rol DL
rol DH
lda DL
clc
adc #SPRITE_MGN_LEFT
sta DL
bcc .skip1
inc DH
.skip1 lda DL
sec
sbc B
sta DL
bcs .skip2
dec DH
.skip2 ldx DH
rts
!zone getSpriteY
; ********************************************************************************
; GET SPRITE Y COORDINATE
;
; Calculate sprite screen y position in pixels.
;
; Input: position Real Stored in X and A registers!
; Position on the screen in FIXED POINT format.
; /X register contains the hi-byte of the position,
; i.e. integer part that represents
; the screen cell.
; /A register contains the lo-byte of the posion,
; i.e. the fractional part that represents
; the position inside the cell.
;
; offset Byte Stored in Y register!
; /Y register contains the ofsset in pixels
;
; Outputs: result Word Stored in X and A registers! (also in DX)
; The sprite y position in PIXELS
; /A register will contain the lo-byte
; of the y position
; /X register will containt he hi-byte
; of the y position
;
; Pseudocode:
; result = position * 8 + SPRITE_MGN_TOP - offset
; ********************************************************************************
getSpriteY
sty B
stx DH
asl ; Catch Carry if negative
rol DH ; A register contains lo-byte of the position
asl
rol DH
asl
rol DH
lda DH
clc
adc #SPRITE_MGN_TOP
sta DH
lda DH
sec
sbc B
sta DH
rts
!zone setSpriteXMSB
; ********************************************************************************
; SET SPRITE MOST SIGNIFICANT BIT
;
; Description: Set sprite most sifnificant bit into the SPRITE_X_MSB
;
; Inputs: X register Byte High byte of the screen position
;
; Y register Byte Sprite number
;
; ********************************************************************************
setSpriteXMSB:
cpx #1
bne .noHiBit
lda bitMask,y
ora SPRITE_X_MSB
sta SPRITE_X_MSB
rts
; Hi bit not active
.noHiBit lda bitMask,y
eor #%11111111
and SPRITE_X_MSB
sta SPRITE_X_MSB
rts
!zone movePlayerSpr
; ********************************************************************************
; MOVE PLAYER SPRITE
;
; Description: Update player sprite position.
; Player sprite ID = 0
; ********************************************************************************
movePlayerSpr
lda player.PosX+1
ldx player.PosX+2
ldy playerSprite.OffsetX
jsr getSpriteX
; A register contains low byte of the x position
sta SPRITE_0_X_POS
; X register contains high byte of the x position
ldy #0 ; Sprite index = 0
jsr setSpriteXMSB
; Set Y position
lda player.PosY+1
ldx player.PosY+2
ldy playerSprite.OffsetY
jsr getSpriteY
; A register contains low byte of the y position
sta SPRITE_0_Y_POS
rts
!zone moveDirSpr
; ********************************************************************************
; MOVE DIRECTION SPRITE
;
; Description: Update direcition sprite position.
; Direction sprite ID = 2.
; ********************************************************************************
moveDirSpr:
lda player.DirVectX+1
sta B
lda player.DirVectX
clc
adc player.PosX+1
tay
lda B
adc player.PosX+2
tax ; X = hi-byte position
tya ; A = lo-byte position
ldy dirSprite.OffsetX ; Y = offset
; A register constains the low byte of the x position
jsr getSpriteX
sta SPRITE_2_X_POS
; X register contains high byte of the x position
ldy #2 ; Sprite index = 2
jsr setSpriteXMSB
lda player.DirVectY+1
sta B
lda player.DirVectY
clc
adc player.PosY+1
tay
lda B
adc player.PosY+2
tax ; X = hi-byte position
tya ; A = lo-byte position
ldy dirSprite.OffsetY ; Y = offset
jsr getSpriteY
sta SPRITE_2_Y_POS
rts
!zone moveRaySpr
; ********************************************************************************
; MOVE RAY SPRITE
;
; Inputs: raySignX
; rayDeltaX
;
; ********************************************************************************
moveRaySpr
lda raySignX
bpl .isPX
lda player.PosX+1 ; If raySignX < 0 then
sec ; x = player.PosX - rayDeltaX (B=lo-byte, A=hi-byte)
sbc rayDeltaX
sta B
lda player.PosX+2
sbc rayDeltaX+1
jmp .skip1
.isPX lda player.PosX+1 ; If raySignX >= 0 then
clc ; x = player.PosX + rayDeltaX (B=lo-byte, A=hi-byte)
adc rayDeltaX
sta B
lda player.PosX+2
adc rayDeltaX+1
.skip1 tax
lda B
ldy raySprite.OffsetX
jsr getSpriteX
sta SPRITE_1_X_POS ; Set the lo-byte position
; X register contains high byte of the x position
ldy #1 ; Sprite index = 1
jsr setSpriteXMSB ; Set the MSB byte of the X position
; Y axis
lda raySignY
bpl .isPY
lda player.PosY+1 ; If raySignY < 0 then
sec ; Y = plPosY - rayDeltaY (B=lo-byte, A=hi-byte)
sbc rayDeltaY
sta B
lda player.PosY+2
sbc rayDeltaY+1
jmp .skip2
.isPY lda player.PosY+1 ; If raySignY >= 0 then
clc ; B = plPosY + rayDeltaY (B=lo-byte, A=hi-byte)
adc rayDeltaY
sta B
lda player.PosY+2
adc rayDeltaY+1
.skip2 tax
lda B
ldy raySprite.OffsetY
jsr getSpriteY
sta SPRITE_1_Y_POS
rts
!zone moveBackSpr
; ********************************************************************************
; MOVE BACKGROUND SPRITE
;
;
; ********************************************************************************
moveBackSpr:
ldy #3
.loop tya
asl
tax
; DX = player.Heading + backSprite[x].Heading
lda backSprite.Heading,x
clc
adc player.Heading
sta DL
lda backSprite.Heading+1,x
adc player.Heading+1
; DX = DX * 2
asl DL
rol
; Limit angle between 0 and 2047 ($07ff)
and #%00000111
sta DH
; Check if DX < 320
lda DL
cmp #$50
lda DH
sbc #$01
bcc .isLess
; If greater than 320 then disable the sprite
lda bitMask,y
eor #$ff
and SPRITE_ENABLE
sta SPRITE_ENABLE
jmp .next
; DX < 320 and DX >=0
.isLess lda bitMask,y
ora SPRITE_ENABLE
sta SPRITE_ENABLE
lda DL
sta SPRITE_0_X_POS,x
lda DH
beq .noHiBit
; Hi bit is active
lda bitMask,y
ora SPRITE_X_MSB
sta SPRITE_X_MSB
jmp .next
; Hi bit is not active
.noHiBit lda bitMask,y
eor #$ff
and SPRITE_X_MSB
sta SPRITE_X_MSB
.next iny
cpy #6
bcc .loop
rts