Added debris.

This commit is contained in:
Steve 2015-12-13 14:50:54 +00:00
parent 99eb742d34
commit 743786efb8
15 changed files with 238 additions and 1 deletions

View File

@ -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

BIN
gfx/debris/debris1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 822 B

BIN
gfx/debris/debris2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
gfx/debris/debris3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
gfx/debris/debris4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 886 B

BIN
gfx/debris/debris5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 889 B

BIN
gfx/debris/debris6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 803 B

View File

@ -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;

View File

@ -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;

141
src/battle/debris.c Normal file
View File

@ -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);
}
}

34
src/battle/debris.h Normal file
View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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;