[Add] Explosions when ships die anyone?

This commit is contained in:
Rtch90 2018-04-14 04:01:35 +01:00
parent 275aa6b71f
commit 25d0b29aea
7 changed files with 120 additions and 7 deletions

View File

@ -10,7 +10,7 @@ include_HEADERS = body.h frame.h generic_system_view.h glfreetype.h gui_button.h
planet.h player.h dynamic_body.h sector.h sector_view.h ship_cpanel.h ship.h space.h star.h star_system.h system_info_view.h \
system_view.h vector3.h view.h world_view.h date.h space_station.h space_station_view.h model_body.h gui_iselectable.h \
ship_type.h object.h info_view.h model_coll_mesh_data.h object_viewer_view.h fixed.h custom_starsystems.h gameconsts.h \
aabb.h serializer.h
aabb.h serializer.h sfx.h
libgui_a_SOURCES = gui_button.cpp gui.cpp gui_fixed.cpp gui_screen.cpp gui_label.cpp gui_toggle_button.cpp gui_radio_button.cpp \
gui_radio_group.cpp gui_image_button.cpp gui_image.cpp gui_image_radio_button.cpp gui_multi_state_image_button.cpp gui_widget.cpp \
@ -20,7 +20,7 @@ Lephisto3D_SOURCES = main.cpp glfreetype.cpp body.cpp space.cpp ship.cpp player.
star.cpp frame.cpp ship_cpanel.cpp sector_view.cpp mtrand.cpp world_view.cpp system_view.cpp \
star_system.cpp sector.cpp system_info_view.cpp generic_system_view.cpp date.cpp space_station.cpp \
space_station_view.cpp model_body.cpp ship_type.cpp info_view.cpp model_coll_mesh_data.cpp \
object_viewer_view.cpp custom_starsystems.cpp serializer.cpp
object_viewer_view.cpp custom_starsystems.cpp serializer.cpp sfx.cpp
Lephisto3D_LDADD = sbre/libsbre.a collider/libcollider.a libgui.a
ModelViewer_SOURCES = sbre_viewer.cpp glfreetype.cpp

View File

@ -7,6 +7,7 @@
#include "space_station.h"
#include "ship.h"
#include "player.h"
#include "sfx.h"
Body::Body(void) {
m_frame = 0;
@ -48,6 +49,7 @@ void Body::Serialize(void) {
case Object::SPACESTATION:
case Object::SHIP:
case Object::PLAYER:
case Object::SFX:
Save();
break;
default:
@ -74,6 +76,8 @@ Body* Body::Unserialize(void) {
b = new Ship(); break;
case Object::PLAYER:
b = new Player(); break;
case Object::SFX:
b = new Sfx(); break;
default:
/* TODO: should assert. */
return 0;

View File

@ -36,7 +36,7 @@ public:
/* For putting on planet surface, oriented +y up. */
void OrientOnSurface(double radius, double latitude, double longitude);
vector3d GetPositionRelTo(const Frame*);
Frame* GetFrame(void) { return m_frame; }
Frame* GetFrame(void) const { return m_frame; }
void SetLabel(const char* label) { m_label = label; }
std::string& GetLabel(void) { return m_label; }
unsigned int GetFlags(void) { return m_flags; }

View File

@ -2,7 +2,7 @@
class Object {
public:
enum Type { OBJECT, BODY, MODELBODY, DYNAMICBODY, SHIP, PLAYER, SPACESTATION, PLANET, STAR };
enum Type { OBJECT, BODY, MODELBODY, DYNAMICBODY, SHIP, PLAYER, SPACESTATION, PLANET, STAR, SFX };
virtual Type GetType(void) { return OBJECT; }
virtual bool IsType(Type c) { return GetType() == c; }
};

76
src/sfx.cpp Normal file
View File

@ -0,0 +1,76 @@
#include "l3d.h"
#include "sfx.h"
#include "frame.h"
#include "star_system.h"
#include "space.h"
#include "serializer.h"
Sfx::Sfx(void) : Body() {
m_pos = vector3d(0, 0, 0);
m_type = TYPE_EXPLOSION;
m_age = 0;
}
void Sfx::Save(void) {
using namespace Serializer::Write;
Body::Save();
wr_vector3d(m_pos);
wr_float(m_age);
wr_int(m_type);
}
void Sfx::Load(void) {
using namespace Serializer::Read;
Body::Load();
m_pos = rd_vector3d();
m_age = rd_float();
m_type = static_cast<Sfx::TYPE>(rd_int());
}
void Sfx::SetPosition(vector3d p) {
m_pos = p;
}
void Sfx::TimeStepUpdate(const float timeStep) {
m_age += timeStep;
switch(m_type) {
case TYPE_EXPLOSION:
if(m_age > 0.2) Space::KillBody(this);
break;
}
}
void Sfx::Render(const Frame* camFrame) {
matrix4x4d ftran;
Frame::GetFrameTransform(GetFrame(), camFrame, ftran);
vector3d fpos = ftran*GetPosition();
glPushMatrix();
glTranslatef(fpos.x, fpos.y, fpos.z);
switch(m_type) {
case TYPE_EXPLOSION:
glPushAttrib(GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
glDisable(GL_LIGHTING);
glColor3f(1,1,0.5);
gluSphere(L3D::gluQuadric, 1000*m_age, 20, 20);
glEnable(GL_BLEND);
glColor4f(1, 0.5, 0, 0.66);
gluSphere(L3D::gluQuadric, 1500*m_age, 20, 20);
glColor4f(1,0,0,0.33);
gluSphere(L3D::gluQuadric, 2000*m_age, 20, 20);
break;
}
glPopMatrix();
}
void Sfx::Add(const Body* b, TYPE t) {
Sfx* sfx = new Sfx();
sfx->m_type = t;
sfx->SetFrame(b->GetFrame());
sfx->SetPosition(b->GetPosition());
Space::AddBody(sfx);
}

30
src/sfx.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include "body.h"
class Frame;
class Sfx: public Body {
public:
OBJDEF(Sfx, Body, SFX);
enum TYPE { TYPE_EXPLOSION };
static void Add(const Body*, TYPE);
Sfx(void);
//vitural ~Sfx(void);
virtual void SetPosition(vector3d p);
virtual vector3d GetPosition(void) const { return m_pos; }
virtual double GetRadius(void) const { return 10; };
virtual void Render(const Frame* camFrame);
void TimeStepUpdate(const float timeStep);
protected:
virtual void Save(void);
virtual void Load(void);
private:
vector3d m_pos;
float m_age;
enum TYPE m_type;
};

View File

@ -7,6 +7,7 @@
#include "space_station.h"
#include "serializer.h"
#include "collider/collider.h"
#include "sfx.h"
static ObjParams params = {
{ 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
@ -118,9 +119,10 @@ void Ship::UpdateMass(void) {
bool Ship::OnDamage(Body* attacker, float kgDamage) {
m_stats.hull_mass_left -= kgDamage*0.001;
if(m_stats.hull_mass_left < 0) Space::KillBody(this);
printf("Auch! %s took %.1f kilos of damage from %s! (%.1f t hull left)\n",
GetLabel().c_str(), kgDamage, attacker->GetLabel().c_str(), m_stats.hull_mass_left);
if(m_stats.hull_mass_left < 0) {
Space::KillBody(this);
Sfx::Add(this, Sfx::TYPE_EXPLOSION);
}
return true;
}
@ -135,6 +137,7 @@ bool Ship::OnCollision(Body* b, Uint32 flags) {
const float v = GetVelocity().Length();
kineticEnergy = KINETIC_ENERGY_MULT * m_stats.total_mass * v * v;
}
/* Hitting space station docking surfaces shouldn't do damage!! */
if(b->IsType(Object::SPACESTATION) && (flags & 0x10)) {
kineticEnergy = 0;
}