entity.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_remove(Entity* entity) {
  45. furi_check(entity->collider != NULL, "Collider not added");
  46. free(entity->collider);
  47. entity->collider = NULL;
  48. entity->collider_dirty = false;
  49. }
  50. void entity_collider_offset_set(Entity* entity, Vector offset) {
  51. entity->collider_offset = offset;
  52. entity->collider_dirty = true;
  53. }
  54. Vector entity_collider_offset_get(Entity* entity) {
  55. return entity->collider_offset;
  56. }
  57. static Vector entity_collider_position_get(Entity* entity) {
  58. return (Vector){
  59. .x = entity->position.x + entity->collider_offset.x,
  60. .y = entity->position.y + entity->collider_offset.y,
  61. };
  62. }
  63. void entity_free(Entity* entity) {
  64. entities_count--;
  65. ENTITY_D("Freeing at %p", entity);
  66. if(entity->context) {
  67. free(entity->context);
  68. }
  69. if(entity->collider) {
  70. free(entity->collider);
  71. }
  72. free(entity);
  73. }
  74. const EntityDescription* entity_description_get(Entity* entity) {
  75. return entity->description;
  76. }
  77. Vector entity_pos_get(Entity* entity) {
  78. return entity->position;
  79. }
  80. void entity_pos_set(Entity* entity, Vector position) {
  81. entity->position = position;
  82. entity->collider_dirty = true;
  83. }
  84. void* entity_context_get(Entity* entity) {
  85. return entity->context;
  86. }
  87. void entity_call_start(Entity* entity, GameManager* manager) {
  88. if(entity->description && entity->description->start) {
  89. entity->description->start(entity, manager, entity->context);
  90. }
  91. }
  92. void entity_call_stop(Entity* entity, GameManager* manager) {
  93. if(entity->description && entity->description->stop) {
  94. entity->description->stop(entity, manager, entity->context);
  95. }
  96. }
  97. void entity_call_update(Entity* entity, GameManager* manager) {
  98. if(entity->description && entity->description->update) {
  99. entity->description->update(entity, manager, entity->context);
  100. }
  101. }
  102. void entity_call_render(Entity* entity, GameManager* manager, Canvas* canvas) {
  103. if(entity->description && entity->description->render) {
  104. entity->description->render(entity, manager, canvas, entity->context);
  105. }
  106. }
  107. void entity_call_collision(Entity* entity, Entity* other, GameManager* manager) {
  108. if(entity->description && entity->description->collision) {
  109. entity->description->collision(entity, other, manager, entity->context);
  110. }
  111. }
  112. bool entity_collider_circle_circle(Entity* entity, Entity* other) {
  113. Vector pos1 = entity_collider_position_get(entity);
  114. Vector pos2 = entity_collider_position_get(other);
  115. float dx = pos1.x - pos2.x;
  116. float dy = pos1.y - pos2.y;
  117. float distance = sqrtf(dx * dx + dy * dy);
  118. return distance < entity->collider->circle.radius + other->collider->circle.radius;
  119. }
  120. bool entity_collider_rect_rect(Entity* entity, Entity* other) {
  121. Vector pos1 = entity_collider_position_get(entity);
  122. Vector pos2 = entity_collider_position_get(other);
  123. float left1 = pos1.x - entity->collider->rect.half_width;
  124. float right1 = pos1.x + entity->collider->rect.half_width;
  125. float top1 = pos1.y - entity->collider->rect.half_height;
  126. float bottom1 = pos1.y + entity->collider->rect.half_height;
  127. float left2 = pos2.x - other->collider->rect.half_width;
  128. float right2 = pos2.x + other->collider->rect.half_width;
  129. float top2 = pos2.y - other->collider->rect.half_height;
  130. float bottom2 = pos2.y + other->collider->rect.half_height;
  131. return left1 < right2 && right1 > left2 && top1 < bottom2 && bottom1 > top2;
  132. }
  133. bool entity_collider_circle_rect(Entity* circle, Entity* rect) {
  134. Vector pos1 = entity_collider_position_get(circle);
  135. Vector pos2 = entity_collider_position_get(rect);
  136. float left = pos2.x - rect->collider->rect.half_width;
  137. float right = pos2.x + rect->collider->rect.half_width;
  138. float top = pos2.y - rect->collider->rect.half_height;
  139. float bottom = pos2.y + rect->collider->rect.half_height;
  140. float closestX = fmaxf(left, fminf(pos1.x, right));
  141. float closestY = fmaxf(top, fminf(pos1.y, bottom));
  142. float dx = pos1.x - closestX;
  143. float dy = pos1.y - closestY;
  144. float distance = sqrtf(dx * dx + dy * dy);
  145. return distance < circle->collider->circle.radius;
  146. }
  147. bool entity_collider_check_collision(Entity* entity, Entity* other) {
  148. furi_check(entity->collider);
  149. furi_check(other->collider);
  150. if(entity->collider->type == ColliderTypeCircle) {
  151. Entity* circle = entity;
  152. if(other->collider->type == ColliderTypeCircle) {
  153. return entity_collider_circle_circle(circle, other);
  154. } else {
  155. return entity_collider_circle_rect(circle, other);
  156. }
  157. } else {
  158. Entity* rect = entity;
  159. if(other->collider->type == ColliderTypeCircle) {
  160. return entity_collider_circle_rect(other, rect);
  161. } else {
  162. return entity_collider_rect_rect(rect, other);
  163. }
  164. }
  165. }
  166. bool entity_collider_exists(Entity* entity) {
  167. return entity->collider != NULL;
  168. }
  169. void entity_send_event(
  170. Entity* sender,
  171. Entity* receiver,
  172. GameManager* manager,
  173. uint32_t type,
  174. EntityEventValue value) {
  175. if(receiver->description && receiver->description->event) {
  176. EntityEvent event = {
  177. .type = type,
  178. .sender = sender,
  179. .value = value,
  180. };
  181. receiver->description->event(receiver, manager, event, receiver->context);
  182. }
  183. }