|
@@ -3,34 +3,34 @@
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
#include <furi.h>
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
|
|
+#define ENTITY_DEBUG(...) FURI_LOG_D("Entity", __VA_ARGS__)
|
|
|
|
|
+
|
|
|
struct Entity {
|
|
struct Entity {
|
|
|
Vector position;
|
|
Vector position;
|
|
|
- EntityBehaviour behaviour;
|
|
|
|
|
|
|
+ const EntityBehaviour* behaviour;
|
|
|
void* context;
|
|
void* context;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-Entity* entity_alloc() {
|
|
|
|
|
|
|
+Entity* entity_alloc(const EntityBehaviour* behaviour) {
|
|
|
Entity* entity = malloc(sizeof(Entity));
|
|
Entity* entity = malloc(sizeof(Entity));
|
|
|
entity->position = VECTOR_ZERO;
|
|
entity->position = VECTOR_ZERO;
|
|
|
- entity->behaviour = ENTITY_BEHAVIOUR_EMPTY;
|
|
|
|
|
|
|
+ entity->behaviour = behaviour;
|
|
|
entity->context = NULL;
|
|
entity->context = NULL;
|
|
|
- FURI_LOG_D("Entity", "Allocated entity at %p", entity);
|
|
|
|
|
|
|
+ if(behaviour && behaviour->context_size > 0) {
|
|
|
|
|
+ entity->context = malloc(behaviour->context_size);
|
|
|
|
|
+ }
|
|
|
|
|
+ ENTITY_DEBUG("Allocated entity at %p", entity);
|
|
|
return entity;
|
|
return entity;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void entity_free(Entity* entity) {
|
|
void entity_free(Entity* entity) {
|
|
|
- FURI_LOG_D("Entity", "Freeing entity at %p", entity);
|
|
|
|
|
|
|
+ ENTITY_DEBUG("Freeing entity at %p", entity);
|
|
|
|
|
+ if(entity->context) {
|
|
|
|
|
+ free(entity->context);
|
|
|
|
|
+ }
|
|
|
free(entity);
|
|
free(entity);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void entity_behaviour_set(Entity* entity, EntityBehaviour behaviour) {
|
|
|
|
|
- entity->behaviour = behaviour;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void entity_context_set(Entity* entity, void* context) {
|
|
|
|
|
- entity->context = context;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
Vector entity_pos_get(Entity* entity) {
|
|
Vector entity_pos_get(Entity* entity) {
|
|
|
return entity->position;
|
|
return entity->position;
|
|
|
}
|
|
}
|
|
@@ -39,35 +39,30 @@ void entity_pos_set(Entity* entity, Vector position) {
|
|
|
entity->position = position;
|
|
entity->position = position;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void entity_pos_add(Entity* entity, Vector delta) {
|
|
|
|
|
- entity->position.x += delta.x;
|
|
|
|
|
- entity->position.y += delta.y;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
void* entity_context_get(Entity* entity) {
|
|
void* entity_context_get(Entity* entity) {
|
|
|
return entity->context;
|
|
return entity->context;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void entity_call_start(Entity* entity) {
|
|
void entity_call_start(Entity* entity) {
|
|
|
- if(entity->behaviour.start) {
|
|
|
|
|
- entity->behaviour.start(entity);
|
|
|
|
|
|
|
+ if(entity->behaviour && entity->behaviour->start) {
|
|
|
|
|
+ entity->behaviour->start(entity->context);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void entity_call_stop(Entity* entity) {
|
|
void entity_call_stop(Entity* entity) {
|
|
|
- if(entity->behaviour.stop) {
|
|
|
|
|
- entity->behaviour.stop(entity->context);
|
|
|
|
|
|
|
+ if(entity->behaviour && entity->behaviour->stop) {
|
|
|
|
|
+ entity->behaviour->stop(entity->context);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void entity_call_update(Entity* entity, Director* director) {
|
|
void entity_call_update(Entity* entity, Director* director) {
|
|
|
- if(entity->behaviour.update) {
|
|
|
|
|
- entity->behaviour.update(entity, director, entity->context);
|
|
|
|
|
|
|
+ if(entity->behaviour && entity->behaviour->update) {
|
|
|
|
|
+ entity->behaviour->update(entity, director, entity->context);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void entity_call_render(Entity* entity, Director* director, Canvas* canvas) {
|
|
void entity_call_render(Entity* entity, Director* director, Canvas* canvas) {
|
|
|
- if(entity->behaviour.render) {
|
|
|
|
|
- entity->behaviour.render(entity, director, canvas, entity->context);
|
|
|
|
|
|
|
+ if(entity->behaviour && entity->behaviour->render) {
|
|
|
|
|
+ entity->behaviour->render(entity, director, canvas, entity->context);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|