2012-09-09 20:50:15 +00:00
|
|
|
/*
|
|
|
|
* Game.cpp
|
|
|
|
*
|
|
|
|
* Created on: 05.07.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Game.h"
|
2013-04-11 18:44:00 +00:00
|
|
|
|
2013-05-08 09:19:21 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2013-07-06 19:21:25 +00:00
|
|
|
#include "generator/Generator.h"
|
2013-07-10 21:25:53 +00:00
|
|
|
#include "items/Heal.h"
|
|
|
|
#include "items/Shield.h"
|
2013-05-08 09:30:02 +00:00
|
|
|
#include "sprites/Enemy.h"
|
2013-05-26 18:16:36 +00:00
|
|
|
#include "sprites/Player.h"
|
|
|
|
#include "util/Yaml.h"
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
const int Game::FPS_GOAL = 60;
|
|
|
|
|
2013-08-18 11:27:32 +00:00
|
|
|
void Game::initPlayer() {
|
|
|
|
mPlayer = std::shared_ptr < Player
|
|
|
|
> (new Player(mWorld, mPathfinder, mGenerator.getPlayerSpawn()));
|
|
|
|
mPlayer->setLeftGadget(std::shared_ptr < Gadget > (new Heal()));
|
|
|
|
mPlayer->setRightGadget(std::shared_ptr < Gadget > (new Shield()));
|
|
|
|
mWorld.insertCharacter(mPlayer);
|
|
|
|
}
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
|
|
|
* Initializes game, including window and objects (sprites).
|
|
|
|
*/
|
2013-06-25 15:44:40 +00:00
|
|
|
Game::Game(tgui::Window& window) :
|
2012-10-01 08:47:42 +00:00
|
|
|
mWindow(window),
|
2013-08-07 15:39:43 +00:00
|
|
|
mWorldView(Vector2f(0, 0), mWindow.getView().getSize()),
|
2013-04-29 14:49:16 +00:00
|
|
|
mGenerator(mWorld, mPathfinder),
|
2012-09-09 20:50:15 +00:00
|
|
|
mQuit(false),
|
|
|
|
mPaused(false) {
|
|
|
|
mWindow.setFramerateLimit(FPS_GOAL);
|
2013-05-20 11:18:01 +00:00
|
|
|
mWindow.setKeyRepeatEnabled(false);
|
2012-09-11 14:22:47 +00:00
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
mGenerator.generateCurrentAreaIfNeeded(Vector2f());
|
2013-08-18 11:27:32 +00:00
|
|
|
initPlayer();
|
2013-06-25 15:44:40 +00:00
|
|
|
|
2013-07-06 12:37:16 +00:00
|
|
|
mHealth = window.add<tgui::Label>();
|
|
|
|
mHealth->setTextSize(20);
|
2013-06-25 15:44:40 +00:00
|
|
|
mAmmo = window.add<tgui::Label>();
|
|
|
|
mAmmo->setTextSize(20);
|
2013-07-06 11:38:15 +00:00
|
|
|
mCurrentWeapon = window.add<tgui::Label>();
|
2013-07-10 21:41:24 +00:00
|
|
|
mCurrentWeapon->setTextSize(14);
|
|
|
|
mLeftGadget = window.add<tgui::Label>();
|
|
|
|
mLeftGadget->setTextSize(14);
|
|
|
|
mRightGadget = window.add<tgui::Label>();
|
|
|
|
mRightGadget->setTextSize(14);
|
2012-10-01 08:15:38 +00:00
|
|
|
}
|
2013-04-11 18:44:00 +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();
|
2013-04-04 21:13:08 +00:00
|
|
|
if (mPaused)
|
2012-12-22 00:24:44 +00:00
|
|
|
elapsed = 0;
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
mWorld.think(elapsed);
|
2013-08-18 11:27:32 +00:00
|
|
|
if (mPlayer->getHealth() == 0) {
|
2013-08-18 11:38:28 +00:00
|
|
|
Vector2f pos = mPlayer->getCrosshairPosition();
|
2013-08-18 11:27:32 +00:00
|
|
|
mWorld.remove(mPlayer);
|
|
|
|
initPlayer();
|
2013-08-18 11:38:28 +00:00
|
|
|
mPlayer->setCrosshairPosition(pos);
|
2013-08-18 11:27:32 +00:00
|
|
|
}
|
2012-09-09 20:50:15 +00:00
|
|
|
|
2012-12-22 13:56:17 +00:00
|
|
|
mWorld.step(elapsed);
|
2013-05-20 21:01:01 +00:00
|
|
|
|
2013-07-06 11:38:15 +00:00
|
|
|
updateGui();
|
2013-06-25 15:44:40 +00:00
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
render();
|
2013-05-20 21:01:01 +00:00
|
|
|
|
|
|
|
mGenerator.generateCurrentAreaIfNeeded(mPlayer->getPosition());
|
2012-09-09 20:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:44:40 +00:00
|
|
|
/**
|
|
|
|
* Displays current player ammo in the ammo widget.
|
|
|
|
*/
|
|
|
|
void
|
2013-07-06 11:38:15 +00:00
|
|
|
Game::updateGui() {
|
2013-06-25 15:44:40 +00:00
|
|
|
int mag = mPlayer->getMagazineAmmo();
|
|
|
|
int total = mPlayer->getTotalAmmo();
|
|
|
|
|
|
|
|
std::string magString = tgui::to_string(mag);
|
|
|
|
if (mag < 10) magString = "0" + magString;
|
|
|
|
|
|
|
|
std::string totalString = tgui::to_string(total);
|
|
|
|
if (total < 100) totalString = "0" + totalString;
|
|
|
|
if (total < 10) totalString = "0" + totalString;
|
|
|
|
|
2013-07-06 12:37:16 +00:00
|
|
|
mHealth->setText(tgui::to_string(mPlayer->getHealth()));
|
2013-06-25 15:44:40 +00:00
|
|
|
mAmmo->setText(magString + "/" + totalString);
|
2013-07-06 11:38:15 +00:00
|
|
|
mCurrentWeapon->setText(mPlayer->getWeaponName());
|
2013-07-10 21:41:24 +00:00
|
|
|
mLeftGadget->setText(mPlayer->getLeftGadgetName());
|
|
|
|
mRightGadget->setText(mPlayer->getRightGadgetName());
|
2013-07-06 11:38:15 +00:00
|
|
|
|
2013-07-06 12:37:16 +00:00
|
|
|
mHealth->setPosition(0, mWindow.getSize().y - mHealth->getSize().y);
|
2013-07-06 11:38:15 +00:00
|
|
|
mAmmo->setPosition(mWindow.getSize().x - mAmmo->getSize().x,
|
|
|
|
mWindow.getSize().y - mAmmo->getSize().y);
|
|
|
|
mCurrentWeapon->setPosition(mWindow.getSize().x - mCurrentWeapon->getSize().x,
|
|
|
|
mAmmo->getPosition().y - mCurrentWeapon->getSize().y);
|
2013-07-10 21:41:24 +00:00
|
|
|
mLeftGadget->setPosition(mWindow.getSize().x / 2 - mLeftGadget->getSize().x - 10,
|
|
|
|
mWindow.getSize().y - mLeftGadget->getSize().y);
|
|
|
|
mRightGadget->setPosition(mWindow.getSize().x / 2 + 10,
|
|
|
|
mWindow.getSize().y - mRightGadget->getSize().y);
|
2013-06-25 15:44:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 20:50:15 +00:00
|
|
|
/**
|
|
|
|
* 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:
|
2013-04-18 10:45:37 +00:00
|
|
|
mPlayer->setCrosshairPosition(convertCoordinates(event.mouseMove.x,
|
|
|
|
event.mouseMove.y));
|
2012-09-09 20:50:15 +00:00
|
|
|
break;
|
2013-06-25 20:00:40 +00:00
|
|
|
case sf::Event::MouseWheelMoved:
|
|
|
|
mPlayer->toggleWeapon();
|
|
|
|
break;
|
2012-09-09 20:50:15 +00:00
|
|
|
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;
|
2013-07-12 22:01:57 +00:00
|
|
|
case sf::Keyboard::F:
|
2013-07-21 08:52:50 +00:00
|
|
|
mPlayer->pickUpItem();
|
2013-07-12 22:01:57 +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;
|
2013-07-06 19:21:25 +00:00
|
|
|
case sf::Keyboard::Q:
|
|
|
|
mPlayer->useLeftGadget();
|
|
|
|
break;
|
|
|
|
case sf::Keyboard::E:
|
|
|
|
mPlayer->useRightGadget();
|
|
|
|
break;
|
2013-06-25 15:44:40 +00:00
|
|
|
case sf::Keyboard::R:
|
|
|
|
mPlayer->reload();
|
|
|
|
break;
|
2013-06-25 20:00:40 +00:00
|
|
|
case sf::Keyboard::Num1:
|
|
|
|
mPlayer->selectFirstWeapon();
|
|
|
|
break;
|
|
|
|
case sf::Keyboard::Num2:
|
|
|
|
mPlayer->selectSecondWeapon();
|
|
|
|
break;
|
2012-09-09 20:50:15 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a screen coordinate to a world coordinate.
|
|
|
|
*/
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2<float>
|
2012-09-09 20:50:15 +00:00
|
|
|
Game::convertCoordinates(int x, int y) {
|
2013-08-07 15:39:43 +00:00
|
|
|
return mWindow.mapPixelToCoords(Vector2i(x, y), mWorldView);
|
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;
|
2013-04-18 10:45:37 +00:00
|
|
|
case sf::Mouse::Right:
|
2013-04-27 12:04:23 +00:00
|
|
|
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
|
|
|
|
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();
|
|
|
|
|
2013-06-25 15:44:40 +00:00
|
|
|
mWorldView.setCenter(mPlayer->getPosition());
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
// Render world and dynamic stuff.
|
2013-06-25 15:44:40 +00:00
|
|
|
mWindow.setView(mWorldView);
|
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());
|
2013-06-25 15:44:40 +00:00
|
|
|
mWindow.drawGUI();
|
2012-09-09 20:50:15 +00:00
|
|
|
|
|
|
|
mWindow.display();
|
|
|
|
}
|