From fdbdac3486a1b20300993de438fdab8a59dabd0f Mon Sep 17 00:00:00 2001 From: Rtch90 Date: Sat, 14 Apr 2018 04:01:35 +0100 Subject: [PATCH] [Add] Explosions when ships die anyone? --- src/Makefile.am | 4 +-- src/body.cpp | 4 +++ src/body.h | 2 +- src/object.h | 2 +- src/sfx.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ src/sfx.h | 30 +++++++++++++++++++ src/ship.cpp | 9 ++++-- 7 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 src/sfx.cpp create mode 100644 src/sfx.h diff --git a/src/Makefile.am b/src/Makefile.am index bbae6ab..bb1f974 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/body.cpp b/src/body.cpp index 15fbf71..3db9ced 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -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; diff --git a/src/body.h b/src/body.h index 1b4159c..b89f56e 100644 --- a/src/body.h +++ b/src/body.h @@ -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; } diff --git a/src/object.h b/src/object.h index fb45f35..53cb1ce 100644 --- a/src/object.h +++ b/src/object.h @@ -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; } }; diff --git a/src/sfx.cpp b/src/sfx.cpp new file mode 100644 index 0000000..6dc9ccc --- /dev/null +++ b/src/sfx.cpp @@ -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(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); +} + diff --git a/src/sfx.h b/src/sfx.h new file mode 100644 index 0000000..905cf6a --- /dev/null +++ b/src/sfx.h @@ -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; +}; + diff --git a/src/ship.cpp b/src/ship.cpp index d99c138..da2214e 100644 --- a/src/ship.cpp +++ b/src/ship.cpp @@ -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; }