From 75b18c03f175a4577c1ce8d28f5e884dae4d5f9c Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 1 Feb 2018 07:50:37 +0000 Subject: [PATCH] Added entity init() functions. --- src/entities/blobs/mia.c | 10 ++++++---- src/entities/blobs/teeka.c | 5 ++++- src/entities/entity.c | 16 +++++++++++++++- src/entities/items/item.c | 11 +++++++++++ src/entities/misc/infoPoint.c | 7 +++++++ src/entities/structures/door.c | 15 +++++++++++++++ src/entities/structures/lift.c | 15 +++++++++++++++ src/entities/structures/powerPoint.c | 19 +++++++++++++++++++ src/entities/structures/powerPoint.h | 2 ++ src/entities/structures/powerPool.c | 7 +++++++ src/entities/structures/pressurePlate.c | 20 ++++++++++++++++++++ src/entities/structures/pushBlock.c | 19 +++++++++++++++++++ src/entities/structures/pushBlock.h | 1 + src/entities/structures/teleporter.c | 18 ++++++++++++++++++ src/entities/unit.c | 10 ++++++---- src/test/atlasTest.c | 21 +++++++++++---------- src/world/worldLoader.c | 6 ++++++ 17 files changed, 182 insertions(+), 20 deletions(-) diff --git a/src/entities/blobs/mia.c b/src/entities/blobs/mia.c index 508b057..d79df3e 100644 --- a/src/entities/blobs/mia.c +++ b/src/entities/blobs/mia.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "mia.h" +static void init(void); static void reset(void); static void tick(void); static void touch(Entity *other); @@ -51,6 +52,7 @@ Entity *initMIA(void) m->spriteFrame = 0; m->spriteTime = rand() % 180; + m->init = init; m->action = nothing; m->reset = reset; m->tick = tick; @@ -63,12 +65,12 @@ Entity *initMIA(void) return (Entity*)m; } -void reinitMIA(Entity *e) +static void init(void) { - if (e->tx == -1 && e->ty == -1) + if (self->tx == -1 && self->ty == -1) { - e->tx = e->x; - e->ty = e->y; + self->tx = self->x; + self->ty = self->y; } } diff --git a/src/entities/blobs/teeka.c b/src/entities/blobs/teeka.c index 7a5ccaf..be706eb 100644 --- a/src/entities/blobs/teeka.c +++ b/src/entities/blobs/teeka.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "teeka.h" +static void (*superTick)(void); static void tick(void); static void lookForEnemies(void); static void preFire(void); @@ -49,6 +50,8 @@ void initTeeka(void) u->health = u->healthMax = 9999; + superTick = u->tick; + u->tick = tick; aimedSprite = getSprite("AimedShot"); @@ -70,7 +73,7 @@ static void tick(void) } } - unitTick(); + superTick(); } static void lookForEnemies(void) diff --git a/src/entities/entity.c b/src/entities/entity.c index a79825d..4fe343f 100644 --- a/src/entities/entity.c +++ b/src/entities/entity.c @@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "entity.h" +static void init(void); +static void reset(void); static void applyDamage(int damage); static float bounce(float x); static SDL_Rect *getBounds(void); @@ -50,6 +52,8 @@ void initEntity(Entity *e) e->thinkTime = 0; + e->init = init; + e->reset = reset; e->animate = animate; e->tick = tick; e->touch = touch; @@ -65,6 +69,16 @@ void initEntity(Entity *e) world.entityTail = e; } +static void init(void) +{ + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Cannot init() entity [name=%s, type=%d, x=%d, y=%d]", self->name, self->type, (int)self->x, (int)self->y); +} + +static void reset(void) +{ + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Cannot reset() entity [name=%s, type=%d, x=%d, y=%d]", self->name, self->type, (int)self->x, (int)self->y); +} + static SDL_Rect *getBounds(void) { self->bounds.x = self->x; @@ -171,7 +185,7 @@ static void load(cJSON *root) static void save(cJSON *root) { - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Entity [name=%s, type=%d, x=%d, y=%d] cannot be saved'", self->name, self->type, (int)self->x, (int)self->y); + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Entity [name=%s, type=%d, x=%d, y=%d] cannot be saved", self->name, self->type, (int)self->x, (int)self->y); exit(1); } diff --git a/src/entities/items/item.c b/src/entities/items/item.c index f1e57fc..499d72d 100644 --- a/src/entities/items/item.c +++ b/src/entities/items/item.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "item.h" +static void init(void); static void reset(void); static void tick(void); static void touch(Entity *other); @@ -53,6 +54,7 @@ Entity *createItem(void) i->sprite[FACING_LEFT] = i->sprite[FACING_RIGHT] = i->sprite[FACING_DIE] = getSprite(i->spriteName); + i->init = init; i->tick = tick; i->touch = touch; i->changeEnvironment = changeEnvironment; @@ -69,6 +71,15 @@ Entity *initItem(void) return createItem(); } +static void init(void) +{ + Item *i; + + i = (Item*)self; + + i->sprite[FACING_LEFT] = i->sprite[FACING_RIGHT] = i->sprite[FACING_DIE] = getSprite(i->spriteName); +} + static void reset(void) { Item *i; diff --git a/src/entities/misc/infoPoint.c b/src/entities/misc/infoPoint.c index 4e9bf83..35770fc 100644 --- a/src/entities/misc/infoPoint.c +++ b/src/entities/misc/infoPoint.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "infoPoint.h" +static void init(void); static void tick(void); static void touch(Entity *other); static void load(cJSON *root); @@ -41,6 +42,7 @@ Entity *initInfoPoint(void) s->firstTouch = 1; + s->init = init; s->tick = tick; s->touch = touch; s->load = load; @@ -49,6 +51,11 @@ Entity *initInfoPoint(void) return (Entity*)s; } +static void init(void) +{ + self->ty = self->y; +} + static void tick(void) { Structure *s; diff --git a/src/entities/structures/door.c b/src/entities/structures/door.c index ecfe66d..7c44c07 100644 --- a/src/entities/structures/door.c +++ b/src/entities/structures/door.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "door.h" +static void init(void); static void tick(void); static void touch(Entity *other); static void openWithKey(void); @@ -57,6 +58,7 @@ Entity *initDoor(void) s->closedY = (int) s->y; } + s->init = init; s->tick = tick; s->touch = touch; s->load = load; @@ -65,6 +67,19 @@ Entity *initDoor(void) return (Entity*)s; } +static void init(void) +{ + Structure *s; + + s = (Structure*)self; + + if (s->closedX == -1 && s->closedY == -1) + { + s->closedX = s->x; + s->closedY = s->y; + } +} + Entity *initBronzeDoor(void) { Structure *s; diff --git a/src/entities/structures/lift.c b/src/entities/structures/lift.c index bd34200..d4e139b 100644 --- a/src/entities/structures/lift.c +++ b/src/entities/structures/lift.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "lift.h" +static void init(void); static void action(void); static void activate(int active); static void load(cJSON *root); @@ -47,6 +48,7 @@ Entity *initLift(Entity *e) s->active = 1; + s->init = init; s->action = action; s->activate = activate; s->load = load; @@ -55,6 +57,19 @@ Entity *initLift(Entity *e) return (Entity*)s; } +static void init(void) +{ + Structure *s; + + s = (Structure*)self; + + if (s->startX == -1 && s->startY == -1) + { + s->startX = s->x; + s->startY = s->y; + } +} + static void action(void) { Structure *s; diff --git a/src/entities/structures/powerPoint.c b/src/entities/structures/powerPoint.c index bc13807..b7cfb91 100644 --- a/src/entities/structures/powerPoint.c +++ b/src/entities/structures/powerPoint.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "powerPoint.h" +static void init(void); static void tick(void); static void action(void); static void touch(Entity *other); @@ -42,6 +43,7 @@ Entity *initPowerPoint(void) s->isStatic = 1; + s->init = init; s->tick = tick; s->action = action; s->touch = touch; @@ -51,6 +53,23 @@ Entity *initPowerPoint(void) return (Entity*)s; } +static void init(void) +{ + Structure *s; + + s = (Structure*)self; + + if (s->requiredPower == 100 && game.cells != 0) + { + s->requiredPower = rrnd(game.cells * 0.7, game.cells * 0.9); + } + else if (s->requiredPower == 0) + { + s->spriteFrame = 3; + s->active = 1; + } +} + static void tick(void) { Structure *s; diff --git a/src/entities/structures/powerPoint.h b/src/entities/structures/powerPoint.h index 08098f1..5e39036 100644 --- a/src/entities/structures/powerPoint.h +++ b/src/entities/structures/powerPoint.h @@ -25,7 +25,9 @@ extern Sprite *getSprite(char *name); extern void activateEntities(char *names, int activate); extern void setGameplayMessage(int type, char *format, ...); extern Structure *createStructure(void); +extern int rrnd(int low, int high); extern Dev dev; extern Entity *self; +extern Game game; extern World world; diff --git a/src/entities/structures/powerPool.c b/src/entities/structures/powerPool.c index a93fc60..72fbb97 100644 --- a/src/entities/structures/powerPool.c +++ b/src/entities/structures/powerPool.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "powerPool.h" +static void init(void); static void tick(void); static void action(void); static void touch(Entity *other); @@ -42,6 +43,7 @@ Entity *initPowerPool(void) s->isStatic = 1; + s->init = init; s->tick = tick; s->action = action; s->touch = touch; @@ -51,6 +53,11 @@ Entity *initPowerPool(void) return (Entity*)s; } +static void init(void) +{ + /* nothing to do */ +} + static void tick(void) { Structure *s; diff --git a/src/entities/structures/pressurePlate.c b/src/entities/structures/pressurePlate.c index 7579d38..0529646 100644 --- a/src/entities/structures/pressurePlate.c +++ b/src/entities/structures/pressurePlate.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "pressurePlate.h" +static void init(void); static void tick(void); static void touch(Entity *other); static void load(cJSON *root); @@ -41,6 +42,7 @@ Entity *initPressurePlate(void) s->isStatic = 1; + s->init = init; s->tick = tick; s->touch = touch; s->load = load; @@ -49,6 +51,24 @@ Entity *initPressurePlate(void) return (Entity*)s; } +static void init(void) +{ + Structure *s; + + s = (Structure*)self; + + if (s->active) + { + s->spriteTime = -1; + s->spriteFrame = 1; + + if (s->isWeighted) + { + s->weightApplied = 5; + } + } +} + static void tick(void) { Structure *s; diff --git a/src/entities/structures/pushBlock.c b/src/entities/structures/pushBlock.c index c17738d..fa2ce3a 100644 --- a/src/entities/structures/pushBlock.c +++ b/src/entities/structures/pushBlock.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "pushBlock.h" +static void init(void); static void activate(int active); static void load(cJSON *root); static void save(cJSON *root); @@ -40,6 +41,7 @@ Entity *initPushBlock(void) s->flags |= EF_EXPLODES | EF_ALWAYS_PROCESS; + s->init = init; s->activate = activate; s->load = load; s->save = save; @@ -47,6 +49,23 @@ Entity *initPushBlock(void) return (Entity*)s; } +static void init(void) +{ + Structure *s; + + s = (Structure*)self; + + sprintf(s->spriteName, "Crate%d", rrnd(1, 4)); + + s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite(s->spriteName); + + if (s->startX == -1 && s->startY == -1) + { + s->startX = s->x; + s->startY = s->y; + } +} + static void activate(int active) { Structure *s; diff --git a/src/entities/structures/pushBlock.h b/src/entities/structures/pushBlock.h index f77ae19..f531c20 100644 --- a/src/entities/structures/pushBlock.h +++ b/src/entities/structures/pushBlock.h @@ -25,5 +25,6 @@ extern void playSound(int snd, int ch); extern void addTeleportStars(Entity *e); extern Structure *createStructure(void); extern Sprite *getSprite(char *name); +extern int rrnd(int low, int high); extern Entity *self; diff --git a/src/entities/structures/teleporter.c b/src/entities/structures/teleporter.c index c69e8ff..b43e6d3 100644 --- a/src/entities/structures/teleporter.c +++ b/src/entities/structures/teleporter.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "teleporter.h" +static void init(void); static void action(void); static void touch(Entity *other); static void activate(int active); @@ -44,6 +45,7 @@ Entity *initTeleporter(void) s->active = 1; + s->init = init; s->action = action; s->touch = touch; s->activate = activate; @@ -53,6 +55,22 @@ Entity *initTeleporter(void) return (Entity*)s; } +static void init(void) +{ + Structure *s; + + s = (Structure*)self; + + if (s->active) + { + s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("TeleporterActive"); + } + else + { + s->sprite[FACING_LEFT] = s->sprite[FACING_RIGHT] = s->sprite[FACING_DIE] = getSprite("TeleporterInactive"); + } +} + static void action(void) { if (self->active) diff --git a/src/entities/unit.c b/src/entities/unit.c index 23af7c2..f60635f 100644 --- a/src/entities/unit.c +++ b/src/entities/unit.c @@ -20,7 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "unit.h" -void unitTick(void); +static void tick(void); +static void init(void); static void attack(void); static int canFire(Entity *target); static SDL_Rect *getCurrentSprite(void); @@ -53,7 +54,8 @@ Unit *createUnit(void) u->startX = u->startY = -1; - u->tick = unitTick; + u->init = init; + u->tick = tick; u->action = lookForPlayer; u->preFire = preFire; u->attack = attack; @@ -65,7 +67,7 @@ Unit *createUnit(void) return u; } -void reInitUnit(Entity *e) +static void init(void) { Unit *u; @@ -83,7 +85,7 @@ void reInitUnit(Entity *e) } } -void unitTick(void) +static void tick(void) { Unit *u; diff --git a/src/test/atlasTest.c b/src/test/atlasTest.c index 80fa2b7..ee4ecc6 100644 --- a/src/test/atlasTest.c +++ b/src/test/atlasTest.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "atlasTest.h" +static Entity *track; static void logic(void); static void draw(void); static int timeout; @@ -39,9 +40,11 @@ void initAtlasTest(void) initEntities(); - timeout = FPS * 2; + timeout = FPS; - cameraTrack((Entity*)world.bob); + track = &world.entityHead; + + cameraTrack(track); } static void logic(void) @@ -61,15 +64,13 @@ static void draw(void) static void trackRandomEntity(void) { - Entity *e; + track = track->next; - for (e = world.entityHead.next ; e != NULL ; e = e->next) + if (track == NULL) { - if (rand() % 4 == 0) - { - cameraTrack(e); - timeout = FPS * 2; - return; - } + track = (Entity*)world.bob; } + + cameraTrack(track); + timeout = FPS; } diff --git a/src/world/worldLoader.c b/src/world/worldLoader.c index f3a0ac7..86af40c 100644 --- a/src/world/worldLoader.c +++ b/src/world/worldLoader.c @@ -128,7 +128,11 @@ static void loadBob(cJSON *root) { world.bob = (Bob*)createEntity("Bob"); + self = (Entity*)world.bob; + world.bob->load(root); + + world.bob->init(); } static void loadEntities(cJSON *root) @@ -148,6 +152,8 @@ static void loadEntities(cJSON *root) self->y = cJSON_GetObjectItem(node, "y")->valueint; self->load(node); + + self->init(); } }