Added entity init() functions.

This commit is contained in:
Steve 2018-02-01 07:50:37 +00:00
parent 3fa7c7a525
commit 75b18c03f1
17 changed files with 182 additions and 20 deletions

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "mia.h" #include "mia.h"
static void init(void);
static void reset(void); static void reset(void);
static void tick(void); static void tick(void);
static void touch(Entity *other); static void touch(Entity *other);
@ -51,6 +52,7 @@ Entity *initMIA(void)
m->spriteFrame = 0; m->spriteFrame = 0;
m->spriteTime = rand() % 180; m->spriteTime = rand() % 180;
m->init = init;
m->action = nothing; m->action = nothing;
m->reset = reset; m->reset = reset;
m->tick = tick; m->tick = tick;
@ -63,12 +65,12 @@ Entity *initMIA(void)
return (Entity*)m; 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; self->tx = self->x;
e->ty = e->y; self->ty = self->y;
} }
} }

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "teeka.h" #include "teeka.h"
static void (*superTick)(void);
static void tick(void); static void tick(void);
static void lookForEnemies(void); static void lookForEnemies(void);
static void preFire(void); static void preFire(void);
@ -49,6 +50,8 @@ void initTeeka(void)
u->health = u->healthMax = 9999; u->health = u->healthMax = 9999;
superTick = u->tick;
u->tick = tick; u->tick = tick;
aimedSprite = getSprite("AimedShot"); aimedSprite = getSprite("AimedShot");
@ -70,7 +73,7 @@ static void tick(void)
} }
} }
unitTick(); superTick();
} }
static void lookForEnemies(void) static void lookForEnemies(void)

View File

@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "entity.h" #include "entity.h"
static void init(void);
static void reset(void);
static void applyDamage(int damage); static void applyDamage(int damage);
static float bounce(float x); static float bounce(float x);
static SDL_Rect *getBounds(void); static SDL_Rect *getBounds(void);
@ -50,6 +52,8 @@ void initEntity(Entity *e)
e->thinkTime = 0; e->thinkTime = 0;
e->init = init;
e->reset = reset;
e->animate = animate; e->animate = animate;
e->tick = tick; e->tick = tick;
e->touch = touch; e->touch = touch;
@ -65,6 +69,16 @@ void initEntity(Entity *e)
world.entityTail = 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) static SDL_Rect *getBounds(void)
{ {
self->bounds.x = self->x; self->bounds.x = self->x;
@ -171,7 +185,7 @@ static void load(cJSON *root)
static void save(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); exit(1);
} }

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "item.h" #include "item.h"
static void init(void);
static void reset(void); static void reset(void);
static void tick(void); static void tick(void);
static void touch(Entity *other); 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->sprite[FACING_LEFT] = i->sprite[FACING_RIGHT] = i->sprite[FACING_DIE] = getSprite(i->spriteName);
i->init = init;
i->tick = tick; i->tick = tick;
i->touch = touch; i->touch = touch;
i->changeEnvironment = changeEnvironment; i->changeEnvironment = changeEnvironment;
@ -69,6 +71,15 @@ Entity *initItem(void)
return createItem(); 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) static void reset(void)
{ {
Item *i; Item *i;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "infoPoint.h" #include "infoPoint.h"
static void init(void);
static void tick(void); static void tick(void);
static void touch(Entity *other); static void touch(Entity *other);
static void load(cJSON *root); static void load(cJSON *root);
@ -41,6 +42,7 @@ Entity *initInfoPoint(void)
s->firstTouch = 1; s->firstTouch = 1;
s->init = init;
s->tick = tick; s->tick = tick;
s->touch = touch; s->touch = touch;
s->load = load; s->load = load;
@ -49,6 +51,11 @@ Entity *initInfoPoint(void)
return (Entity*)s; return (Entity*)s;
} }
static void init(void)
{
self->ty = self->y;
}
static void tick(void) static void tick(void)
{ {
Structure *s; Structure *s;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "door.h" #include "door.h"
static void init(void);
static void tick(void); static void tick(void);
static void touch(Entity *other); static void touch(Entity *other);
static void openWithKey(void); static void openWithKey(void);
@ -57,6 +58,7 @@ Entity *initDoor(void)
s->closedY = (int) s->y; s->closedY = (int) s->y;
} }
s->init = init;
s->tick = tick; s->tick = tick;
s->touch = touch; s->touch = touch;
s->load = load; s->load = load;
@ -65,6 +67,19 @@ Entity *initDoor(void)
return (Entity*)s; 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) Entity *initBronzeDoor(void)
{ {
Structure *s; Structure *s;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "lift.h" #include "lift.h"
static void init(void);
static void action(void); static void action(void);
static void activate(int active); static void activate(int active);
static void load(cJSON *root); static void load(cJSON *root);
@ -47,6 +48,7 @@ Entity *initLift(Entity *e)
s->active = 1; s->active = 1;
s->init = init;
s->action = action; s->action = action;
s->activate = activate; s->activate = activate;
s->load = load; s->load = load;
@ -55,6 +57,19 @@ Entity *initLift(Entity *e)
return (Entity*)s; 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) static void action(void)
{ {
Structure *s; Structure *s;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "powerPoint.h" #include "powerPoint.h"
static void init(void);
static void tick(void); static void tick(void);
static void action(void); static void action(void);
static void touch(Entity *other); static void touch(Entity *other);
@ -42,6 +43,7 @@ Entity *initPowerPoint(void)
s->isStatic = 1; s->isStatic = 1;
s->init = init;
s->tick = tick; s->tick = tick;
s->action = action; s->action = action;
s->touch = touch; s->touch = touch;
@ -51,6 +53,23 @@ Entity *initPowerPoint(void)
return (Entity*)s; 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) static void tick(void)
{ {
Structure *s; Structure *s;

View File

@ -25,7 +25,9 @@ extern Sprite *getSprite(char *name);
extern void activateEntities(char *names, int activate); extern void activateEntities(char *names, int activate);
extern void setGameplayMessage(int type, char *format, ...); extern void setGameplayMessage(int type, char *format, ...);
extern Structure *createStructure(void); extern Structure *createStructure(void);
extern int rrnd(int low, int high);
extern Dev dev; extern Dev dev;
extern Entity *self; extern Entity *self;
extern Game game;
extern World world; extern World world;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "powerPool.h" #include "powerPool.h"
static void init(void);
static void tick(void); static void tick(void);
static void action(void); static void action(void);
static void touch(Entity *other); static void touch(Entity *other);
@ -42,6 +43,7 @@ Entity *initPowerPool(void)
s->isStatic = 1; s->isStatic = 1;
s->init = init;
s->tick = tick; s->tick = tick;
s->action = action; s->action = action;
s->touch = touch; s->touch = touch;
@ -51,6 +53,11 @@ Entity *initPowerPool(void)
return (Entity*)s; return (Entity*)s;
} }
static void init(void)
{
/* nothing to do */
}
static void tick(void) static void tick(void)
{ {
Structure *s; Structure *s;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "pressurePlate.h" #include "pressurePlate.h"
static void init(void);
static void tick(void); static void tick(void);
static void touch(Entity *other); static void touch(Entity *other);
static void load(cJSON *root); static void load(cJSON *root);
@ -41,6 +42,7 @@ Entity *initPressurePlate(void)
s->isStatic = 1; s->isStatic = 1;
s->init = init;
s->tick = tick; s->tick = tick;
s->touch = touch; s->touch = touch;
s->load = load; s->load = load;
@ -49,6 +51,24 @@ Entity *initPressurePlate(void)
return (Entity*)s; 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) static void tick(void)
{ {
Structure *s; Structure *s;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "pushBlock.h" #include "pushBlock.h"
static void init(void);
static void activate(int active); 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);
@ -40,6 +41,7 @@ Entity *initPushBlock(void)
s->flags |= EF_EXPLODES | EF_ALWAYS_PROCESS; s->flags |= EF_EXPLODES | EF_ALWAYS_PROCESS;
s->init = init;
s->activate = activate; s->activate = activate;
s->load = load; s->load = load;
s->save = save; s->save = save;
@ -47,6 +49,23 @@ Entity *initPushBlock(void)
return (Entity*)s; 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) static void activate(int active)
{ {
Structure *s; Structure *s;

View File

@ -25,5 +25,6 @@ extern void playSound(int snd, int ch);
extern void addTeleportStars(Entity *e); extern void addTeleportStars(Entity *e);
extern Structure *createStructure(void); extern Structure *createStructure(void);
extern Sprite *getSprite(char *name); extern Sprite *getSprite(char *name);
extern int rrnd(int low, int high);
extern Entity *self; extern Entity *self;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "teleporter.h" #include "teleporter.h"
static void init(void);
static void action(void); static void action(void);
static void touch(Entity *other); static void touch(Entity *other);
static void activate(int active); static void activate(int active);
@ -44,6 +45,7 @@ Entity *initTeleporter(void)
s->active = 1; s->active = 1;
s->init = init;
s->action = action; s->action = action;
s->touch = touch; s->touch = touch;
s->activate = activate; s->activate = activate;
@ -53,6 +55,22 @@ Entity *initTeleporter(void)
return (Entity*)s; 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) static void action(void)
{ {
if (self->active) if (self->active)

View File

@ -20,7 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "unit.h" #include "unit.h"
void unitTick(void); static void tick(void);
static void init(void);
static void attack(void); static void attack(void);
static int canFire(Entity *target); static int canFire(Entity *target);
static SDL_Rect *getCurrentSprite(void); static SDL_Rect *getCurrentSprite(void);
@ -53,7 +54,8 @@ Unit *createUnit(void)
u->startX = u->startY = -1; u->startX = u->startY = -1;
u->tick = unitTick; u->init = init;
u->tick = tick;
u->action = lookForPlayer; u->action = lookForPlayer;
u->preFire = preFire; u->preFire = preFire;
u->attack = attack; u->attack = attack;
@ -65,7 +67,7 @@ Unit *createUnit(void)
return u; return u;
} }
void reInitUnit(Entity *e) static void init(void)
{ {
Unit *u; Unit *u;
@ -83,7 +85,7 @@ void reInitUnit(Entity *e)
} }
} }
void unitTick(void) static void tick(void)
{ {
Unit *u; Unit *u;

View File

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "atlasTest.h" #include "atlasTest.h"
static Entity *track;
static void logic(void); static void logic(void);
static void draw(void); static void draw(void);
static int timeout; static int timeout;
@ -39,9 +40,11 @@ void initAtlasTest(void)
initEntities(); initEntities();
timeout = FPS * 2; timeout = FPS;
cameraTrack((Entity*)world.bob); track = &world.entityHead;
cameraTrack(track);
} }
static void logic(void) static void logic(void)
@ -61,15 +64,13 @@ static void draw(void)
static void trackRandomEntity(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) track = (Entity*)world.bob;
{
cameraTrack(e);
timeout = FPS * 2;
return;
}
} }
cameraTrack(track);
timeout = FPS;
} }

View File

@ -128,7 +128,11 @@ static void loadBob(cJSON *root)
{ {
world.bob = (Bob*)createEntity("Bob"); world.bob = (Bob*)createEntity("Bob");
self = (Entity*)world.bob;
world.bob->load(root); world.bob->load(root);
world.bob->init();
} }
static void loadEntities(cJSON *root) static void loadEntities(cJSON *root)
@ -148,6 +152,8 @@ static void loadEntities(cJSON *root)
self->y = cJSON_GetObjectItem(node, "y")->valueint; self->y = cJSON_GetObjectItem(node, "y")->valueint;
self->load(node); self->load(node);
self->init();
} }
} }