From 567bb432ef1bb318f63b269bae6cffc0d45db547 Mon Sep 17 00:00:00 2001 From: "Martin Felis (berta)" Date: Thu, 8 Apr 2010 19:03:16 +0200 Subject: [PATCH] Put the variables of the ship as static members of ShipEntity, introduced variable ship_maxattackrate --- asteroids.rc | 1 + asteroids/ShipEntity.cc | 29 ++++++++++++++++++++--------- asteroids/ShipEntity.h | 7 +++++++ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/asteroids.rc b/asteroids.rc index 74d6a0f..5153b49 100644 --- a/asteroids.rc +++ b/asteroids.rc @@ -8,3 +8,4 @@ bind space attack set ship_maxspeed 10 set ship_acceleration 10 set ship_rotationspeed 180 +set ship_maxattackrate 10 diff --git a/asteroids/ShipEntity.cc b/asteroids/ShipEntity.cc index aaf60a0..9a4fc88 100644 --- a/asteroids/ShipEntity.cc +++ b/asteroids/ShipEntity.cc @@ -11,9 +11,10 @@ namespace asteroids { -static Engine::Variable var_ship_acceleration ("ship_acceleration", "10"); -static Engine::Variable var_ship_maxspeed ("ship_maxspeed", "10"); -static Engine::Variable var_ship_rotationspeed ("ship_rotationspeed", "180"); +Engine::Variable ShipEntity::VarAcceleration ("ship_acceleration", "10"); +Engine::Variable ShipEntity::VarMaxSpeed ("ship_maxspeed", "10"); +Engine::Variable ShipEntity::VarRotationSpeed ("ship_rotationspeed", "180"); +Engine::Variable ShipEntity::VarMaxAttackRate ("ship_maxattackrate", "10"); void ShipEntity::Update (float delta_sec) { if (!mPhysicState || !mControllerState) @@ -38,7 +39,7 @@ void ShipEntity::Update (float delta_sec) { // set the local velocity as the current state of the keys are if (mControllerState->GetKey (EntityKeyStateForward)) { - local_velocity[0] += delta_sec * var_ship_acceleration.GetFloatValue(); + local_velocity[0] += delta_sec * ShipEntity::VarAcceleration.GetFloatValue(); mState = Accelerating; } @@ -49,17 +50,21 @@ void ShipEntity::Update (float delta_sec) { mPhysicState->SetVelocity(local_velocity); if (mControllerState->GetKey (EntityKeyStateTurnLeft)) { - mPhysicState->mOrientation[1] += delta_sec * var_ship_rotationspeed.GetFloatValue(); + mPhysicState->mOrientation[1] += delta_sec * ShipEntity::VarRotationSpeed.GetFloatValue(); } if (mControllerState->GetKey (EntityKeyStateTurnRight)) { - mPhysicState->mOrientation[1] -= delta_sec * var_ship_rotationspeed.GetFloatValue(); + mPhysicState->mOrientation[1] -= delta_sec * ShipEntity::VarRotationSpeed.GetFloatValue(); } // Check for the maximum speed float speed = mPhysicState->mVelocity.length(); - if (speed > var_ship_maxspeed.GetFloatValue()) { - mPhysicState->mVelocity *= var_ship_maxspeed.GetFloatValue() / speed; + if (speed > ShipEntity::VarMaxSpeed.GetFloatValue()) { + mPhysicState->mVelocity *= ShipEntity::VarMaxSpeed.GetFloatValue() / speed; } + + // update the attack timer if neccessary + if (mAttackTimer > 0.) + mAttackTimer -= delta_sec; } bool ShipEntity::CollisionEvent (Engine::EntityBase* entity) { @@ -99,6 +104,12 @@ void ShipEntity::Attack () { if (!mAlive) return; + // skip the attack if we still have to wait + if (mAttackTimer > 0.) + return; + + mAttackTimer = 1. / ShipEntity::VarMaxAttackRate.GetFloatValue(); + Engine::EntityPhysicState* entity_physic = Engine::GetEntityPhysicState (mId); vector3d attack_dir (1., 0., 0.); @@ -114,7 +125,7 @@ void ShipEntity::Attack () { rocket_physics->mPosition += entity_physic->mPosition; rocket_physics->mOrientation = entity_physic->mOrientation; rocket_physics->mVelocity = attack_dir.normalize(); - rocket_physics->mVelocity *= var_ship_maxspeed.GetFloatValue() + 0.1; + rocket_physics->mVelocity *= ShipEntity::VarMaxSpeed.GetFloatValue() + 0.1; } } diff --git a/asteroids/ShipEntity.h b/asteroids/ShipEntity.h index 02ac7ea..c00bf31 100644 --- a/asteroids/ShipEntity.h +++ b/asteroids/ShipEntity.h @@ -40,6 +40,7 @@ struct ShipEntity: public Engine::EntityBase { mAlive = true; mFadeTimer = 0.; mState = Idle; + mAttackTimer = 0.; } virtual ~ShipEntity() {}; @@ -50,7 +51,13 @@ struct ShipEntity: public Engine::EntityBase { bool mAlive; float mFadeTimer; + float mAttackTimer; State mState; + + static Engine::Variable VarAcceleration; + static Engine::Variable VarMaxSpeed; + static Engine::Variable VarRotationSpeed; + static Engine::Variable VarMaxAttackRate; }; }