intermediate commit: still working on view state managing

This commit is contained in:
2010-11-27 08:38:32 +01:00
parent 3cbbedde7e
commit f8e5b7e873
9 changed files with 110 additions and 52 deletions
+1
View File
@@ -25,6 +25,7 @@ BEGIN_ENUM(ViewState)
DECL_ENUM_ELEMENT(ViewStateGameRunning),
DECL_ENUM_ELEMENT(ViewStatePaused),
DECL_ENUM_ELEMENT(ViewStatePlayerDied),
DECL_ENUM_ELEMENT(ViewStateShipExplodeFade),
DECL_ENUM_ELEMENT(ViewStateLevelComplete),
DECL_ENUM_ELEMENT(ViewStateShowHighscore),
DECL_ENUM_ELEMENT(ViewStateEnterPlayername),
+3 -6
View File
@@ -395,27 +395,24 @@ bool Model::OnGameOver() {
};
void Model::OnNewGame() {
ClearEntities();
mNewestHighscoreEntryIndex = 99999;
mPlayerLives = 1;
mCurrentLevelIndex = 0;
mPoints = 0;
DoLoadLevel (mLevelList[mCurrentLevelIndex].c_str());
}
void Model::OnShipExplode () {
mPlayerLives --;
ClearEntities();
if (mPlayerLives == 0) {
Engine::EventBasePtr gameover_event (new Engine::EventBase());
gameover_event->mEventType = EventGameOver;
QueueEvent (gameover_event);
} else {
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
}
SetGameState(GameStatePaused);
}
void Model::OnCreateEntity (const int type, const unsigned int id) {
+5 -17
View File
@@ -20,17 +20,6 @@ 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 (GameStatePaused);
}
return;
}
mState = Idle;
// the local velocity
@@ -74,9 +63,6 @@ bool ShipEntity::CollisionEvent (Engine::EntityBase* entity) {
Engine::LogMessage ("You died!");
mPhysicState->mStatic = true;
mAlive = false;
mFadeTimer = 3.;
mState = Dying;
Engine::EventBasePtr explode_event (new Engine::EventBase());
@@ -89,11 +75,13 @@ bool ShipEntity::CollisionEvent (Engine::EntityBase* entity) {
Engine::LogMessage ("You just killed yourself!");
mPhysicState->mStatic = true;
mAlive = false;
mFadeTimer = 1.;
mState = Dying;
Engine::EventBasePtr explode_event (new Engine::EventBase());
explode_event->mEventType = EventShipExplode;
explode_event->mEventUnsignedInt = mId;
QueueEvent (explode_event);
return true;
}
-2
View File
@@ -37,8 +37,6 @@ struct ShipEntity: public Engine::EntityBase {
mType = GameEntityTypeShip;
mBaseType = Engine::EntityBaseTypeActor;
mAlive = true;
mFadeTimer = 0.;
mState = Idle;
mAttackTimer = 0.;
}
+40 -25
View File
@@ -71,6 +71,7 @@ int View::OnInit (int argc, char* argv[]) {
Engine::RegisterListener (this, EventAccelerateStart);
Engine::RegisterListener (this, EventAccelerateStop);
Engine::RegisterListener (this, EventShipExplode);
Engine::RegisterListener (this, EventGameOver);
PushViewState (ViewStateMainMenu);
@@ -95,33 +96,46 @@ bool View::OnReceiveEvent (const Engine::EventBasePtr &event) {
return true;
break;
case EventShipExplode:
if (event->mEventType == EventShipExplode) {
Engine::EntityBase *ship_entity = Engine::GetEntity (event->mEventUnsignedInt);
vector3d position = ship_entity->mPhysicState->mPosition;
vector3d orientation = ship_entity->mPhysicState->mOrientation;
vector3d velocity = ship_entity->mPhysicState->mVelocity;
unsigned int i;
mShipPartsEntityIds.clear();
for (i = 0; i < mShipPartsSprite.GetSubSpriteCount(); i++) {
Engine::EntityBase* part_sprite_particle = Engine::CreateEntity (GameEntityTypeShipPart);
part_sprite_particle->mPhysicState->mPosition = position;
part_sprite_particle->mPhysicState->mOrientation = orientation;
part_sprite_particle->mPhysicState->mVelocity = velocity;
part_sprite_particle->mPhysicState->mVelocity = vector3d (velocity[0] * (rand()/float(RAND_MAX)) * 1.7, 0., velocity[2] * (rand()/float(RAND_MAX)) * 1.5);
part_sprite_particle->mPhysicState->mAngleVelocity = (rand()/float(RAND_MAX) - 0.5 ) * 100.;
mShipPartsEntityIds.push_back(part_sprite_particle->mId);
}
}
Engine::LogDebug ("Received Ship Explode Event: %d", event->mEventType);
return true;
case EventGameOver:
PopViewState();
PushViewState(ViewStateGameOver);
GetModel()->SetGameState(GameStatePaused);
break;
case EventShipExplode: {
Engine::LogDebug ("Received Ship Explode Event: %d", event->mEventType);
PopViewState();
PushViewState(ViewStatePlayerDied);
GetModel()->SetGameState(GameStatePaused);
// insert sprits that contains parts of the ship
Engine::EntityBase *ship_entity = Engine::GetEntity (event->mEventUnsignedInt);
vector3d position = ship_entity->mPhysicState->mPosition;
vector3d orientation = ship_entity->mPhysicState->mOrientation;
vector3d velocity = ship_entity->mPhysicState->mVelocity;
unsigned int i;
mShipPartsEntityIds.clear();
for (i = 0; i < mShipPartsSprite.GetSubSpriteCount(); i++) {
Engine::EntityBase* part_sprite_particle = Engine::CreateEntity (GameEntityTypeShipPart);
part_sprite_particle->mPhysicState->mPosition = position;
part_sprite_particle->mPhysicState->mOrientation = orientation;
part_sprite_particle->mPhysicState->mVelocity = velocity;
part_sprite_particle->mPhysicState->mVelocity = vector3d (velocity[0] * (rand()/float(RAND_MAX)) * 1.7, 0., velocity[2] * (rand()/float(RAND_MAX)) * 1.5);
part_sprite_particle->mPhysicState->mAngleVelocity = (rand()/float(RAND_MAX) - 0.5 ) * 100.;
mShipPartsEntityIds.push_back(part_sprite_particle->mId);
}
// We do not need the entity anymore
Engine::KillEntity(event->mEventUnsignedInt);
return true;
break;
}
default: Engine::LogWarning ("Received Event with type %d but don't know what to do with it!", event->mEventType);
break;
@@ -459,6 +473,7 @@ void View::DrawUiGameRunning() {
if (Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) {
PushViewState(ViewStatePaused);
GetModel()->SetGameState(GameStatePaused);
}
}