entity.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "entity.h"
  2. #include "entity_i.h"
  3. #include <stdlib.h>
  4. #include <furi.h>
  5. #ifdef ENTITY_DEBUG
  6. #define ENTITY_D(...) FURI_LOG_D("Entity", __VA_ARGS__)
  7. #else
  8. #define ENTITY_D(...)
  9. #endif
  10. static int32_t entities_count = 0;
  11. int32_t entities_get_count(void) {
  12. return entities_count;
  13. }
  14. Entity* entity_alloc(const EntityDescription* description) {
  15. entities_count++;
  16. Entity* entity = malloc(sizeof(Entity));
  17. entity->position = VECTOR_ZERO;
  18. entity->description = description;
  19. entity->context = NULL;
  20. if(description && description->context_size > 0) {
  21. entity->context = malloc(description->context_size);
  22. }
  23. entity->collider = NULL;
  24. entity->collider_offset = VECTOR_ZERO;
  25. ENTITY_D("Allocated at %p", entity);
  26. entity->collider_dirty = false;
  27. return entity;
  28. }
  29. void entity_collider_add_circle(Entity* entity, float radius) {
  30. furi_check(entity->collider == NULL, "Collider already added");
  31. entity->collider = malloc(sizeof(Collider));
  32. entity->collider->type = ColliderTypeCircle;
  33. entity->collider->circle.radius = radius;
  34. entity->collider_dirty = true;
  35. }
  36. void entity_collider_add_rect(Entity* entity, float width, float height) {
  37. furi_check(entity->collider == NULL, "Collider already added");
  38. entity->collider = malloc(sizeof(Collider));
  39. entity->collider->type = ColliderTypeRect;
  40. entity->collider->rect.half_width = width / 2;
  41. entity->collider->rect.half_height = height / 2;
  42. entity->collider_dirty = true;
  43. }
  44. void entity_collider_offset_set(Entity* entity, Vector offset) {
  45. entity->collider_offset = offset;
  46. entity->collider_dirty = true;
  47. }
  48. Vector entity_collider_offset_get(Entity* entity) {
  49. return entity->collider_offset;
  50. }
  51. static Vector entity_collider_position_get(Entity* entity) {
  52. return (Vector){
  53. .x = entity->position.x + entity->collider_offset.x,
  54. .y = entity->position.y + entity->collider_offset.y,
  55. };
  56. }
  57. void entity_free(Entity* entity) {
  58. entities_count--;
  59. ENTITY_D("Freeing at %p", entity);
  60. if(entity->context) {
  61. free(entity->context);
  62. }
  63. if(entity->collider) {
  64. free(entity->collider);
  65. }
  66. free(entity);
  67. }
  68. const EntityDescription* entity_description_get(Entity* entity) {
  69. return entity->description;
  70. }
  71. Vector entity_pos_get(Entity* entity) {
  72. return entity->position;
  73. }
  74. void entity_pos_set(Entity* entity, Vector position) {
  75. entity->position = position;
  76. entity->collider_dirty = true;
  77. }
  78. void* entity_context_get(Entity* entity) {
  79. return entity->context;
  80. }
  81. void entity_call_start(Level* level, Entity* entity) {
  82. if(entity->description && entity->description->start) {
  83. entity->description->start(entity, level, entity->context);
  84. }
  85. }
  86. void entity_call_stop(Level* level, Entity* entity) {
  87. if(entity->description && entity->description->stop) {
  88. entity->description->stop(entity, level, entity->context);
  89. }
  90. }
  91. void entity_call_update(Entity* entity, GameManager* manager) {
  92. if(entity->description && entity->description->update) {
  93. entity->description->update(entity, manager, entity->context);
  94. }
  95. }
  96. void entity_call_render(Entity* entity, GameManager* manager, Canvas* canvas) {
  97. if(entity->description && entity->description->render) {
  98. entity->description->render(entity, manager, canvas, entity->context);
  99. }
  100. }
  101. void entity_call_collision(Entity* entity, Entity* other, GameManager* manager) {
  102. if(entity->description && entity->description->collision) {
  103. entity->description->collision(entity, other, manager, entity->context);
  104. }
  105. }
  106. bool entity_collider_circle_circle(Entity* entity, Entity* other) {
  107. Vector pos1 = entity_collider_position_get(entity);
  108. Vector pos2 = entity_collider_position_get(other);
  109. float dx = pos1.x - pos2.x;
  110. float dy = pos1.y - pos2.y;
  111. float distance = sqrtf(dx * dx + dy * dy);
  112. return distance < entity->collider->circle.radius + other->collider->circle.radius;
  113. }
  114. bool entity_collider_rect_rect(Entity* entity, Entity* other) {
  115. Vector pos1 = entity_collider_position_get(entity);
  116. Vector pos2 = entity_collider_position_get(other);
  117. float left1 = pos1.x - entity->collider->rect.half_width;
  118. float right1 = pos1.x + entity->collider->rect.half_width;
  119. float top1 = pos1.y - entity->collider->rect.half_height;
  120. float bottom1 = pos1.y + entity->collider->rect.half_height;
  121. float left2 = pos2.x - other->collider->rect.half_width;
  122. float right2 = pos2.x + other->collider->rect.half_width;
  123. float top2 = pos2.y - other->collider->rect.half_height;
  124. float bottom2 = pos2.y + other->collider->rect.half_height;
  125. return left1 < right2 && right1 > left2 && top1 < bottom2 && bottom1 > top2;
  126. }
  127. bool entity_collider_circle_rect(Entity* entity, Entity* other) {
  128. Vector pos1 = entity_collider_position_get(entity);
  129. Vector pos2 = entity_collider_position_get(other);
  130. float left = pos2.x - other->collider->rect.half_width;
  131. float right = pos2.x + other->collider->rect.half_width;
  132. float top = pos2.y - other->collider->rect.half_height;
  133. float bottom = pos2.y + other->collider->rect.half_height;
  134. float closestX = fmaxf(left, fminf(pos1.x, right));
  135. float closestY = fmaxf(top, fminf(pos1.y, bottom));
  136. float dx = pos1.x - closestX;
  137. float dy = pos1.y - closestY;
  138. float distance = sqrtf(dx * dx + dy * dy);
  139. return distance < entity->collider->circle.radius;
  140. }
  141. bool entity_collider_check_collision(Entity* entity, Entity* other) {
  142. furi_check(entity->collider);
  143. furi_check(other->collider);
  144. // if(entity->description == other->description) {
  145. // return false;
  146. // }
  147. if(entity->collider->type == ColliderTypeCircle) {
  148. if(other->collider->type == ColliderTypeCircle) {
  149. return entity_collider_circle_circle(entity, other);
  150. } else {
  151. return entity_collider_circle_rect(entity, other);
  152. }
  153. } else {
  154. if(other->collider->type == ColliderTypeCircle) {
  155. return entity_collider_circle_rect(other, entity);
  156. } else {
  157. return entity_collider_rect_rect(entity, other);
  158. }
  159. }
  160. }
  161. bool entity_collider_exists(Entity* entity) {
  162. return entity->collider != NULL;
  163. }
  164. void entity_send_event(Entity* entity, uint32_t type, EntityEventValue value) {
  165. if(entity->description && entity->description->event) {
  166. EntityEvent event = {
  167. .type = type,
  168. .sender = entity,
  169. .value = value,
  170. };
  171. entity->description->event(entity, NULL, event, entity->context);
  172. }
  173. }