Further item code updates: allow items to be created and collected.

This commit is contained in:
Steve 2015-11-16 12:31:50 +00:00
parent a040a8de98
commit 8c0b7fdb2f
9 changed files with 118 additions and 8 deletions

View File

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

Binary file not shown.

View File

@ -43,10 +43,14 @@ void loadItemDefs(void)
e = malloc(sizeof(Entity));
memset(e, 0, sizeof(Entity));
e->type = ET_ITEM;
e->active = 1;
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);
e->health = e->maxHealth = FPS;
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
defTail->next = e;
@ -66,6 +70,10 @@ Entity *spawnItem(char *name)
memcpy(item, def, sizeof(Entity));
item->dx = rand() % 100 - rand() % 100;
item->dx *= 0.01;
item->dy = rand() % 100 - rand() % 100;
item->dy *= 0.01;
item->action = action;
return item;
@ -98,8 +106,13 @@ static void action(void)
{
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");
self->alive = ALIVE_DEAD;
playSound(SND_GET_ITEM);
addHudMessage(colors.white, "Picked up %s", self->name);
updateObjective(self->name, TT_ITEM);
checkTrigger(self->name, TRIGGER_ITEM);
}
}
}

View File

@ -31,5 +31,10 @@ 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 void playSound(int id);
extern void addHudMessage(SDL_Color c, char *format, ...);
extern void updateObjective(char *name, int type);
extern void checkTrigger(char *name, int type);
extern Entity *self;
extern Colors colors;

View File

@ -83,9 +83,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
enum
{
ET_FIGHTER,
ET_ITEM,
ET_WAYPOINT,
ET_EXTRACTION_POINT,
ET_ITEM
ET_EXTRACTION_POINT
};
enum
@ -154,6 +154,7 @@ enum
SND_EXPLOSION_2,
SND_EXPLOSION_3,
SND_EXPLOSION_4,
SND_GET_ITEM,
SND_MISSILE,
SND_BOOST,
SND_GUI_CLICK,
@ -176,7 +177,8 @@ enum
TT_DESTROY,
TT_DISABLE,
TT_WAYPOINT,
TT_ESCAPED
TT_ESCAPED,
TT_ITEM
};
enum
@ -184,7 +186,8 @@ enum
TRIGGER_TIME,
TRIGGER_KILLS,
TRIGGER_WAYPOINT,
TRIGGER_ESCAPES
TRIGGER_ESCAPES,
TRIGGER_ITEM
};
enum

View File

@ -27,6 +27,8 @@ static void loadFighters(cJSON *node);
static void loadFighterGroups(cJSON *node);
static void loadEntities(cJSON *node);
static void loadEntityGroups(cJSON *node);
static void loadItems(cJSON *node);
static void loadItemGroups(cJSON *node);
static unsigned long hashcode(const char *str);
static char **toFighterTypeArray(char *types, int *numTypes);
static void loadEpicData(cJSON *node);
@ -65,6 +67,10 @@ void loadMission(char *filename)
loadEntityGroups(cJSON_GetObjectItem(root, "entityGroups"));
loadItems(cJSON_GetObjectItem(root, "items"));
loadItemGroups(cJSON_GetObjectItem(root, "itemGroups"));
STRNCPY(music, cJSON_GetObjectItem(root, "music")->valuestring, MAX_NAME_LENGTH);
if (cJSON_GetObjectItem(root, "epic"))
@ -422,7 +428,87 @@ static void loadEntityGroups(cJSON *node)
STRNCPY(e->groupName, groupName, MAX_NAME_LENGTH);
}
e->id = battle.entId++;
e->x = x;
e->y = y;
e->x += (rand() % scatter) - (rand() % scatter);
e->y += (rand() % scatter) - (rand() % scatter);
SDL_QueryTexture(e->texture, NULL, NULL, &e->w, &e->h);
}
node = node->next;
}
}
}
static void loadItems(cJSON *node)
{
}
static void loadItemGroups(cJSON *node)
{
Entity *e;
char *name, *groupName, *type;
int i, scatter, number;
long flags;
float x, y;
flags = -1;
scatter = 1;
if (node)
{
node = node->child;
while (node)
{
type = cJSON_GetObjectItem(node, "type")->valuestring;
number = cJSON_GetObjectItem(node, "number")->valueint;
x = cJSON_GetObjectItem(node, "x")->valuedouble * GRID_CELL_WIDTH;
y = cJSON_GetObjectItem(node, "y")->valuedouble * GRID_CELL_HEIGHT;
name = NULL;
groupName = NULL;
if (cJSON_GetObjectItem(node, "name"))
{
name = cJSON_GetObjectItem(node, "name")->valuestring;
}
if (cJSON_GetObjectItem(node, "groupName"))
{
groupName = cJSON_GetObjectItem(node, "groupName")->valuestring;
}
if (cJSON_GetObjectItem(node, "scatter"))
{
scatter = cJSON_GetObjectItem(node, "scatter")->valueint;
}
if (cJSON_GetObjectItem(node, "flags"))
{
flags = flagsToLong(cJSON_GetObjectItem(node, "flags")->valuestring);
}
for (i = 0 ; i < number ; i++)
{
e = spawnItem(type);
if (name)
{
STRNCPY(e->name, name, MAX_NAME_LENGTH);
}
if (groupName)
{
STRNCPY(e->groupName, groupName, MAX_NAME_LENGTH);
}
if (flags != -1)
{
e->flags = flags;
}
e->x = x;
e->y = y;

View File

@ -39,6 +39,7 @@ extern Entity *spawnWaypoint(void);
extern void activateNextWaypoint(void);
extern void selectWidget(const char *name, const char *group);
extern Entity *spawnExtractionPoint(void);
extern Entity *spawnItem(char *type);
extern void failIncompleteObjectives(void);
extern void fleeAllEnemies(void);

View File

@ -47,6 +47,7 @@ void initLookups(void)
addLookup("TT_DISABLE", TT_DISABLE);
addLookup("TT_WAYPOINT", TT_WAYPOINT);
addLookup("TT_ESCAPED", TT_ESCAPED);
addLookup("TT_ITEM", TT_ITEM);
addLookup("WT_BUTTON", WT_BUTTON);
addLookup("WT_SELECT", WT_SELECT);

View File

@ -98,6 +98,7 @@ static void loadSounds(void)
sounds[SND_MISSILE] = Mix_LoadWAV("sound/65787__iwilldstroyu__laserrocket.ogg");
sounds[SND_BOOST] = Mix_LoadWAV("sound/18380__inferno__hvrl.ogg");
/*sounds[SND_ECM] = Mix_LoadWAV("sound/18380__inferno__hvrl.ogg");*/
sounds[SND_GET_ITEM] = Mix_LoadWAV("sound/56246__q-k__latch-04.ogg");
sounds[SND_EXPLOSION_1] = Mix_LoadWAV("sound/162265__qubodup__explosive.ogg");
sounds[SND_EXPLOSION_2] = Mix_LoadWAV("sound/207322__animationisaac__short-explosion.ogg");
sounds[SND_EXPLOSION_3] = Mix_LoadWAV("sound/254071__tb0y298__firework-explosion.ogg");