SG 1 год назад
Родитель
Сommit
514c1e4fff
2 измененных файлов с 36 добавлено и 9 удалено
  1. 22 7
      entity.c
  2. 14 2
      entity.h

+ 22 - 7
entity.c

@@ -3,7 +3,11 @@
 #include <stdlib.h>
 #include <furi.h>
 
-#define ENTITY_DEBUG(...) FURI_LOG_D("Entity", __VA_ARGS__)
+#ifdef ENTITY_DEBUG
+#define ENTITY_D(...) FURI_LOG_D("Entity", __VA_ARGS__)
+#else
+#define ENTITY_D(...)
+#endif
 
 static int32_t entities_count = 0;
 
@@ -27,13 +31,13 @@ typedef struct {
             float height;
         } rect;
     };
-} ColliderDescription;
+} Collider;
 
 struct Entity {
     Vector position;
     const EntityDescription* description;
     void* context;
-    ColliderDescription* collider;
+    Collider* collider;
 };
 
 Entity* entity_alloc(const EntityDescription* description) {
@@ -46,20 +50,20 @@ Entity* entity_alloc(const EntityDescription* description) {
         entity->context = malloc(description->context_size);
     }
     entity->collider = NULL;
-    ENTITY_DEBUG("Allocated at %p", entity);
+    ENTITY_D("Allocated at %p", entity);
     return entity;
 }
 
 void entity_collider_add_circle(Entity* entity, float radius) {
     furi_check(entity->collider == NULL, "Collider already added");
-    entity->collider = malloc(sizeof(ColliderDescription));
+    entity->collider = malloc(sizeof(Collider));
     entity->collider->type = ColliderTypeCircle;
     entity->collider->circle.radius = radius;
 }
 
 void entity_collider_add_rect(Entity* entity, float width, float height) {
     furi_check(entity->collider == NULL, "Collider already added");
-    entity->collider = malloc(sizeof(ColliderDescription));
+    entity->collider = malloc(sizeof(Collider));
     entity->collider->type = ColliderTypeRect;
     entity->collider->rect.width = width;
     entity->collider->rect.height = height;
@@ -67,7 +71,7 @@ void entity_collider_add_rect(Entity* entity, float width, float height) {
 
 void entity_free(Entity* entity) {
     entities_count--;
-    ENTITY_DEBUG("Freeing at %p", entity);
+    ENTITY_D("Freeing at %p", entity);
     if(entity->context) {
         free(entity->context);
     }
@@ -180,4 +184,15 @@ bool entity_collider_check_collision(Entity* entity, Entity* other) {
 
 bool entity_collider_exists(Entity* entity) {
     return entity->collider != NULL;
+}
+
+void entity_send_event(Entity* entity, uint32_t type, EntityEventValue value) {
+    if(entity->description && entity->description->event) {
+        EntityEvent event = {
+            .type = type,
+            .sender = entity,
+            .value = value,
+        };
+        entity->description->event(entity, NULL, event, entity->context);
+    }
 }

+ 14 - 2
entity.h

@@ -16,12 +16,24 @@ typedef struct {
 
 #define VECTOR_ZERO ((Vector){0, 0})
 
+typedef union {
+    uint32_t value;
+    void* pointer;
+} EntityEventValue;
+
+typedef struct {
+    uint32_t type;
+    Entity* sender;
+    EntityEventValue value;
+} EntityEvent;
+
 typedef struct {
     void (*start)(Entity* self, Level* level, void* context);
     void (*stop)(Entity* self, Level* level, void* context);
     void (*update)(Entity* self, Director* director, void* context);
     void (*render)(Entity* self, Director* director, Canvas* canvas, void* context);
     void (*collision)(Entity* self, Entity* other, Director* director, void* context);
+    void (*event)(Entity* self, Director* director, EntityEvent event, void* context);
     size_t context_size;
 } EntityDescription;
 
@@ -33,12 +45,12 @@ void entity_pos_set(Entity* entity, Vector position);
 
 void* entity_context_get(Entity* entity);
 
-typedef struct Collider Collider;
-
 void entity_collider_add_circle(Entity* entity, float radius);
 
 void entity_collider_add_rect(Entity* entity, float width, float height);
 
+void entity_send_event(Entity* entity, uint32_t type, EntityEventValue value);
+
 #ifdef __cplusplus
 }
 #endif