Load and save for some entities.

This commit is contained in:
Steve 2018-01-30 22:47:40 +00:00
parent 514a2df3be
commit 0705225253
32 changed files with 408 additions and 3 deletions

View File

@ -25,6 +25,8 @@ static void tick(void);
static void touch(Entity *other);
static void preTeleport(void);
static void teleport(void);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initMIA(void)
{
@ -53,6 +55,8 @@ Entity *initMIA(void)
m->reset = reset;
m->tick = tick;
m->touch = touch;
m->load = load;
m->save = save;
m->isMissionTarget = 1;
@ -144,3 +148,22 @@ static void teleport(void)
m->alive = ALIVE_DEAD;
}
}
static void load(cJSON *root)
{
MIA *m;
m = (MIA*)self;
m->active = cJSON_GetObjectItem(root, "isMissionTarget")->valueint;
}
static void save(cJSON *root)
{
MIA *m;
m = (MIA*)self;
cJSON_AddStringToObject(root, "type", "MIA");
cJSON_AddNumberToObject(root, "isMissionTarget", m->isMissionTarget);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Sprite *getSprite(char *name);
extern void nothing(void);

View File

@ -24,6 +24,10 @@ void animateEntity(void);
static void applyDamage(int damage);
static float bounce(float x);
static SDL_Rect *getBounds(void);
static void tick(void);
static void touch(Entity *other);
static void load(cJSON *root);
static void save(cJSON *root);
void initEntity(Entity *e)
{
@ -46,10 +50,15 @@ void initEntity(Entity *e)
e->thinkTime = 0;
e->animate = animateEntity;
e->tick = tick;
e->touch = touch;
e->applyDamage = applyDamage;
e->bounce = bounce;
e->getBounds = getBounds;
e->load = load;
e->save = save;
world.entityTail->next = e;
world.entityTail = e;
}
@ -117,6 +126,26 @@ static void applyDamage(int damage)
}
static void tick(void)
{
}
static void touch(Entity *other)
{
}
static void load(cJSON *root)
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_ERROR, "Entity [name=%s, type=%d, x=%d, y=%d] cannot be loaded", self->name, self->type, (int)self->x, (int)self->y);
exit(1);
}
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);
exit(1);
}
void dropCarriedItem(void)
{
EntityExt *e;

View File

@ -21,6 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "pistolBlob.h"
static int canFire(Entity *target);
static void (*superSave)(cJSON *root);
static void save(cJSON *root);
Entity *initPistolBlob(void)
{
@ -37,8 +39,11 @@ Entity *initPistolBlob(void)
u->weaponType = WPN_AIMED_PISTOL;
u->maxShotsToFire = 3;
superSave = u->save;
u->canFire = canFire;
u->save = save;
return (Entity*)u;
}
@ -47,3 +52,10 @@ static int canFire(Entity *target)
{
return 1;
}
static void save(cJSON *root)
{
superSave(root);
cJSON_AddStringToObject(root, "type", "PistolBlob");
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Unit *createUnit(void);
extern void initEvilBlob(Unit *u);

View File

@ -28,6 +28,8 @@ static void die(void);
static void destructablePickupItem(Structure *s);
static void enemyPickupItem(Unit *u);
static void bobPickupItem(void);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *createItem(void)
{
@ -58,6 +60,8 @@ Entity *createItem(void)
i->changeEnvironment = changeEnvironment;
i->reset = reset;
i->die = die;
i->load = load;
i->save = save;
return (Entity*)i;
}
@ -220,3 +224,37 @@ static void die(void)
{
/* we will handle this ourselves! */
}
static void load(cJSON *root)
{
Item *i;
i = (Item*)self;
i->canBeCarried = cJSON_GetObjectItem(root, "canBeCarried")->valueint;
i->canBePickedUp = cJSON_GetObjectItem(root, "canBePickedUp")->valueint;
i->isMissionTarget = cJSON_GetObjectItem(root, "isMissionTarget")->valueint;
STRNCPY(i->spriteName, cJSON_GetObjectItem(root, "spriteName")->valuestring, MAX_NAME_LENGTH);
i->startX = cJSON_GetObjectItem(root, "startX")->valueint;
i->startY = cJSON_GetObjectItem(root, "startY")->valueint;
if (cJSON_GetObjectItem(root, "collected"))
{
i->collected = cJSON_GetObjectItem(root, "collected")->valueint;
}
}
static void save(cJSON *root)
{
Item *i;
i = (Item*)self;
cJSON_AddStringToObject(root, "type", "Item");
cJSON_AddNumberToObject(root, "canBeCarried", i->canBeCarried);
cJSON_AddNumberToObject(root, "canBePickedUp", i->canBePickedUp);
cJSON_AddNumberToObject(root, "isMissionTarget", i->isMissionTarget);
cJSON_AddStringToObject(root, "spriteName", i->spriteName);
cJSON_AddNumberToObject(root, "startX", i->startX);
cJSON_AddNumberToObject(root, "startY", i->startY);
cJSON_AddNumberToObject(root, "collected", i->collected);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern void playSound(int snd, int ch);
extern void setGameplayMessage(int type, char *format, ...);

View File

@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void tick(void);
static void touch(Entity *other);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initInfoPoint(void)
{
@ -41,6 +43,8 @@ Entity *initInfoPoint(void)
s->tick = tick;
s->touch = touch;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -92,3 +96,27 @@ static void touch(Entity *other)
}
}
}
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
STRNCPY(s->message, cJSON_GetObjectItem(root, "message")->valuestring, MAX_DESCRIPTION_LENGTH);
if (cJSON_GetObjectItem(root, "firstTouch"))
{
s->firstTouch = cJSON_GetObjectItem(root, "firstTouch")->valueint;
}
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "InfoPoint");
cJSON_AddStringToObject(root, "message", s->name);
cJSON_AddNumberToObject(root, "firstTouch", s->firstTouch);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Sprite *getSprite(char *name);
extern void showInfoMessage(char *message);

View File

@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void tick(void);
static void touch(Entity *other);
static void load(cJSON *root);
static void save(cJSON *root);
void initCardReader(Entity *e)
{
@ -50,6 +52,8 @@ void initCardReader(Entity *e)
s->tick = tick;
s->touch = touch;
s->load = load;
s->save = save;
}
static void tick(void)
@ -96,3 +100,26 @@ static void touch(Entity *other)
s->bobTouching = 2;
}
}
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
s->active = cJSON_GetObjectItem(root, "active")->valueint;
STRNCPY(s->requiredItem, cJSON_GetObjectItem(root, "requiredCard")->valuestring, MAX_NAME_LENGTH);
STRNCPY(s->targetNames, cJSON_GetObjectItem(root, "targetNames")->valuestring, MAX_DESCRIPTION_LENGTH);
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "CardReader");
cJSON_AddNumberToObject(root, "active", s->active);
cJSON_AddStringToObject(root, "requiredCard", s->requiredItem);
cJSON_AddStringToObject(root, "targetNames", s->targetNames);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern void initEntity(Entity *e);
extern Sprite *getSprite(char *name);

View File

@ -26,6 +26,8 @@ static void openWithKey(void);
static int isClosed(void);
static int isOpening(void);
static int isClosing(void);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initDoor(void)
{
@ -57,6 +59,8 @@ Entity *initDoor(void)
s->tick = tick;
s->touch = touch;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -252,3 +256,40 @@ static int isClosed(void)
return (s->state == DOOR_CLOSED && ((int) s->x == s->closedX && (int) s->y == s->closedY));
}
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
s->active = cJSON_GetObjectItem(root, "isLocked")->valueint;
if (cJSON_GetObjectItem(root, "requiredKey"))
{
STRNCPY(s->requiredItem, cJSON_GetObjectItem(root, "requiredKey")->valuestring, MAX_NAME_LENGTH);
}
s->tx = cJSON_GetObjectItem(root, "tx")->valueint;
s->ty = cJSON_GetObjectItem(root, "ty")->valueint;
s->speed = cJSON_GetObjectItem(root, "speed")->valueint;
s->state = cJSON_GetObjectItem(root, "state")->valueint;
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "Door");
cJSON_AddNumberToObject(root, "isLocked", s->isLocked);
cJSON_AddNumberToObject(root, "tx", s->tx);
cJSON_AddNumberToObject(root, "ty", s->ty);
cJSON_AddNumberToObject(root, "speed", s->speed);
cJSON_AddNumberToObject(root, "state", s->state);
cJSON_AddStringToObject(root, "requiredKey", s->requiredItem);
if (strcmp(s->sprite[0]->name, "Door") != 0)
{
cJSON_AddStringToObject(root, "type", s->sprite[0]->name);
}
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Sprite *getSprite(char *name);
extern void playSound(int snd, int ch);

View File

@ -24,7 +24,7 @@ Entity *initHorizontalDoor(void)
{
Structure *s;
s = createStructure();
s = (Structure*)initDoor();
s->type = ET_DOOR;

View File

@ -22,3 +22,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern Sprite *getSprite(char *name);
extern Structure *createStructure(void);
extern Entity *initDoor(void);

View File

@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void action(void);
static void activate(int active);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initLift(Entity *e)
{
@ -47,6 +49,8 @@ Entity *initLift(Entity *e)
s->action = action;
s->activate = activate;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -110,3 +114,36 @@ static void activate(int active)
}
}
}
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
s->active = cJSON_GetObjectItem(root, "active")->valueint;
s->tx = cJSON_GetObjectItem(root, "tx")->valueint;
s->ty = cJSON_GetObjectItem(root, "ty")->valueint;
s->speed = cJSON_GetObjectItem(root, "speed")->valueint;
s->state = cJSON_GetObjectItem(root, "state")->valueint;
s->startX = cJSON_GetObjectItem(root, "startX")->valueint;
s->startY = cJSON_GetObjectItem(root, "startY")->valueint;
s->waitTime = cJSON_GetObjectItem(root, "waitTime")->valueint;
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "Lift");
cJSON_AddNumberToObject(root, "active", s->active);
cJSON_AddNumberToObject(root, "tx", s->tx);
cJSON_AddNumberToObject(root, "ty", s->ty);
cJSON_AddNumberToObject(root, "speed", s->speed);
cJSON_AddNumberToObject(root, "state", s->state);
cJSON_AddNumberToObject(root, "startX", s->startX);
cJSON_AddNumberToObject(root, "startY", s->startY);
cJSON_AddNumberToObject(root, "waitTime", s->waitTime);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Sprite *getSprite(char *name);
extern void getSlope(int x1, int y1, int x2, int y2, float *dx, float *dy);

View File

@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void tick(void);
static void action(void);
static void touch(Entity *other);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initPowerPoint(void)
{
@ -43,6 +45,8 @@ Entity *initPowerPoint(void)
s->tick = tick;
s->action = action;
s->touch = touch;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -113,3 +117,24 @@ static void touch(Entity *other)
}
}
}
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
s->requiredPower = cJSON_GetObjectItem(root, "requiredPower")->valueint;
STRNCPY(s->targetNames, cJSON_GetObjectItem(root, "targetNames")->valuestring, MAX_NAME_LENGTH);
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "PowerPoint");
cJSON_AddNumberToObject(root, "requiredPower", s->requiredPower);
cJSON_AddStringToObject(root, "targetNames", s->targetNames);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Sprite *getSprite(char *name);
extern void activateEntities(char *names, int activate);

View File

@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void tick(void);
static void action(void);
static void touch(Entity *other);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initPowerPool(void)
{
@ -43,6 +45,8 @@ Entity *initPowerPool(void)
s->tick = tick;
s->action = action;
s->touch = touch;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -100,3 +104,13 @@ static void touch(Entity *other)
}
}
}
static void load(cJSON *root)
{
/* nothing to do */
}
static void save(cJSON *root)
{
cJSON_AddStringToObject(root, "type", "PowerPool");
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Structure *createStructure(void);
extern Sprite *getSprite(char *name);

View File

@ -22,6 +22,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void tick(void);
static void touch(Entity *other);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initPressurePlate(void)
{
@ -41,6 +43,8 @@ Entity *initPressurePlate(void)
s->tick = tick;
s->touch = touch;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -85,3 +89,29 @@ static void touch(Entity *other)
s->weightApplied = 5;
}
}
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
if (cJSON_GetObjectItem(root, "active"))
{
s->active = cJSON_GetObjectItem(root, "active")->valueint;
}
s->isWeighted = cJSON_GetObjectItem(root, "isWeighted")->valueint;
STRNCPY(s->targetNames, cJSON_GetObjectItem(root, "targetNames")->valuestring, MAX_DESCRIPTION_LENGTH);
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "PressurePlate");
cJSON_AddNumberToObject(root, "active", s->active);
cJSON_AddNumberToObject(root, "isWeighted", s->isWeighted);
cJSON_AddStringToObject(root, "targetNames", s->targetNames);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern Structure *createStructure(void);
extern Sprite *getSprite(char *name);

View File

@ -21,6 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "pushBlock.h"
static void activate(int active);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initPushBlock(void)
{
@ -37,6 +39,8 @@ Entity *initPushBlock(void)
s->flags |= EF_EXPLODES | EF_ALWAYS_PROCESS;
s->activate = activate;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -57,3 +61,26 @@ static void activate(int active)
playSound(SND_APPEAR, CH_ANY);
}
}
static void load(cJSON *root)
{
Structure *s;
s = (Structure*)self;
STRNCPY(s->spriteName, cJSON_GetObjectItem(root, "spriteName")->valuestring, MAX_NAME_LENGTH);
s->startX = cJSON_GetObjectItem(root, "startX")->valueint;
s->startY = cJSON_GetObjectItem(root, "startY")->valueint;
}
static void save(cJSON *root)
{
Structure *s;
s = (Structure*)self;
cJSON_AddStringToObject(root, "type", "PressurePlate");
cJSON_AddStringToObject(root, "spriteName", s->spriteName);
cJSON_AddNumberToObject(root, "startX", s->startX);
cJSON_AddNumberToObject(root, "startY", s->startY);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern void playSound(int snd, int ch);
extern void addTeleportStars(Entity *e);

View File

@ -23,6 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static void action(void);
static void touch(Entity *other);
static void activate(int active);
static void load(cJSON *root);
static void save(cJSON *root);
Entity *initTeleporter(void)
{
@ -43,6 +45,8 @@ Entity *initTeleporter(void)
s->action = action;
s->touch = touch;
s->activate = activate;
s->load = load;
s->save = save;
return (Entity*)s;
}
@ -93,3 +97,13 @@ static void activate(int active)
}
}
}
static void load(cJSON *root)
{
/* nothing to do */
}
static void save(cJSON *root)
{
cJSON_AddStringToObject(root, "type", "Teleporter");
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../../common.h"
#include "../../json/cJSON.h"
extern void playSound(int snd, int ch);
extern void addTeleporterEffect(float x, float y);

View File

@ -24,6 +24,8 @@ void unitTick(void);
static void attack(void);
static int canFire(Entity *target);
static void preFire(void);
static void load(cJSON *root);
static void save(cJSON *root);
Unit *createUnit(void)
{
@ -55,6 +57,8 @@ Unit *createUnit(void)
u->preFire = preFire;
u->attack = attack;
u->canFire = canFire;
u->load = load;
u->save = save;
return u;
}
@ -219,3 +223,32 @@ static int canFire(Entity *target)
{
return 0;
}
static void load(cJSON *root)
{
Unit *u;
u = (Unit*)self;
u->canCarryItem = cJSON_GetObjectItem(root, "canCarryItem")->valueint;
u->startX = cJSON_GetObjectItem(root, "startX")->valueint;
u->startY = cJSON_GetObjectItem(root, "startY")->valueint;
u->isMissionTarget = cJSON_GetObjectItem(root, "isMissionTarget")->valueint;
u->health = cJSON_GetObjectItem(root, "health")->valueint;
u->healthMax = cJSON_GetObjectItem(root, "healthMax")->valueint;
}
static void save(cJSON *root)
{
Unit *u;
u = (Unit*)self;
cJSON_AddStringToObject(root, "type", "Unit");
cJSON_AddNumberToObject(root, "canCarryItem", u->canCarryItem);
cJSON_AddNumberToObject(root, "startX", u->startX);
cJSON_AddNumberToObject(root, "startY", u->startY);
cJSON_AddNumberToObject(root, "isMissionTarget", u->isMissionTarget);
cJSON_AddNumberToObject(root, "health", u->health);
cJSON_AddNumberToObject(root, "healthMax", u->healthMax);
}

View File

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../common.h"
#include "../json/cJSON.h"
extern void initEntity(Entity *e);
extern void lookForPlayer(void);

View File

@ -32,6 +32,7 @@ typedef struct Widget Widget;
typedef struct Atlas Atlas;
typedef struct Bucket Bucket;
typedef struct EntityDef EntityDef;
typedef struct cJSON cJSON;
typedef struct Entity Entity;
typedef struct EntityExt EntityExt;
@ -144,7 +145,8 @@ struct Entity {
void (*applyDamage)(int amount);
void (*changeEnvironment)(void);
Sprite *(*getCurrentSprite)(void);
int (*preSave)(void);
void (*load)(cJSON *root);
void (*save)(cJSON *root);
SDL_Rect *(*getBounds)(void);
Entity *next;
};

View File

@ -142,7 +142,17 @@ static void loadEntities(cJSON *root)
for (node = root->child ; node != NULL ; node = node->next)
{
createEntity(cJSON_GetObjectItem(node, "type")->valuestring);
self = createEntity(cJSON_GetObjectItem(node, "type")->valuestring);
if (cJSON_GetObjectItem(node, "name"))
{
STRNCPY(self->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
}
self->x = cJSON_GetObjectItem(node, "x")->valueint;
self->y = cJSON_GetObjectItem(node, "y")->valueint;
self->load(node);
}
}

View File

@ -27,4 +27,5 @@ extern Entity *createEntity(char *typeStr);
extern long lookup(const char *name);
extern void initBob(Bob *b);
extern Entity *self;
extern World world;