Moved Character handling to world, improved some includes.
This commit is contained in:
parent
7a074b7c3d
commit
30de9e397b
16 changed files with 64 additions and 67 deletions
|
@ -15,6 +15,7 @@
|
||||||
#include "sprites/Enemy.h"
|
#include "sprites/Enemy.h"
|
||||||
#include "util/Loader.h"
|
#include "util/Loader.h"
|
||||||
#include "util/ResourceManager.h"
|
#include "util/ResourceManager.h"
|
||||||
|
#include "util/Yaml.h"
|
||||||
|
|
||||||
/// Goal amount of frames per second.
|
/// Goal amount of frames per second.
|
||||||
const int Game::FPS_GOAL = 60;
|
const int Game::FPS_GOAL = 60;
|
||||||
|
@ -57,12 +58,12 @@ Game::generate() {
|
||||||
mTileManager.insertTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL);
|
mTileManager.insertTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
mWorld.insert(std::shared_ptr<Sprite>(new Enemy(mWorld,
|
mWorld.insertCharacter(std::shared_ptr<Character>(new Enemy(mWorld,
|
||||||
sf::Vector2f(400.0f, 200.0f), Yaml("enemy.yaml"))));
|
sf::Vector2f(400.0f, 200.0f), Yaml("enemy.yaml"))));
|
||||||
|
|
||||||
mPlayer = std::shared_ptr<Player>(new Player(mWorld,
|
mPlayer = std::shared_ptr<Player>(new Player(mWorld,
|
||||||
sf::Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
|
sf::Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
|
||||||
mWorld.insert(mPlayer);
|
mWorld.insertCharacter(mPlayer);
|
||||||
|
|
||||||
mWorld.generateAreas();
|
mWorld.generateAreas();
|
||||||
}
|
}
|
||||||
|
@ -87,7 +88,7 @@ Game::loop() {
|
||||||
elapsed = 0;
|
elapsed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Character::think(elapsed);
|
mWorld.think(elapsed);
|
||||||
|
|
||||||
mWorld.step(elapsed);
|
mWorld.step(elapsed);
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "sprites/Player.h"
|
#include "sprites/Player.h"
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
|
|
||||||
|
class TileManager;
|
||||||
class Player;
|
class Player;
|
||||||
class World;
|
class World;
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,33 @@ World::remove(std::shared_ptr<Sprite> drawable) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a character into the world. A character can only be inserted once.
|
||||||
|
* Also calls insert(character);
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
World::insertCharacter(std::shared_ptr<Character> character) {
|
||||||
|
#ifndef NDEBUG
|
||||||
|
auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
|
||||||
|
assert(item == mCharacters.end());
|
||||||
|
#endif
|
||||||
|
mCharacters.push_back(character);
|
||||||
|
insert(character);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a character from the world.
|
||||||
|
* Also calls remove(character);
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
World::removeCharacter(std::shared_ptr<Character> character) {
|
||||||
|
auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
|
||||||
|
if (item != mCharacters.end()) {
|
||||||
|
mCharacters.erase(item);
|
||||||
|
}
|
||||||
|
remove(character);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate path finding base data.
|
* Generate path finding base data.
|
||||||
*
|
*
|
||||||
|
@ -255,6 +282,18 @@ World::step(int elapsed) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls Character::onThink for each character.
|
||||||
|
*
|
||||||
|
* @param elapsed Time since last call.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
World::think(int elapsed) {
|
||||||
|
for (auto it : mCharacters) {
|
||||||
|
it->onThink(elapsed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for collisions using Seperating Axis Theorem (SAT).
|
* Tests for collisions using Seperating Axis Theorem (SAT).
|
||||||
*
|
*
|
||||||
|
|
|
@ -8,14 +8,10 @@
|
||||||
#ifndef DG_WORLD_H_
|
#ifndef DG_WORLD_H_
|
||||||
#define DG_WORLD_H_
|
#define DG_WORLD_H_
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
|
||||||
|
|
||||||
#include "abstract/Sprite.h"
|
#include "abstract/Sprite.h"
|
||||||
|
#include "abstract/Character.h"
|
||||||
|
|
||||||
class Sprite;
|
class Character;
|
||||||
class Sprite;
|
class Sprite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,7 +25,10 @@ class World : public sf::Drawable {
|
||||||
public:
|
public:
|
||||||
void insert(std::shared_ptr<Sprite> drawable);
|
void insert(std::shared_ptr<Sprite> drawable);
|
||||||
void remove(std::shared_ptr<Sprite> drawable);
|
void remove(std::shared_ptr<Sprite> drawable);
|
||||||
|
void insertCharacter(std::shared_ptr<Character> character);
|
||||||
|
void removeCharacter(std::shared_ptr<Character> character);
|
||||||
void step(int elapsed);
|
void step(int elapsed);
|
||||||
|
void think(int elapsed);
|
||||||
void generateAreas();
|
void generateAreas();
|
||||||
std::vector<sf::Vector2f> getPath(const sf::Vector2f& start,
|
std::vector<sf::Vector2f> getPath(const sf::Vector2f& start,
|
||||||
const sf::Vector2f& end, float radius) const;
|
const sf::Vector2f& end, float radius) const;
|
||||||
|
@ -82,6 +81,7 @@ private:
|
||||||
private:
|
private:
|
||||||
std::map<Sprite::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
|
std::map<Sprite::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;
|
||||||
std::vector<Area> mAreas; //< This has to be a vector as objects are compared by address.
|
std::vector<Area> mAreas; //< This has to be a vector as objects are compared by address.
|
||||||
|
std::vector<std::shared_ptr<Character> > mCharacters;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* DG_WORLD_H_ */
|
#endif /* DG_WORLD_H_ */
|
||||||
|
|
|
@ -12,8 +12,10 @@
|
||||||
|
|
||||||
#include <Thor/Vectors.hpp>
|
#include <Thor/Vectors.hpp>
|
||||||
|
|
||||||
|
#include "../items/Weapon.h"
|
||||||
#include "../sprites/Corpse.h"
|
#include "../sprites/Corpse.h"
|
||||||
#include "../util/Log.h"
|
#include "../util/Log.h"
|
||||||
|
#include "../util/Yaml.h"
|
||||||
|
|
||||||
const std::string Character::KEY_HEALTH = "health";
|
const std::string Character::KEY_HEALTH = "health";
|
||||||
const int Character::DEFAULT_HEALTH = 100;
|
const int Character::DEFAULT_HEALTH = 100;
|
||||||
|
@ -22,7 +24,6 @@ const float Character::DEFAULT_SPEED = 100;
|
||||||
const float Character::POINT_REACHED_DISTANCE = 1.0f;
|
const float Character::POINT_REACHED_DISTANCE = 1.0f;
|
||||||
const std::string Character::KEY_WEAPON = "weapon";
|
const std::string Character::KEY_WEAPON = "weapon";
|
||||||
const std::string Character::DEFAULT_WEAPON = "weapon.yaml";
|
const std::string Character::DEFAULT_WEAPON = "weapon.yaml";
|
||||||
std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves pointer to this instance in static var for think().
|
* Saves pointer to this instance in static var for think().
|
||||||
|
@ -33,30 +34,11 @@ Character::Character(World& world, const Data& data, const Yaml& config) :
|
||||||
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
|
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
|
||||||
mCurrentHealth(mMaxHealth),
|
mCurrentHealth(mMaxHealth),
|
||||||
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
||||||
mWeapon(world, *this, Yaml(config.get(KEY_WEAPON, DEFAULT_WEAPON))),
|
mWeapon(new Weapon(world, *this, Yaml(config.get(KEY_WEAPON, DEFAULT_WEAPON)))),
|
||||||
mStartPathfinding(false) {
|
mStartPathfinding(false) {
|
||||||
mCharacterInstances.push_back(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes pointer from static variable mCharacterInstances.
|
|
||||||
*/
|
|
||||||
Character::~Character() {
|
Character::~Character() {
|
||||||
auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this);
|
|
||||||
assert(it != mCharacterInstances.end());
|
|
||||||
mCharacterInstances.erase(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls onThink on all Actor instances.
|
|
||||||
*
|
|
||||||
* @param elapsed Amount of time to simulate.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
Character::think(int elapsed) {
|
|
||||||
for (auto i : mCharacterInstances) {
|
|
||||||
i->onThink(elapsed);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -83,7 +65,7 @@ Character::onDamage(int damage) {
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Character::onThink(int elapsed) {
|
Character::onThink(int elapsed) {
|
||||||
mWeapon.onThink(elapsed);
|
mWeapon->onThink(elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -108,7 +90,7 @@ Character::getMovementSpeed() const {
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Character::pullTrigger() {
|
Character::pullTrigger() {
|
||||||
mWeapon.pullTrigger();
|
mWeapon->pullTrigger();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -116,7 +98,7 @@ Character::pullTrigger() {
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
Character::releaseTrigger() {
|
Character::releaseTrigger() {
|
||||||
mWeapon.releaseTrigger();
|
mWeapon->releaseTrigger();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,13 +12,9 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Sprite.h"
|
#include "Sprite.h"
|
||||||
#include "../World.h"
|
|
||||||
#include "../items/Weapon.h"
|
|
||||||
#include "../util/Yaml.h"
|
|
||||||
|
|
||||||
class World;
|
class World;
|
||||||
class Weapon;
|
class Weapon;
|
||||||
class Instances;
|
|
||||||
class Sprite;
|
class Sprite;
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
|
||||||
|
@ -31,8 +27,6 @@ public:
|
||||||
Character(World& world, const Data& data, const Yaml& config);
|
Character(World& world, const Data& data, const Yaml& config);
|
||||||
virtual ~Character() = 0;
|
virtual ~Character() = 0;
|
||||||
|
|
||||||
static void think(int elapsed);
|
|
||||||
|
|
||||||
void onDamage(int damage);
|
void onDamage(int damage);
|
||||||
|
|
||||||
// Protected functions.
|
// Protected functions.
|
||||||
|
@ -56,14 +50,13 @@ private:
|
||||||
/// The distance to a point where it is considered reached (in pixels).
|
/// The distance to a point where it is considered reached (in pixels).
|
||||||
static const float POINT_REACHED_DISTANCE;
|
static const float POINT_REACHED_DISTANCE;
|
||||||
|
|
||||||
static std::vector<Character*> mCharacterInstances;
|
friend class World;
|
||||||
|
|
||||||
World& mWorld;
|
World& mWorld;
|
||||||
|
|
||||||
const int mMaxHealth;
|
const int mMaxHealth;
|
||||||
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
|
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
|
||||||
const float mMovementSpeed;
|
const float mMovementSpeed;
|
||||||
Weapon mWeapon;
|
std::unique_ptr<Weapon> mWeapon;
|
||||||
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
|
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
|
||||||
bool mStartPathfinding; //< True if a movement destination was just set.
|
bool mStartPathfinding; //< True if a movement destination was just set.
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include "../util/Loader.h"
|
#include "../util/Loader.h"
|
||||||
#include "../util/Log.h"
|
#include "../util/Log.h"
|
||||||
#include "../util/ResourceManager.h"
|
#include "../util/ResourceManager.h"
|
||||||
|
#include "../util/Yaml.h"
|
||||||
|
|
||||||
const std::string Sprite::KEY_SIZE = "size";
|
const std::string Sprite::KEY_SIZE = "size";
|
||||||
const std::string Sprite::KEY_RADIUS = "radius";
|
const std::string Sprite::KEY_RADIUS = "radius";
|
||||||
|
|
|
@ -8,10 +8,9 @@
|
||||||
#ifndef DG_SPRITE_H_
|
#ifndef DG_SPRITE_H_
|
||||||
#define DG_SPRITE_H_
|
#define DG_SPRITE_H_
|
||||||
|
|
||||||
#include <SFML/System.hpp>
|
#include <memory>
|
||||||
#include <SFML/Graphics.hpp>
|
|
||||||
|
|
||||||
#include "../util/Yaml.h"
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,6 @@ const bool Weapon::DEFAULT_AUTOMATIC = false;
|
||||||
|
|
||||||
Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) :
|
Weapon::Weapon(World& world, Sprite& holder, const Yaml& config) :
|
||||||
Emitter(world),
|
Emitter(world),
|
||||||
mWorld(world),
|
|
||||||
mHolder(holder),
|
mHolder(holder),
|
||||||
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
||||||
mLastShotWaitInterval(0),
|
mLastShotWaitInterval(0),
|
||||||
|
|
|
@ -10,16 +10,12 @@
|
||||||
|
|
||||||
#include <Thor/Particles.hpp>
|
#include <Thor/Particles.hpp>
|
||||||
|
|
||||||
#include "../abstract/Sprite.h"
|
|
||||||
#include "../particle/Emitter.h"
|
#include "../particle/Emitter.h"
|
||||||
#include "../util/Timer.h"
|
|
||||||
#include "../util/Yaml.h"
|
|
||||||
|
|
||||||
class Emitter;
|
|
||||||
class Instances;
|
|
||||||
class Sprite;
|
class Sprite;
|
||||||
class Timer;
|
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
class World;
|
||||||
|
class Particle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loading mechanism:
|
* Loading mechanism:
|
||||||
|
@ -48,7 +44,6 @@ private:
|
||||||
static const std::string KEY_AUTOMATIC;
|
static const std::string KEY_AUTOMATIC;
|
||||||
static const bool DEFAULT_AUTOMATIC;
|
static const bool DEFAULT_AUTOMATIC;
|
||||||
|
|
||||||
World& mWorld;
|
|
||||||
Sprite& mHolder;
|
Sprite& mHolder;
|
||||||
|
|
||||||
sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
|
sf::Vector2f mOffset; //< Offset to the point where bullets are inserted (from holder center).
|
||||||
|
|
|
@ -8,11 +8,9 @@
|
||||||
#ifndef DG_EMITTER_H_
|
#ifndef DG_EMITTER_H_
|
||||||
#define DG_EMITTER_H_
|
#define DG_EMITTER_H_
|
||||||
|
|
||||||
#include "../abstract/Sprite.h"
|
|
||||||
#include "../World.h"
|
#include "../World.h"
|
||||||
#include "Particle.h"
|
#include "Particle.h"
|
||||||
|
|
||||||
class Sprite;
|
|
||||||
class World;
|
class World;
|
||||||
class Particle;
|
class Particle;
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
#define DG_CORPSE_H_
|
#define DG_CORPSE_H_
|
||||||
|
|
||||||
#include "../abstract/Sprite.h"
|
#include "../abstract/Sprite.h"
|
||||||
#include "../util/Yaml.h"
|
|
||||||
|
|
||||||
class Sprite;
|
class Sprite;
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
|
|
||||||
#include "Enemy.h"
|
#include "Enemy.h"
|
||||||
|
|
||||||
#include "Corpse.h"
|
|
||||||
|
|
||||||
Enemy::Enemy(World& collection, const sf::Vector2f& position, const Yaml& config) :
|
Enemy::Enemy(World& collection, const sf::Vector2f& position, const Yaml& config) :
|
||||||
Character(collection, Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
|
Character(collection, Data(position, 0, CATEGORY_ACTOR, MASK_ALL),
|
||||||
config) {
|
config) {
|
||||||
|
|
|
@ -8,15 +8,10 @@
|
||||||
#ifndef DG_ENEMY_H_
|
#ifndef DG_ENEMY_H_
|
||||||
#define DG_ENEMY_H
|
#define DG_ENEMY_H
|
||||||
|
|
||||||
#include <SFML/System.hpp>
|
|
||||||
|
|
||||||
#include "../abstract/Character.h"
|
#include "../abstract/Character.h"
|
||||||
#include "../World.h"
|
|
||||||
#include "../util/Yaml.h"
|
|
||||||
|
|
||||||
class Character;
|
class Character;
|
||||||
class World;
|
class World;
|
||||||
class Instances;
|
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
|
||||||
class Enemy : public Character {
|
class Enemy : public Character {
|
||||||
|
|
|
@ -12,12 +12,8 @@
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
#include "../abstract/Character.h"
|
#include "../abstract/Character.h"
|
||||||
#include "../items/Weapon.h"
|
|
||||||
#include "../util/Yaml.h"
|
|
||||||
|
|
||||||
class Character;
|
class Character;
|
||||||
class Instances;
|
|
||||||
class Weapon;
|
|
||||||
class Yaml;
|
class Yaml;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "../abstract/Sprite.h"
|
#include "../abstract/Sprite.h"
|
||||||
#include "../util/Loader.h"
|
#include "../util/Loader.h"
|
||||||
#include "../util/ResourceManager.h"
|
#include "../util/ResourceManager.h"
|
||||||
|
#include "../util/Yaml.h"
|
||||||
|
|
||||||
const sf::Vector2i TileManager::TILE_SIZE = sf::Vector2i(100, 100);
|
const sf::Vector2i TileManager::TILE_SIZE = sf::Vector2i(100, 100);
|
||||||
|
|
||||||
|
|
Reference in a new issue