diff --git a/data/battle/items.json b/data/battle/items.json new file mode 100644 index 0000000..bf44259 --- /dev/null +++ b/data/battle/items.json @@ -0,0 +1,6 @@ +[ + { + "name" : "Smile", + "textureName" : "gfx/items/smile.png" + } +] diff --git a/gfx/items/smile.png b/gfx/items/smile.png new file mode 100644 index 0000000..e8cb27c Binary files /dev/null and b/gfx/items/smile.png differ diff --git a/makefile b/makefile index 9f5d563..7962925 100644 --- a/makefile +++ b/makefile @@ -25,7 +25,7 @@ OBJS += effects.o entities.o extractionPoint.o OBJS += fighters.o fighterDefs.o OBJS += galacticMap.o game.o grid.o OBJS += hud.o -OBJS += init.o io.o +OBJS += init.o io.o items.o OBJS += load.o lookup.o OBJS += main.o mission.o missionInfo.o OBJS += objectives.o options.o diff --git a/src/battle/items.c b/src/battle/items.c new file mode 100644 index 0000000..8e26e3b --- /dev/null +++ b/src/battle/items.c @@ -0,0 +1,117 @@ +/* +Copyright (C) 2015 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#include "items.h" + +static void action(void); +static Entity *getItemDef(char *name); + +static Entity defHead, *defTail; + +void loadItemDefs(void) +{ + cJSON *root, *node; + char *text; + Entity *e; + + text = readFile("data/battle/items.json"); + + root = cJSON_Parse(text); + + memset(&defHead, 0, sizeof(Entity)); + defTail = &defHead; + + for (node = root->child ; node != NULL ; node = node->next) + { + e = malloc(sizeof(Entity)); + memset(e, 0, sizeof(Entity)); + + STRNCPY(e->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(e->defName, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); + e->texture = getTexture(cJSON_GetObjectItem(node, "textureName")->valuestring); + + SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h); + + defTail->next = e; + } + + cJSON_Delete(root); + free(text); +} + +Entity *spawnItem(char *name) +{ + Entity *item, *def; + + item = spawnEntity(); + + def = getItemDef(name); + + memcpy(item, def, sizeof(Entity)); + + item->action = action; + + return item; +} + +static Entity *getItemDef(char *name) +{ + Entity *e; + + for (e = defHead.next ; e != NULL ; e = e->next) + { + if (strcmp(e->name, name) == 0) + { + return e; + } + } + + printf("Error: no such item '%s'\n", name); + exit(1); +} + +static void action(void) +{ + Entity *e, **candidates; + int i; + + candidates = getAllEntsWithin(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, self); + + for (i = 0, e = candidates[i] ; e != NULL ; e = candidates[++i]) + { + if ((e->flags & EF_COLLECTS_ITEMS) && collision(self->x - (self->w / 2), self->y - (self->h / 2), self->w, self->h, e->x - (e->w / 2), e->y - (e->h / 2), e->w, e->h)) + { + e->alive = ALIVE_DEAD; + printf("Picked up an Item\n"); + } + } +} + +void destroyItemDefs(void) +{ + Entity *f; + + while (defHead.next) + { + f = defHead.next; + defHead.next = f->next; + free(f); + } +} diff --git a/src/battle/items.h b/src/battle/items.h new file mode 100644 index 0000000..83532b1 --- /dev/null +++ b/src/battle/items.h @@ -0,0 +1,35 @@ +/* +Copyright (C) 2015 Parallel Realities + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +*/ + +#include "SDL2/SDL.h" + +#include "../defs.h" +#include "../structs.h" +#include "../json/cJSON.h" + +extern char *readFile(char *filename); +extern SDL_Texture *getTexture(char *filename); +extern long lookup(char *name); +extern long flagsToLong(char *flags); +extern Entity *spawnEntity(void); +extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); +extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); + +extern Entity *self; diff --git a/src/system/init.c b/src/system/init.c index 172e723..b6bbaad 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -148,6 +148,8 @@ void initGameSystem(void) loadFighterDefs(); + loadItemDefs(); + initBulletDefs(); initStarSystems(); @@ -247,6 +249,8 @@ void cleanup(void) destroyBulletDefs(); + destroyItemDefs(); + destroyStarSystems(); destroyBattle(); diff --git a/src/system/init.h b/src/system/init.h index 863e3fc..285a52d 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -40,6 +40,7 @@ extern int writeFile(char *filename, char *data); extern char *getSaveFilePath(char *filename); extern void initSounds(void); extern void loadFighterDefs(void); +extern void loadItemDefs(void); extern void initFonts(void); extern void initBulletDefs(void); extern void initLookups(void); @@ -53,6 +54,7 @@ extern void destroySounds(void); extern void destroyGame(void); extern void destroyFighterDefs(void); extern void destroyBulletDefs(void); +extern void destroyItemDefs(void); extern void destroyStarSystems(void); extern void destroyBattle(void); extern void destroyTextures(void);