2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Game.cpp
|
|
|
|
*
|
|
|
|
* Created on: 05.07.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Game.h"
|
|
|
|
|
2012-10-01 07:12:00 +00:00
|
|
|
#include <Thor/Graphics.hpp>
|
|
|
|
|
2012-10-04 17:10:12 +00:00
|
|
|
#include "Instances.h"
|
2012-10-01 09:02:44 +00:00
|
|
|
#include "abstract/Character.h"
|
2012-09-12 12:21:57 +00:00
|
|
|
#include "sprite/Cover.h"
|
|
|
|
#include "sprite/Enemy.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
#include "util/Loader.h"
|
|
|
|
#include "util/ResourceManager.h"
|
|
|
|
#include "util/String.h"
|
|
|
|
|
|
|
|
/// Goal amount of frames per second.
|
|
|
|
const int Game::FPS_GOAL = 60;
|
|
|
|
|
|
|
|
/// Milliseconds per tick at FPS_GOAL.
|
|
|
|
const float Game::TICKS_GOAL = 1000 / Game::FPS_GOAL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes game, including window and objects (sprites).
|
|
|
|
*/
|
2012-10-01 08:47:42 +00:00
|
|
|
Game::Game(sf::RenderWindow& window) :
|
2012-09-09 20:50:15 +00:00
|
|
|
mWorld(b2Vec2(0, 0)),
|
2012-10-01 08:47:42 +00:00
|
|
|
mWindow(window),
|
|
|
|
mView(Vector2f(0, 0), mWindow.getView().getSize()),
|
2012-09-09 20:50:15 +00:00
|
|
|
//mFps("test"),
|
2012-10-01 07:12:00 +00:00
|
|
|
mTileManager(mWorld),
|
2012-10-01 08:15:38 +00:00
|
|
|
mPathfinder(mWorld),
|
2012-09-09 20:50:15 +00:00
|
|
|
mElapsed(0),
|
|
|
|
mQuit(false),
|
|
|
|
mPaused(false) {
|
|
|
|
mWindow.setFramerateLimit(FPS_GOAL);
|
2012-09-12 12:21:57 +00:00
|
|
|
mWindow.setKeyRepeatEnabled(true);
|
2012-09-11 14:22:47 +00:00
|
|
|
mWorld.SetContactListener(this);
|
|
|
|
|
2012-10-01 08:15:38 +00:00
|
|
|
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);
|
|
|
|
|
2012-10-04 17:10:12 +00:00
|
|
|
Instances instances(mPathfinder, mTileManager, mCollection, mWorld);
|
|
|
|
|
|
|
|
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(instances, Vector2f(400.0f, 200.0f))));
|
2012-10-01 07:12:00 +00:00
|
|
|
mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(300, 200), Vector2i(100, 150),
|
2012-10-01 08:15:38 +00:00
|
|
|
mWorld)));
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-10-04 17:10:12 +00:00
|
|
|
mPlayer = std::unique_ptr<Player>(new Player(instances, Vector2f(200.0f, 100.0f)));
|
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() {
|
|
|
|
sf::Uint32 left = 0;
|
|
|
|
while (!mQuit) {
|
|
|
|
|
|
|
|
input();
|
|
|
|
|
|
|
|
for (; !mPaused && (left >= TICKS_GOAL); left -= TICKS_GOAL) {
|
2012-10-01 09:02:44 +00:00
|
|
|
Character::think(TICKS_GOAL);
|
2012-09-09 20:50:15 +00:00
|
|
|
mWorld.Step(1.0f / FPS_GOAL, 8, 3);
|
|
|
|
|
|
|
|
mCollection.checkDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
//mFps.setString(getFps());
|
|
|
|
|
|
|
|
tick();
|
|
|
|
left += mElapsed;
|
|
|
|
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves ticks since last call.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::tick() {
|
|
|
|
mElapsed = mClock.restart().asMilliseconds();
|
|
|
|
if (mPaused) {
|
|
|
|
mElapsed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
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) {
|
|
|
|
return mWindow.convertCoords(Vector2i(x, y), mView);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles mouse key up events.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::mouseUp(const sf::Event& event) {
|
|
|
|
switch (event.mouseButton.button) {
|
|
|
|
case sf::Mouse::Left:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->fire();
|
2012-09-09 20:50:15 +00:00
|
|
|
break;
|
|
|
|
case sf::Mouse::Right:
|
2012-10-01 08:15:38 +00:00
|
|
|
mPlayer->move(convertCoordinates(event.mouseButton.x, event.mouseButton.y));
|
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);
|
|
|
|
|
|
|
|
mWindow.draw(mTileManager);
|
|
|
|
mWindow.draw(mCollection);
|
2012-10-01 08:15:38 +00:00
|
|
|
mWindow.draw(*mPlayer);
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
// Render GUI and static stuff.
|
|
|
|
mWindow.setView(mWindow.getDefaultView());
|
|
|
|
|
|
|
|
//mWindow.draw(mFps);
|
|
|
|
|
|
|
|
mWindow.display();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current FPS as string.
|
|
|
|
*/
|
2012-10-11 21:24:22 +00:00
|
|
|
String
|
2012-09-09 20:50:15 +00:00
|
|
|
Game::getFps() {
|
|
|
|
return str((mElapsed != 0) ? 1000.0f / mElapsed : 0.0f, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin of collision, call callback function on both objects.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Game::BeginContact(b2Contact* contact) {
|
|
|
|
Physical& first = *static_cast<Physical*>(contact->GetFixtureA()->GetBody()->GetUserData());
|
|
|
|
Physical& second = *static_cast<Physical*>(contact->GetFixtureB()->GetBody()->GetUserData());
|
|
|
|
|
|
|
|
if (!first.doesCollide(second) || !second.doesCollide(first)) {
|
|
|
|
contact->SetEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
first.onCollide(second, second.getCategory());
|
|
|
|
second.onCollide(first, first.getCategory());
|
|
|
|
}
|