fysxasteroids/asteroids/ShipEntity.cc

123 lines
3.3 KiB
C++

#include "Engine.h"
#include "Model.h"
#include "ShipEntity.h"
#include "RocketEntity.h"
#include "Controller.h"
#include "AsteroidsEvents.h"
#include "coll2d.h"
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");
void ShipEntity::Update (float delta_sec) {
if (!mPhysicState || !mControllerState)
return;
// If we die, we have to decrease the fade timer
if (!mAlive) {
mFadeTimer -= delta_sec;
if (mFadeTimer <= 0.) {
Model *model = (Model*) Engine::EngineGetModel();
model->SetGameState (GameStatePlayerDied);
}
return;
}
mState = Idle;
// the local velocity
vector3d local_velocity = mPhysicState->mVelocity;
mPhysicState->LocalizeRotation (local_velocity);
// 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();
mState = Accelerating;
}
// now transform these to global velocities
mPhysicState->GlobalizeRotation (local_velocity);
// now we can update the new global velocity
mPhysicState->SetVelocity(local_velocity);
if (mControllerState->GetKey (EntityKeyStateTurnLeft)) {
mPhysicState->mOrientation[1] += delta_sec * var_ship_rotationspeed.GetFloatValue();
}
if (mControllerState->GetKey (EntityKeyStateTurnRight)) {
mPhysicState->mOrientation[1] -= delta_sec * var_ship_rotationspeed.GetFloatValue();
}
// Check for the maximum speed
float speed = mPhysicState->mVelocity.length();
if (speed > var_ship_maxspeed.GetFloatValue()) {
mPhysicState->mVelocity *= var_ship_maxspeed.GetFloatValue() / speed;
}
}
bool ShipEntity::CollisionEvent (Engine::EntityBase* entity) {
GameEntityType other_type = (GameEntityType) entity->mType;
if (other_type == GameEntityTypeAsteroid) {
Engine::LogMessage ("You died!");
mPhysicState->mStatic = true;
mAlive = false;
mFadeTimer = 3.;
mState = Dying;
Engine::EventBasePtr explode_event (new Engine::EventBase());
explode_event->mEventType = EventShipExplode;
explode_event->mEventUnsignedInt = mId;
QueueEvent (explode_event);
return true;
} else if (other_type == GameEntityTypeRocket) {
Engine::LogMessage ("You just killed yourself!");
mPhysicState->mStatic = true;
mAlive = false;
mFadeTimer = 1.;
mState = Dying;
return true;
}
return false;
}
void ShipEntity::Attack () {
if (!mAlive)
return;
Engine::LogMessage ("ATTACK");
Engine::EntityPhysicState* entity_physic = Engine::GetEntityPhysicState (mId);
vector3d attack_dir (1., 0., 0.);
entity_physic->GlobalizeRotation (attack_dir);
RocketEntity *rocket_entity = (RocketEntity*) Engine::CreateEntity (GameEntityTypeRocket);
rocket_entity->mSecToLive = 1.75;
RocketEntityPhysicState *rocket_physics = (RocketEntityPhysicState*) rocket_entity->mPhysicState;
rocket_physics->mPosition = attack_dir;
rocket_physics->mPosition *= mPhysicState->mRadius;
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;
}
}