2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Game.cpp
|
|
|
|
*
|
|
|
|
* Created on: 05.07.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Game.h"
|
2013-03-29 17:41:42 +00:00
|
|
|
|
2013-03-29 17:34:51 +00:00
|
|
|
#include "sprites/Enemy.h"
|
2013-03-29 17:41:42 +00:00
|
|
|
#include "sprites/Player.h"
|
2013-03-29 16:59:35 +00:00
|
|
|
#include "util/Yaml.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
/// Goal amount of frames per second.
|
|
|
|
const int Game::FPS_GOAL = 60;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes game, including window and objects (sprites).
|
|
|
|
*/
|
2012-10-01 08:47:42 +00:00
|
|
|
Game::Game(sf::RenderWindow& window) :
|
|
|
|
mWindow(window),
|
2012-12-22 14:10:26 +00:00
|
|
|
mView(sf::Vector2f(0, 0), mWindow.getView().getSize()),
|
2012-12-22 00:24:44 +00:00
|
|
|
mTileManager(mWorld),
|
2012-09-09 20:50:15 +00:00
|
|
|
mQuit(false),
|
|
|
|
mPaused(false) {
|
|
|
|
mWindow.setFramerateLimit(FPS_GOAL);
|
2012-12-22 00:14:30 +00:00
|
|
|
mWindow.setKeyRepeatEnabled(true);
|
2012-09-11 14:22:47 +00:00
|
|
|
|
2012-10-01 08:15:38 +00:00
|
|
|
generate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a predefined map.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::generate() {
|
2013-03-03 20:26:29 +00:00
|
|
|
for (int x = 0; x < 11; x++)
|
|
|
|
mTileManager.insertTile(TileManager::TilePosition(x, 0), TileManager::Type::WALL);
|
|
|
|
for (int x = 0; x < 11; x++)
|
|
|
|
mTileManager.insertTile(TileManager::TilePosition(x, 10), TileManager::Type::WALL);
|
2013-03-27 13:37:32 +00:00
|
|
|
for (int y = 1; y < 10; y++)
|
2013-03-03 20:26:29 +00:00
|
|
|
mTileManager.insertTile(TileManager::TilePosition(0, y), TileManager::Type::WALL);
|
2013-03-27 13:37:32 +00:00
|
|
|
for (int y = 1; y < 10; y++)
|
2013-03-03 20:26:29 +00:00
|
|
|
mTileManager.insertTile(TileManager::TilePosition(10, y), TileManager::Type::WALL);
|
2012-10-01 08:15:38 +00:00
|
|
|
|
2013-03-03 20:26:29 +00:00
|
|
|
for (int x = 1; x < 10; x++)
|
|
|
|
for (int y = 1; y < 10; y++)
|
|
|
|
mTileManager.insertTile(TileManager::TilePosition(x, y), TileManager::Type::FLOOR);
|
2012-10-01 08:15:38 +00:00
|
|
|
|
2013-03-03 20:26:29 +00:00
|
|
|
for (int x = 1; x < 5; x++) {
|
|
|
|
mTileManager.removeTile(TileManager::TilePosition(x, 4));
|
|
|
|
mTileManager.insertTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL);
|
|
|
|
}
|
2012-10-01 08:15:38 +00:00
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
mWorld.insertCharacter(std::shared_ptr<Character>(new Enemy(mWorld,
|
2012-12-22 14:10:26 +00:00
|
|
|
sf::Vector2f(400.0f, 200.0f), Yaml("enemy.yaml"))));
|
2012-10-04 17:10:12 +00:00
|
|
|
|
2013-03-19 19:43:27 +00:00
|
|
|
mPlayer = std::shared_ptr<Player>(new Player(mWorld,
|
2012-12-22 14:10:26 +00:00
|
|
|
sf::Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
|
2013-03-29 16:59:35 +00:00
|
|
|
mWorld.insertCharacter(mPlayer);
|
2013-03-27 13:37:32 +00:00
|
|
|
|
|
|
|
mWorld.generateAreas();
|
2012-10-01 08:15:38 +00:00
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
|
|
|
* Closes window.
|
|
|
|
*/
|
|
|
|
Game::~Game() {
|
|
|
|
mWindow.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the game loop.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::loop() {
|
|
|
|
while (!mQuit) {
|
|
|
|
|
|
|
|
input();
|
|
|
|
|
2012-12-22 00:24:44 +00:00
|
|
|
int elapsed = mClock.restart().asMilliseconds();
|
|
|
|
if (mPaused) {
|
|
|
|
elapsed = 0;
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
mWorld.think(elapsed);
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-22 13:56:17 +00:00
|
|
|
mWorld.step(elapsed);
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles general game input.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::input() {
|
|
|
|
sf::Event event;
|
|
|
|
while (mWindow.pollEvent(event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case sf::Event::Closed:
|
|
|
|
mQuit = true;
|
|
|
|
break;
|
|
|
|
case sf::Event::KeyPressed:
|
|
|
|
keyDown(event);
|
|
|
|
break;
|
|
|
|
case sf::Event::KeyReleased:
|
|
|
|
keyUp(event);
|
|
|
|
break;
|
2012-12-24 00:14:22 +00:00
|
|
|
case sf::Event::MouseButtonPressed:
|
|
|
|
mouseDown(event);
|
|
|
|
break;
|
2012-09-09 20:50:15 +00:00
|
|
|
case sf::Event::MouseButtonReleased:
|
|
|
|
mouseUp(event);
|
|
|
|
break;
|
|
|
|
case sf::Event::MouseMoved:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setCrosshairPosition(convertCoordinates(event.mouseMove.x, event.mouseMove.y));
|
2012-09-09 20:50:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles key up event. This is used for events that only fire once per keypress.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::keyUp(const sf::Event& event) {
|
|
|
|
switch (event.key.code) {
|
|
|
|
case sf::Keyboard::Escape:
|
|
|
|
mQuit = true;
|
|
|
|
break;
|
|
|
|
case sf::Keyboard::Space:
|
|
|
|
mPaused = !mPaused;
|
|
|
|
break;
|
2012-09-12 18:10:29 +00:00
|
|
|
case sf::Keyboard::W:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::UP, true);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::S:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::DOWN, true);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::A:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::LEFT, true);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::D:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::RIGHT, true);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
2012-09-09 20:50:15 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles key down event. This is used for any events that refire automatically.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::keyDown(const sf::Event& event) {
|
|
|
|
switch (event.key.code) {
|
2012-09-12 18:10:29 +00:00
|
|
|
case sf::Keyboard::W:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::UP, false);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::S:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::DOWN, false);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::A:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::LEFT, false);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
|
|
|
case sf::Keyboard::D:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->setDirection(Player::Direction::RIGHT, false);
|
2012-09-12 18:10:29 +00:00
|
|
|
break;
|
2012-09-09 20:50:15 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a screen coordinate to a world coordinate.
|
|
|
|
*/
|
|
|
|
sf::Vector2<float>
|
|
|
|
Game::convertCoordinates(int x, int y) {
|
2012-12-22 14:10:26 +00:00
|
|
|
return mWindow.convertCoords(sf::Vector2i(x, y), mView);
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 00:14:22 +00:00
|
|
|
void
|
|
|
|
Game::mouseDown(const sf::Event& event) {
|
|
|
|
switch(event.mouseButton.button) {
|
|
|
|
case sf::Mouse::Left:
|
|
|
|
mPlayer->pullTrigger();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
|
|
|
* Handles mouse key up events.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::mouseUp(const sf::Event& event) {
|
|
|
|
switch (event.mouseButton.button) {
|
|
|
|
case sf::Mouse::Left:
|
2012-12-24 00:14:22 +00:00
|
|
|
mPlayer->releaseTrigger();
|
2012-09-09 20:50:15 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders world and GUI.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::render() {
|
|
|
|
mWindow.clear();
|
|
|
|
|
2012-10-01 08:15:38 +00:00
|
|
|
mView.setCenter(mPlayer->getPosition());
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
// Render world and dynamic stuff.
|
|
|
|
mWindow.setView(mView);
|
|
|
|
|
2012-12-22 13:56:17 +00:00
|
|
|
mWindow.draw(mWorld);
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
// Render GUI and static stuff.
|
|
|
|
mWindow.setView(mWindow.getDefaultView());
|
|
|
|
|
|
|
|
mWindow.display();
|
|
|
|
}
|