Added Locations for use with mission scripting.

This commit is contained in:
Steve 2015-12-22 12:42:04 +00:00
parent 15d7faf386
commit 1884c8371e
7 changed files with 145 additions and 7 deletions

View File

@ -16,7 +16,7 @@ OBJS += fighters.o
OBJS += galacticMap.o game.o grid.o OBJS += galacticMap.o game.o grid.o
OBJS += hud.o OBJS += hud.o
OBJS += init.o input.o io.o items.o OBJS += init.o input.o io.o items.o
OBJS += load.o lookup.o OBJS += load.o locations.o lookup.o
OBJS += main.o messageBox.o mission.o missionInfo.o OBJS += main.o messageBox.o mission.o missionInfo.o
OBJS += objectives.o options.o OBJS += objectives.o options.o
OBJS += player.o OBJS += player.o

View File

@ -46,6 +46,7 @@ void initBattle(void)
battle.entityTail = &battle.entityHead; battle.entityTail = &battle.entityHead;
battle.effectTail = &battle.effectHead; battle.effectTail = &battle.effectHead;
battle.objectiveTail = &battle.objectiveHead; battle.objectiveTail = &battle.objectiveHead;
battle.locationTail = &battle.locationHead;
app.delegate.logic = &logic; app.delegate.logic = &logic;
app.delegate.draw = &draw; app.delegate.draw = &draw;
@ -147,17 +148,17 @@ static void doBattle(void)
if (player != NULL) if (player != NULL)
{ {
doLocations();
doMessageBox(); doMessageBox();
}
if (battle.status == MS_IN_PROGRESS)
if (battle.status == MS_IN_PROGRESS)
{
if (player != NULL)
{ {
doScript(); doScript();
} }
} }
else
if (battle.status != MS_IN_PROGRESS)
{ {
battle.missionFinishedTimer--; battle.missionFinishedTimer--;
} }
@ -194,6 +195,11 @@ static void draw(void)
drawEffects(); drawEffects();
if (dev.debug)
{
drawLocations();
}
drawHud(); drawHud();
if (player != NULL) if (player != NULL)
@ -358,6 +364,7 @@ void destroyBattle(void)
Debris *d; Debris *d;
Effect *e; Effect *e;
Objective *o; Objective *o;
Location *l;
while (battle.entityHead.next) while (battle.entityHead.next)
{ {
@ -399,6 +406,14 @@ void destroyBattle(void)
} }
battle.objectiveTail = &battle.objectiveHead; battle.objectiveTail = &battle.objectiveHead;
while (battle.locationHead.next)
{
l = battle.locationHead.next;
battle.locationHead.next = l->next;
free(l);
}
battle.locationTail = &battle.locationHead;
cJSON_Delete(battle.missionJSON); cJSON_Delete(battle.missionJSON);
resetHud(); resetHud();

View File

@ -78,6 +78,8 @@ extern void initBullets(void);
extern void initDebris(void); extern void initDebris(void);
extern void doDebris(void); extern void doDebris(void);
extern void drawDebris(void); extern void drawDebris(void);
extern void doLocations(void);
extern void drawLocations(void);
extern App app; extern App app;
extern Battle battle; extern Battle battle;

52
src/battle/locations.c Normal file
View File

@ -0,0 +1,52 @@
/*
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 "locations.h"
void doLocations(void)
{
Location *l, *prev;
prev = &battle.locationHead;
for (l = battle.locationHead.next ; l != NULL ; l = l->next)
{
if (getDistance(player->x, player->y, l->x, l->y) <= l->size)
{
runScriptFunction(l->name);
prev->next = l->next;
free(l);
l = prev;
}
prev = l;
}
}
void drawLocations(void)
{
Location *l;
for (l = battle.locationHead.next ; l != NULL ; l = l->next)
{
drawCircle(l->x - battle.camera.x, l->y - battle.camera.y, l->size, 0, 255, 0, 255);
}
}

28
src/battle/locations.h Normal file
View File

@ -0,0 +1,28 @@
/*
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 "../common.h"
extern Battle battle;
extern Entity *player;
extern int getDistance(int x1, int y1, int x2, int y2);
extern void runScriptFunction(char *format, ...);
extern void drawCircle(int cx, int cy, int radius, int r, int g, int b, int a);

View File

@ -26,6 +26,7 @@ static void loadFighters(cJSON *node);
static void loadCapitalShips(cJSON *node); static void loadCapitalShips(cJSON *node);
static void loadEntities(cJSON *node); static void loadEntities(cJSON *node);
static void loadItems(cJSON *node); static void loadItems(cJSON *node);
static void loadLocations(cJSON *node);
static unsigned long hashcode(const char *str); static unsigned long hashcode(const char *str);
static char **toTypeArray(char *types, int *numTypes); static char **toTypeArray(char *types, int *numTypes);
static void loadEpicData(cJSON *node); static void loadEpicData(cJSON *node);
@ -62,6 +63,8 @@ void loadMission(char *filename)
loadItems(cJSON_GetObjectItem(root, "items")); loadItems(cJSON_GetObjectItem(root, "items"));
loadLocations(cJSON_GetObjectItem(root, "locations"));
STRNCPY(music, cJSON_GetObjectItem(root, "music")->valuestring, MAX_NAME_LENGTH); STRNCPY(music, cJSON_GetObjectItem(root, "music")->valuestring, MAX_NAME_LENGTH);
if (cJSON_GetObjectItem(root, "epic")) if (cJSON_GetObjectItem(root, "epic"))
@ -630,6 +633,34 @@ static void loadItems(cJSON *node)
} }
} }
static void loadLocations(cJSON *node)
{
Location *l;
if (node)
{
node = node->child;
while (node)
{
l = malloc(sizeof(Location));
memset(l, 0, sizeof(Location));
battle.locationTail->next = l;
battle.locationTail = l;
STRNCPY(l->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH);
l->x = cJSON_GetObjectItem(node, "x")->valueint * GRID_CELL_WIDTH;
l->y = cJSON_GetObjectItem(node, "y")->valueint * GRID_CELL_HEIGHT;
l->size = cJSON_GetObjectItem(node, "size")->valueint;
l->x += (GRID_CELL_WIDTH / 2);
l->y += (GRID_CELL_HEIGHT / 2);
node = node->next;
}
}
}
static char **toTypeArray(char *types, int *numTypes) static char **toTypeArray(char *types, int *numTypes)
{ {
int i; int i;

View File

@ -35,6 +35,7 @@ typedef struct HudMessage HudMessage;
typedef struct MessageBox MessageBox; typedef struct MessageBox MessageBox;
typedef struct GridCell GridCell; typedef struct GridCell GridCell;
typedef struct ScriptRunner ScriptRunner; typedef struct ScriptRunner ScriptRunner;
typedef struct Location Location;
typedef struct { typedef struct {
int debug; int debug;
@ -202,6 +203,14 @@ struct Effect {
Effect *next; Effect *next;
}; };
struct Location {
char name[MAX_NAME_LENGTH];
int x;
int y;
int size;
Location *next;
};
struct Objective { struct Objective {
int active; int active;
char description[MAX_DESCRIPTION_LENGTH]; char description[MAX_DESCRIPTION_LENGTH];
@ -281,6 +290,7 @@ typedef struct {
Debris debrisHead, *debrisTail; Debris debrisHead, *debrisTail;
Effect effectHead, *effectTail; Effect effectHead, *effectTail;
Objective objectiveHead, *objectiveTail; Objective objectiveHead, *objectiveTail;
Location locationHead, *locationTail;
struct cJSON *missionJSON; struct cJSON *missionJSON;
unsigned int stats[STAT_MAX]; unsigned int stats[STAT_MAX];
GridCell grid[GRID_SIZE][GRID_SIZE]; GridCell grid[GRID_SIZE][GRID_SIZE];