Skip to content

Commit

Permalink
Finished prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
Zegis committed Apr 27, 2014
1 parent e78d5a5 commit 30fabe7
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 38 deletions.
116 changes: 84 additions & 32 deletions Game.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Game.h"


Game::Game(void)
Game::Game(void): MAXLVL(2)
{
if(InitializeAllegro())
{
Expand Down Expand Up @@ -30,8 +30,6 @@ Game::Game(void)
follower = new Sprite(followerBMP, true);
map = new TileMap();

map->LoadMap("map01.txt", player, follower);

inGame = true;
al_set_target_bitmap(al_get_backbuffer(display));
}
Expand Down Expand Up @@ -74,7 +72,10 @@ void Game::Run()
{

if(GameMenu())
{
currentLvl = 1;
GameLoop();
}
else
al_rest(0.5);
}
Expand Down Expand Up @@ -121,44 +122,82 @@ void Game::GameLoop()
{
ALLEGRO_EVENT ev;


Draw();

al_start_timer(timer);

while(inGame)
{
al_wait_for_event(evQueue, &ev);
std::cout << "Poziom: " << currentLvl << std::endl;

if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
break;
else if(ev.keyboard.keycode == ALLEGRO_KEY_LEFT)
player->setVelocityX(-1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_RIGHT)
player->setVelocityX(1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_DOWN)
player->setVelocityY(1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_PAD_4)
follower->setVelocityX(-1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_PAD_5)
follower->setVelocityX(0);
else if(ev.keyboard.keycode == ALLEGRO_KEY_PAD_6)
follower->setVelocityX(1);
}
else if(ev.type == ALLEGRO_EVENT_TIMER)
map->LoadMap(assembleMapName(), player, follower);
al_start_timer(timer);

inLevel = true;

Draw();

while(inLevel)
{
UpdateObject(player);
UpdateObject(follower);
Draw();
al_wait_for_event(evQueue, &ev);

if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
{
inGame = false;
break;
}
else if(ev.keyboard.keycode == ALLEGRO_KEY_LEFT)
player->setVelocityX(-1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_RIGHT)
player->setVelocityX(1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_DOWN)
player->setVelocityY(1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_PAD_4)
follower->setVelocityX(-1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_PAD_5)
follower->stop();
else if(ev.keyboard.keycode == ALLEGRO_KEY_PAD_6)
follower->setVelocityX(1);
else if(ev.keyboard.keycode == ALLEGRO_KEY_R)
{
std::cout << "Restart!\n";
inLevel = false;
break;
}

}
else if(ev.type == ALLEGRO_EVENT_TIMER)
{
UpdateObject(player);
UpdateObject(follower);
Draw();
}
}

follower->stop();
player->stop();
al_rest(0.5);
}

DrawEnd();
al_rest(1);
}

string Game::assembleMapName()
{
stringstream str;

str << "map";

if(currentLvl < 10)
str << "0" << currentLvl;
else
str << currentLvl;

str << ".txt";

return str.str();

}

void Game::UpdateObject(Sprite* object)
{
if(object->isFollower())
Expand Down Expand Up @@ -187,6 +226,7 @@ void Game::UpdateObject(Sprite* object)
if(!object->isFollower())
{
object->setVelocityX(0);

object->setVelocityY(0);
}

Expand All @@ -195,6 +235,11 @@ void Game::UpdateObject(Sprite* object)

bool Game::CheckForTileCollision(int newX, int newY, bool isFollower)
{
if(isFollower && newX == player->getX() && newY == player->getY())
return false;
else if( newX == follower->getX() && newY == follower->getY())
return false;

if (newX < 0 || newX > 6 || newY > 9)
return false;
else if(map->getTile(newX,newY) == map->air)
Expand All @@ -214,7 +259,14 @@ void Game::CheckObjectCollisions(Sprite* object)
Sprite* collisionSprite = GetSpriteCollision(object);

if( collisionSprite != NULL && object->isFollower())
inGame = false;
{
if(currentLvl < MAXLVL)
++currentLvl;
else
inGame = false;

inLevel = false;
}
}

Sprite* Game::GetSpriteCollision(Sprite* object)
Expand Down Expand Up @@ -254,7 +306,7 @@ void Game::Draw()

void Game::DrawEnd()
{
if(!inGame)
if(!inGame && !inLevel)
{
al_clear_to_color(al_map_rgb(0,0,0));

Expand Down
10 changes: 10 additions & 0 deletions Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <allegro5\allegro_ttf.h>
#include "Sprite.h"
#include "TileMap.h"
#include <sstream>

using std::stringstream;

class Game
{
Expand All @@ -31,10 +34,15 @@ class Game
int TILE_SIZE;
int YOFFSET;

int const MAXLVL;

int currentLvl;

void Draw();
void DrawEnd();

bool inGame;
bool inLevel;

void UpdateObject(Sprite* object);

Expand All @@ -47,6 +55,8 @@ class Game

bool GameMenu();

string assembleMapName();

public:
Game(void);
~Game(void);
Expand Down
6 changes: 6 additions & 0 deletions Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ ALLEGRO_BITMAP * Sprite::getImage()
return bmp;
}

void Sprite::stop()
{
dx = 0;
dy = 0;
}

Sprite::~Sprite(void)
{
}
2 changes: 2 additions & 0 deletions Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ class Sprite
void update(long elapsedTime);

bool isFollower();

void stop();
};

4 changes: 3 additions & 1 deletion TileMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ ALLEGRO_BITMAP* TileMap::getTile(int x, int y)
void TileMap::LoadMap(string mapName, Sprite* player, Sprite* follower)
{
fstream mapFile(mapName, fstream::in);

objects.clear();

int y = 0;
string mapLine;
Expand All @@ -67,7 +69,7 @@ void TileMap::LoadMap(string mapName, Sprite* player, Sprite* follower)
else if (mapLine[i] == '0') Images[y][i] = ground;
else if (mapLine[i] == 'R') Images[y][i] = rock;
else if (mapLine[i] == '1'){
Images[y][i] = ground;
Images[y][i] = air;
objects.push_back(new Sprite(treasure,i,y));
}
}
Expand Down
10 changes: 5 additions & 5 deletions map01.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.......
.......
.@...F.
0000R..
R0RRR0R
0000000
0000000
R0RRRRR
0000000
RRRRRR0
0001000
RF.@01R
0RRR0R0
0000000
0000000
10 changes: 10 additions & 0 deletions map02.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.......
.......
.@...F.
0000R..
R0RRR0R
0000000
0000000
0000000
RRRRRR0
0001000

0 comments on commit 30fabe7

Please sign in to comment.