Activate entities correctly.

This commit is contained in:
Steve 2018-02-03 15:53:21 +00:00
parent b8aa460221
commit e97d38e278
5 changed files with 55 additions and 4 deletions

View File

@ -26,6 +26,7 @@ static void action(void);
static void applyDamage(int damage); static void applyDamage(int damage);
static float bounce(float x); static float bounce(float x);
static void tick(void); static void tick(void);
static void activate(int active);
static void touch(Entity *other); static void touch(Entity *other);
static void animate(void); static void animate(void);
static void load(cJSON *root); static void load(cJSON *root);
@ -61,6 +62,7 @@ void initEntity(Entity *e)
e->animate = animate; e->animate = animate;
e->tick = tick; e->tick = tick;
e->touch = touch; e->touch = touch;
e->activate = activate;
e->applyDamage = applyDamage; e->applyDamage = applyDamage;
e->bounce = bounce; e->bounce = bounce;
e->getCurrentSprite = getCurrentSprite; e->getCurrentSprite = getCurrentSprite;
@ -147,6 +149,10 @@ static void touch(Entity *other)
{ {
} }
static void activate(int active)
{
}
static SDL_Rect *getCurrentSprite(void) static SDL_Rect *getCurrentSprite(void)
{ {
return &self->sprite[self->facing]->frames[self->spriteFrame]->rect; return &self->sprite[self->facing]->frames[self->spriteFrame]->rect;

View File

@ -27,6 +27,7 @@ static void openWithKey(void);
static int isClosed(void); static int isClosed(void);
static int isOpening(void); static int isOpening(void);
static int isClosing(void); static int isClosing(void);
static void activate(int active);
static void load(cJSON *root); static void load(cJSON *root);
static void save(cJSON *root); static void save(cJSON *root);
@ -55,6 +56,7 @@ Entity *initDoor(void)
s->init = init; s->init = init;
s->tick = tick; s->tick = tick;
s->touch = touch; s->touch = touch;
s->activate = activate;
s->load = load; s->load = load;
s->save = save; 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)); 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) static void load(cJSON *root)
{ {
Structure *s; Structure *s;

View File

@ -30,6 +30,8 @@ extern void removeItem(char *name);
extern Structure *createStructure(void); extern Structure *createStructure(void);
extern char *getLookupName(const char *prefix, long num); extern char *getLookupName(const char *prefix, long num);
extern long lookup(const char *name); extern long lookup(const char *name);
extern int isOnScreen(Entity *e);
extern void observeActivation(Entity *e);
extern Entity *self; extern Entity *self;
extern Dev dev; extern Dev dev;

View File

@ -771,8 +771,29 @@ static int isObserving(void)
return 0; 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) void teleportEntity(Entity *e, float tx, float ty)

View File

@ -478,10 +478,11 @@ void observeActivation(Entity *e)
{ {
world.entitiesToObserve[i] = e; world.entitiesToObserve[i] = e;
world.observationTimer = FPS * 2; 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);
} }