lol its in c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.7 KiB

2 years ago
2 years ago
  1. #include "actor.h"
  2. #include <stddef.h>
  3. #include "game.h"
  4. #include "player.h"
  5. #include "sdk/oam.h"
  6. #include "util.h"
  7. #define MAX_UNIQUE_MOBS 3
  8. #define MAX_ACTORS 5
  9. #define NUM_OAM_ENTRIES 40
  10. #define TILE_WIDTH 8
  11. #define FRAMES_PER_ANIM_FRAME 20
  12. #define PLAYER_ACTOR_IDX 0
  13. static actor_anim_state_t ANIM_TOTAL_FRAMES[] = {2, 1};
  14. static uint8_t ANIM_OFFSETS[] = {0, 2 * 4};
  15. static uint8_t mob_ids[MAX_UNIQUE_MOBS];
  16. static uint8_t *mob_anim_data[MAX_UNIQUE_MOBS];
  17. static actor_t all_actors[MAX_ACTORS];
  18. static uint8_t actor_load_mob_anim_data(uint8_t mob_id);
  19. actor_t *PLAYER_ACTOR = &all_actors[PLAYER_ACTOR_IDX];
  20. void actor_init(void) {
  21. actor_reset();
  22. PLAYER_ACTOR->active = 1;
  23. PLAYER_ACTOR->flip = 0;
  24. PLAYER_ACTOR->anim = ANIM_STAND;
  25. PLAYER_ACTOR->frame_idx = 0;
  26. PLAYER_ACTOR->frame_counter = 0;
  27. PLAYER_ACTOR->x = PLAYER.x;
  28. PLAYER_ACTOR->y = PLAYER.y_hi;
  29. }
  30. void actor_reset(void) {
  31. for (uint8_t i = 0; i < ARRSIZE(mob_ids); ++i) {
  32. mob_ids[i] = 0xff;
  33. }
  34. for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) {
  35. all_actors[i].active = 0;
  36. }
  37. }
  38. actor_t *actor_create_mob(uint8_t id) {
  39. uint8_t mob_anim_idx = actor_load_mob_anim_data(id);
  40. if (mob_anim_idx == 0xff) {
  41. return NULL;
  42. }
  43. actor_t *a = NULL;
  44. for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) {
  45. if (all_actors[i].active) {
  46. continue;
  47. }
  48. a = &all_actors[i];
  49. a->active = 1;
  50. a->flip = 0;
  51. a->mob_anim_idx = mob_anim_idx;
  52. a->anim = ANIM_STAND;
  53. a->frame_idx = 0;
  54. a->frame_counter = 0;
  55. break;
  56. }
  57. return a;
  58. }
  59. void actor_remove(actor_t *a) { a->active = 0; }
  60. void actor_update(void) {
  61. struct oam_entry *oam_ptr = &shadow_oam[0];
  62. actor_t *a = &all_actors[0];
  63. for (uint8_t i = 0; i < ARRSIZE(all_actors); ++i) {
  64. if (!a->active) {
  65. a++;
  66. continue;
  67. }
  68. a->frame_counter++;
  69. if (a->frame_counter == FRAMES_PER_ANIM_FRAME) {
  70. a->frame_counter = 0;
  71. a->frame_idx = (a->frame_idx + 1) % ANIM_TOTAL_FRAMES[a->anim];
  72. }
  73. const uint8_t *anim_ptr;
  74. if (i == 0) {
  75. anim_ptr = &player_map[ANIM_OFFSETS[a->anim]];
  76. } else {
  77. anim_ptr = mob_anim_data[a->mob_anim_idx];
  78. }
  79. anim_ptr += a->frame_idx * 4;
  80. oam_ptr->y = a->y + 16;
  81. oam_ptr->x = a->x + 8;
  82. oam_ptr->tile = anim_ptr[a->flip] + TILE_INDEX_PLAYER;
  83. oam_ptr->attr = a->flip ? (1 << 5) : 0;
  84. oam_ptr++;
  85. oam_ptr->y = a->y + 16;
  86. oam_ptr->x = a->x + TILE_WIDTH + 8;
  87. oam_ptr->tile = anim_ptr[1 - a->flip] + TILE_INDEX_PLAYER;
  88. oam_ptr->attr = a->flip ? (1 << 5) : 0;
  89. oam_ptr++;
  90. oam_ptr->y = a->y + TILE_WIDTH + 16;
  91. oam_ptr->x = a->x + 8;
  92. oam_ptr->tile = anim_ptr[a->flip + 2] + TILE_INDEX_PLAYER;
  93. oam_ptr->attr = a->flip ? (1 << 5) : 0;
  94. oam_ptr++;
  95. oam_ptr->y = a->y + TILE_WIDTH + 16;
  96. oam_ptr->x = a->x + TILE_WIDTH + 8;
  97. oam_ptr->tile = anim_ptr[1 - a->flip + 2] + TILE_INDEX_PLAYER;
  98. oam_ptr->attr = a->flip ? (1 << 5) : 0;
  99. oam_ptr++;
  100. a++;
  101. }
  102. }
  103. static uint8_t actor_load_mob_anim_data(uint8_t mob_id) {
  104. uint8_t mob_anim_idx = 0xff;
  105. for (uint8_t i = 0; i < ARRSIZE(mob_ids); ++i) {
  106. if (mob_ids[i] == mob_id) {
  107. mob_anim_idx = i;
  108. break;
  109. }
  110. }
  111. if (mob_anim_idx == 0xff) {
  112. for (uint8_t i = 0; i < ARRSIZE(mob_ids); ++i) {
  113. if (mob_ids[i] == 0xff) {
  114. mob_ids[i] = mob_id;
  115. // TODO: Load data into mob_anim_data[i]
  116. mob_anim_idx = i;
  117. break;
  118. }
  119. }
  120. }
  121. return mob_anim_idx;
  122. }