aboutsummaryrefslogtreecommitdiff
path: root/entity/bunny.gbasm
blob: 8870dcc98103601139f19b6c0779b730148c327a (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
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
Move_Bunny:
	LD A, $mem_bunny_flags
	LD E, $01
	BIT 0, A
	JR Z, =.Normal_speed
	LD E, $02
	.Normal_speed:

	LD A, $00
	LD $mem_bunny_status_tile, A

	.Finish_unfreeze:
		LD A, $mem_moving_animation_step
		CP $00
		JR NZ, =.Finish_unfreeze.end
		LD A, $mem_bunny_status
		CP $04
		JR NZ, =.Finish_unfreeze.end
		LD A, $00
		LD $mem_bunny_status, A
	.Finish_unfreeze.end:

	.Freeze_shiver:
		LD A, $mem_bunny_status
		CP $01
		JR NZ, =.Freeze_shiver.end
		LD A, $f5
		LD $mem_bunny_status_tile, A
		LD A, $mem_moving_animation_step
		CP $00
		JP NZ, =.Freeze_shiver.end
		LD A, $mem_bunny_direction
		RES 3, A
		LD $mem_bunny_direction, A
		LD A, $00
		LD $mem_bunny_animation, A

		LD A, $mem_bunny_status_clear_turn_counter
		DEC A
		CP $00
		LD $mem_bunny_status_clear_turn_counter, A
		JR Z, =.Unfreeze

		CALL =RNG_Step
		CP $55
		JP NC, =.Skip_turn
		LD A, $04
		LD $mem_bunny_animation, A

		CALL =RNG_Step
		CP $55
		JP NC, =.Skip_turn

		.Unfreeze:
		LD A, $04
		LD $mem_bunny_status, A
		LD A, $mem_bunny_flags
		RES 1, A
		LD $mem_bunny_flags, A
		JP =.Skip_turn
	.Freeze_shiver.end:

	.Poison_effect:
		LD A, $mem_bunny_status
		CP $08
		JP NZ, =.Poison_effect.end
		LD A, $f6
		LD $mem_bunny_status_tile, A
		LD A, $mem_moving_animation_step
		CP $0c
		JR NZ, =.Poison_effect.end

		LD A, $mem_bunny_status_clear_turn_counter
		DEC A
		CP $00
		LD $mem_bunny_status_clear_turn_counter, A
		JR Z, =.Unpoison

		CALL =RNG_Step
		AND $07
		CP $00
		JR Z, =.Unpoison

		CALL =RNG_Step
		AND $01
		CP $00
		JR Z, =.Poison_effect.end

		PUSH BC
		LD A, $mem_moving_animation_step
		LD B, A
		LD A, $1f
		SUB B
		LD $mem_blinking_animation_counter, A
		.SET_SOUND_EFFECT =_sound_effect_Damage
		LD A, $mem_bunny_flags
		SET 3, A
		LD $mem_bunny_flags, A
		POP BC

		LD A, $mem_bunny_health
		SUB $01
		JR C, =.Poison_effect.health_underflow_fix
		DAA
		LD $mem_bunny_health, A
		JR =.Poison_effect.Skip_health_underflow_fix
		.Poison_effect.health_underflow_fix:
		LD A, $00
		LD $mem_bunny_health, A
		.Poison_effect.Skip_health_underflow_fix:

		PUSH BC
		LD A, $mem_bunny_predicted_x
		LD B, A
		LD A, $mem_bunny_predicted_y
		LD C, A
		LD A, $04
		PUSH DE
		LD E, $20
		LD D, $00
		CALL =Try_Launch_Animation
		POP DE
		POP BC
		JR =.Poison_effect.end

		.Unpoison:
		LD A, $00
		LD $mem_bunny_status, A
	.Poison_effect.end:

	.Should_turn_be_skipped:
		LD A, $mem_bunny_status
		BIT 0, A
		JR Z, =.no_skip
	.Skip_turn:
		LD A, $mem_map_loading_flags
		SET 3, A
		LD $mem_map_loading_flags, A

		RET

	.no_skip:

	.Start_action_or_movement:
		LD A, $mem_map_loading_flags
		BIT 4, A
		JP NZ, =.Start_action_or_movement.start_from_interaction

		BIT 3, A
		JP NZ, =.Start_action_or_movement.end

		LD A, $mem_requested_mode
		CP $enum_dungeon_mode
		JP NZ, =.Start_action_or_movement.end

		LD A, $mem_bunny_direction
		AND $70
		LD D, A
		SWAP A
		OR D
		LD $mem_bunny_direction, A

		.Start_action_or_movement.test_movement:

		LD A, $mem_button_direction
		CP $00
		JP Z, =.Start_action_or_movement.end
		LD D, A
		SWAP A
		OR D
		SET 3, A
		LD D, A

		LD A, $mem_map_loading_flags
		SET 3, A
		LD $mem_map_loading_flags, A

		LD A, $mem_button_action
		BIT 1, A
		JR NZ, =.Start_action_or_movement.cancel_movement

		LD A, D
		CALL =Get_Position_After_Move
		CALL =Is_Collisionable
		CALL =Carve_Entity_Collision_Map
		CP $00
		JR Z, =.Start_action_or_movement.not_collision
		.Start_action_or_movement.cancel_movement:
		RES 3, D

		LD A, $mem_map_loading_flags
		RES 3, A
		LD $mem_map_loading_flags, A

		.Start_action_or_movement.not_collision:
		LD A, D

		LD $mem_bunny_direction, A
		LD A, $00
		LD $mem_bunny_animation, A

		BIT 3, D
		JR Z, =.Start_action_or_movement.end

		LD A, B
		LD $mem_bunny_predicted_x, A
		LD A, C
		LD $mem_bunny_predicted_y, A

		JR =.Start_action_or_movement.end

		.Start_action_or_movement.start_from_interaction:
		LD A, $mem_map_loading_flags
		RES 4, A
		SET 3, A
		LD $mem_map_loading_flags, A
	.Start_action_or_movement.end:

	.Reset_Blinking_flag:
		LD A, $mem_map_loading_flags
		BIT 3, A
		JR Z, =.Reset_Blinking_flag.end
		LD A, $mem_moving_animation_step
		CP $00
		JR NZ, =.Reset_Blinking_flag.end
		LD A, $mem_bunny_flags
		RES 3, A
		LD $mem_bunny_flags, A
	.Reset_Blinking_flag.end:

	.Delayed_Attack:
		LD A, $mem_moving_animation_step
		CP $08
		JR NZ, =.Delayed_Attack.end

		LD A, $mem_map_loading_flags
		BIT 3, A
		JR Z, =.Delayed_Attack.end

		LD A, $mem_entity_being_attacked_low
		CP $00
		JR Z, =.Delayed_Attack.end

		ADD $06
		LD L, A
		LD H, high($mem_entities_list)
		LD A, (HL)
		SUB $01
		DAA
		JR C, =.skip_update_health
		LD (HL), A

		.skip_update_health:
		INC HL

		SET 3, (HL)
		LD A, $24
		LD $mem_blinking_animation_counter, A
		.SET_SOUND_EFFECT =_sound_effect_Damage

		LD A, $00
		LD $mem_entity_being_attacked_low, A
	.Delayed_Attack.end:

	.End_movement:
		LD A, $mem_map_loading_flags
		BIT 3, A
		JP Z, =.End_movement.end
		LD A, $mem_moving_animation_step
		CP $0f
		JP NZ, =.End_movement.end
		LD A, $mem_bunny_direction
		LD D, A
		RES 3, A
		BIT 3, D
		LD $mem_bunny_direction, A
		LD A, $00
		LD $mem_bunny_animation, A
		JP Z, =.End_movement.end

		LD A, $mem_bunny_x
		LD B, A
		LD A, $mem_bunny_y
		LD C, A
		LD A, E
		.ADD_A_TO_DIRECTION_BC
		LD A, B
		LD $mem_bunny_x, A
		LD A, C
		LD $mem_bunny_y, A

		LD A, $mem_bunny_flags
		RES 0, A
		LD $mem_bunny_flags, A

		PUSH DE
		.End_movement.Update_current_room:
			LD A, $ff
			LD $mem_bunny_current_room_idx, A

			LD HL, $mem_room_list

			.End_movement.Update_current_room.loop:
			LD A, $mem_bunny_x
			LD D, A
			LD E, (HL)
			CP E
			JR C, =.End_movement.Update_current_room.skip

			LD A, E
			INC HL
			ADD (HL)
			CP D
			JR C, =.End_movement.Update_current_room.skip

			INC HL

			LD A, $mem_bunny_y
			LD D, A
			LD E, (HL)
			CP E
			JR C, =.End_movement.Update_current_room.skip

			LD A, E
			INC HL
			ADD (HL)
			CP D
			JR C, =.End_movement.Update_current_room.skip

			LD A, L
			SUB $83
			SRA A
			SRA A

			LD $mem_bunny_current_room_idx, A
			JR =.End_movement.Update_current_room.end

			.End_movement.Update_current_room.skip:
			LD A, L
			AND $fc
			ADD $04
			LD L, A
			CP $a0
		JR NZ, =.End_movement.Update_current_room.loop
		.End_movement.Update_current_room.end:
		POP DE

		LD A, $mem_bunny_x
		LD $mem_bunny_predicted_x, A
		LD A, $mem_bunny_y
		LD $mem_bunny_predicted_y, A

		LD A, $mem_bunny_next_mana_regen
		DEC A
		AND $07
		LD $mem_bunny_next_mana_regen, A
		CP $00
		JR NZ, =.End_movement.skip_mana_regen
		LD A, $mem_bunny_mana
		INC A
		DAA
		JR C, =.End_movement.skip_mana_regen
		CP $31
		JR NC, =.End_movement.skip_mana_regen
		LD $mem_bunny_mana, A

		.End_movement.skip_mana_regen:

	.End_movement.end:
	
	.Interaction:
		LD A, $mem_map_loading_flags
		BIT 3, A
		JP NZ, =.Interaction.end
		.Interaction.test_action:
		LD A, $mem_button_action
		LD B, A
		LD A, $mem_last_button_action
		XOR B
		AND B
		CP $00
		JP Z, =.Interaction.end
		BIT 0, A
		JP Z, =.Interaction.end

		LD A, $mem_bunny_x
		LD B, A
		LD A, $mem_bunny_y
		LD C, A
		LD A, $mem_bunny_direction
		SET 3, A
		AND $0f
		LD D, A
		LD A, $01
		.ADD_A_TO_DIRECTION_BC

		LD HL, $mem_entities_list
		.Interaction.entities_loop:
			LD A, L
			AND $f0
			ADD $10
			LD L, A
			CP $00
			JR Z, =.Interaction.for_else

			LD A, (HL+)
			CP $00
			JR Z, =.Interaction.entities_loop.next

			LD A, (HL+)
			CP B
			JR NZ, =.Interaction.entities_loop.next

			LD A, (HL+)
			CP C
			JR NZ, =.Interaction.entities_loop.next

			INC HL
			INC HL
			PUSH HL
			PUSH BC

			LD BC, =Interaction_Jump_table
			LD A, (HL)

			.JUMP_TABLE

			POP BC
			POP HL
			JR =.Interaction.end

			.Interaction.entities_loop.next:
			JR =.Interaction.entities_loop

			.Interaction.for_else:
			LD A, $mem_bunny_direction
			RES 3, A
			LD $mem_bunny_direction, A
			LD A, $01
			LD $mem_bunny_animation, A

			LD A, $mem_map_loading_flags
			SET 4, A
			LD $mem_map_loading_flags, A
	.Interaction.end:

	.Check_End_Action:
		LD A, $mem_bunny_animation
		CP $01
		JR NZ, =.Check_End_Action.end

	.Check_End_Action.end:

	RET

Center_viewport_around_entity: ; Entity pointer in HL
	INC HL
	LD A, (HL)
	SUB $05
	LD $mem_viewport_x, A

	LD A, (HL+)
	LD B, A

	LD A, (HL)
	SUB $05
	LD $mem_viewport_y, A

	LD A, (HL+)
	LD C, A
	LD D, (HL)
	LD E, $00
	BIT 3, D
	JR Z, =.moving_animation_end
	LD A, $mem_moving_animation_step
	LD E, A
	LD A, L
	AND $f0
	OR $07
	LD L, A
	BIT 0, (HL)
	JR Z, =.moving_animation_end
	SLA E
	.moving_animation_end:
	
	LD A, B
	SWAP A
	AND $f0
	SUB $48
	LD B, A
	
	LD A, C
	SWAP A
	AND $f0
	SUB $48
	LD C, A

	LD A, E
	.ADD_A_TO_DIRECTION_BC
	
	LD A, B
	LD $mem_prepared_viewport_x, A
	LD A, C
	LD $mem_prepared_viewport_y, A

	RET