Start of triggers.

This commit is contained in:
Steve 2015-10-26 08:10:13 +00:00
parent 37a0c77284
commit d0a3d0d6c5
18 changed files with 205 additions and 19 deletions

View File

@ -7,5 +7,13 @@
"player" : { "player" : {
"type" : "Nymph", "type" : "Nymph",
"side" : "SIDE_CSN" "side" : "SIDE_CSN"
} },
"triggers" : [
{
"type" : "TRIGGER_TIME",
"targetName" : "TIME",
"targetValue" : 10,
"action" : "TA_COMPLETE_MISSION"
}
]
} }

View File

@ -30,7 +30,7 @@ OBJS += objectives.o options.o
OBJS += player.o OBJS += player.o
OBJS += radar.o OBJS += radar.o
OBJS += save.o sound.o starfield.o starSystems.o stats.o OBJS += save.o sound.o starfield.o starSystems.o stats.o
OBJS += testMission.o textures.o text.o title.o transition.o OBJS += testMission.o textures.o text.o title.o transition.o triggers.o
OBJS += util.o OBJS += util.o
OBJS += widgets.o OBJS += widgets.o

View File

@ -44,6 +44,7 @@ void initBattle(void)
battle.fighterTail = &battle.fighterHead; battle.fighterTail = &battle.fighterHead;
battle.effectTail = &battle.effectHead; battle.effectTail = &battle.effectHead;
battle.objectiveTail = &battle.objectiveHead; battle.objectiveTail = &battle.objectiveHead;
battle.triggerTail = &battle.triggerHead;
app.delegate.logic = &logic; app.delegate.logic = &logic;
app.delegate.draw = &draw; app.delegate.draw = &draw;
@ -138,6 +139,12 @@ static void doBattle(void)
} }
} }
} }
battle.stats[STAT_TIME]++;
if (battle.stats[STAT_TIME] % FPS == 0)
{
checkTrigger("TIME", TRIGGER_TIME);
}
} }
static void draw(void) static void draw(void)
@ -293,7 +300,8 @@ static void postBattle(void)
{ {
int i; int i;
for (i = 0 ; i < STAT_MAX ; i++) /* we don't want to count the time when adding up stats */
for (i = 0 ; i < STAT_TIME ; i++)
{ {
game.stats[i] += battle.stats[i]; game.stats[i] += battle.stats[i];
} }
@ -311,6 +319,7 @@ void destroyBattle(void)
Bullet *b; Bullet *b;
Effect *e; Effect *e;
Objective *o; Objective *o;
Trigger *t;
while (battle.fighterHead.next) while (battle.fighterHead.next)
{ {
@ -343,4 +352,12 @@ void destroyBattle(void)
free(o); free(o);
} }
battle.objectiveTail = &battle.objectiveHead; battle.objectiveTail = &battle.objectiveHead;
while (battle.triggerHead.next)
{
t = battle.triggerHead.next;
battle.triggerHead.next = t->next;
free(t);
}
battle.triggerTail = &battle.triggerHead;
} }

View File

@ -48,7 +48,6 @@ extern void initGalacticMap(void);
extern void drawWidgets(char *groupName); extern void drawWidgets(char *groupName);
extern void selectWidget(const char *name, const char *group); extern void selectWidget(const char *name, const char *group);
extern Widget *getWidget(const char *name, const char *group); extern Widget *getWidget(const char *name, const char *group);
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern void doWidgets(void); extern void doWidgets(void);
extern void loadMission(char *filename); extern void loadMission(char *filename);
extern void resetHud(void); extern void resetHud(void);
@ -61,10 +60,9 @@ extern void scrollBackground(float x, float y);
extern void initOptions(void (*returnFromOptions)(void)); extern void initOptions(void (*returnFromOptions)(void));
extern void drawOptions(void); extern void drawOptions(void);
extern void playSound(int id); extern void playSound(int id);
extern void initPlayer(void); extern void checkTrigger(char *name, int type);
extern App app; extern App app;
extern Battle battle; extern Battle battle;
extern Colors colors;
extern Fighter *player; extern Fighter *player;
extern Game game; extern Game game;

View File

@ -290,6 +290,8 @@ void doFighters(void)
updateObjective(f->name, TT_DESTROY); updateObjective(f->name, TT_DESTROY);
updateCondition(f->name, TT_DESTROY); updateCondition(f->name, TT_DESTROY);
checkTrigger(f->name, TRIGGER_KILLS);
} }
if (f == battle.fighterTail) if (f == battle.fighterTail)

View File

@ -37,6 +37,7 @@ extern void updateObjective(char *name, int type);
extern void updateCondition(char *name, int type); extern void updateCondition(char *name, int type);
extern Fighter *getFighterDef(char *name); extern Fighter *getFighterDef(char *name);
extern void addHudMessage(SDL_Color c, char *format, ...); extern void addHudMessage(SDL_Color c, char *format, ...);
extern void checkTrigger(char *name, int type);
extern App app; extern App app;
extern Battle battle; extern Battle battle;

View File

@ -60,10 +60,7 @@ void doObjectives(void)
{ {
if (battle.numObjectivesTotal > 0 && battle.numObjectivesComplete == battle.numObjectivesTotal) if (battle.numObjectivesTotal > 0 && battle.numObjectivesComplete == battle.numObjectivesTotal)
{ {
battle.status = MS_COMPLETE; completeMission();
battle.missionFinishedTimer = FPS;
game.stats[STAT_MISSIONS_COMPLETED]++;
completeConditions(); completeConditions();
@ -72,8 +69,7 @@ void doObjectives(void)
if (objectiveFailed) if (objectiveFailed)
{ {
battle.status = MS_FAILED; failMission();
battle.missionFinishedTimer = FPS;
failIncompleteObjectives(); failIncompleteObjectives();
} }

View File

@ -25,6 +25,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern void updateChallenges(void); extern void updateChallenges(void);
extern void addHudMessage(SDL_Color c, char *format, ...); extern void addHudMessage(SDL_Color c, char *format, ...);
extern void completeMission(void);
extern void failMission(void);
extern Battle battle; extern Battle battle;
extern Colors colors; extern Colors colors;

67
src/battle/triggers.c Normal file
View File

@ -0,0 +1,67 @@
/*
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 "triggers.h"
static int conditionMet(Trigger *trigger);
static void fireTrigger(Trigger *trigger);
void checkTrigger(char *name, int type)
{
Trigger *trigger;
for (trigger = battle.triggerHead.next ; trigger != NULL ; trigger = trigger->next)
{
if (trigger->type == type && strcmp(trigger->targetName, name) == 0 && conditionMet(trigger))
{
fireTrigger(trigger);
}
}
}
static int conditionMet(Trigger *trigger)
{
switch (trigger->type)
{
case TRIGGER_TIME:
return trigger->targetValue == battle.stats[STAT_TIME] / FPS;
case TRIGGER_KILLS:
return trigger->targetValue == battle.stats[STAT_ENEMIES_KILLED];
}
return 0;
}
static void fireTrigger(Trigger *trigger)
{
switch (trigger->action)
{
case TA_COMPLETE_MISSION:
addHudMessage(colors.green, "Mission Complete!");
completeMission();
break;
case TA_FAIL_MISSION:
addHudMessage(colors.red, "Mission Failed!");
failMission();
break;
}
}

31
src/battle/triggers.h Normal file
View File

@ -0,0 +1,31 @@
/*
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"
extern void completeMission(void);
extern void failMission(void);
extern void addHudMessage(SDL_Color c, char *format, ...);
extern Battle battle;
extern Colors colors;

View File

@ -143,6 +143,18 @@ enum
TT_DISABLE TT_DISABLE
}; };
enum
{
TRIGGER_TIME,
TRIGGER_KILLS
};
enum
{
TA_COMPLETE_MISSION,
TA_FAIL_MISSION
};
enum enum
{ {
MS_START, MS_START,

View File

@ -64,7 +64,6 @@ extern void playSound(int id);
extern void blitRotated(SDL_Texture *texture, int x, int y, int angle); extern void blitRotated(SDL_Texture *texture, int x, int y, int angle);
extern void initStatsDisplay(void); extern void initStatsDisplay(void);
extern void handleStatsKB(void); extern void handleStatsKB(void);
extern Mission *getMission(char *filename);
extern void updateStarSystemMissions(void); extern void updateStarSystemMissions(void);
extern App app; extern App app;

View File

@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "mission.h" #include "mission.h"
static void loadObjectives(cJSON *node); static void loadObjectives(cJSON *node);
static void loadTriggers(cJSON *node);
static void loadPlayer(cJSON *node); static void loadPlayer(cJSON *node);
static void loadFighters(cJSON *node); static void loadFighters(cJSON *node);
static void loadFighterGroups(cJSON *node); static void loadFighterGroups(cJSON *node);
@ -48,6 +49,8 @@ void loadMission(char *filename)
battle.planet.y = rand() % SCREEN_HEIGHT - rand() % SCREEN_HEIGHT; battle.planet.y = rand() % SCREEN_HEIGHT - rand() % SCREEN_HEIGHT;
loadObjectives(cJSON_GetObjectItem(root, "objectives")); loadObjectives(cJSON_GetObjectItem(root, "objectives"));
loadTriggers(cJSON_GetObjectItem(root, "triggers"));
loadPlayer(cJSON_GetObjectItem(root, "player")); loadPlayer(cJSON_GetObjectItem(root, "player"));
@ -80,6 +83,20 @@ void loadMission(char *filename)
playMusic(music); playMusic(music);
} }
void completeMission(void)
{
battle.status = MS_COMPLETE;
battle.missionFinishedTimer = FPS;
game.stats[STAT_MISSIONS_COMPLETED]++;
}
void failMission(void)
{
battle.status = MS_FAILED;
battle.missionFinishedTimer = FPS;
}
static void loadObjectives(cJSON *node) static void loadObjectives(cJSON *node)
{ {
Objective *o; Objective *o;
@ -92,6 +109,8 @@ static void loadObjectives(cJSON *node)
{ {
o = malloc(sizeof(Objective)); o = malloc(sizeof(Objective));
memset(o, 0, sizeof(Objective)); memset(o, 0, sizeof(Objective));
battle.objectiveTail->next = o;
battle.objectiveTail = o;
STRNCPY(o->description, cJSON_GetObjectItem(node, "description")->valuestring, MAX_DESCRIPTION_LENGTH); STRNCPY(o->description, cJSON_GetObjectItem(node, "description")->valuestring, MAX_DESCRIPTION_LENGTH);
STRNCPY(o->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH); STRNCPY(o->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH);
@ -108,8 +127,30 @@ static void loadObjectives(cJSON *node)
o->isOptional = cJSON_GetObjectItem(node, "isOptional")->valueint; o->isOptional = cJSON_GetObjectItem(node, "isOptional")->valueint;
} }
battle.objectiveTail->next = o; node = node->next;
battle.objectiveTail = o; }
}
}
static void loadTriggers(cJSON *node)
{
Trigger *t;
if (node)
{
node = node->child;
while (node)
{
t = malloc(sizeof(Trigger));
memset(t, 0, sizeof(Trigger));
battle.triggerTail->next = t;
battle.triggerTail = t;
t->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring);
STRNCPY(t->targetName, cJSON_GetObjectItem(node, "targetName")->valuestring, MAX_NAME_LENGTH);
t->targetValue = cJSON_GetObjectItem(node, "targetValue")->valueint;
t->action = lookup(cJSON_GetObjectItem(node, "action")->valuestring);
node = node->next; node = node->next;
} }

View File

@ -25,8 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_STAT_ITEMS 9 #define MAX_STAT_ITEMS 9
extern void selectWidget(const char *name, const char *group);
extern Widget *getWidget(const char *name, const char *group);
extern void drawWidgets(char *groupName); extern void drawWidgets(char *groupName);
extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...); extern void drawText(int x, int y, int size, int align, SDL_Color c, const char *format, ...);
extern void blit(SDL_Texture *texture, int x, int y, int centered); extern void blit(SDL_Texture *texture, int x, int y, int centered);

View File

@ -27,7 +27,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern void cleanup(void); extern void cleanup(void);
extern void initSDL(void); extern void initSDL(void);
extern void initBattle(void);
extern void initGameSystem(void); extern void initGameSystem(void);
extern void initTitle(void); extern void initTitle(void);
extern void loadTestMission(char *filename); extern void loadTestMission(char *filename);

View File

@ -32,6 +32,7 @@ typedef struct Mission Mission;
typedef struct Pulse Pulse; typedef struct Pulse Pulse;
typedef struct Widget Widget; typedef struct Widget Widget;
typedef struct HudMessage HudMessage; typedef struct HudMessage HudMessage;
typedef struct Trigger Trigger;
typedef struct { typedef struct {
float x; float x;
@ -174,6 +175,14 @@ struct Challenge {
Challenge *next; Challenge *next;
}; };
struct Trigger {
int type;
char targetName[MAX_NAME_LENGTH];
int targetValue;
int action;
Trigger *next;
};
struct Mission { struct Mission {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
char description[MAX_DESCRIPTION_LENGTH]; char description[MAX_DESCRIPTION_LENGTH];
@ -217,6 +226,7 @@ typedef struct {
Bullet bulletHead, *bulletTail; Bullet bulletHead, *bulletTail;
Effect effectHead, *effectTail; Effect effectHead, *effectTail;
Objective objectiveHead, *objectiveTail; Objective objectiveHead, *objectiveTail;
Trigger triggerHead, *triggerTail;
unsigned int stats[STAT_MAX]; unsigned int stats[STAT_MAX];
} Battle; } Battle;

View File

@ -25,7 +25,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../json/cJSON.h" #include "../json/cJSON.h"
extern char *readFile(char *filename); extern char *readFile(char *filename);
extern StarSystem *getStarSystem(char *name);
extern Mission *getMission(char *filename); extern Mission *getMission(char *filename);
extern Challenge *getChallenge(Mission *mission, int type); extern Challenge *getChallenge(Mission *mission, int type);
extern int lookup(char *lookup); extern int lookup(char *lookup);

View File

@ -91,6 +91,12 @@ void initLookups(void)
addLookup("STAT_PLAYER_KILLED", STAT_PLAYER_KILLED); addLookup("STAT_PLAYER_KILLED", STAT_PLAYER_KILLED);
addLookup("STAT_DISABLED", STAT_DISABLED); addLookup("STAT_DISABLED", STAT_DISABLED);
addLookup("STAT_TIME", STAT_TIME); addLookup("STAT_TIME", STAT_TIME);
addLookup("TRIGGER_TIME", TRIGGER_TIME);
addLookup("TRIGGER_KILLS", TRIGGER_KILLS);
addLookup("TA_COMPLETE_MISSION", TA_COMPLETE_MISSION);
addLookup("TA_FAIL_MISSION", TA_FAIL_MISSION);
} }
static void addLookup(char *name, long value) static void addLookup(char *name, long value)