Moved all map/world generation to Game::generate().

This commit is contained in:
Felix Ableitner 2012-10-01 10:15:38 +02:00
parent 8d295df943
commit 178a040f54
4 changed files with 52 additions and 46 deletions

View file

@ -33,7 +33,6 @@ Game::Game(const Vector2i& resolution) :
//mFps("test"), //mFps("test"),
mTileManager(mWorld), mTileManager(mWorld),
mPathfinder(mWorld), mPathfinder(mWorld),
mPlayer(mWorld, mCollection, Vector2f(200.0f, 100.0f), mPathfinder),
mElapsed(0), mElapsed(0),
mQuit(false), mQuit(false),
mPaused(false) { mPaused(false) {
@ -41,13 +40,32 @@ Game::Game(const Vector2i& resolution) :
mWindow.setKeyRepeatEnabled(true); mWindow.setKeyRepeatEnabled(true);
mWorld.SetContactListener(this); mWorld.SetContactListener(this);
mTileManager.generate(); generate();
}
/**
* Generates a predefined map.
*/
void
Game::generate() {
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
mTileManager.setTile(TileManager::TilePosition(x, y), TileManager::Type::WALL);
for (int x = 1; x < 9; x++)
for (int y = 1; y < 9; y++)
mTileManager.setTile(TileManager::TilePosition(x, y), TileManager::Type::FLOOR);
for (int x = 1; x < 5; x++)
mTileManager.setTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL);
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f), mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f),
mCollection))); mCollection)));
mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(300, 200), Vector2i(100, 150), mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(300, 200), Vector2i(100, 150),
mWorld))); mWorld)));
}
mPlayer = std::unique_ptr<Player>(new Player(mWorld, mCollection, Vector2f(200.0f, 100.0f), mPathfinder));
}
/** /**
* Closes window. * Closes window.
*/ */
@ -113,7 +131,7 @@ Game::input() {
mouseUp(event); mouseUp(event);
break; break;
case sf::Event::MouseMoved: case sf::Event::MouseMoved:
mPlayer.setCrosshairPosition(convertCoordinates(event.mouseMove.x, event.mouseMove.y)); mPlayer->setCrosshairPosition(convertCoordinates(event.mouseMove.x, event.mouseMove.y));
break; break;
default: default:
break; break;
@ -134,16 +152,16 @@ Game::keyUp(const sf::Event& event) {
mPaused = !mPaused; mPaused = !mPaused;
break; break;
case sf::Keyboard::W: case sf::Keyboard::W:
mPlayer.setDirection(Player::Direction::UP, true); mPlayer->setDirection(Player::Direction::UP, true);
break; break;
case sf::Keyboard::S: case sf::Keyboard::S:
mPlayer.setDirection(Player::Direction::DOWN, true); mPlayer->setDirection(Player::Direction::DOWN, true);
break; break;
case sf::Keyboard::A: case sf::Keyboard::A:
mPlayer.setDirection(Player::Direction::LEFT, true); mPlayer->setDirection(Player::Direction::LEFT, true);
break; break;
case sf::Keyboard::D: case sf::Keyboard::D:
mPlayer.setDirection(Player::Direction::RIGHT, true); mPlayer->setDirection(Player::Direction::RIGHT, true);
break; break;
default: default:
break; break;
@ -157,16 +175,16 @@ void
Game::keyDown(const sf::Event& event) { Game::keyDown(const sf::Event& event) {
switch (event.key.code) { switch (event.key.code) {
case sf::Keyboard::W: case sf::Keyboard::W:
mPlayer.setDirection(Player::Direction::UP, false); mPlayer->setDirection(Player::Direction::UP, false);
break; break;
case sf::Keyboard::S: case sf::Keyboard::S:
mPlayer.setDirection(Player::Direction::DOWN, false); mPlayer->setDirection(Player::Direction::DOWN, false);
break; break;
case sf::Keyboard::A: case sf::Keyboard::A:
mPlayer.setDirection(Player::Direction::LEFT, false); mPlayer->setDirection(Player::Direction::LEFT, false);
break; break;
case sf::Keyboard::D: case sf::Keyboard::D:
mPlayer.setDirection(Player::Direction::RIGHT, false); mPlayer->setDirection(Player::Direction::RIGHT, false);
break; break;
default: default:
break; break;
@ -188,10 +206,10 @@ void
Game::mouseUp(const sf::Event& event) { Game::mouseUp(const sf::Event& event) {
switch (event.mouseButton.button) { switch (event.mouseButton.button) {
case sf::Mouse::Left: case sf::Mouse::Left:
mPlayer.fire(); mPlayer->fire();
break; break;
case sf::Mouse::Right: case sf::Mouse::Right:
mPlayer.move(convertCoordinates(event.mouseButton.x, event.mouseButton.y)); mPlayer->move(convertCoordinates(event.mouseButton.x, event.mouseButton.y));
break; break;
default: default:
break; break;
@ -205,14 +223,14 @@ void
Game::render() { Game::render() {
mWindow.clear(); mWindow.clear();
mView.setCenter(mPlayer.getPosition()); mView.setCenter(mPlayer->getPosition());
// Render world and dynamic stuff. // Render world and dynamic stuff.
mWindow.setView(mView); mWindow.setView(mView);
mWindow.draw(mTileManager); mWindow.draw(mTileManager);
mWindow.draw(mCollection); mWindow.draw(mCollection);
mWindow.draw(mPlayer); mWindow.draw(*mPlayer);
// Render GUI and static stuff. // Render GUI and static stuff.
mWindow.setView(mWindow.getDefaultView()); mWindow.setView(mWindow.getDefaultView());

View file

@ -45,6 +45,7 @@ private:
void keyUp(const sf::Event& event); void keyUp(const sf::Event& event);
void mouseUp(const sf::Event& event); void mouseUp(const sf::Event& event);
void generate();
sf::String getFps(); sf::String getFps();
sf::Vector2<float> convertCoordinates(int x, int y); sf::Vector2<float> convertCoordinates(int x, int y);
@ -63,7 +64,7 @@ private:
Collection mCollection; Collection mCollection;
TileManager mTileManager; TileManager mTileManager;
Pathfinder mPathfinder; Pathfinder mPathfinder;
Player mPlayer; std::unique_ptr<Player> mPlayer;
/// Milliseconds since the last tick. /// Milliseconds since the last tick.
sf::Uint32 mElapsed; sf::Uint32 mElapsed;

View file

@ -75,20 +75,6 @@ TileManager::Tile::getTilePosition() const {
return TilePosition(getPosition().x / TILE_SIZE.x, getPosition().y / TILE_SIZE.y); return TilePosition(getPosition().x / TILE_SIZE.x, getPosition().y / TILE_SIZE.y);
} }
/**
* Fills the world with predefined tiles.
*/
void
TileManager::generate() {
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
setTile(TilePosition(x, y), Type::WALL);
for (int x = 1; x < 9; x++)
for (int y = 1; y < 9; y++)
setTile(TilePosition(x, y), Type::FLOOR);
}
/** /**
* Insert a tile at the position. Deletes an existing tile first if one is at the position. * Insert a tile at the position. Deletes an existing tile first if one is at the position.
* *

View file

@ -22,19 +22,8 @@
class Sprite; class Sprite;
class TileManager : public sf::Drawable { class TileManager : public sf::Drawable {
// Public variables. // Public types.
public: public:
/// The size of a single tile (pixels).
static const Vector2i TILE_SIZE;
// Public functions.
public:
TileManager(b2World& world);
void generate();
// Private types.
private:
enum class Type { enum class Type {
FLOOR, FLOOR,
WALL WALL
@ -45,12 +34,24 @@ private:
*/ */
typedef Vector2i TilePosition; typedef Vector2i TilePosition;
// Public variables.
public:
/// The size of a single tile (pixels).
static const Vector2i TILE_SIZE;
// Public functions.
public:
TileManager(b2World& world);
void setTile(const TilePosition& position, Type type);
// Private types.
private:
class Tile; class Tile;
// Private functions. // Private functions.
private: private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const; void draw(sf::RenderTarget& target, sf::RenderStates states) const;
void setTile(const TilePosition& position, Type type);
// Private variables. // Private variables.
private: private: