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.

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