From f220145d7d58b90fcb2749496874349e44f68e34 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 5 Feb 2013 20:12:09 +0000 Subject: [PATCH] [Add] Radial collision detection. [Add] Pilots now take damage. --- src/opengl.c | 5 +++++ src/pilot.c | 16 +++++++++++++--- src/pilot.h | 3 +++ src/player.c | 11 +---------- src/weapon.c | 39 +++++++++++++++++++++++++++++++-------- src/weapon.h | 2 +- 6 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/opengl.c b/src/opengl.c index bcb5a92..3486090 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -510,6 +510,11 @@ int gl_init(void) { supported = 1; } } + + for(i = 0; modes[i]; ++i) + free(modes[i]); + free(modes); + // Make sure fullscreen mode is supported. if(flags & SDL_FULLSCREEN && !supported) { WARN("Fullscreen mode %dx%d is not supported by your current setup, switching to another mode", diff --git a/src/pilot.c b/src/pilot.c index 257257c..7fa82d6 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -12,8 +12,8 @@ static unsigned int pilot_id = 0; // Stack of pilots - yes, they come in stacks now. -static Pilot** pilot_stack; -static int pilots = 0; +Pilot** pilot_stack; +int pilots = 0; extern Pilot* player; // External. @@ -58,7 +58,7 @@ void pilot_shoot(Pilot* p, int secondary) { switch(p->outfits[i].outfit->type) { case OUTFIT_TYPE_BOLT: 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(); break; 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. void pilot_render(Pilot* p) { int sprite; diff --git a/src/pilot.h b/src/pilot.h index e8b0efb..f3a4832 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -38,10 +38,13 @@ typedef struct Pilot { Task* task; // Current action. } Pilot; +// Grabing pilot crap. extern Pilot* player; // The player. Pilot* get_pilot(unsigned int id); +// MISC. void pilot_shoot(Pilot* p, int secondary); +void pilot_hit(Pilot* p, double damage_shield, double damage_armor); // Creation. void pilot_init(Pilot* dest, Ship* ship, char* name, const double dir, diff --git a/src/player.c b/src/player.c index d0dfb3a..4c66d0f 100644 --- a/src/player.c +++ b/src/player.c @@ -24,12 +24,11 @@ static double player_acc = 0.; // Accel velocity from input. static int player_primary = 0; // Player is shooting primary weapon. extern void pilot_render(Pilot* pilot); // Extern is in Pilot.* -static void player_renderGUI(void); // Render the player. void player_render(void) { pilot_render(player); - player_renderGUI(); + // Render gui. } // 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); } -// ================ -// GUI! -// ================ -static void player_renderGUI(void) { - -} - - // ================ // INPUT! // ================ diff --git a/src/weapon.c b/src/weapon.c index 2653e6c..0b79ce5 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -7,15 +7,24 @@ #include "main.h" #include "log.h" #include "rng.h" +#include "pilot.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 { 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. 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. } Weapon; @@ -30,10 +39,10 @@ static int mfrontLayer = 0; // Allocated memory size. 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_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_free(Weapon* w); @@ -65,7 +74,7 @@ void weapons_update(const double dt, WeaponLayer layer) { default: 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. -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); (*w->solid->update)(w->solid, dt); 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; double mass = 1; // Presumer lasers have a mass of 1. double rdir = dir; // Real direction (accuracy). Weapon* w = MALLOC_L(Weapon); + w->parent = parent; // Non-Changeable. w->outfit = outfit; // Non-Changeable. w->update = weapon_update; w->timer = SDL_GetTicks(); @@ -101,6 +122,7 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* switch(outfit->type) { case OUTFIT_TYPE_BOLT: 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)); w->solid = solid_create(mass, rdir, pos, &v); break; @@ -111,13 +133,14 @@ static Weapon* weapon_create(const Outfit* outfit, const double dir, const Vec2* } // 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)) { ERR("Trying to create a weapon from a non-Weapon type Outfit"); return; } - Weapon* w = weapon_create(outfit, dir, pos, vel); + Weapon* w = weapon_create(outfit, dir, pos, vel, parent); // Set the propper layer. Weapon** curLayer = NULL; diff --git a/src/weapon.h b/src/weapon.h index eb5cf2b..9fe79a8 100644 --- a/src/weapon.h +++ b/src/weapon.h @@ -5,7 +5,7 @@ typedef enum { WEAPON_LAYER_BG, WEAPON_LAYER_FG } WeaponLayer; 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);