[Add] Radial collision detection.

[Add] Pilots now take damage.
This commit is contained in:
Allanis 2013-02-05 20:12:09 +00:00
parent ccdeb8d13b
commit f220145d7d
6 changed files with 54 additions and 22 deletions

View File

@ -510,6 +510,11 @@ int gl_init(void) {
supported = 1; supported = 1;
} }
} }
for(i = 0; modes[i]; ++i)
free(modes[i]);
free(modes);
// Make sure fullscreen mode is supported. // Make sure fullscreen mode is supported.
if(flags & SDL_FULLSCREEN && !supported) { if(flags & SDL_FULLSCREEN && !supported) {
WARN("Fullscreen mode %dx%d is not supported by your current setup, switching to another mode", WARN("Fullscreen mode %dx%d is not supported by your current setup, switching to another mode",

View File

@ -12,8 +12,8 @@
static unsigned int pilot_id = 0; static unsigned int pilot_id = 0;
// Stack of pilots - yes, they come in stacks now. // Stack of pilots - yes, they come in stacks now.
static Pilot** pilot_stack; Pilot** pilot_stack;
static int pilots = 0; int pilots = 0;
extern Pilot* player; extern Pilot* player;
// External. // External.
@ -58,7 +58,7 @@ void pilot_shoot(Pilot* p, int secondary) {
switch(p->outfits[i].outfit->type) { switch(p->outfits[i].outfit->type) {
case OUTFIT_TYPE_BOLT: case OUTFIT_TYPE_BOLT:
weapon_add(p->outfits[i].outfit, p->solid->dir, &p->solid->pos, weapon_add(p->outfits[i].outfit, p->solid->dir, &p->solid->pos,
&p->solid->vel, (p==player) ? WEAPON_LAYER_FG : WEAPON_LAYER_BG); &p->solid->vel, p->id, (p==player) ? WEAPON_LAYER_FG : WEAPON_LAYER_BG);
p->outfits[i].timer = SDL_GetTicks(); p->outfits[i].timer = SDL_GetTicks();
break; break;
default: default:
@ -67,6 +67,16 @@ void pilot_shoot(Pilot* p, int secondary) {
} }
} }
// Damage the pilot.
void pilot_hit(Pilot* p, double damage_shield, double damage_armor) {
if(p->shield -= damage_shield > 0.)
p->shield -= damage_shield;
else if(p->shield > 0.) {
// Shields can take part of the blow.
p->armor -= p->shield/damage_shield*damage_armor;
}
}
// Render the pilot. // Render the pilot.
void pilot_render(Pilot* p) { void pilot_render(Pilot* p) {
int sprite; int sprite;

View File

@ -38,10 +38,13 @@ typedef struct Pilot {
Task* task; // Current action. Task* task; // Current action.
} Pilot; } Pilot;
// Grabing pilot crap.
extern Pilot* player; // The player. extern Pilot* player; // The player.
Pilot* get_pilot(unsigned int id); Pilot* get_pilot(unsigned int id);
// MISC.
void pilot_shoot(Pilot* p, int secondary); void pilot_shoot(Pilot* p, int secondary);
void pilot_hit(Pilot* p, double damage_shield, double damage_armor);
// Creation. // Creation.
void pilot_init(Pilot* dest, Ship* ship, char* name, const double dir, void pilot_init(Pilot* dest, Ship* ship, char* name, const double dir,

View File

@ -24,12 +24,11 @@ static double player_acc = 0.; // Accel velocity from input.
static int player_primary = 0; // Player is shooting primary weapon. static int player_primary = 0; // Player is shooting primary weapon.
extern void pilot_render(Pilot* pilot); // Extern is in Pilot.* extern void pilot_render(Pilot* pilot); // Extern is in Pilot.*
static void player_renderGUI(void);
// Render the player. // Render the player.
void player_render(void) { void player_render(void) {
pilot_render(player); pilot_render(player);
player_renderGUI(); // Render gui.
} }
// Used in pilot.c // Used in pilot.c
@ -44,14 +43,6 @@ void player_think(Pilot* player) {
vect_pset(&player->solid->force, player->ship->thrust * player_acc, player->solid->dir); vect_pset(&player->solid->force, player->ship->thrust * player_acc, player->solid->dir);
} }
// ================
// GUI!
// ================
static void player_renderGUI(void) {
}
// ================ // ================
// INPUT! // INPUT!
// ================ // ================

View File

@ -7,15 +7,24 @@
#include "main.h" #include "main.h"
#include "log.h" #include "log.h"
#include "rng.h" #include "rng.h"
#include "pilot.h"
#include "weapon.h" #include "weapon.h"
#define SIZE_APROX 0.8 // Aproximation for circle collision detection.
// Some stuff from pilot.
extern Pilot** pilot_stack;
extern int pilots;
typedef struct Weapon { typedef struct Weapon {
Solid* solid; // Actually has its own solid. :D Solid* solid; // Actually has its own solid. :D
unsigned int parent; // The pilot that just shot at you!
const Outfit* outfit; // Related outfit that fired. const Outfit* outfit; // Related outfit that fired.
unsigned int timer; // Mainly used to see when the weapon was fired. unsigned int timer; // Mainly used to see when the weapon was fired.
void(*update)(struct Weapon*, const double); // Position update and render. // Update position and render.
void(*update)(struct Weapon*, const double, WeaponLayer); // Position update and render.
void(*think)(struct Weapon*); // Some missiles need to be inteligent. void(*think)(struct Weapon*); // Some missiles need to be inteligent.
} Weapon; } Weapon;
@ -30,10 +39,10 @@ static int mfrontLayer = 0; // Allocated memory size.
static Weapon* weapon_create(const Outfit* outfit, const double dir, static Weapon* weapon_create(const Outfit* outfit, const double dir,
const Vec2* pos, const Vec2* vel); const Vec2* pos, const Vec2* vel, unsigned int parent);
static void weapon_render(const Weapon* w); static void weapon_render(const Weapon* w);
static void weapon_update(Weapon* w, const double dt); static void weapon_update(Weapon* w, const double dt, WeaponLayer layer);
static void weapon_destroy(Weapon* w, WeaponLayer layer); static void weapon_destroy(Weapon* w, WeaponLayer layer);
static void weapon_free(Weapon* w); static void weapon_free(Weapon* w);
@ -65,7 +74,7 @@ void weapons_update(const double dt, WeaponLayer layer) {
default: default:
break; break;
} }
weapon_update(wlayer[i], dt); weapon_update(wlayer[i], dt, layer);
} }
} }
@ -81,18 +90,30 @@ static void weapon_render(const Weapon* w) {
} }
// Update the weapon. // Update the weapon.
static void weapon_update(Weapon* w, const double dt) { static void weapon_update(Weapon* w, const double dt, WeaponLayer layer) {
int i;
for(i = 0; i < pilots; i++) {
if((w->parent != pilot_stack[i]->id) &&
(DIST(w->solid->pos, pilot_stack[i]->solid->pos) < (SIZE_APROX +
w->outfit->gfx_space->sw/2. + pilot_stack[i]->ship->gfx_ship->sw/2.))) {
pilot_hit(pilot_stack[i], w->outfit->damage_shield, w->outfit->damage_armor);
weapon_destroy(w, layer);
return;
}
}
if(w->think) (*w->think)(w); if(w->think) (*w->think)(w);
(*w->solid->update)(w->solid, dt); (*w->solid->update)(w->solid, dt);
weapon_render(w); weapon_render(w);
} }
static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* pos, const Vec2* vel) { static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* pos,
const Vec2* vel, unsigned int parent) {
Vec2 v; Vec2 v;
double mass = 1; // Presumer lasers have a mass of 1. double mass = 1; // Presumer lasers have a mass of 1.
double rdir = dir; // Real direction (accuracy). double rdir = dir; // Real direction (accuracy).
Weapon* w = MALLOC_L(Weapon); Weapon* w = MALLOC_L(Weapon);
w->parent = parent; // Non-Changeable.
w->outfit = outfit; // Non-Changeable. w->outfit = outfit; // Non-Changeable.
w->update = weapon_update; w->update = weapon_update;
w->timer = SDL_GetTicks(); w->timer = SDL_GetTicks();
@ -101,6 +122,7 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
switch(outfit->type) { switch(outfit->type) {
case OUTFIT_TYPE_BOLT: case OUTFIT_TYPE_BOLT:
rdir += RNG(-outfit->accuracy/2., outfit->accuracy/2.)/180.*M_PI; rdir += RNG(-outfit->accuracy/2., outfit->accuracy/2.)/180.*M_PI;
if((rdir > 2.*M_PI) || (rdir < 0.)) rdir = fmod(rdir, 2.*M_PI);
vect_cset(&v, VX(*vel)+outfit->speed*cos(rdir), VANGLE(*vel)+outfit->speed*sin(rdir)); vect_cset(&v, VX(*vel)+outfit->speed*cos(rdir), VANGLE(*vel)+outfit->speed*sin(rdir));
w->solid = solid_create(mass, rdir, pos, &v); w->solid = solid_create(mass, rdir, pos, &v);
break; break;
@ -111,13 +133,14 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2*
} }
// Add a new weapon. // Add a new weapon.
void weapon_add(const Outfit* outfit, const double dir, const Vec2* pos, const Vec2* vel, WeaponLayer layer) { void weapon_add(const Outfit* outfit, const double dir, const Vec2* pos, const Vec2* vel,
unsigned int parent, WeaponLayer layer) {
if(!outfit_isWeapon(outfit)) { if(!outfit_isWeapon(outfit)) {
ERR("Trying to create a weapon from a non-Weapon type Outfit"); ERR("Trying to create a weapon from a non-Weapon type Outfit");
return; return;
} }
Weapon* w = weapon_create(outfit, dir, pos, vel); Weapon* w = weapon_create(outfit, dir, pos, vel, parent);
// Set the propper layer. // Set the propper layer.
Weapon** curLayer = NULL; Weapon** curLayer = NULL;

View File

@ -5,7 +5,7 @@
typedef enum { WEAPON_LAYER_BG, WEAPON_LAYER_FG } WeaponLayer; typedef enum { WEAPON_LAYER_BG, WEAPON_LAYER_FG } WeaponLayer;
void weapon_add(const Outfit* outfit, const double dir, void weapon_add(const Outfit* outfit, const double dir,
const Vec2* pos, const Vec2* vel, WeaponLayer layer); const Vec2* pos, const Vec2* vel, unsigned int parent, WeaponLayer layer);
void weapons_update(const double dt, WeaponLayer layer); void weapons_update(const double dt, WeaponLayer layer);