entity.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "entity.h"
  2. #include "entity_i.h"
  3. #include <stdlib.h>
  4. #include <furi.h>
  5. #define ENTITY_DEBUG(...) FURI_LOG_D("Entity", __VA_ARGS__)
  6. struct Entity {
  7. Vector position;
  8. const EntityBehaviour* behaviour;
  9. void* context;
  10. };
  11. Entity* entity_alloc(const EntityBehaviour* behaviour) {
  12. Entity* entity = malloc(sizeof(Entity));
  13. entity->position = VECTOR_ZERO;
  14. entity->behaviour = behaviour;
  15. entity->context = NULL;
  16. if(behaviour && behaviour->context_size > 0) {
  17. entity->context = malloc(behaviour->context_size);
  18. }
  19. ENTITY_DEBUG("Allocated entity at %p", entity);
  20. return entity;
  21. }
  22. void entity_free(Entity* entity) {
  23. ENTITY_DEBUG("Freeing entity at %p", entity);
  24. if(entity->context) {
  25. free(entity->context);
  26. }
  27. free(entity);
  28. }
  29. Vector entity_pos_get(Entity* entity) {
  30. return entity->position;
  31. }
  32. void entity_pos_set(Entity* entity, Vector position) {
  33. entity->position = position;
  34. }
  35. void* entity_context_get(Entity* entity) {
  36. return entity->context;
  37. }
  38. void entity_call_start(Entity* entity) {
  39. if(entity->behaviour && entity->behaviour->start) {
  40. entity->behaviour->start(entity->context);
  41. }
  42. }
  43. void entity_call_stop(Entity* entity) {
  44. if(entity->behaviour && entity->behaviour->stop) {
  45. entity->behaviour->stop(entity->context);
  46. }
  47. }
  48. void entity_call_update(Entity* entity, Director* director) {
  49. if(entity->behaviour && entity->behaviour->update) {
  50. entity->behaviour->update(entity, director, entity->context);
  51. }
  52. }
  53. void entity_call_render(Entity* entity, Director* director, Canvas* canvas) {
  54. if(entity->behaviour && entity->behaviour->render) {
  55. entity->behaviour->render(entity, director, canvas, entity->context);
  56. }
  57. }