nice level intros

main
Martin Felis (berta) 2011-03-22 23:01:44 +01:00
parent e2fd3393d3
commit 3e17f56760
13 changed files with 235 additions and 19 deletions

View File

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

View File

@ -41,7 +41,6 @@ void Controller::ResetPlayerEntity () {
int i;
for (i = 0; i < EntityControllerMaxKeyStates; i++)
player_entity->UnsetControllerKeyState(i);
}
// We definitely have to reset the player entity id

View File

@ -75,6 +75,10 @@ class Model : public Engine::ModelBase {
std::list<HighscoreEntry> mHighscoreList;
unsigned int mNewestHighscoreEntryIndex;
unsigned int GetCurrentLevelIndex () {
return mCurrentLevelIndex;
}
protected:
/** \brief Initializes the system */
virtual int OnInit (int argc, char* argv[]);

View File

@ -252,10 +252,13 @@ void View::Draw() {
std::map<unsigned int, Engine::EntityBase*>::iterator entity_iterator;
Model *game_model = static_cast<Model*> (mModel);
unsigned int game_state = game_model->GetGameState();
unsigned int view_state = GetViewState();
DrawStars ();
if ( game_state == GameStateRunning || GetViewState() == ViewStateEditor) {
if ( game_state == GameStateRunning
|| view_state == ViewStateLevelIntro
|| view_state == ViewStateEditor) {
DrawWorld ();
}
@ -430,6 +433,9 @@ void View::DrawUi () {
case ViewStatePlayerDied:
DrawUiPlayerDied();
break;
case ViewStateLevelIntro:
DrawUiLevelIntro();
break;
case ViewStateLevelComplete:
DrawUiLevelComplete();
break;
@ -578,6 +584,80 @@ void View::DrawUiGameOver() {
}
}
void View::DrawUiLevelIntro() {
SelectFont ("console.ttf size=23");
stringstream level_info_stream;
level_info_stream << "Level " << GetModel()->GetCurrentLevelIndex() + 1;
int level_number_dest_x = (screen_right - screen_left) * 0.5 - 180;
int level_number_dest_y = (screen_bottom - screen_top) * 0.5 - 60;
int level_number_start_x = level_number_dest_x;
int level_number_start_y = (screen_top);
int level_number_delta_x = level_number_dest_x - level_number_start_x;
int level_number_delta_y = level_number_dest_y - level_number_start_y;
Engine::GUI::Label (20, level_info_stream.str().c_str(),
level_number_dest_x - Engine::GetTimer("LevelIntroLevelNumber") * level_number_delta_x,
level_number_dest_y - Engine::GetTimer("LevelIntroLevelNumber") * level_number_delta_y
);
int level_title_dest_x = (screen_right - screen_left) * 0.5;
int level_title_dest_y = (screen_bottom) * 0.5 - 0;
int level_title_start_x = (screen_right - screen_left) * 0.5;
int level_title_start_y = (screen_bottom);
int level_title_delta_x = level_title_dest_x - level_title_start_x;
int level_title_delta_y = level_title_dest_y - level_title_start_y;
SelectFont("console.ttf size=46 color=#808080");
Engine::GUI::LabelCentered (21, GetModel()->GetLevelTitle().c_str(),
level_title_dest_x - Engine::GetTimer("LevelIntroLevelTitle") * level_title_delta_x - 2,
level_title_dest_y - Engine::GetTimer("LevelIntroLevelTitle") * level_title_delta_y + 2
);
SelectFont("console.ttf size=46 color=#ffffff");
Engine::GUI::LabelCentered (22, GetModel()->GetLevelTitle().c_str(),
level_title_dest_x - Engine::GetTimer("LevelIntroLevelTitle") * level_title_delta_x,
level_title_dest_y - Engine::GetTimer("LevelIntroLevelTitle") * level_title_delta_y
);
SelectFont ("console.ttf size=23");
level_info_stream.str("");
level_info_stream << "by " << GetModel()->GetLevelAuthor();
int level_author_dest_x = (screen_right - screen_left) * 0.5 + 80;
int level_author_dest_y = (screen_bottom - screen_top) * 0.5 + 20;
int level_author_start_x = screen_right;
int level_author_start_y = level_author_dest_y;
int level_author_delta_x = level_author_dest_x - level_author_start_x;
int level_author_delta_y = level_author_dest_y - level_author_start_y;
Engine::GUI::Label (22, level_info_stream.str().c_str(),
level_author_dest_x - Engine::GetTimer("LevelIntroLevelAuthor") * level_author_delta_x,
level_author_dest_y - Engine::GetTimer("LevelIntroLevelAuthor") * level_author_delta_y
);
if (Engine::CheckTimer("LevelIntroLevelStart")) {
if(Engine::GUI::Button (1, "Start", (screen_right - button_width) * 0.5, screen_bottom * 0.5 + 180, button_width, button_height)) {
PopViewState();
GetModel()->SetGameState(GameStateRunning);
}
}
if (Engine::GUI::CheckKeyPressed(SDLK_ESCAPE) ) {
PushViewState(ViewStatePaused);
}
}
void View::DrawUiLevelComplete() {
DrawPageTitle ("Level Complete!");
@ -585,6 +665,11 @@ void View::DrawUiLevelComplete() {
if(Engine::GUI::Button (1, "Next level ...", (screen_right - button_width) * 0.5, screen_bottom * 0.5 + 60, button_width, button_height)) {
PopViewState();
PushViewState(ViewStateLevelIntro);
Engine::StartTimer ("LevelIntroLevelNumber", 0.4);
Engine::StartTimer ("LevelIntroLevelTitle", 0.6);
Engine::StartTimer ("LevelIntroLevelAuthor", 0.8);
Engine::StartTimer ("LevelIntroLevelStart", 1.0);
// we have to take care when we are testing the level to not proceed to
// the next level...
@ -597,6 +682,7 @@ void View::DrawUiLevelComplete() {
}
} else {
GetModel()->ProceedToNextLevel();
GetModel()->SetGameState(GameStatePaused);
}
}
}
@ -607,7 +693,9 @@ void View::DrawUiGamePaused() {
if (Engine::GUI::Button (1, "Resume Game", screen_right * 0.5 - 100, 200, button_width, button_height)) {
PopViewState();
GetModel()->SetGameState(GameStateRunning);
if (GetViewState() == ViewStateGameRunning)
GetModel()->SetGameState(GameStateRunning);
}
if (Engine::GUI::Button (2, "Options", screen_right * 0.5 - 100, 250, button_width, button_height)) {
@ -891,9 +979,14 @@ void View::DrawUiEnterPlayername() {
if (Engine::GUI::Button (3, "Start Game", screen_right - 180 - 20, 500, 180, 40)) {
PopViewState();
PushViewState(ViewStateGameRunning);
GetModel()->OnNewGame();
GetModel()->SetGameState(GameStateRunning);
PushViewState(ViewStateGameRunning);
PushViewState(ViewStateLevelIntro);
Engine::StartTimer ("LevelIntroLevelNumber", 0.4);
Engine::StartTimer ("LevelIntroLevelTitle", 0.6);
Engine::StartTimer ("LevelIntroLevelAuthor", 0.8);
Engine::StartTimer ("LevelIntroLevelStart", 1.0);
GetModel()->SetGameState(GameStatePaused);
}
if (Engine::GUI::Button (5, "Back", 20, 500, 180, 40)

View File

@ -86,6 +86,7 @@ class View : public Engine::ViewBase {
void DrawUiMainMenu();
void DrawUiGameRunning();
void DrawUiGameOver();
void DrawUiLevelIntro();
void DrawUiLevelComplete();
void DrawUiGamePaused();
void DrawUiPlayerDied();

View File

@ -1,5 +1,7 @@
# Format
# <Type> <player?> <xpos> <ypos> <zpos> <zrot> <yrot> <xrot> <xvel> <yvel> <zvel> <rotvel>
Title Let's sing a song
Author Martin Felis
GameEntityTypeShip 1 0 0 0 0 90 0 0 0 0 0
GameEntityTypeAsteroid 0 7 0 -3 0 0 0 0.6 0 -0.1 -10
GameEntityTypeAsteroid 0 -5 0 1 0 0 0 0.3 0 0.4 5

View File

@ -1,5 +1,7 @@
# Format
# <Type> <player?> <xpos> <ypos> <zpos> <zrot> <yrot> <xrot> <xvel> <yvel> <zvel> <rotvel>
Title How about some cake?
Author Martin Felis
GameEntityTypeShip 1 0 0 0 0 90 0 0 0 0 0
GameEntityTypeAsteroid 0 2 0 -6 0 0 0 -0.6 0 0.3 -10
GameEntityTypeAsteroid 0 -3 0 8 0 0 0 -0.9 0 -0.4 15

View File

@ -1,6 +1,8 @@
# Format
# <Type> <player?> <xpos> <ypos> <zpos> <zrot> <yrot> <xrot> <xvel> <yvel> <zvel> <rotvel>
GameEntityTypeShip 1 0 0 0 0 90 0 0 0 0 0
Title Are you kidding?
Author Martin Felis
GGameEntityTypeShip 1 0 0 0 0 90 0 0 0 0 0
GameEntityTypeAsteroid 0 3 0 -2 0 -0.6 0 -0.6 0 0.4 0
GameEntityTypeAsteroid 0 -1 0 8 0 0 0 -0.9 0 0.4 15
GameEntityTypeAsteroid 0 -2 0 -4 0 1. 0 0.2 0 -0.8 10

View File

@ -1,5 +1,7 @@
# Format
# <Type> <player?> <xpos> <ypos> <zpos> <zrot> <yrot> <xrot> <xvel> <yvel> <zvel> <rotvel>
Title Yeah right!
Author Martin Felis
GameEntityTypeShip 1 0 0 0 0 90 0 0 0 0 0
GameEntityTypeAsteroid 0 9 0 0 0 -1. 0 0.2 0 0. 0
GameEntityTypeAsteroid 0 7.5 0 -4 0 -1. 0 -0.325 0 0.2 0

View File

@ -147,7 +147,6 @@ void LabelCentered (int id, const char* caption, int x, int y) {
view = EngineGetView ();
assert (view);
SelectFont("console.ttf size=23");
view->DrawGLStringMeasure(caption, &width, &height);
view->DrawGLString(x - 0.5 * width, y + height * 0.5, caption);
}

View File

@ -377,6 +377,33 @@ bool CheckTimer(const std::string &id) {
}
*/
// /** \brief Starts a timer with the given id that expires after sec seconds */
void StartTimer(const std::string &id, float sec) {
if (!ModelInstance) {
LogError ("Couldn't query Timer: Model not initialized!");
}
ModelInstance->StartTimer(id, sec);
}
// /** \brief Checks whether a timer expired */
bool CheckTimer(const std::string &id) {
if (!ModelInstance) {
LogError ("Couldn't query Timer: Model not initialized!");
}
return ModelInstance->CheckTimer(id);
}
// /** \brief Checks whether a timer expired */
float GetTimer(const std::string &id) {
if (!ModelInstance) {
LogError ("Couldn't query Timer: Model not initialized!");
}
return ModelInstance->GetTimer(id);
}
}

View File

@ -16,6 +16,55 @@ const unsigned int NullEntityId = std::numeric_limits<unsigned int>::max() - 1;
struct EntityBase;
struct Timer {
Timer() :
mRunning(false),
mCurrentValue(0.)
{}
Timer (float sec_value) :
mRunning(true),
mCurrentValue (sec_value)
{}
Timer (const Timer& timer) :
mRunning (timer.mRunning),
mCurrentValue (timer.mCurrentValue)
{}
Timer& operator= (const Timer& timer) {
if (this != &timer) {
mRunning = timer.mRunning;
mCurrentValue = timer.mCurrentValue;
}
return *this;
}
void Set (float sec_value) {
mCurrentValue = sec_value;
}
float Get () {
return mCurrentValue;
}
bool Query() {
if (mCurrentValue <= 0.)
return true;
return false;
}
void Start() {
mRunning = true;
}
void Pause() {
mRunning = false;
}
bool Update(float sec_value) {
if (mRunning)
mCurrentValue -= sec_value;
return Query();
}
bool mRunning;
float mCurrentValue;
};
/** \brief Represents the current state of the Engine
*
* Represents the State of the Engine and is unaware of anything except itself.
@ -42,6 +91,7 @@ class ModelBase : public Module {
mDurationApplicationStart = current_frame;
mDeltaSec = current_frame - last_frame;
last_frame = current_frame;
UpdateTimers (mDeltaSec);
}
float GetFrameDuration () {
return mDeltaSec;
@ -103,30 +153,63 @@ class ModelBase : public Module {
};
unsigned int GetGameState () { return mGameState; };
/*
void StartTimer(const std::string &id, float msec) {
void StartTimer(const std::string &id, float sec) {
TimerIter cur_timer = mTimers.find(id);
if (cur_timer != mTimers.end()) {
cur_timer->second.Set(msec);
cur_timer->second.Set(sec);
cur_timer->second.Start();
return;
}
mTimers[id] = Timer(id, msec);
mTimers[id] = Timer(sec);
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 true;
}
return cur_timer->second.Query();
}
*/
float GetTimer (const std::string &id) {
TimerIter cur_timer = mTimers.find(id);
if (cur_timer == mTimers.end()) {
return 0.;
}
return cur_timer->second.Get();
}
void PauseTimers () {
for (TimerIter iter = mTimers.begin(); iter != mTimers.end(); iter++) {
iter->second.Pause();
}
}
void ResumeTimers () {
for (TimerIter iter = mTimers.begin(); iter != mTimers.end(); iter++) {
iter->second.Start();
}
}
void UpdateTimers (float sec) {
TimerIter iter = mTimers.begin();
while (iter != mTimers.end()) {
bool timer_running = iter->second.Update(sec);
if (timer_running) {
LogDebug ("Erasing expired timer %s", iter->first.c_str());
mTimers.erase(iter++);
}
else
++iter;
}
}
protected:
/** \brief Initializes the system */
@ -153,10 +236,8 @@ class ModelBase : public Module {
/** \brief contains all Engine::Entities that ceased to exist */
std::vector<unsigned int> mKilledEntities;
/*
std::map<std::string, Timer> mTimers;
typedef std::map<std::string, Timer>::iterator TimerIter;
*/
unsigned int mEntityIdCounter;
unsigned int mPlayerEntityId;

View File

@ -26,10 +26,13 @@ float GetFrameDuration ();
float GetDurationApplicationStart ();
// /** \brief Starts a timer with the given id that expires after sec seconds */
// void StartTimer(const std::string &id, float sec);
//
void StartTimer(const std::string &id, float sec);
// /** \brief Checks whether a timer expired */
// bool CheckTimer(const std::string &id);
bool CheckTimer(const std::string &id);
// /** \brief Checks whether a timer expired */
float GetTimer(const std::string &id);
}