This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/Game.cpp

202 lines
4 KiB
C++
Raw Normal View History

/*
* Game.cpp
*
* Created on: 05.07.2012
* Author: Felix
*/
#include "Game.h"
2013-04-11 18:44:00 +00:00
2013-04-12 18:34:48 +00:00
#include "generator/Generator.h"
2013-03-29 17:34:51 +00:00
#include "sprites/Enemy.h"
#include "sprites/Player.h"
#include "util/Yaml.h"
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()),
2013-04-29 14:49:16 +00:00
mGenerator(mWorld, mPathfinder),
mQuit(false),
mPaused(false) {
mWindow.setFramerateLimit(FPS_GOAL);
mWindow.setKeyRepeatEnabled(true);
2012-09-11 14:22:47 +00:00
2013-04-29 14:49:16 +00:00
mGenerator.generateTiles(sf::IntRect(-32, -32, 64, 64));
mPlayer = std::shared_ptr<Player>(new Player(mWorld, mPathfinder,
mGenerator.getPlayerSpawn(), Yaml("player.yaml")));
2013-04-11 18:44:00 +00:00
mWorld.insertCharacter(mPlayer);
}
2013-04-11 18:44:00 +00:00
/**
* Closes window.
*/
Game::~Game() {
mWindow.close();
}
/**
* Runs the game loop.
*/
void
Game::loop() {
while (!mQuit) {
input();
int elapsed = mClock.restart().asMilliseconds();
2013-04-04 21:13:08 +00:00
if (mPaused)
elapsed = 0;
mWorld.think(elapsed);
2012-12-22 13:56:17 +00:00
mWorld.step(elapsed);
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;
case sf::Event::MouseButtonReleased:
mouseUp(event);
break;
case sf::Event::MouseMoved:
2013-04-18 10:45:37 +00:00
mPlayer->setCrosshairPosition(convertCoordinates(event.mouseMove.x,
event.mouseMove.y));
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:
mPlayer->setDirection(Player::Direction::UP, true);
2012-09-12 18:10:29 +00:00
break;
case sf::Keyboard::S:
mPlayer->setDirection(Player::Direction::DOWN, true);
2012-09-12 18:10:29 +00:00
break;
case sf::Keyboard::A:
mPlayer->setDirection(Player::Direction::LEFT, true);
2012-09-12 18:10:29 +00:00
break;
case sf::Keyboard::D:
mPlayer->setDirection(Player::Direction::RIGHT, true);
2012-09-12 18:10:29 +00:00
break;
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:
mPlayer->setDirection(Player::Direction::UP, false);
2012-09-12 18:10:29 +00:00
break;
case sf::Keyboard::S:
mPlayer->setDirection(Player::Direction::DOWN, false);
2012-09-12 18:10:29 +00:00
break;
case sf::Keyboard::A:
mPlayer->setDirection(Player::Direction::LEFT, false);
2012-09-12 18:10:29 +00:00
break;
case sf::Keyboard::D:
mPlayer->setDirection(Player::Direction::RIGHT, false);
2012-09-12 18:10:29 +00:00
break;
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-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;
2013-04-18 10:45:37 +00:00
case sf::Mouse::Right:
mPlayer->setDestination(convertCoordinates(event.mouseButton.x,
event.mouseButton.y));
2013-05-01 16:04:41 +00:00
break;
2012-12-24 00:14:22 +00:00
default:
break;
}
}
2013-05-01 16:04:41 +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();
break;
default:
break;
}
}
/**
* Renders world and GUI.
*/
void
Game::render() {
mWindow.clear();
mView.setCenter(mPlayer->getPosition());
// Render world and dynamic stuff.
mWindow.setView(mView);
2012-12-22 13:56:17 +00:00
mWindow.draw(mWorld);
// Render GUI and static stuff.
mWindow.setView(mWindow.getDefaultView());
mWindow.display();
}