Tetris/playstate.cpp
2014-11-07 20:47:50 +01:00

360 lines
6.2 KiB
C++

# include "game.hpp"
# include "states.hpp"
PlayState PlayState::m_PlayState;
void PlayState::Init()
{
Map = map();
Part = part();
Next = part();
level = 0;
lines = 0;
score = 0;
tick = 1;
nameEntered = false;
nameEntering = false;
message = NULL;
textColor = make_color(255, 255, 255);
SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
# ifdef DEBUG
std::cout << "PlayState Init Successful" << std::endl;
# endif
}
void PlayState::Clean()
{
SDL_EnableKeyRepeat ( 0, 0 );
# ifdef DEBUG
std::cout << "PlayState Clean Successful" << std::endl;
# endif
}
void PlayState::Pause()
{
SDL_EnableKeyRepeat ( 0, 0 );
# ifdef DEBUG
std::cout << "PlayState Paused" << std::endl;
# endif
}
void PlayState::Resume()
{
SDL_EnableKeyRepeat ( REPAET_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
# ifdef DEBUG
std::cout << "PlayState Resumed" << std::endl;
# endif
}
void PlayState::HandleEvents ( game* Game )
{
SDL_Event event;
while ( SDL_PollEvent ( &event ) )
{
if ( event.type == SDL_QUIT)
Game->Quit();
if( nameEntering == true && nameEntered == false )
{
//Get user input
name.handle_input(Game, &event);
//If the enter key was pressed
if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) )
{
//Change the flag
nameEntered = true;
nameEntering = false;
Save(name.GetStr(), lines);
name.Clear();
//Free the old message surface
SDL_FreeSurface( message );
//Change the message
}
name.show_centered(Game);
}
else
{
switch ( event.type )
{
case SDL_KEYDOWN:
switch ( event.key.keysym.sym )
{
case SDLK_ESCAPE:
{
//Game->SetRunning(false);
Game->PushState ( PauseState::Instance() );
}
break;
case SDLK_RIGHT:
{
part p = Part;
# ifdef DEBUG
std::cout << "move to Right: ";
# endif
Part.move ( 1, 0 );
if ( Map.isCollision ( Part, true ) )
{
# ifdef DEBUG
std::cout << "Collision" << std::endl;
# endif
Part = p;
}
}
break;
case SDLK_LEFT:
{
part p = Part;
# ifdef DEBUG
std::cout << "move to Left: ";
# endif
Part.move ( -1, 0 );
if ( Map.isCollision ( Part, true ) )
{
# ifdef DEBUG
std::cout << "Collision" << std::endl;
# endif
Part = p;
}
}
break;
case SDLK_DOWN:
# ifdef DEBUG
std::cout << "move to Down: ";
# endif
if ( !Map.isCollision ( Part ) )
{
Part.move ( 0, 1 );
}
else
{
# ifdef DEBUG
std::cout << "Collision" << std::endl;
# endif
}
break;
case SDLK_UP:
{
part p = Part;
# ifdef DEBUG
std::cout << "Rotate: ";
# endif
Part.rotate();
if ( Map.isCollision ( Part, true ) )
{
# ifdef DEBUG
std::cout << "Map: Collission" << std::endl;
# endif
Part = p;
}
}
break;
case SDLK_SPACE:
# ifdef DEBUG
std::cout << "move to Bottom" << std::endl;
# endif
while ( !Map.isCollision ( Part ) )
{
Part.move ( 0, 1 );
tick = FPS - 1;
}
break;
case SDLK_RETURN:
{
# ifdef DEBUG
std::cout << "continue" << std::endl;
# endif
//Game->m_bGameOver = false;
}
break;
}
break;
}
}
}
}
void PlayState::Update ( game* Game )
{
if ( Map.isFull() )
{
nameEntering = true;
if (nameEntered)
{
nameEntering = false;
nameEntered = false;
//text.setString("Score " + intToStr(Map.getScore()) + "\n" + "Press enter");
Map.reset();
lines = 0;
level = 1;
}
}
if (!nameEntering)
{
if ( level == 0 )
{
level = 1;
}
if ( tick % ( FPS / ( ( level > 30 ) ? 30 : level ) ) == 0 && !Map.isCollision ( Part ) )
{
# ifdef DEBUG
std::cout << "move to Down: ";
# endif
Part.move ( 0, 1 );
}
if ( Map.isCollision ( Part ) && tick + 1 == ( FPS / ( ( level > 30 ) ? 30 : level ) ) )
{
int count;
Map.addPart ( Part );
count = Map.destroyLines();
lines += count;
// level = (lines >= 5) ? (lines / 10) : 0;
level = lines / 10 + 1;
# ifdef DEBUG
std::cout << "Lines: " << lines << std::endl;
std::cout << "Level: " << level << std::endl;
# endif
Part = Next;
Next = part();
}
if ( tick < FPS )
{
tick++;
}
else
{
tick = 1;
}
}
}
void PlayState::Draw ( game* Game )
{
if (!nameEntering)
{
SDL_FillRect ( Game->GetScreen(), NULL, 0x000000 );
SDL_Rect rect = {WIDTH * SIZE, 0, 130, HEIGHT * SIZE};
SDL_FillRect ( Game->GetScreen(), &rect, 0x111111 );
Map.draw ( Game->GetScreen() );
Part.draw ( Game->GetScreen() );
Next.draw ( 165, 100, Game->GetScreen() );
PlayInfo(this, Game);
}
else
{
SDL_FillRect ( Game->GetScreen(), NULL, 0x000000 );
message = TTF_RenderText_Solid( Game->GetfontGame(), "Enter Name: ", textColor );
apply_surface(10, 40, message, Game->GetScreen());
name.show_centered(Game);
}
SDL_Flip ( Game->GetScreen() );
}
void PlayState::Save ( string Name, int Lines )
{
ofstream file;
file.open(".score", ios_base::app);
if (file.is_open())
{
file << Name << " " << Lines << endl;
file.close();
}
else
{
cerr << "Cannot open file '.score'" << endl;
}
}
void PlayInfo ( PlayState * Info, game * Game )
{
SDL_Color textColor;
SDL_Surface * message;
char temp[10];
textColor = make_color(255, 255, 255);
message = TTF_RenderText_Solid( Game->GetfontGame(), "Lines", textColor);
apply_surface(220, 150, message, Game->GetScreen());
std::sprintf ( temp, "%d", Info->lines);
message = TTF_RenderText_Solid( Game->GetfontGame(), temp, textColor);
apply_surface(220, 185, message, Game->GetScreen());
message = TTF_RenderText_Solid( Game->GetfontGame(), "Level", textColor);
apply_surface(220, 230, message, Game->GetScreen());
std::sprintf ( temp, "%d", Info->level);
message = TTF_RenderText_Solid( Game->GetfontGame(), temp, textColor);
apply_surface(220, 265, message, Game->GetScreen());
SDL_FreeSurface(message);
}