From 495a9ccb74d80743559ea7037b2e37e9cfb44381 Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 27 Feb 2013 22:16:09 +0000 Subject: [PATCH] [Add] Enemy pilots die with a big ass explosion. :D --- src/pilot.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/pilot.h | 1 + src/player.c | 3 ++- src/rng.h | 3 ++- src/spfx.c | 9 ++++++--- src/spfx.h | 4 +++- src/weapon.c | 9 +++++++-- 7 files changed, 74 insertions(+), 11 deletions(-) diff --git a/src/pilot.c b/src/pilot.c index 5f7ce0d..b152202 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -7,6 +7,8 @@ #include "weapon.h" #include "pack.h" #include "xml.h" +#include "spfx.h" +#include "rng.h" #include "pilot.h" #define XML_ID "Fleets" // XML section identifier. @@ -42,6 +44,7 @@ static void pilot_hyperspace(Pilot* pilot); void pilot_render(Pilot* pilot); static void pilot_free(Pilot* p); static Fleet* fleet_parse(const xmlNodePtr parent); +static void pilot_dead(Pilot* p); // Get the next pilot based on id. unsigned int pilot_getNext(const unsigned int id) { @@ -150,7 +153,7 @@ static void pilot_shootWeapon(Pilot* p, PilotOutfit* w, const unsigned int t) { // WElll... Trying to shoot when you have no ammo?? FUUU quantity = (outfit_isAmmo(w->outfit) && p->secondary) ? p->secondary->quantity : w->quantity; - delay = (outfit_isWeapon(w->outfit)) ? w->outfit->u.wpn.delay : w->outfit->u.lau.delay; + delay = (outfit_isWeapon(w->outfit)) ? w->outfit->u.wpn.delay : w->outfit->u.wpn.delay; // Check to see if weapon is ready. if((SDL_GetTicks() - w->timer) < (unsigned int)(delay/quantity)) return; @@ -206,8 +209,7 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, dam_mod = 0.; if(!pilot_isFlag(p, PILOT_DEAD)) { - pilot_setFlag(p, PILOT_DEAD); - + pilot_dead(p); // Adjust the combat rating based on pilot mass. if(shooter == PLAYER_ID) combat_rating += MAX(1, p->ship->mass/50); } @@ -220,6 +222,14 @@ void pilot_hit(Pilot* p, const Solid* w, const unsigned int shooter, w->vel.y * (dam_mod/6. + w->mass/p->solid->mass/6.)); } +void pilot_dead(Pilot* p) { + // Basically just set the timers.. + p->timer[0] = SDL_GetTicks(); // No need for AI anymore. + p->ptimer = p->timer[0] + 1000 + (unsigned int)sqrt(10*p->armour_max*p->shield_max); + p->timer[1] = p->timer[0]; // Explosion timer. + pilot_setFlag(p, PILOT_DEAD); +} + // Set the pilot's ammo based on their secondary weapon. void pilot_setAmmo(Pilot* p) { int i; @@ -253,6 +263,46 @@ void pilot_render(Pilot* p) { // Update the pilot. static void pilot_update(Pilot* pilot, const double dt) { + unsigned int t; + double px, py, vx, vy; + + if((pilot != player) && pilot_isFlag(pilot, PILOT_DEAD)) { + t = SDL_GetTicks(); + + if(t > pilot->ptimer) { + pilot_setFlag(pilot, PILOT_DELETE); // It'll get deleted next frame. + return; + } + + if(!pilot_isFlag(pilot, PILOT_EXPLODED) && (t > pilot->ptimer - 200)) { + spfx_add(spfx_get("ExpL"), + VX(pilot->solid->pos), VY(pilot->solid->pos), + VX(pilot->solid->vel), VY(pilot->solid->vel), SPFX_LAYER_BACK); + pilot_setFlag(pilot, PILOT_EXPLODED); + } + else if(t > pilot->timer[1]) { + pilot->timer[1] = t + + (unsigned int)(100*(double)(pilot->ptimer - pilot->timer[1]) / + (double)(pilot->ptimer - pilot->timer[0])); + + // Random position on ship. + px = VX(pilot->solid->pos) + pilot->ship->gfx_space->sw * RNGF() + - pilot->ship->gfx_space->sw/2.; + py = VY(pilot->solid->pos) + pilot->ship->gfx_space->sh * RNGF() + - pilot->ship->gfx_space->sh/2.; + vx = VX(pilot->solid->vel); + vy = VY(pilot->solid->vel); + + if(RNGF() > 0.8) + spfx_add(spfx_get("ExpM"), px, py, vx, vy, SPFX_LAYER_BACK); + else + spfx_add(spfx_get("ExpS"), px, py, vx, vy, SPFX_LAYER_BACK); + } + } + else if((pilot != player) && (pilot->armour <= 0.)) // PWNED! + pilot_dead(pilot); + + // Pupose fallthrough to get the movement similar to disabled. if(pilot != player && pilot->armour < PILOT_DISABLED_ARMOUR * pilot->armour_max) { // We are disabled. pilot_setFlag(pilot, PILOT_DISABLED); diff --git a/src/pilot.h b/src/pilot.h index e328ac7..c03dd88 100644 --- a/src/pilot.h +++ b/src/pilot.h @@ -34,6 +34,7 @@ #define PILOT_BOARDED (1<<8) // Pilot has been boarded already! #define PILOT_DISABLED (1<<9) // Pilot is disabled. #define PILOT_DEAD (1<<10) // Pilot is on it's death bed. +#define PILOT_EXPLODED (1<<11) // Pilot did dinal death explosion. #define PILOT_DELETE (1<<15) // Pilot will get delete asap. // Just makes life simpler. diff --git a/src/player.c b/src/player.c index ba35b39..b0c178e 100644 --- a/src/player.c +++ b/src/player.c @@ -274,7 +274,8 @@ void player_render(void) { // Render the player target graphics. if(player_target != PLAYER_ID) p = pilot_get(player_target); else p = NULL; - if(p == NULL) player_target = PLAYER_ID; // No more pilot target. + if((p == NULL) || pilot_isFlag(p, PILOT_DEAD)) + player_target = PLAYER_ID; // No more pilot target. else { // There is still a pilot target. if(pilot_isDisabled(p)) c = &cInert; diff --git a/src/rng.h b/src/rng.h index a6aeb51..208d8ef 100644 --- a/src/rng.h +++ b/src/rng.h @@ -1,7 +1,8 @@ #pragma once #include -#define RNG(L,H) (rand()%(int)(H-L+1)+L) +#define RNG(L,H) ((int)L + (int)((double)(H-L+1) * (rand()/(RAND_MAX+1.)))) +#define RNGF() (rand()/(RAND_MAX+1.)) void rng_init(void); diff --git a/src/spfx.c b/src/spfx.c index 1d3428f..ff54469 100644 --- a/src/spfx.c +++ b/src/spfx.c @@ -103,7 +103,10 @@ void spfx_free(void) { spfx_neffects = 0; } -void spfx_add(int effect, const Vec2* pos, const Vec2* vel, const int layer) { +void spfx_add(int effect, + const double px, const double py, + const double vx, const double vy, + const int layer) { SPFX* cur_spfx; if(layer == SPFX_LAYER_FRONT) { @@ -128,8 +131,8 @@ void spfx_add(int effect, const Vec2* pos, const Vec2* vel, const int layer) { } cur_spfx->effect = effect; - vectcpy(&cur_spfx->pos, pos); - vectcpy(&cur_spfx->vel, vel); + vect_csetmin(&cur_spfx->pos, px, py); + vect_csetmin(&cur_spfx->vel, vx, vy); cur_spfx->t = SDL_GetTicks(); } diff --git a/src/spfx.h b/src/spfx.h index 03d399c..e4a7f20 100644 --- a/src/spfx.h +++ b/src/spfx.h @@ -7,7 +7,9 @@ // Stack manipulation. int spfx_get(char* name); void spfx_add(const int effect, - const Vec2* pos, const Vec2* vel, const int layer); + const double px, const double py, + const double vx, const double vy, + const int layer); // Stack mass manipulation functions. void spfx_update(const double dt); diff --git a/src/weapon.c b/src/weapon.c index 662f203..fa5c274 100644 --- a/src/weapon.c +++ b/src/weapon.c @@ -260,9 +260,14 @@ static void weapon_hit(Weapon* w, Pilot* p, WeaponLayer layer) { // Someone should let the ai know it's been attacked. if(!pilot_isPlayer(p)) { ai_attacked(p, w->parent); - spfx_add(outfit_spfx(w->outfit), &w->solid->pos, &p->solid->vel, SPFX_LAYER_BACK); + spfx_add(outfit_spfx(w->outfit), + VX(w->solid->pos), VY(w->solid->pos), + VX(p->solid->vel), VY(p->solid->vel), SPFX_LAYER_BACK); } else - spfx_add(outfit_spfx(w->outfit), &w->solid->pos, &p->solid->vel, SPFX_LAYER_FRONT); + spfx_add(outfit_spfx(w->outfit), + VX(w->solid->pos), VY(w->solid->pos), + VX(p->solid->vel), VY(p->solid->vel), SPFX_LAYER_FRONT); + if(w->parent == PLAYER_ID) // Make hostile to player. pilot_setFlag(p, PILOT_HOSTILE);