From 7008e8523619eb777cf88aa9a280d2f331647ae7 Mon Sep 17 00:00:00 2001 From: Aziz Bekkine Date: Thu, 19 Apr 2018 05:47:15 +0300 Subject: [PATCH] Basic thruster effects implemented (Closes #59) - EffectsManager class created. - Thruster effects (main & bow) implemented. - Thrust & Moments reset if hull depleted. - Effects life cycle updated in universe thread. --- effects_manager.h | 107 ++++++++++++++++++++++++++++++++++++++++++++++ game_play.h | 8 ++++ space_ship.h | 30 ++++++++++--- universe.h | 13 ++++++ 4 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 effects_manager.h diff --git a/effects_manager.h b/effects_manager.h new file mode 100644 index 0000000..25a4ee9 --- /dev/null +++ b/effects_manager.h @@ -0,0 +1,107 @@ +#ifndef EFFECTS_MANAGER_H_ +#define EFFECTS_MANAGER_H_ + +#include +#include +#include +#include + +class EffectsManager { +public: + EffectsManager() {} + ~EffectsManager() {} + void Init() {} + void Update(double time_step) { + UpdateThrust(time_step); + } + void Render() { + RenderThrust(); + } + // BEGIN Thruster + struct ThrustParticle { + ThrustParticle() + : x(0.0), y(0.0), vx(0.0), vy(0.0), life(0.0) {} + ~ThrustParticle() {} + void SetPosition(b2Vec2 position) { + x = position.x; + y = position.y; + life = 0.25; + } + void SetThrust(b2Vec2 thrust, b2Vec2 velocity) { + double ta = atan2(thrust.y, thrust.x) - M_PI; + ta = fmod(ta, 2.0 * M_PI); + double td = (drand48() * M_PI / 6.0) - M_PI / 12.0; + vx = velocity.x + 1.00 * thrust.Length() * cos(ta + td); + vy = velocity.y + 1.00 * thrust.Length() * sin(ta + td); + } + void Update(double time_step) { + if (life > 0.0) { + life -= time_step; + x += time_step * vx; + y += time_step * vy; + } + } + double x, y; + double vx, vy; + double life; + }; + ThrustParticle thrust_particles_[100]; + int tp_index_ = 0; + void MainThruster(b2Vec2 thrust, b2Vec2 position, b2Vec2 velocity) { + if (thrust.Length() > 0.0) { + thrust_particles_[tp_index_].SetPosition(position); + thrust_particles_[tp_index_].SetThrust(thrust, velocity); + tp_index_++; + tp_index_ = tp_index_ % 100; + } + } + int bow_index; + void BowThruster(double moment, double angle, b2Vec2 position, b2Vec2 velocity) { + bow_index++; + bow_index &= 0x3; + if (bow_index != 0) return; + + double abs_moment = fabs(moment); + if (abs_moment > 0.0) { + b2Vec2 t; + b2Vec2 p; + double a; + if (moment > 0.0) { + a = (angle - 180.0) * M_PI / 180.0; + } else if (moment < 0.0) { + a = (angle) * M_PI / 180.0; + } + p.x = position.x + 1.0 * cos((angle + 90.0) * M_PI / 180.0); + p.y = position.y + 1.0 * sin((angle + 90.0) * M_PI / 180.0); + t.x = 20.0 * abs_moment * cos(a); + t.y = 20.0 * abs_moment * sin(a); + thrust_particles_[tp_index_].SetPosition(p); + thrust_particles_[tp_index_].SetThrust(t, velocity); + tp_index_++; + tp_index_ = tp_index_ % 100; + } + } + void UpdateThrust(double time_step) { + + for (int i=0; i<100; ++i) { + thrust_particles_[i].Update(time_step); + } + } + void RenderThrust() { + + glColor3f(1.0, 1.0, 1.0); + glPointSize(3.0); + glBegin(GL_POINTS); + for (int i=0; i<100; ++i) { + if (thrust_particles_[i].life > 0.0) { + glColor4f(1.0, 1.0, 1.0, 4.0 * thrust_particles_[i].life); + glVertex2d(thrust_particles_[i].x, thrust_particles_[i].y); + } + } + glEnd(); + } + // END Thruster +}; + +#endif // EFFECTS_MANAGER_H_ + diff --git a/game_play.h b/game_play.h index adf5280..a4461af 100644 --- a/game_play.h +++ b/game_play.h @@ -11,6 +11,7 @@ #include "planet.h" #include "space_ship.h" +#include "effects_manager.h" #include "object_manager.h" @@ -19,6 +20,7 @@ class GamePlay { double debug_scale_; int num_planets_; Planet* planet_; + EffectsManager* effects_; Background background_; public: @@ -33,6 +35,7 @@ class GamePlay { ship_ = static_cast(OBJMGR.Get("ship")); num_planets_ = *(static_cast(OBJMGR.Get("nplanets"))); planet_ = static_cast(OBJMGR.Get("planets")); + effects_ = static_cast(OBJMGR.Get("effects")); } SpaceShip* ship_; @@ -62,6 +65,7 @@ class GamePlay { RenderBackground(); RenderUniverse(); + RenderEffects(); glPopMatrix(); @@ -107,6 +111,10 @@ class GamePlay { glPopMatrix(); } } + void RenderEffects() { + + effects_->Render(); + } void RenderBackground() { background_.Render(ship_x_, ship_y_, ship_angle_); diff --git a/space_ship.h b/space_ship.h index fbd7466..0b299d7 100644 --- a/space_ship.h +++ b/space_ship.h @@ -6,6 +6,9 @@ #include +#include "effects_manager.h" +#include "object_manager.h" + #include "data_bus.h" #include "systems/engine_system_interface.h" @@ -24,6 +27,7 @@ class SpaceShip { RadarSystemInterface * radar_; GenericHudDevice hud_; HOTASDevice hotas_; + EffectsManager * effects_; // TODO const float kMaxHullStrength; @@ -49,6 +53,7 @@ class SpaceShip { SpaceShip() : engine_(0) , radar_(0) + , effects_(0) , kMaxHullStrength(10.0) , kImpulseThreshold(1.0) , hull_strength_(kMaxHullStrength) @@ -148,6 +153,8 @@ class SpaceShip { hud_.Init(); hotas_.Init(); + + effects_ = static_cast(OBJMGR.Get("effects")); } // Begin -- Handlers for Engine system. void hndThrustOut(double value) { @@ -161,11 +168,24 @@ class SpaceShip { // Gravity physics_body_->ApplyForceToCenter(gravity_, true); // Thrust - thrust_.x = thrust_force_ * cos(0.5 * M_PI + physics_body_->GetAngle()); - thrust_.y = thrust_force_ * sin(0.5 * M_PI + physics_body_->GetAngle()); - physics_body_->ApplyForceToCenter(thrust_, true); - // Moment - physics_body_->ApplyTorque(moment_, true); + if (hull_strength_ > 0.0) { + thrust_.x = thrust_force_ * cos(0.5 * M_PI + physics_body_->GetAngle()); + thrust_.y = thrust_force_ * sin(0.5 * M_PI + physics_body_->GetAngle()); + physics_body_->ApplyForceToCenter(thrust_, true); + + effects_->MainThruster(thrust_, position_, velocity_); + + // Moment + physics_body_->ApplyTorque(moment_, true); + + effects_->BowThruster(moment_, angle_, position_, velocity_); + } + else { + thrust_force_ = 0.0; + moment_ = 0.0; + thrust_.x = 0.0; + thrust_.y = 0.0; + } // Get velocity for devices. velocity_ = physics_body_->GetLinearVelocity(); double speed = velocity_.Length(); diff --git a/universe.h b/universe.h index 9810ff5..4341b75 100644 --- a/universe.h +++ b/universe.h @@ -7,6 +7,7 @@ #include "space_ship.h" #include "planet.h" +#include "effects_manager.h" #include "object_manager.h" @@ -74,6 +75,7 @@ class Universe { , world_(0) , space_ship_(0) , planets_(0) + , effects_(0) , state_(GameDefinitions::gameState_InMenu) {} ~Universe() { @@ -89,6 +91,10 @@ class Universe { } void Init() { + effects_ = new EffectsManager(); + effects_->Init(); + OBJMGR.Set("effects", effects_); + // Instantiate player ship. space_ship_ = new SpaceShip(); space_ship_->SetPosition(0.0, 100.0); @@ -167,6 +173,8 @@ class Universe { UpdatePlanet(); + UpdateEffects(delta_time); + Step(delta_time); t_end_ = t_begin_; @@ -187,6 +195,10 @@ class Universe { space_ship_->Update(delta_time); } + void UpdateEffects(double delta_time) { + + effects_->Update(delta_time); + } void UpdatePlanet() { for (int i=0; i