From a040a8de989dba1f4f8a7a86efcbeb58a8824598 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 16 Nov 2015 11:27:03 +0000 Subject: [PATCH] Start of items. --- data/battle/items.json | 6 +++ gfx/items/smile.png | Bin 0 -> 517 bytes makefile | 2 +- src/battle/items.c | 117 +++++++++++++++++++++++++++++++++++++++++ src/battle/items.h | 35 ++++++++++++ src/system/init.c | 4 ++ src/system/init.h | 2 + 7 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 data/battle/items.json create mode 100644 gfx/items/smile.png create mode 100644 src/battle/items.c create mode 100644 src/battle/items.h 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 0000000000000000000000000000000000000000..e8cb27c76a5c4fcfe2f82cd51a17690e5ca71236 GIT binary patch literal 517 zcmV+g0{Z=lP)X1^@s6FWx?200006VoOIv0RI60 z0RN!9r;`8x010qNS#tmY3ljhU3ljkVnw%H_000McNliru-wO~5H7g5m-1Gnd02y>e zSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00C`DL_t(Y$HkVhRl_h4hClhG zjC=B)S{A_RVSo$(8Ne9;dRPE@yg&wk0YY!H0IlE40C8^$q(r{Ru>=XZ_gu)9?(=`A z(@7CwZA}KGW>_js6S`IfIKDB@wF9wM0w2H%SRDeZ1CRyq2uuQhx&WS(W*r1FD(-=q zN5PPlzzgsu)hQ2T30(3w%Ymf}OT>f)aIyUhy?eaj=>ML}kS8K1+i=Onk>e@w(AGOx zBJwifc$NdRwr1=qn!?YEBiqzQ$bvEnYU{n-7&SI_W&MsEQ0m$0LnlSJB5|yo!m_qz zoE+0*Um5u7=p+z;L}@Bu<155i>&aQAG>ui%fdwRaPfgF7()x8lO>8&@AON4%Z{m=f zn5}EWJ20=S$mHo6y1k^W{i-Zr08#+&_5_!0q5y7XT}fC#4ohIJG+*|`NKAM&Z99#0 z>{GMuts@h{JJb9&zbK~?n9HVj-aEs;--3F;Zr`eu4iE1)N^yTC$Xl0y00000NkvXX Hu0mjfB8bzJ literal 0 HcmV?d00001 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);