home *** CD-ROM | disk | FTP | other *** search
- #include "Game.h"
- #include "Level.h"
- #include "Pacman.h"
- #include <Log/LogPtr.h>
- #include <Converter.h>
- #include <InputForGameExe/InputManagerPtr.h>
-
- class Game::State
- {
- protected:
- Game * const game;
- const state_enum state;
- public:
- State( Game * game, state_enum state )
- : game( game ),
- state( state )
- {}
- state_enum get_state()const
- {
- return state;
- }
- virtual ~State()
- {}
- virtual state_enum get_new_state()const=0;
-
- virtual void life_cycle(float delta_time)
- {}
- };
-
- class Game::IntroState : public Game::State
- {
- Animation2D anim;
- bool quit;
- public:
- IntroState( Game * game )
- : State( game, INTRO ),
- anim( game->get_scene(), 0, "data/intro" ),
- quit( false )
- {}
- virtual state_enum get_new_state()const
- {
- return quit ? MAIN_MENU : state;
- }
- virtual void life_cycle(float delta_time)
- {
- if( !InputManagerPtr()->key_queue_empty() )
- {
- InputKeyEvent ev = InputManagerPtr()->get_key_event();
- if( ev.is_pressed() )
- quit = true;
- }
- }
- };
-
- class Game::MainMenuState : public Game::State
- {
- int curr;
- const int n_curr;
- bool quit;
- std::vector<Animation2D *> butt;
- Animation2D header;
- Animation2D footer;
- public:
- MainMenuState( Game * game )
- : State( game, MAIN_MENU ),
- header( game->get_scene(), 0, "data/header" ),
- footer( game->get_scene(), 0, "data/footer" ),
- curr( game->curr_main_menu_item ),
- n_curr( 4 ),
- quit( false )
- {
- if( curr < 0 || curr >= n_curr )
- {
- LogPtr()->warning( "curr_main_menu_item became invalid between lifetimes of Game::MainMenuState");
- curr = 0;
- }
- static const char * names[] = { "data/newgame", "data/help", "data/hiscore", "data/exit" };
- header.move( 35, 20 );
- for( int i = 0; i < n_curr; ++i )
- {
- butt.push_back( new Animation2D( game->get_scene(), 0, names[ i ] ) );
- butt.back()->move( 35, 20 + (i + 1)*39 );
- butt.back()->start( 0, true );
- }
- footer.move( 35, 20 + (i + 1)*39 );
-
- header.start( 0, true );
- footer.start( 0, true );
- butt[curr]->start( 1, true );
- }
- virtual ~MainMenuState()
- {
- while( !butt.empty() )
- {
- delete butt.back(); butt.pop_back();
- }
- game->curr_main_menu_item = curr;
- }
- virtual state_enum get_new_state()const
- {
- if( !quit )
- return state;
- switch( curr )
- {
- case 0:
- return LEVEL;
- case 1:
- return HELP;
- case 2:
- return HISCORE;
- case 3:
- return NAG;
- }
- return NAG; // exit, when error
- }
- virtual void life_cycle(float delta_time)
- {
- if( !InputManagerPtr()->key_queue_empty() )
- {
- InputKeyEvent ev = InputManagerPtr()->get_key_event();
- if( !quit && ev.is_pressed() )
- {
- butt[curr]->start( 0, true );
- if( ev.get_key_name() == "Up" )
- {
- curr = (curr + n_curr - 1) % n_curr;
- }
- else
- if( ev.get_key_name() == "Down" )
- {
- curr = (curr + 1) % n_curr;
- }
- else
- if( ev.get_key_name() == "Enter" )
- {
- quit = true;
- }
- butt[curr]->start( 1, true );
- }
- }
- }
- };
-
- class Game::HiscoreState : public Game::State
- {
- bool quit;
- Animation2D header;
- std::vector<FontObject2D *> font_score;
- std::vector<FontObject2D *> font_name;
- public:
- HiscoreState( Game * game )
- : State( game, HISCORE ),
- header( game->get_scene(), 0, "data/hiscore_pic" ),
- quit( false )
- {
- header.move( 35, 20 );
- header.start( 0, true );
- for( int i = 0; i < 6; ++i )
- {
- font_score.push_back( new FontObject2D( game->get_scene(), 0, "data/small_alphabet" ) );
- font_name.push_back( new FontObject2D( game->get_scene(), 0, "data/small_alphabet" ) );
- font_score.back()->move( 190, 80 + 20*i );
- font_name.back()->move( 20, 80 + 20*i );
-
- int score = game->hiscore.get_entry( i ).get_score();
- String name = game->hiscore.get_entry( i ).get_player_name();
-
- if( !name.empty() && score != 0 )
- {
- font_score.back()->change_text( Converter::convert( score ) );
- font_name.back()->change_text( name );
- }
- }
- }
- virtual ~HiscoreState()
- {
- while( !font_score.empty() )
- {
- delete font_score.back(); font_score.pop_back();
- }
- while( !font_name.empty() )
- {
- delete font_name.back(); font_name.pop_back();
- }
- }
- virtual state_enum get_new_state()const
- {
- return quit ? MAIN_MENU : state;
- }
- virtual void life_cycle(float delta_time)
- {
- if( !InputManagerPtr()->key_queue_empty() )
- {
- InputKeyEvent ev = InputManagerPtr()->get_key_event();
- if( ev.is_pressed() )
- quit = true;
- }
- }
- };
-
- class Game::EnterNameState : public Game::State
- {
- bool quit;
- Animation2D header;
- String name;
- FontObject2D font_name;
- public:
- EnterNameState( Game * game )
- : State( game, ENTER_NAME ),
- header( game->get_scene(), 0, "data/enter_name" ),
- font_name( game->get_scene(), 0, "data/small_alphabet" ),
- quit( false )
- {
- header.move( 35, 20 );
- header.start( 0, true );
- font_name.move( 20, 80 );
-
- if( game->score <= game->hiscore.get_worst_score() )
- quit = true;
- }
- virtual state_enum get_new_state()const
- {
- return quit ? HISCORE : state;
- }
- virtual void life_cycle(float delta_time)
- {
- if( !InputManagerPtr()->key_queue_empty() )
- {
- InputKeyEvent ev = InputManagerPtr()->get_key_event();
- if( ev.is_pressed() && ev.get_key_name() == "Enter" && !name.empty() )
- {
- game->hiscore.add_entry( Hiscore::Entry( name, game->score ) );
- quit = true;
- }
- else
- if( ev.is_text() )
- {
- name += ev.get_key_name();
- }
- }
- name = font_name.change_text( name );
- }
- };
-
- class Game::HelpState : public Game::State
- {
- bool quit;
- Animation2D anim;
- public:
- HelpState( Game * game )
- : State( game, HELP ),
- anim( game->get_scene(), 0, "data/help_pic" ),
- quit( false )
- {
- anim.move( 0, 0 );
- anim.start( 0, true );
- }
- virtual state_enum get_new_state()const
- {
- return quit ? MAIN_MENU : state;
- }
- virtual void life_cycle(float delta_time)
- {
- if( !InputManagerPtr()->key_queue_empty() )
- {
- InputKeyEvent ev = InputManagerPtr()->get_key_event();
- if( ev.is_pressed() )
- quit = true;
- }
- }
- };
-
- class Game::LevelState : public Game::State
- {
- FontObject2D lives_text;
- FontObject2D score_text;
- /**
- * @supplierCardinality 1
- */
- Level * level;
- /**
- * @supplierCardinality 1
- */
- Pacman * pacman;
- bool quit;
- int level_num;
- bool load_level()
- {
- pacman->put_on_level( 0 );
- delete level; level = 0;
- level = new Level( game->get_scene(), level_num );
- if( !*level )
- {
- quit = true;
- return false;
- }
- pacman->put_on_level( level );
- level->add_external_creature( pacman );
- return true;
- }
- public:
- LevelState( Game * game )
- : State( game, LEVEL ),
- score_text( game->get_scene(), 3, "data/small_alphabet" ),
- lives_text( game->get_scene(), 3, "data/small_alphabet" ),
- pacman( new Pacman( game->get_scene() ) ),
- level_num( 0 ),
- quit( false ),
- level( 0 )
- {
- score_text.move( 10, 220 );
- score_text.change_text( "0" );
-
- lives_text.move( 180, 220 );
- lives_text.change_text( "0" );
-
- load_level();
- }
- virtual ~LevelState()
- {
- delete level; level = 0;
- delete pacman; pacman = 0;
- }
- virtual state_enum get_new_state()const
- {
- if( quit )
- return ENTER_NAME;
- if( !pacman )
- return ENTER_NAME;
- if( pacman->get_life() < 1 )
- return ENTER_NAME;
- return state;
- }
- virtual void life_cycle(float delta_time)
- {
- if( quit )
- return;
- if( !InputManagerPtr()->key_queue_empty() )
- {
- InputKeyEvent ev = InputManagerPtr()->get_key_event();
- if( ev.is_pressed() )
- {
- if( ev.get_key_name() == "Up" )
- {
- if ( pacman )
- pacman->key_down( Direction::UP );
- }
- else
- if( ev.get_key_name() == "Down" )
- {
- if ( pacman )
- pacman->key_down( Direction::DOWN );
- }
- else
- if( ev.get_key_name() == "Left" )
- {
- if ( pacman )
- pacman->key_down( Direction::LEFT );
- }
- else
- if( ev.get_key_name() == "Right" )
- {
- if ( pacman )
- pacman->key_down( Direction::RIGHT );
- }
- else
- if( ev.get_key_name() == "Esc" )
- {
- quit = true;
- }
- }
- }
- if( level )
- {
- level->life_cycle( delta_time );
- if( level->get_dot_count() <= 0 )
- {
- ++level_num;
- load_level();
- }
- }
- if( pacman )
- {
- game->score = pacman->get_score();
- score_text.change_text( String("Score: ") + Converter::convert( int(pacman->get_score()) ) );
- lives_text.change_text( String("Lives: ") + Converter::convert( int(pacman->get_life()) ) );
- }
- }
- };
-
- class Game::NagState : public Game::State
- {
- bool quit;
- Animation2D anim;
- public:
- NagState( Game * game )
- : State( game, NAG ),
- anim( game->get_scene(), 0, "data/nag" ),
- quit( false )
- {
- anim.move( 0, 0 );
- anim.start( 0, true );
- }
- virtual state_enum get_new_state()const
- {
- return quit ? FINISHED: state;
- }
- virtual void life_cycle(float delta_time)
- {
- if( !InputManagerPtr()->key_queue_empty() )
- {
- InputKeyEvent ev = InputManagerPtr()->get_key_event();
- if( ev.is_pressed() )
- quit = true;
- }
- }
- };
-
- class Game::FinishedState : public Game::State
- {
- public:
- FinishedState( Game * game )
- : State( game, FINISHED )
- {}
- virtual state_enum get_new_state()const
- {
- return state;
- }
- };
-
- Game::Game(Scene2D * scene)
- : scene( scene ),
- state( 0 ),
- hiscore( 6 ),
- curr_main_menu_item( 0 )
- {
- state = new IntroState( this );
- }
-
- Game::~Game()
- {
- delete state; state = 0;
- }
-
- bool Game::is_alive()const
- {
- return state->get_state() != FINISHED;
- }
-
- void Game::life_cycle(float delta_time)
- {
- state->life_cycle( delta_time );
-
- state_enum new_state = state->get_new_state();
- if( new_state != state->get_state() )
- {
- delete state; state = 0;
- switch( new_state )
- {
- case INTRO:
- state = new IntroState( this );
- break;
- case MAIN_MENU:
- state = new MainMenuState( this );
- break;
- case ENTER_NAME:
- state = new EnterNameState( this );
- break;
- case HISCORE:
- state = new HiscoreState( this );
- break;
- case HELP:
- state = new HelpState( this );
- break;
- case LEVEL:
- state = new LevelState( this );
- break;
- case NAG:
- state = new NagState( this );
- break;
- case FINISHED:
- state = new FinishedState( this );
- break;
- default:
- // ┼±δΦ ≈≥ε-≥ε φσ ≥ε, Γ√⌡εΣΦ∞
- LogPtr()->error( "Unpossible state switch" );
- state = new FinishedState( this );
- break;
- }
- }
- }
-