diff --git a/common.mk b/common.mk index 9be17b4..a39f80f 100644 --- a/common.mk +++ b/common.mk @@ -11,7 +11,7 @@ DEPS += defs.h structs.h OBJS += ai.o OBJS += battle.o bullets.o OBJS += capitalShips.o challenges.o cJSON.o -OBJS += draw.o +OBJS += debris.o draw.o OBJS += effects.o entities.o extractionPoint.o OBJS += fighters.o OBJS += galacticMap.o game.o grid.o diff --git a/gfx/debris/debris1.png b/gfx/debris/debris1.png new file mode 100644 index 0000000..3f24190 Binary files /dev/null and b/gfx/debris/debris1.png differ diff --git a/gfx/debris/debris2.png b/gfx/debris/debris2.png new file mode 100644 index 0000000..2a66d7e Binary files /dev/null and b/gfx/debris/debris2.png differ diff --git a/gfx/debris/debris3.png b/gfx/debris/debris3.png new file mode 100644 index 0000000..8d429c8 Binary files /dev/null and b/gfx/debris/debris3.png differ diff --git a/gfx/debris/debris4.png b/gfx/debris/debris4.png new file mode 100644 index 0000000..217fed6 Binary files /dev/null and b/gfx/debris/debris4.png differ diff --git a/gfx/debris/debris5.png b/gfx/debris/debris5.png new file mode 100644 index 0000000..39ba84f Binary files /dev/null and b/gfx/debris/debris5.png differ diff --git a/gfx/debris/debris6.png b/gfx/debris/debris6.png new file mode 100644 index 0000000..4de2362 Binary files /dev/null and b/gfx/debris/debris6.png differ diff --git a/src/battle/battle.c b/src/battle/battle.c index b86c352..71c02cc 100644 --- a/src/battle/battle.c +++ b/src/battle/battle.c @@ -42,6 +42,7 @@ void initBattle(void) { memset(&battle, 0, sizeof(Battle)); battle.bulletTail = &battle.bulletHead; + battle.debrisTail = &battle.debrisHead; battle.entityTail = &battle.entityHead; battle.effectTail = &battle.effectHead; battle.objectiveTail = &battle.objectiveHead; @@ -68,6 +69,8 @@ void initBattle(void) initMissionInfo(); + initDebris(); + resetWaypoints(); show = SHOW_BATTLE; @@ -136,6 +139,8 @@ static void doBattle(void) doEffects(); + doDebris(); + doPlayer(); doMessageBox(); @@ -177,6 +182,8 @@ static void draw(void) drawBullets(); + drawDebris(); + drawEffects(); drawHud(); @@ -339,6 +346,7 @@ void destroyBattle(void) { Entity *ent; Bullet *b; + Debris *d; Effect *e; Objective *o; @@ -358,6 +366,14 @@ void destroyBattle(void) } battle.bulletTail = &battle.bulletHead; + while (battle.debrisHead.next) + { + d = battle.debrisHead.next; + battle.debrisHead.next = d->next; + free(d); + } + battle.debrisTail = &battle.debrisHead; + while (battle.effectHead.next) { e = battle.effectHead.next; diff --git a/src/battle/battle.h b/src/battle/battle.h index 16b3a78..398fb7b 100644 --- a/src/battle/battle.h +++ b/src/battle/battle.h @@ -75,6 +75,9 @@ extern void doMessageBox(void); extern void drawMessageBox(void); extern void resetMessageBox(void); extern void initBullets(void); +extern void initDebris(void); +extern void doDebris(void); +extern void drawDebris(void); extern App app; extern Battle battle; diff --git a/src/battle/debris.c b/src/battle/debris.c new file mode 100644 index 0000000..e1ae101 --- /dev/null +++ b/src/battle/debris.c @@ -0,0 +1,141 @@ +/* +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 "debris.h" + +static void changeCourse(Debris *d); + +static Debris *debrisToDraw[MAX_DEBRIS_TO_DRAW]; +static SDL_Texture *debrisTexture[MAX_DEBRIS_TEXTURES]; + +void initDebris(void) +{ + memset(debrisToDraw, 0, sizeof(Debris*) * MAX_DEBRIS_TO_DRAW); + + debrisTexture[0] = getTexture("gfx/debris/debris1.png"); + debrisTexture[1] = getTexture("gfx/debris/debris2.png"); + debrisTexture[2] = getTexture("gfx/debris/debris3.png"); + debrisTexture[3] = getTexture("gfx/debris/debris4.png"); + debrisTexture[4] = getTexture("gfx/debris/debris5.png"); + debrisTexture[5] = getTexture("gfx/debris/debris6.png"); +} + +void addDebris(int x, int y, int amount) +{ + int i; + Debris *d; + + for (i = 0 ; i < amount ; i++) + { + d = malloc(sizeof(Debris)); + memset(d, 0, sizeof(Debris)); + battle.debrisTail->next = d; + battle.debrisTail = d; + + d->x = x; + d->y = y; + d->dx = rand() % 500 - rand() % 500; + d->dx *= 0.01; + d->dy = rand() % 500 - rand() % 500; + d->dy *= 0.01; + d->health = FPS + (FPS * (rand() % 3)); + + d->texture = debrisTexture[rand() % MAX_DEBRIS_TEXTURES]; + } +} + +void doDebris(void) +{ + int i; + Debris *d, *prev; + + memset(debrisToDraw, 0, sizeof(Debris*) * MAX_DEBRIS_TO_DRAW); + + prev = &battle.debrisHead; + + i = 0; + + for (d = battle.debrisHead.next ; d != NULL ; d = d->next) + { + d->x += d->dx; + d->y += d->dy; + + if (--d->thinkTime <= 0) + { + changeCourse(d); + } + + d->angle = mod(d->angle + 1, 360); + + addDebrisFire(d->x, d->y); + + if (--d->health <= 0) + { + if (d == battle.debrisTail) + { + battle.debrisTail = prev; + } + + prev->next = d->next; + free(d); + d = prev; + } + else + { + if (collision(d->x - 16 - battle.camera.x, d->y - 16 - battle.camera.y, 32, 32, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)) + { + debrisToDraw[i++] = d; + if (i >= MAX_DEBRIS_TO_DRAW) + { + printf("Too much debris to draw\n"); + exit(1); + } + } + } + + prev = d; + } +} + +static void changeCourse(Debris *d) +{ + float dir; + + dir = rand() % 25 - rand() % 25; + dir *= 0.01; + d->dx += dir; + + dir = rand() % 25 - rand() % 25; + dir *= 0.01; + d->dy += dir; + + d->thinkTime = 1 + (rand() % 5); +} + +void drawDebris(void) +{ + int i; + Debris *d; + + for (i = 0, d = debrisToDraw[i] ; d != NULL ; d = debrisToDraw[++i]) + { + blitRotated(d->texture, d->x - battle.camera.x, d->y - battle.camera.y, d->angle); + } +} diff --git a/src/battle/debris.h b/src/battle/debris.h new file mode 100644 index 0000000..203f936 --- /dev/null +++ b/src/battle/debris.h @@ -0,0 +1,34 @@ +/* +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" + +#include "../json/cJSON.h" + +#define MAX_DEBRIS_TO_DRAW 512 +#define MAX_DEBRIS_TEXTURES 6 + +extern float mod(float n, float x); +extern void blitRotated(SDL_Texture *texture, int x, int y, int angle); +extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); +extern SDL_Texture *getTexture(char *filename); +extern void addDebrisFire(int x, int y); + +extern Battle battle; diff --git a/src/battle/effects.c b/src/battle/effects.c index 224e25f..99b6e74 100644 --- a/src/battle/effects.c +++ b/src/battle/effects.c @@ -140,6 +140,31 @@ void addSmallFighterExplosion(void) e->y -= e->size / 2; } +void addDebrisFire(int x, int y) +{ + Effect *e; + + e = malloc(sizeof(Effect)); + memset(e, 0, sizeof(Effect)); + battle.effectTail->next = e; + battle.effectTail = e; + + e->type = EFFECT_TEXTURE; + + e->x = x + (rand() % 8 - rand() % 8); + e->y = y + (rand() % 8 - rand() % 8); + e->texture = explosionTexture; + e->health = 0; + e->size = 8 + rand() % 9; + + setRandomFlameHue(e); + + e->a = rand() % 256; + + e->x -= e->size / 2; + e->y -= e->size / 2; +} + void addSmallExplosion(void) { int i; diff --git a/src/battle/fighters.c b/src/battle/fighters.c index e33818c..24d69ca 100644 --- a/src/battle/fighters.c +++ b/src/battle/fighters.c @@ -469,6 +469,7 @@ static void immediateDie(void) self->alive = ALIVE_DEAD; addSmallExplosion(); playBattleSound(SND_EXPLOSION_1 + rand() % 4, self->x, self->y); + addDebris(self->x, self->y, 8); } static void spinDie(void) @@ -491,6 +492,7 @@ static void spinDie(void) self->alive = ALIVE_DEAD; addSmallExplosion(); playBattleSound(SND_EXPLOSION_1 + rand() % 4, self->x, self->y); + addDebris(self->x, self->y, 8); } } @@ -512,6 +514,7 @@ static void straightDie(void) self->alive = ALIVE_DEAD; addSmallExplosion(); playBattleSound(SND_EXPLOSION_1 + rand() % 4, self->x, self->y); + addDebris(self->x, self->y, 8); } } diff --git a/src/battle/fighters.h b/src/battle/fighters.h index 30608d8..1e5ae89 100644 --- a/src/battle/fighters.h +++ b/src/battle/fighters.h @@ -44,6 +44,7 @@ extern void addShieldSplinterEffect(Entity *ent); extern void completeMission(void); extern void runScriptFunction(char *format, ...); extern char *getFileLocation(char *filename); +extern void addDebris(int x, int y, int amount); extern App app; extern Battle battle; diff --git a/src/structs.h b/src/structs.h index a757746..a990b13 100644 --- a/src/structs.h +++ b/src/structs.h @@ -23,6 +23,7 @@ typedef struct Lookup Lookup; typedef struct Weapon Weapon; typedef struct Entity Entity; typedef struct Bullet Bullet; +typedef struct Debris Debris; typedef struct Effect Effect; typedef struct Objective Objective; typedef struct StarSystem StarSystem; @@ -140,6 +141,18 @@ struct Bullet { Bullet *next; }; +struct Debris { + float x; + float y; + float dx; + float dy; + int health; + int thinkTime; + float angle; + SDL_Texture *texture; + Debris *next; +}; + typedef struct { float x; float y; @@ -247,6 +260,7 @@ typedef struct { PointF planet; Entity entityHead, *entityTail; Bullet bulletHead, *bulletTail; + Debris debrisHead, *debrisTail; Effect effectHead, *effectTail; Objective objectiveHead, *objectiveTail; struct cJSON *missionJSON;