[Add] Radial collision detection.
[Add] Pilots now take damage.
This commit is contained in:
parent
ccdeb8d13b
commit
f220145d7d
@ -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",
|
||||
|
16
src/pilot.c
16
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;
|
||||
|
@ -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,
|
||||
|
11
src/player.c
11
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!
|
||||
// ================
|
||||
|
39
src/weapon.c
39
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;
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user