diff --git a/data/missions/test/test.json b/data/missions/test/test.json index d09f980..1767e0c 100644 --- a/data/missions/test/test.json +++ b/data/missions/test/test.json @@ -8,12 +8,12 @@ "type" : "Nymph", "side" : "SIDE_CSN" }, - "triggers" : [ + "entities" : [ { - "type" : "TRIGGER_TIME", - "targetName" : "TIME", - "targetValue" : 10, - "action" : "TA_COMPLETE_MISSION" + "name" : "Waypoint", + "type" : "ET_WAYPOINT", + "x" : 100, + "y" : 100 } ] } diff --git a/gfx/entities/waypoint.png b/gfx/entities/waypoint.png new file mode 100644 index 0000000..e84fa0e Binary files /dev/null and b/gfx/entities/waypoint.png differ diff --git a/makefile b/makefile index c6e149d..408a707 100644 --- a/makefile +++ b/makefile @@ -19,7 +19,7 @@ OBJS += ai.o OBJS += battle.o bullets.o OBJS += challenges.o cJSON.o OBJS += draw.o -OBJS += effects.o +OBJS += effects.o entities.o OBJS += fighters.o fighterDefs.o OBJS += galacticMap.o game.o OBJS += hud.o @@ -32,7 +32,7 @@ OBJS += radar.o OBJS += save.o sound.o starfield.o starSystems.o stats.o OBJS += testMission.o textures.o text.o title.o transition.o triggers.o OBJS += util.o -OBJS += widgets.o +OBJS += waypoints.o widgets.o # top-level rule to create the program. all: $(PROG) diff --git a/src/battle/battle.c b/src/battle/battle.c index 1d6a0cb..80e4e78 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -41,6 +41,7 @@ void initBattle(void) { memset(&battle, 0, sizeof(Battle)); battle.bulletTail = &battle.bulletHead; + battle.entityTail = &battle.entityHead; battle.fighterTail = &battle.fighterHead; battle.effectTail = &battle.effectHead; battle.objectiveTail = &battle.objectiveHead; @@ -119,6 +120,8 @@ static void doBattle(void) doFighters(); + doEntities(); + doEffects(); doPlayer(); @@ -161,6 +164,8 @@ static void draw(void) drawFighters(); + drawEntities(); + drawEffects(); drawHud(); @@ -316,6 +321,7 @@ static void postBattle(void) void destroyBattle(void) { Fighter *f; + Entity *ent; Bullet *b; Effect *e; Objective *o; @@ -329,6 +335,14 @@ void destroyBattle(void) } battle.fighterTail = &battle.fighterHead; + while (battle.entityHead.next) + { + ent = battle.entityHead.next; + battle.entityHead.next = ent->next; + free(ent); + } + battle.entityTail = &battle.entityHead; + while (battle.bulletHead.next) { b = battle.bulletHead.next; diff --git a/src/battle/battle.h b/src/battle/battle.h index a1cbaa8..44bef77 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -35,6 +35,8 @@ extern void drawBullets(void); extern void doStars(float dx, float dy); extern void drawStars(void); extern void doFighters(void); +extern void doEntities(void); +extern void drawEntities(void); extern void drawFighters(void); extern void initStars(void); extern void doPlayer(void); diff --git a/src/battle/entities.c b/src/battle/entities.c new file mode 100644 index 0000000..6fea766 --- /dev/null +++ b/src/battle/entities.c @@ -0,0 +1,64 @@ +/* +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 "entities.h" + +void doEntities(void) +{ + Entity *e, *prev; + + prev = &battle.entityHead; + + for (e = battle.entityHead.next ; e != NULL ; e = e->next) + { + e->x += e->dx; + e->y += e->dy; + + e->x -= battle.ssx; + e->y -= battle.ssy; + + if (e->action != NULL) + { + if (--e->thinkTime <= 0) + { + e->action(); + } + } + + if (e->health <= 0) + { + prev->next = e->next; + free(e); + e = prev; + } + + prev = e; + } +} + +void drawEntities(void) +{ + Entity *e; + + for (e = battle.entityHead.next ; e != NULL ; e = e->next) + { + blitRotated(e->texture, e->x, e->y, e->angle); + } +} diff --git a/src/battle/entities.h b/src/battle/entities.h new file mode 100644 index 0000000..518d828 --- /dev/null +++ b/src/battle/entities.h @@ -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 "SDL2/SDL.h" + +#include "../defs.h" +#include "../structs.h" + +extern void blitRotated(SDL_Texture *t, int x, int y, int angle); + +extern Battle battle; diff --git a/src/battle/objectives.h b/src/battle/objectives.h index 4688adf..f18dfc5 100644 --- a/src/battle/objectives.h +++ b/src/battle/objectives.h @@ -30,4 +30,3 @@ extern void failMission(void); extern Battle battle; extern Colors colors; -extern Game game; diff --git a/src/battle/waypoints.c b/src/battle/waypoints.c new file mode 100644 index 0000000..3a69da0 --- /dev/null +++ b/src/battle/waypoints.c @@ -0,0 +1,42 @@ +/* +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 "waypoints.h" + +static void rotate(void); + +Entity *spawnWaypoint(void) +{ + Entity *waypoint = malloc(sizeof(Entity)); + memset(waypoint, 0, sizeof(Entity)); + battle.entityTail->next = waypoint; + battle.entityTail = waypoint; + + waypoint->health = waypoint->maxHealth = FPS; + waypoint->texture = getTexture("gfx/entities/waypoint.png"); + waypoint->action = rotate; + + return waypoint; +} + +static void rotate(void) +{ + +} diff --git a/src/battle/waypoints.h b/src/battle/waypoints.h new file mode 100644 index 0000000..588a06a --- /dev/null +++ b/src/battle/waypoints.h @@ -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 "SDL2/SDL.h" + +#include "../defs.h" +#include "../structs.h" + +extern Battle battle; + +extern SDL_Texture *getTexture(char *filename); diff --git a/src/defs.h b/src/defs.h index 8a285cd..9a05eb1 100644 --- a/src/defs.h +++ b/src/defs.h @@ -57,6 +57,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define FF_IMMORTAL (2 << 2) #define FF_MISSION_TARGET (2 << 3) +enum +{ + ET_WAYPOINT +}; + enum { TA_LEFT, diff --git a/src/galaxy/mission.c b/src/galaxy/mission.c index 53f7fdb..6496c22 100644 --- a/src/galaxy/mission.c +++ b/src/galaxy/mission.c @@ -25,6 +25,7 @@ static void loadTriggers(cJSON *node); static void loadPlayer(cJSON *node); static void loadFighters(cJSON *node); static void loadFighterGroups(cJSON *node); +static void loadEntities(cJSON *node); static unsigned long hashcode(const char *str); static char **toFighterTypeArray(char *types, int *numTypes); @@ -58,6 +59,8 @@ void loadMission(char *filename) loadFighterGroups(cJSON_GetObjectItem(root, "fighterGroups")); + loadEntities(cJSON_GetObjectItem(root, "entities")); + STRNCPY(music, cJSON_GetObjectItem(root, "music")->valuestring, MAX_NAME_LENGTH); cJSON_Delete(root); @@ -246,6 +249,40 @@ static void loadFighterGroups(cJSON *node) } } +static void loadEntities(cJSON *node) +{ + Entity *e; + int type; + + if (node) + { + node = node->child; + + while (node) + { + type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); + + switch (type) + { + case ET_WAYPOINT: + e = spawnWaypoint(); + break; + } + + STRNCPY(e->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); + e->x = cJSON_GetObjectItem(node, "x")->valueint; + e->y = cJSON_GetObjectItem(node, "y")->valueint; + + if (cJSON_GetObjectItem(node, "flags")) + { + e->flags = flagsToLong(cJSON_GetObjectItem(node, "flags")->valuestring); + } + + node = node->next; + } + } +} + static char **toFighterTypeArray(char *types, int *numTypes) { int i; diff --git a/src/galaxy/mission.h b/src/galaxy/mission.h index b1e1282..9ba9e15 100644 --- a/src/galaxy/mission.h +++ b/src/galaxy/mission.h @@ -35,6 +35,7 @@ extern void playMusic(char *filename); extern void stopMusic(void); extern void initPlayer(void); extern long flagsToLong(char *flags); +extern Entity *spawnWaypoint(void); extern Battle battle; extern Fighter *player; diff --git a/src/json/cJSON.c b/src/json/cJSON.c index 46cbf72..a330f69 100644 --- a/src/json/cJSON.c +++ b/src/json/cJSON.c @@ -338,7 +338,7 @@ cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int r /* Default options for cJSON_Parse */ cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);} -/* Render a cJSON item/Fighter/structure to text. */ +/* Render a cJSON item/entity/structure to text. */ char *cJSON_Print(cJSON *item) {return print_value(item,0,1,0);} char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0,0);} diff --git a/src/json/cJSON.h b/src/json/cJSON.h index ce38cf2..466d10d 100644 --- a/src/json/cJSON.h +++ b/src/json/cJSON.h @@ -65,13 +65,13 @@ extern void cJSON_InitHooks(cJSON_Hooks* hooks); /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */ extern cJSON *cJSON_Parse(const char *value); -/* Render a cJSON Fighter to text for transfer/storage. Free the char* when finished. */ +/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */ extern char *cJSON_Print(cJSON *item); -/* Render a cJSON Fighter to text for transfer/storage without any formatting. Free the char* when finished. */ +/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ extern char *cJSON_PrintUnformatted(cJSON *item); -/* Render a cJSON Fighter to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ extern char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt); -/* Delete a cJSON Fighter and all subentities. */ +/* Delete a cJSON entity and all subentities. */ extern void cJSON_Delete(cJSON *c); /* Returns the number of items in an array (or object). */ diff --git a/src/structs.h b/src/structs.h index 88b6fc5..3d1d523 100644 --- a/src/structs.h +++ b/src/structs.h @@ -23,6 +23,7 @@ typedef struct Texture Texture; typedef struct Lookup Lookup; typedef struct Weapon Weapon; typedef struct Fighter Fighter; +typedef struct Entity Entity; typedef struct Bullet Bullet; typedef struct Effect Effect; typedef struct Objective Objective; @@ -68,6 +69,27 @@ struct Weapon { int y; }; +struct Entity { + int type; + char name[MAX_NAME_LENGTH]; + int side; + float x; + float y; + float dx; + float dy; + int angle; + int health; + int maxHealth; + int shield; + int thinkTime; + long flags; + void (*action)(void); + void (*defaultAction)(void); + void (*die)(void); + SDL_Texture *texture; + Entity *next; +}; + struct Fighter { char name[MAX_NAME_LENGTH]; int active; @@ -223,6 +245,7 @@ typedef struct { SDL_Texture *background, *planetTexture; PointF planet; Fighter fighterHead, *fighterTail; + Entity entityHead, *entityTail; Bullet bulletHead, *bulletTail; Effect effectHead, *effectTail; Objective objectiveHead, *objectiveTail; diff --git a/src/system/lookup.c b/src/system/lookup.c index 2be68e3..c9f28c7 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -30,6 +30,8 @@ void initLookups(void) memset(&head, 0, sizeof(Lookup)); tail = &head; + addLookup("ET_WAYPOINT", ET_WAYPOINT); + addLookup("FF_NO_KILL", FF_NO_KILL); addLookup("FF_DISABLE", FF_DISABLE); addLookup("FF_IMMORTAL", FF_IMMORTAL);