#include "entity.h" #include "game.h" #include "entitiesInclude.h" const EntityEntry entityEntries[ENTITY_COUNT] = { (EntityEntry){initOldMint, updateOldMint, NULL, false, true}, (EntityEntry){initStickyNickel, updateStickyNickel, NULL, false, true}, (EntityEntry){initTree, updateTree, NULL, false, true}, (EntityEntry){initBush, updateBush, NULL, false, true}, (EntityEntry){initFlower, updateFlower, NULL, false, true}, (EntityEntry){initPond, updatePond, NULL, true, true}, (EntityEntry){initUtilityPole, NULL, NULL, false, false}, (EntityEntry){initSamantha, updateSamantha, NULL, false, true}, (EntityEntry){initSamanthasSpot, updateSamanthasSpot, NULL, true, false}, (EntityEntry){initTrashcan, updateTrashcan, NULL, false, true}, (EntityEntry){initTrash, updateTrash, NULL, false, true}, (EntityEntry){initMedicalTrash, updateMedicalTrash, NULL, false, true}, (EntityEntry){initJohn, updateJohn, NULL, false, true}, (EntityEntry){initRon, updateRon, NULL, false, true} }; Entity createEntity(EntityId id, Vector3 position) { Entity entity; entity.id = id; entity.state = ENTITY_DEFAULT_STATE; entity.data = NULL; // Run init callback. InitEntityCallback initCallback = entityEntries[id].initCallback; if (initCallback != NULL) { initCallback(&entity); } entity.position = Vector3Zero(); setEntityPosition(&entity, position); return entity; } void updateEntity(Entity* entity, Game* game) { //DrawBoundingBox(entity->box, BLUE); UpdateEntityCallback updateCallback = entityEntries[entity->id].updateCallback; if (updateCallback != NULL) { updateCallback(entity, game); } } void closeEntity(Entity* entity) { if (entity->id == ENTITY_NONE) { return; } CloseEntityCallback closeCallback = entityEntries[entity->id].closeCallback; if (closeCallback != NULL) { closeCallback(entity); } entity->id = ENTITY_NONE; } void setEntityPosition(Entity* entity, Vector3 position) { Vector3 movedBy = Vector3Subtract(position, entity->position); entity->position = position; entity->box.min = Vector3Add(entity->box.min, movedBy); entity->box.max = Vector3Add(entity->box.max, movedBy); } void placeEntityOnGround(Entity* entity, const World* world) { Vector3 position = entity->position; position.y = getWorldHeightAtLocation( world, entity->position.x, entity->position.z); position.y += (entity->box.max.y - entity->box.min.y) / 2.0; setEntityPosition(entity, position); } bool entityIsPlace(EntityId id) { return entityEntries[id].isPlace; } bool entityCanBeSelected(EntityId id) { return entityEntries[id].canBeSelected; } InteractionCommand interactWithOldMint(Entity* entity, Game* game, Selection selection) { if (selection == SELECTION_INTERACT) { // Run display message code. return INTERACTION_TALK; } else { return INTERACTION_END; } } InteractionCommand interactWithEntity(Entity* entity, Game* game, Selection selection) { switch (entity->id) { case OLD_MINT: // For testing. return interactWithOldMint(entity, game, selection); default: break; } return INTERACTION_END; } BoundingBox entityBoxFromScale(float scale, float width, float height) { Vector2 size = (Vector2){width / height * scale, scale}; size = Vector2Scale(size, 0.5); return (BoundingBox){ .min = (Vector3){-size.x, -size.y, -size.x}, .max = (Vector3){size.x, size.y, size.x} }; }