diff --git a/src/entities/entity.c b/src/entities/entity.c index cede9dd..5cbe7ce 100644 --- a/src/entities/entity.c +++ b/src/entities/entity.c @@ -26,6 +26,7 @@ static void action(void); static void applyDamage(int damage); static float bounce(float x); static void tick(void); +static void activate(int active); static void touch(Entity *other); static void animate(void); static void load(cJSON *root); @@ -61,6 +62,7 @@ void initEntity(Entity *e) e->animate = animate; e->tick = tick; e->touch = touch; + e->activate = activate; e->applyDamage = applyDamage; e->bounce = bounce; e->getCurrentSprite = getCurrentSprite; @@ -147,6 +149,10 @@ static void touch(Entity *other) { } +static void activate(int active) +{ +} + static SDL_Rect *getCurrentSprite(void) { return &self->sprite[self->facing]->frames[self->spriteFrame]->rect; diff --git a/src/entities/structures/door.c b/src/entities/structures/door.c index 7038816..f0bdf98 100644 --- a/src/entities/structures/door.c +++ b/src/entities/structures/door.c @@ -27,6 +27,7 @@ static void openWithKey(void); static int isClosed(void); static int isOpening(void); static int isClosing(void); +static void activate(int active); static void load(cJSON *root); static void save(cJSON *root); @@ -55,6 +56,7 @@ Entity *initDoor(void) s->init = init; s->tick = tick; s->touch = touch; + s->activate = activate; s->load = load; s->save = save; @@ -266,6 +268,25 @@ static int isClosed(void) return (s->state == DOOR_CLOSED && ((int) s->x == s->closedX && (int) s->y == s->closedY)); } +static void activate(int active) +{ + Structure *s = (Structure*)self; + + s->state = (s->state == DOOR_CLOSED) ? DOOR_OPEN : DOOR_CLOSED; + + playSound(SND_DOOR_START, CH_MECHANICAL); + + if (active) + { + observeActivation(self); + + if (!isOnScreen(self)) + { + setGameplayMessage(MSG_GAMEPLAY, "Door opened ..."); + } + } +} + static void load(cJSON *root) { Structure *s; diff --git a/src/entities/structures/door.h b/src/entities/structures/door.h index af447a8..3010bbb 100644 --- a/src/entities/structures/door.h +++ b/src/entities/structures/door.h @@ -30,6 +30,8 @@ extern void removeItem(char *name); extern Structure *createStructure(void); extern char *getLookupName(const char *prefix, long num); extern long lookup(const char *name); +extern int isOnScreen(Entity *e); +extern void observeActivation(Entity *e); extern Entity *self; extern Dev dev; diff --git a/src/world/entities.c b/src/world/entities.c index 6e106e6..e5d31bf 100644 --- a/src/world/entities.c +++ b/src/world/entities.c @@ -771,8 +771,29 @@ static int isObserving(void) return 0; } -void activateEntities(char *names, int activate) +void activateEntities(char *names, int active) { + Entity *oldSelf; + char *name; + + oldSelf = self; + + name = strtok(names, "|"); + + while (name) + { + for (self = world.entityHead.next ; self != NULL ; self = self->next) + { + if (strcmp(self->name, name) == 0) + { + self->activate(active); + } + } + + name = strtok(NULL, "|"); + } + + self = oldSelf; } void teleportEntity(Entity *e, float tx, float ty) diff --git a/src/world/world.c b/src/world/world.c index aa08516..5ab822c 100644 --- a/src/world/world.c +++ b/src/world/world.c @@ -478,10 +478,11 @@ void observeActivation(Entity *e) { world.entitiesToObserve[i] = e; world.observationTimer = FPS * 2; + return; } } + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Can't observe entity - out of array space"); + exit(1); } - - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Can't observe entity - out of array space"); - exit(1); }