diff --git a/src/entities/blobs/mia.c b/src/entities/blobs/mia.c index ff4ac67..508b057 100644 --- a/src/entities/blobs/mia.c +++ b/src/entities/blobs/mia.c @@ -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); +} diff --git a/src/entities/blobs/mia.h b/src/entities/blobs/mia.h index 1dfd640..3578db7 100644 --- a/src/entities/blobs/mia.h +++ b/src/entities/blobs/mia.h @@ -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); diff --git a/src/entities/entities.c b/src/entities/entities.c index 711ef1b..867c5f8 100644 --- a/src/entities/entities.c +++ b/src/entities/entities.c @@ -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; diff --git a/src/entities/evilBlobs/pistolBlob.c b/src/entities/evilBlobs/pistolBlob.c index 186b92b..8d7a7cd 100644 --- a/src/entities/evilBlobs/pistolBlob.c +++ b/src/entities/evilBlobs/pistolBlob.c @@ -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"); +} diff --git a/src/entities/evilBlobs/pistolBlob.h b/src/entities/evilBlobs/pistolBlob.h index de1b0d2..aa4290f 100644 --- a/src/entities/evilBlobs/pistolBlob.h +++ b/src/entities/evilBlobs/pistolBlob.h @@ -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); diff --git a/src/entities/items/item.c b/src/entities/items/item.c index db17e65..f02e28d 100644 --- a/src/entities/items/item.c +++ b/src/entities/items/item.c @@ -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); +} diff --git a/src/entities/items/item.h b/src/entities/items/item.h index 26bb443..114d4d9 100644 --- a/src/entities/items/item.h +++ b/src/entities/items/item.h @@ -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, ...); diff --git a/src/entities/misc/infoPoint.c b/src/entities/misc/infoPoint.c index 1757ca1..4e9bf83 100644 --- a/src/entities/misc/infoPoint.c +++ b/src/entities/misc/infoPoint.c @@ -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); +} diff --git a/src/entities/misc/infoPoint.h b/src/entities/misc/infoPoint.h index d7eda86..c7d77d1 100644 --- a/src/entities/misc/infoPoint.h +++ b/src/entities/misc/infoPoint.h @@ -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); diff --git a/src/entities/structures/cardReader.c b/src/entities/structures/cardReader.c index 3508db5..12dea36 100644 --- a/src/entities/structures/cardReader.c +++ b/src/entities/structures/cardReader.c @@ -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); +} diff --git a/src/entities/structures/cardReader.h b/src/entities/structures/cardReader.h index 3b40a8e..0d25c2a 100644 --- a/src/entities/structures/cardReader.h +++ b/src/entities/structures/cardReader.h @@ -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); diff --git a/src/entities/structures/door.c b/src/entities/structures/door.c index 3461540..ecfe66d 100644 --- a/src/entities/structures/door.c +++ b/src/entities/structures/door.c @@ -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); + } +} diff --git a/src/entities/structures/door.h b/src/entities/structures/door.h index f6644b4..2f1d791 100644 --- a/src/entities/structures/door.h +++ b/src/entities/structures/door.h @@ -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); diff --git a/src/entities/structures/horizontalDoor.c b/src/entities/structures/horizontalDoor.c index 5aa0b5f..796182e 100644 --- a/src/entities/structures/horizontalDoor.c +++ b/src/entities/structures/horizontalDoor.c @@ -24,7 +24,7 @@ Entity *initHorizontalDoor(void) { Structure *s; - s = createStructure(); + s = (Structure*)initDoor(); s->type = ET_DOOR; diff --git a/src/entities/structures/horizontalDoor.h b/src/entities/structures/horizontalDoor.h index 6a17410..c7f62e6 100644 --- a/src/entities/structures/horizontalDoor.h +++ b/src/entities/structures/horizontalDoor.h @@ -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); diff --git a/src/entities/structures/lift.c b/src/entities/structures/lift.c index ec93899..bd34200 100644 --- a/src/entities/structures/lift.c +++ b/src/entities/structures/lift.c @@ -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); +} diff --git a/src/entities/structures/lift.h b/src/entities/structures/lift.h index d2908d9..9fab32b 100644 --- a/src/entities/structures/lift.h +++ b/src/entities/structures/lift.h @@ -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); diff --git a/src/entities/structures/powerPoint.c b/src/entities/structures/powerPoint.c index 0435238..bc13807 100644 --- a/src/entities/structures/powerPoint.c +++ b/src/entities/structures/powerPoint.c @@ -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); +} diff --git a/src/entities/structures/powerPoint.h b/src/entities/structures/powerPoint.h index d67ce52..08098f1 100644 --- a/src/entities/structures/powerPoint.h +++ b/src/entities/structures/powerPoint.h @@ -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); diff --git a/src/entities/structures/powerPool.c b/src/entities/structures/powerPool.c index 071fcee..a93fc60 100644 --- a/src/entities/structures/powerPool.c +++ b/src/entities/structures/powerPool.c @@ -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"); +} diff --git a/src/entities/structures/powerPool.h b/src/entities/structures/powerPool.h index 80b9c24..2765461 100644 --- a/src/entities/structures/powerPool.h +++ b/src/entities/structures/powerPool.h @@ -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); diff --git a/src/entities/structures/pressurePlate.c b/src/entities/structures/pressurePlate.c index 69c8a56..7579d38 100644 --- a/src/entities/structures/pressurePlate.c +++ b/src/entities/structures/pressurePlate.c @@ -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); +} diff --git a/src/entities/structures/pressurePlate.h b/src/entities/structures/pressurePlate.h index 2a701bd..0b1e5e3 100644 --- a/src/entities/structures/pressurePlate.h +++ b/src/entities/structures/pressurePlate.h @@ -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); diff --git a/src/entities/structures/pushBlock.c b/src/entities/structures/pushBlock.c index 3f79b80..f982e39 100644 --- a/src/entities/structures/pushBlock.c +++ b/src/entities/structures/pushBlock.c @@ -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); +} diff --git a/src/entities/structures/pushBlock.h b/src/entities/structures/pushBlock.h index 9c1bfbc..3664f5b 100644 --- a/src/entities/structures/pushBlock.h +++ b/src/entities/structures/pushBlock.h @@ -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); diff --git a/src/entities/structures/teleporter.c b/src/entities/structures/teleporter.c index 5c1296c..30f4a5d 100644 --- a/src/entities/structures/teleporter.c +++ b/src/entities/structures/teleporter.c @@ -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"); +} diff --git a/src/entities/structures/teleporter.h b/src/entities/structures/teleporter.h index c77eacd..68ee13a 100644 --- a/src/entities/structures/teleporter.h +++ b/src/entities/structures/teleporter.h @@ -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); diff --git a/src/entities/unit.c b/src/entities/unit.c index 5553cbe..cf2b706 100644 --- a/src/entities/unit.c +++ b/src/entities/unit.c @@ -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); +} diff --git a/src/entities/unit.h b/src/entities/unit.h index 38d5586..7779c6b 100644 --- a/src/entities/unit.h +++ b/src/entities/unit.h @@ -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); diff --git a/src/structs.h b/src/structs.h index 595542b..a153b8f 100644 --- a/src/structs.h +++ b/src/structs.h @@ -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; }; diff --git a/src/world/worldLoader.c b/src/world/worldLoader.c index d1f7078..1f76a25 100644 --- a/src/world/worldLoader.c +++ b/src/world/worldLoader.c @@ -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); } } diff --git a/src/world/worldLoader.h b/src/world/worldLoader.h index 53392ff..424b987 100644 --- a/src/world/worldLoader.h +++ b/src/world/worldLoader.h @@ -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;