intermediate commit: still working on view state managing

main
Martin Felis (berta) 2010-11-27 08:38:32 +01:00
parent 3cbbedde7e
commit f8e5b7e873
9 changed files with 110 additions and 52 deletions

View File

@ -25,6 +25,7 @@ BEGIN_ENUM(ViewState)
DECL_ENUM_ELEMENT(ViewStateGameRunning), DECL_ENUM_ELEMENT(ViewStateGameRunning),
DECL_ENUM_ELEMENT(ViewStatePaused), DECL_ENUM_ELEMENT(ViewStatePaused),
DECL_ENUM_ELEMENT(ViewStatePlayerDied), DECL_ENUM_ELEMENT(ViewStatePlayerDied),
DECL_ENUM_ELEMENT(ViewStateShipExplodeFade),
DECL_ENUM_ELEMENT(ViewStateLevelComplete), DECL_ENUM_ELEMENT(ViewStateLevelComplete),
DECL_ENUM_ELEMENT(ViewStateShowHighscore), DECL_ENUM_ELEMENT(ViewStateShowHighscore),
DECL_ENUM_ELEMENT(ViewStateEnterPlayername), DECL_ENUM_ELEMENT(ViewStateEnterPlayername),

View File

@ -395,27 +395,24 @@ bool Model::OnGameOver() {
}; };
void Model::OnNewGame() { void Model::OnNewGame() {
ClearEntities();
mNewestHighscoreEntryIndex = 99999; mNewestHighscoreEntryIndex = 99999;
mPlayerLives = 1; mPlayerLives = 1;
mCurrentLevelIndex = 0; mCurrentLevelIndex = 0;
mPoints = 0; mPoints = 0;
DoLoadLevel (mLevelList[mCurrentLevelIndex].c_str()); DoLoadLevel (mLevelList[mCurrentLevelIndex].c_str());
} }
void Model::OnShipExplode () { void Model::OnShipExplode () {
mPlayerLives --; mPlayerLives --;
ClearEntities();
if (mPlayerLives == 0) { if (mPlayerLives == 0) {
Engine::EventBasePtr gameover_event (new Engine::EventBase()); Engine::EventBasePtr gameover_event (new Engine::EventBase());
gameover_event->mEventType = EventGameOver; gameover_event->mEventType = EventGameOver;
QueueEvent (gameover_event); QueueEvent (gameover_event);
} else {
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
} }
SetGameState(GameStatePaused);
} }
void Model::OnCreateEntity (const int type, const unsigned int id) { void Model::OnCreateEntity (const int type, const unsigned int id) {

View File

@ -20,17 +20,6 @@ void ShipEntity::Update (float delta_sec) {
if (!mPhysicState || !mControllerState) if (!mPhysicState || !mControllerState)
return; 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; mState = Idle;
// the local velocity // the local velocity
@ -74,9 +63,6 @@ bool ShipEntity::CollisionEvent (Engine::EntityBase* entity) {
Engine::LogMessage ("You died!"); Engine::LogMessage ("You died!");
mPhysicState->mStatic = true; mPhysicState->mStatic = true;
mAlive = false;
mFadeTimer = 3.;
mState = Dying; mState = Dying;
Engine::EventBasePtr explode_event (new Engine::EventBase()); Engine::EventBasePtr explode_event (new Engine::EventBase());
@ -89,11 +75,13 @@ bool ShipEntity::CollisionEvent (Engine::EntityBase* entity) {
Engine::LogMessage ("You just killed yourself!"); Engine::LogMessage ("You just killed yourself!");
mPhysicState->mStatic = true; mPhysicState->mStatic = true;
mAlive = false;
mFadeTimer = 1.;
mState = Dying; mState = Dying;
Engine::EventBasePtr explode_event (new Engine::EventBase());
explode_event->mEventType = EventShipExplode;
explode_event->mEventUnsignedInt = mId;
QueueEvent (explode_event);
return true; return true;
} }

View File

@ -37,8 +37,6 @@ struct ShipEntity: public Engine::EntityBase {
mType = GameEntityTypeShip; mType = GameEntityTypeShip;
mBaseType = Engine::EntityBaseTypeActor; mBaseType = Engine::EntityBaseTypeActor;
mAlive = true;
mFadeTimer = 0.;
mState = Idle; mState = Idle;
mAttackTimer = 0.; mAttackTimer = 0.;
} }

View File

@ -71,6 +71,7 @@ int View::OnInit (int argc, char* argv[]) {
Engine::RegisterListener (this, EventAccelerateStart); Engine::RegisterListener (this, EventAccelerateStart);
Engine::RegisterListener (this, EventAccelerateStop); Engine::RegisterListener (this, EventAccelerateStop);
Engine::RegisterListener (this, EventShipExplode); Engine::RegisterListener (this, EventShipExplode);
Engine::RegisterListener (this, EventGameOver);
PushViewState (ViewStateMainMenu); PushViewState (ViewStateMainMenu);
@ -95,33 +96,46 @@ bool View::OnReceiveEvent (const Engine::EventBasePtr &event) {
return true; return true;
break; break;
case EventShipExplode: case EventGameOver:
if (event->mEventType == EventShipExplode) { PopViewState();
Engine::EntityBase *ship_entity = Engine::GetEntity (event->mEventUnsignedInt); PushViewState(ViewStateGameOver);
vector3d position = ship_entity->mPhysicState->mPosition; GetModel()->SetGameState(GameStatePaused);
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;
break; 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); default: Engine::LogWarning ("Received Event with type %d but don't know what to do with it!", event->mEventType);
break; break;
@ -459,6 +473,7 @@ void View::DrawUiGameRunning() {
if (Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) { if (Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) {
PushViewState(ViewStatePaused); PushViewState(ViewStatePaused);
GetModel()->SetGameState(GameStatePaused);
} }
} }

View File

@ -44,6 +44,7 @@ class Logging;
class Commands; class Commands;
class Variables; class Variables;
class Variable; class Variable;
class Timer;
/** \brief The outermost class which contains just everything! /** \brief The outermost class which contains just everything!
* *
@ -74,7 +75,7 @@ class Engine : public Module {
mSoundManager = NULL; mSoundManager = NULL;
} }
virtual void MainLoop () { void MainLoop () {
OnMainLoop (); OnMainLoop ();
} }

View File

@ -49,7 +49,7 @@ void ModelBase::OnDestroy () {
*/ */
void ModelBase::Process () { void ModelBase::Process () {
// Process the controllers and state of all entities // Process the controllers and state of all entities
std::map<unsigned int, EntityBase*>::iterator entity_iter = mEntities.begin(); EntityIter entity_iter = mEntities.begin();
do { do {
if (entity_iter->second == NULL) { if (entity_iter->second == NULL) {
LogError ("Entity with id %d does not exist!", entity_iter->first); LogError ("Entity with id %d does not exist!", entity_iter->first);
@ -58,6 +58,12 @@ void ModelBase::Process () {
entity_iter++; entity_iter++;
} while (entity_iter != mEntities.end()); } while (entity_iter != mEntities.end());
// Update the timers
for (TimerIter timer_iter = mTimers.begin(); timer_iter != mTimers.end(); timer_iter++) {
timer_iter->second.Update(mDeltaSec);
timer_iter++;
}
// simulate the world // simulate the world
mPhysics->Simulate (mDeltaSec, this); mPhysics->Simulate (mDeltaSec, this);
@ -285,6 +291,23 @@ EntityPhysicState * GetEntityPhysicState (unsigned int id) {
return NULL; return NULL;
} }
void StartTimer(const std::string &id, float sec) {
if (!ModelInstance) {
LogError ("Couldn't execute GetEntity(): Model not initialized!");
}
ModelInstance->StartTimer(id, sec);
}
bool CheckTimer(const std::string &id) {
if (!ModelInstance) {
LogError ("Couldn't execute GetEntity(): Model not initialized!");
}
return ModelInstance->CheckTimer(id);
}
} }

View File

@ -3,6 +3,7 @@
#include "Engine.h" #include "Engine.h"
#include "EntityBase.h" #include "EntityBase.h"
#include "Timer.h"
namespace Engine { namespace Engine {
@ -79,6 +80,29 @@ class ModelBase : public Module {
}; };
unsigned int GetGameState () { return mGameState; }; unsigned int GetGameState () { return mGameState; };
void StartTimer(const std::string &id, float msec) {
TimerIter cur_timer = mTimers.find(id);
if (cur_timer != mTimers.end()) {
cur_timer->second.Set(msec);
cur_timer->second.Start();
return;
}
mTimers[id] = Timer(id, msec);
assert (mTimers.size() > 0);
assert (mTimers[id].IsActive() == true);
}
bool CheckTimer(const std::string &id) {
TimerIter cur_timer = mTimers.find(id);
if (cur_timer == mTimers.end()) {
return false;
}
return cur_timer->second.Query();
}
protected: protected:
/** \brief Initializes the system */ /** \brief Initializes the system */
virtual int OnInit (int argc, char* argv[]); virtual int OnInit (int argc, char* argv[]);
@ -99,9 +123,14 @@ class ModelBase : public Module {
/** \brief contains all Engine::Entities */ /** \brief contains all Engine::Entities */
std::map<unsigned int, EntityBase *> mEntities; std::map<unsigned int, EntityBase *> mEntities;
typedef std::map<unsigned int, EntityBase *>::iterator EntityIter;
/** \brief contains all Engine::Entities that ceased to exist */ /** \brief contains all Engine::Entities that ceased to exist */
std::vector<unsigned int> mKilledEntities; std::vector<unsigned int> mKilledEntities;
std::map<std::string, Timer> mTimers;
typedef std::map<std::string, Timer>::iterator TimerIter;
unsigned int mEntityIdCounter; unsigned int mEntityIdCounter;
unsigned int mPlayerEntityId; unsigned int mPlayerEntityId;

View File

@ -9,6 +9,12 @@ unsigned int GetPlayerEntityId ();
/** \brief Returns the duration of the frame in seconds */ /** \brief Returns the duration of the frame in seconds */
float GetFrameDuration (); float GetFrameDuration ();
/** \brief Starts a timer with the given id that expires after sec seconds */
void StartTimer(const std::string &id, float sec);
/** \brief Checks whether a timer expired */
bool CheckTimer(const std::string &id);
} }
#endif /* _MODELGLOBAL_H */ #endif /* _MODELGLOBAL_H */