entity.c 6.1 KB

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