Start of items.

This commit is contained in:
Steve 2015-11-16 11:27:03 +00:00
parent 6bdee92b35
commit a040a8de98
7 changed files with 165 additions and 1 deletions

6
data/battle/items.json Normal file
View File

@ -0,0 +1,6 @@
[
{
"name" : "Smile",
"textureName" : "gfx/items/smile.png"
}
]

BIN
gfx/items/smile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 B

View File

@ -25,7 +25,7 @@ OBJS += effects.o entities.o extractionPoint.o
OBJS += fighters.o fighterDefs.o OBJS += fighters.o fighterDefs.o
OBJS += galacticMap.o game.o grid.o OBJS += galacticMap.o game.o grid.o
OBJS += hud.o OBJS += hud.o
OBJS += init.o io.o OBJS += init.o io.o items.o
OBJS += load.o lookup.o OBJS += load.o lookup.o
OBJS += main.o mission.o missionInfo.o OBJS += main.o mission.o missionInfo.o
OBJS += objectives.o options.o OBJS += objectives.o options.o

117
src/battle/items.c Normal file
View File

@ -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);
}
}

35
src/battle/items.h Normal file
View File

@ -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;

View File

@ -148,6 +148,8 @@ void initGameSystem(void)
loadFighterDefs(); loadFighterDefs();
loadItemDefs();
initBulletDefs(); initBulletDefs();
initStarSystems(); initStarSystems();
@ -247,6 +249,8 @@ void cleanup(void)
destroyBulletDefs(); destroyBulletDefs();
destroyItemDefs();
destroyStarSystems(); destroyStarSystems();
destroyBattle(); destroyBattle();

View File

@ -40,6 +40,7 @@ extern int writeFile(char *filename, char *data);
extern char *getSaveFilePath(char *filename); extern char *getSaveFilePath(char *filename);
extern void initSounds(void); extern void initSounds(void);
extern void loadFighterDefs(void); extern void loadFighterDefs(void);
extern void loadItemDefs(void);
extern void initFonts(void); extern void initFonts(void);
extern void initBulletDefs(void); extern void initBulletDefs(void);
extern void initLookups(void); extern void initLookups(void);
@ -53,6 +54,7 @@ extern void destroySounds(void);
extern void destroyGame(void); extern void destroyGame(void);
extern void destroyFighterDefs(void); extern void destroyFighterDefs(void);
extern void destroyBulletDefs(void); extern void destroyBulletDefs(void);
extern void destroyItemDefs(void);
extern void destroyStarSystems(void); extern void destroyStarSystems(void);
extern void destroyBattle(void); extern void destroyBattle(void);
extern void destroyTextures(void); extern void destroyTextures(void);