-
Notifications
You must be signed in to change notification settings - Fork 813
/
core.asm
633 lines (574 loc) · 9.98 KB
/
core.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
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
ClearSpriteAnims:
ld hl, wSpriteAnimData
ld bc, wSpriteAnimDataEnd - wSpriteAnimData
.loop
ld [hl], 0
inc hl
dec bc
ld a, c
or b
jr nz, .loop
ret
PlaySpriteAnimationsAndDelayFrame:
call PlaySpriteAnimations
call DelayFrame
ret
PlaySpriteAnimations:
push hl
push de
push bc
push af
ld a, LOW(wShadowOAM)
ld [wCurSpriteOAMAddr], a
call DoNextFrameForAllSprites
pop af
pop bc
pop de
pop hl
ret
DoNextFrameForAllSprites:
ld hl, wSpriteAnimationStructs
ld e, NUM_SPRITE_ANIM_STRUCTS
.loop
ld a, [hl]
and a
jr z, .next ; This struct is deinitialized.
ld c, l
ld b, h
push hl
push de
call DoSpriteAnimFrame ; Uses a massive dw
call UpdateAnimFrame
pop de
pop hl
jr c, .done
.next
ld bc, SPRITEANIMSTRUCT_LENGTH
add hl, bc
dec e
jr nz, .loop
ld a, [wCurSpriteOAMAddr]
ld l, a
ld h, HIGH(wShadowOAM)
.loop2 ; Clear (wShadowOAM + [wCurSpriteOAMAddr] --> wShadowOAMEnd)
ld a, l
cp LOW(wShadowOAMEnd)
jr nc, .done
xor a
ld [hli], a
jr .loop2
.done
ret
DoNextFrameForFirst16Sprites:
ld hl, wSpriteAnimationStructs
ld e, NUM_SPRITE_ANIM_STRUCTS
.loop
ld a, [hl]
and a
jr z, .next
ld c, l
ld b, h
push hl
push de
call DoSpriteAnimFrame ; Uses a massive dw
call UpdateAnimFrame
pop de
pop hl
jr c, .done
.next
ld bc, SPRITEANIMSTRUCT_LENGTH
add hl, bc
dec e
jr nz, .loop
ld a, [wCurSpriteOAMAddr]
ld l, a
ld h, HIGH(wShadowOAMSprite16)
.loop2 ; Clear (wShadowOAM + [wCurSpriteOAMAddr] --> Sprites + $40)
ld a, l
cp LOW(wShadowOAMSprite16)
jr nc, .done
xor a
ld [hli], a
jr .loop2
.done
ret
_InitSpriteAnimStruct::
; Initialize animation a at pixel x=e, y=d
; Find if there's any room in the wSpriteAnimationStructs array, which is 10x16
push de
push af
ld hl, wSpriteAnimationStructs
ld e, NUM_SPRITE_ANIM_STRUCTS
.loop
ld a, [hl]
and a
jr z, .found
ld bc, SPRITEANIMSTRUCT_LENGTH
add hl, bc
dec e
jr nz, .loop
; We've reached the end. There is no more room here.
; Return carry.
pop af
pop de
scf
ret
.found
; Back up the structure address to bc.
ld c, l
ld b, h
; Increment [wSpriteAnimCount], skipping a 0 value.
ld hl, wSpriteAnimCount
inc [hl]
ld a, [hl]
and a
jr nz, .nonzero
inc [hl]
.nonzero
; Get row a of SpriteAnimObjects, copy the pointer into de
pop af
ld e, a
ld d, 0
ld hl, SpriteAnimObjects
add hl, de
add hl, de
add hl, de
ld e, l
ld d, h
; Set hl to the first field (field 0) in the current structure.
ld hl, SPRITEANIMSTRUCT_INDEX
add hl, bc
; Load the index.
ld a, [wSpriteAnimCount]
ld [hli], a ; SPRITEANIMSTRUCT_INDEX
; Copy the table entry to the next two fields.
ld a, [de]
ld [hli], a ; SPRITEANIMSTRUCT_FRAMESET_ID
inc de
ld a, [de]
ld [hli], a ; SPRITEANIMSTRUCT_ANIM_SEQ_ID
inc de
; Look up the third field in the wSpriteAnimDict mapping.
; Take the mapped value and load it in.
ld a, [de]
call GetSpriteAnimVTile
ld [hli], a ; SPRITEANIMSTRUCT_TILE_ID
pop de
; Set hl to field 4 (X coordinate). Kinda pointless, because we're presumably already here.
ld hl, SPRITEANIMSTRUCT_XCOORD
add hl, bc
; Load the original value of de into here.
ld a, e
ld [hli], a ; SPRITEANIMSTRUCT_XCOORD
ld a, d
ld [hli], a ; SPRITEANIMSTRUCT_YCOORD
; load 0 into the next four fields
xor a
ld [hli], a ; SPRITEANIMSTRUCT_XOFFSET
ld [hli], a ; SPRITEANIMSTRUCT_YOFFSET
xor a
ld [hli], a ; SPRITEANIMSTRUCT_DURATION
ld [hli], a ; SPRITEANIMSTRUCT_DURATIONOFFSET
; load -1 into the next field
dec a
ld [hli], a ; SPRITEANIMSTRUCT_FRAME
; load 0 into the last five fields
xor a
ld [hli], a ; SPRITEANIMSTRUCT_JUMPTABLE_INDEX
ld [hli], a ; SPRITEANIMSTRUCT_VAR1
ld [hli], a ; SPRITEANIMSTRUCT_VAR2
ld [hli], a ; SPRITEANIMSTRUCT_VAR3
ld [hl], a ; SPRITEANIMSTRUCT_VAR4
; back up the address of the first field to wSpriteAnimAddrBackup
ld a, c
ld [wSpriteAnimAddrBackup], a
ld a, b
ld [wSpriteAnimAddrBackup + 1], a
ret
DeinitializeSprite:
; Clear the index field of the struct in bc.
ld hl, SPRITEANIMSTRUCT_INDEX
add hl, bc
ld [hl], 0
ret
DeinitializeAllSprites:
; Clear the index field of every struct in the wSpriteAnimationStructs array.
ld hl, wSpriteAnimationStructs
ld bc, SPRITEANIMSTRUCT_LENGTH
ld e, NUM_SPRITE_ANIM_STRUCTS
xor a
.loop
ld [hl], a
add hl, bc
dec e
jr nz, .loop
ret
UpdateAnimFrame:
call InitSpriteAnimBuffer ; init WRAM
call GetSpriteAnimFrame ; read from a memory array
cp oamwait_command
jr z, .done
cp oamdelete_command
jr z, .delete
call GetFrameOAMPointer
; add byte to [wCurAnimVTile]
ld a, [wCurAnimVTile]
add [hl]
ld [wCurAnimVTile], a
inc hl
; load pointer into hl
ld a, [hli]
ld h, [hl]
ld l, a
push bc
ld a, [wCurSpriteOAMAddr]
ld e, a
ld d, HIGH(wShadowOAM)
ld a, [hli]
ld c, a ; number of objects
.loop
; first byte: y (px)
; [de] = [wCurAnimYCoord] + [wCurAnimYOffset] + [wGlobalAnimYOffset] + AddOrSubtractY([hl])
ld a, [wCurAnimYCoord]
ld b, a
ld a, [wCurAnimYOffset]
add b
ld b, a
ld a, [wGlobalAnimYOffset]
add b
ld b, a
call AddOrSubtractY
add b
ld [de], a
inc hl
inc de
; second byte: x (px)
; [de] = [wCurAnimXCoord] + [wCurAnimXOffset] + [wGlobalAnimXOffset] + AddOrSubtractX([hl])
ld a, [wCurAnimXCoord]
ld b, a
ld a, [wCurAnimXOffset]
add b
ld b, a
ld a, [wGlobalAnimXOffset]
add b
ld b, a
call AddOrSubtractX
add b
ld [de], a
inc hl
inc de
; third byte: vtile
; [de] = [wCurAnimVTile] + [hl]
ld a, [wCurAnimVTile]
add [hl]
ld [de], a
inc hl
inc de
; fourth byte: attributes
; [de] = GetSpriteOAMAttr([hl])
call GetSpriteOAMAttr
ld [de], a
inc hl
inc de
ld a, e
ld [wCurSpriteOAMAddr], a
cp LOW(wShadowOAMEnd)
jr nc, .reached_the_end
dec c
jr nz, .loop
pop bc
jr .done
.delete
; Removes the object from the screen, as opposed to `oamend` which just stops all motion
call DeinitializeSprite
.done
and a
ret
.reached_the_end
pop bc
scf
ret
AddOrSubtractY:
push hl
ld a, [hl]
ld hl, wCurSpriteOAMFlags
bit OAM_Y_FLIP, [hl]
jr z, .ok
; -8 - a
add 8
xor $ff
inc a
.ok
pop hl
ret
AddOrSubtractX:
push hl
ld a, [hl]
ld hl, wCurSpriteOAMFlags
bit OAM_X_FLIP, [hl]
jr z, .ok
; -8 - a
add 8
xor $ff
inc a
.ok
pop hl
ret
GetSpriteOAMAttr:
ld a, [wCurSpriteOAMFlags]
ld b, a
ld a, [hl]
xor b
and PRIORITY | Y_FLIP | X_FLIP
ld b, a
ld a, [hl]
and ~(PRIORITY | Y_FLIP | X_FLIP)
or b
ret
InitSpriteAnimBuffer:
xor a
ld [wCurSpriteOAMFlags], a
ld hl, SPRITEANIMSTRUCT_TILE_ID
add hl, bc
ld a, [hli]
ld [wCurAnimVTile], a
ld a, [hli]
ld [wCurAnimXCoord], a
ld a, [hli]
ld [wCurAnimYCoord], a
ld a, [hli]
ld [wCurAnimXOffset], a
ld a, [hli]
ld [wCurAnimYOffset], a
ret
GetSpriteAnimVTile:
; a = wSpriteAnimDict[a] if a in wSpriteAnimDict else vtile offset $00
push hl
push bc
ld hl, wSpriteAnimDict
ld b, a
ld c, NUM_SPRITEANIMDICT_ENTRIES
.loop
ld a, [hli]
cp b
jr z, .ok
inc hl
dec c
jr nz, .loop
xor a
jr .done
.ok
ld a, [hl]
.done
pop bc
pop hl
ret
_ReinitSpriteAnimFrame::
ld hl, SPRITEANIMSTRUCT_FRAMESET_ID
add hl, bc
ld [hl], a
ld hl, SPRITEANIMSTRUCT_DURATION
add hl, bc
ld [hl], 0
ld hl, SPRITEANIMSTRUCT_FRAME
add hl, bc
ld [hl], -1
ret
GetSpriteAnimFrame:
.loop
ld hl, SPRITEANIMSTRUCT_DURATION
add hl, bc
ld a, [hl]
and a
jr z, .next_frame
dec [hl]
call .GetPointer
ld a, [hli]
push af
jr .okay
.next_frame
ld hl, SPRITEANIMSTRUCT_FRAME
add hl, bc
inc [hl]
call .GetPointer
ld a, [hli]
cp oamrestart_command
jr z, .restart
cp oamend_command
jr z, .repeat_last
push af
ld a, [hl]
push hl
and ~(Y_FLIP << 1 | X_FLIP << 1)
ld hl, SPRITEANIMSTRUCT_DURATIONOFFSET
add hl, bc
add [hl]
ld hl, SPRITEANIMSTRUCT_DURATION
add hl, bc
ld [hl], a
pop hl
.okay
ld a, [hl]
and Y_FLIP << 1 | X_FLIP << 1 ; The << 1 is compensated in the "oamframe" macro
srl a
ld [wCurSpriteOAMFlags], a
pop af
ret
.repeat_last
xor a
ld hl, SPRITEANIMSTRUCT_DURATION
add hl, bc
ld [hl], a
ld hl, SPRITEANIMSTRUCT_FRAME
add hl, bc
dec [hl]
dec [hl]
jr .loop
.restart
xor a
ld hl, SPRITEANIMSTRUCT_DURATION
add hl, bc
ld [hl], a
dec a
ld hl, SPRITEANIMSTRUCT_FRAME
add hl, bc
ld [hl], a
jr .loop
.GetPointer:
ld hl, SPRITEANIMSTRUCT_FRAMESET_ID
add hl, bc
ld e, [hl]
ld d, 0
ld hl, SpriteAnimFrameData
add hl, de
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, SPRITEANIMSTRUCT_FRAME
add hl, bc
ld l, [hl]
ld h, 0
add hl, hl
add hl, de
ret
GetFrameOAMPointer:
ld e, a
ld d, 0
ld hl, SpriteAnimOAMData
add hl, de
add hl, de
add hl, de
ret
UnusedLoadSpriteAnimGFX: ; unreferenced
push hl
ld l, a
ld h, 0
add hl, hl
add hl, hl
ld de, UnusedSpriteAnimGFX
add hl, de
ld c, [hl]
inc hl
ld b, [hl]
inc hl
ld e, [hl]
inc hl
ld d, [hl]
pop hl
push bc
call Request2bpp
pop bc
ret
INCLUDE "data/sprite_anims/objects.asm"
INCLUDE "engine/sprite_anims/functions.asm"
INCLUDE "data/sprite_anims/framesets.asm"
INCLUDE "data/sprite_anims/oam.asm"
INCLUDE "data/sprite_anims/unused_gfx.asm"
Sprites_Cosine:
; a = d * cos(a * pi/32)
add %010000 ; cos(x) = sin(x + pi/2)
; fallthrough
Sprites_Sine:
; a = d * sin(a * pi/32)
calc_sine_wave
AnimateEndOfExpBar:
ldh a, [hSGB]
ld de, EndOfExpBarGFX
and a
jr z, .load
ld de, SGBEndOfExpBarGFX
.load
ld hl, vTiles0 tile $00
lb bc, BANK(EndOfExpBarGFX), 1
call Request2bpp
ld c, 8
ld d, 0
.loop
push bc
call .AnimateFrame
call DelayFrame
pop bc
inc d
inc d
dec c
jr nz, .loop
call ClearSprites
ret
.AnimateFrame:
ld hl, wShadowOAMSprite00
ld c, 8 ; number of animated circles
.anim_loop
ld a, c
and a
ret z
dec c
ld a, c
; multiply by 8
sla a
sla a
sla a
push af
push de
push hl
call Sprites_Sine
pop hl
pop de
add 13 * TILE_WIDTH
ld [hli], a ; y
pop af
push de
push hl
call Sprites_Cosine
pop hl
pop de
add 10 * TILE_WIDTH + 4
ld [hli], a ; x
ld a, $0
ld [hli], a ; tile id
ld a, PAL_BATTLE_OB_BLUE
ld [hli], a ; attributes
jr .anim_loop
EndOfExpBarGFX:
INCBIN "gfx/battle/expbarend.2bpp"
SGBEndOfExpBarGFX:
INCBIN "gfx/battle/expbarend_sgb.2bpp"
ClearSpriteAnims2:
push hl
push de
push bc
push af
ld hl, wSpriteAnimData
ld bc, wSpriteAnimDataEnd - wSpriteAnimData
.loop
ld [hl], 0
inc hl
dec bc
ld a, c
or b
jr nz, .loop
pop af
pop bc
pop de
pop hl
ret