Removed Instances class.

This commit is contained in:
Felix Ableitner 2012-12-22 01:44:36 +01:00
parent d923e94541
commit 402cd138d3
9 changed files with 47 additions and 34 deletions

View file

@ -11,7 +11,6 @@
#include "abstract/Character.h" #include "abstract/Character.h"
#include "sprites/Enemy.h" #include "sprites/Enemy.h"
#include "types/Instances.h"
#include "types/String.h" #include "types/String.h"
#include "util/Loader.h" #include "util/Loader.h"
#include "util/ResourceManager.h" #include "util/ResourceManager.h"
@ -50,11 +49,11 @@ Game::generate() {
for (int x = 1; x < 5; x++) for (int x = 1; x < 5; x++)
mTileManager.setTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL); mTileManager.setTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL);
Instances instances(mPathfinder, mTileManager, mCollection, mWorld); mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, mCollection, mPathfinder,
Vector2f(400.0f, 200.0f), Yaml("enemy.yaml"))));
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(instances, Vector2f(400.0f, 200.0f), Yaml("enemy.yaml")))); mPlayer = std::unique_ptr<Player>(new Player(mWorld, mCollection, mPathfinder,
Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
mPlayer = std::unique_ptr<Player>(new Player(instances, Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
} }
/** /**
* Closes window. * Closes window.

View file

@ -25,28 +25,29 @@ 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().
*/ */
Character::Character(const Instances& instances, const String& texturePath, Character::Character(World& world, Collection& collection, Pathfinder& pathfinder,
const PhysicalData& data, const Yaml& config) : const String& texturePath, const PhysicalData& data, const Yaml& config) :
Sprite(config, data), Sprite(config, data),
mCollection(collection),
mPathfinder(pathfinder),
mWorld(world),
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(instances, *this, Yaml("weapon.yaml")), mWeapon(world, collection, *this, Yaml("weapon.yaml")),
mInstances(instances),
mStartPathfinding(false) { mStartPathfinding(false) {
mCharacterInstances.push_back(this); mCharacterInstances.push_back(this);
} }
/** /**
* Deletes pointer from static variable mCharacterInstances, inserts body into world * Deletes pointer from static variable mCharacterInstances, inserts body into world.
* (done here to avoid altering Box2D data during timestep).
*/ */
Character::~Character() { Character::~Character() {
auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this); auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this);
assert(it != mCharacterInstances.end()); assert(it != mCharacterInstances.end());
mCharacterInstances.erase(it); mCharacterInstances.erase(it);
mInstances.collection.insert(std::shared_ptr<Sprite>(new Corpse(mInstances.world, mCollection.insert(std::shared_ptr<Sprite>(new Corpse(mWorld,
getPosition(), Yaml("body.yaml")))); getPosition(), Yaml("body.yaml"))));
} }
@ -118,7 +119,7 @@ Character::fire() {
*/ */
bool bool
Character::setDestination(const Vector2f& destination) { Character::setDestination(const Vector2f& destination) {
mPath = mInstances.pathfinder.getPath(*this, destination); mPath = mPathfinder.getPath(*this, destination);
// Make sure we found a path. // Make sure we found a path.
if (mPath.empty()) { if (mPath.empty()) {
LOG_I("No path found to destination."); LOG_I("No path found to destination.");

View file

@ -11,14 +11,17 @@
#include <vector> #include <vector>
#include "Sprite.h" #include "Sprite.h"
#include "../World.h"
#include "../items/Weapon.h" #include "../items/Weapon.h"
#include "../types/Instances.h"
#include "../types/String.h" #include "../types/String.h"
#include "../util/Pathfinder.h"
#include "../util/Yaml.h" #include "../util/Yaml.h"
class World;
class Weapon; class Weapon;
class Instances; class Instances;
class Sprite; class Sprite;
class Pathfinder;
class Yaml; class Yaml;
/** /**
@ -27,8 +30,8 @@ class Yaml;
class Character : public Sprite { class Character : public Sprite {
// Public functions. // Public functions.
public: public:
Character(const Instances& instances, const String& texturePath, Character(World& world, Collection& collection, Pathfinder& pathfinder,
const PhysicalData& data, const Yaml& config); const String& texturePath, const PhysicalData& data, const Yaml& config);
virtual ~Character() = 0; virtual ~Character() = 0;
static void think(float elapsedTime); static void think(float elapsedTime);
@ -55,11 +58,14 @@ private:
static std::vector<Character*> mCharacterInstances; static std::vector<Character*> mCharacterInstances;
Collection& mCollection;
Pathfinder& mPathfinder;
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; Weapon mWeapon;
Instances mInstances;
std::vector<Vector2f> mPath; //< Contains nodes to reach a set destination. std::vector<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.
}; };

View file

@ -19,10 +19,10 @@ const String Weapon::DEFAULT_BULLET = "bullet.yaml";
const String Weapon::KEY_INTERVAL = "interval"; const String Weapon::KEY_INTERVAL = "interval";
const int Weapon::DEFAULT_INTERVAL = 250; const int Weapon::DEFAULT_INTERVAL = 250;
Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) : Weapon::Weapon(World& world, Collection& collection, Body& holder, const Yaml& config) :
Emitter(instances.collection), Emitter(collection),
mHolder(holder), mHolder(holder),
mWorld(instances.world), mWorld(world),
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)), mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) { mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
Vector2f holderSize = mHolder.getSize(); Vector2f holderSize = mHolder.getSize();

View file

@ -12,7 +12,6 @@
#include "../abstract/Body.h" #include "../abstract/Body.h"
#include "../particle/Emitter.h" #include "../particle/Emitter.h"
#include "../types/Instances.h"
#include "../util/Timer.h" #include "../util/Timer.h"
#include "../util/Yaml.h" #include "../util/Yaml.h"
@ -30,7 +29,7 @@ class Yaml;
class Weapon : public Emitter { class Weapon : public Emitter {
// Public functions. // Public functions.
public: public:
Weapon(const Instances& instances, Body& holder, const Yaml& config); Weapon(World& world, Collection& collection, Body& holder, const Yaml& config);
void fire(); void fire();

View file

@ -9,11 +9,14 @@
#include "Corpse.h" #include "Corpse.h"
Enemy::Enemy(const Instances& instances, const Vector2f& position, const Yaml& config) : Enemy::Enemy(World& world, Collection& collection, Pathfinder& pathfinder,
Character(instances, "enemy.png", PhysicalData(position, instances.world, const Vector2f& position, const Yaml& config) :
CATEGORY_ACTOR, MASK_ALL, true, false, true), config), Character(world, collection, pathfinder, "enemy.png",
mWorld(instances.world), PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL,
mCollection(instances.collection) { true, false, true),
config),
mWorld(world),
mCollection(collection) {
} }
void void

View file

@ -8,12 +8,13 @@
#ifndef DG_ENEMY_H_ #ifndef DG_ENEMY_H_
#define DG_ENEMY_H #define DG_ENEMY_H
#include "../types/Instances.h" #include "../World.h"
#include "../abstract/Character.h" #include "../abstract/Character.h"
#include "../util/Collection.h" #include "../util/Collection.h"
#include "../types/Vector.h" #include "../types/Vector.h"
#include "../util/Yaml.h" #include "../util/Yaml.h"
class World;
class Character; class Character;
class Collection; class Collection;
class Instances; class Instances;
@ -22,7 +23,8 @@ class Yaml;
class Enemy : public Character { class Enemy : public Character {
// Public functions. // Public functions.
public: public:
Enemy(const Instances& instances, const Vector2f& position, const Yaml& config); Enemy(World& world, Collection& collection, Pathfinder& pathfinder,
const Vector2f& position, const Yaml& config);
// Private functions. // Private functions.
private: private:

View file

@ -16,9 +16,12 @@
/** /**
* Initializes Sprite. * Initializes Sprite.
*/ */
Player::Player(const Instances& instances, const Vector2f& position, const Yaml& config) : Player::Player(World& world, Collection& collection, Pathfinder& pathfinder,
Character(instances, "player.png", PhysicalData(position, instances.world, const Vector2f& position, const Yaml& config) :
CATEGORY_ACTOR, MASK_ALL, true, false, true), config), Character(world, collection, pathfinder, "player.png",
PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL, true,
false, true),
config),
mDirection(0) { mDirection(0) {
} }

View file

@ -13,7 +13,6 @@
#include "../abstract/Character.h" #include "../abstract/Character.h"
#include "../items/Weapon.h" #include "../items/Weapon.h"
#include "../types/Instances.h"
#include "../types/Vector.h" #include "../types/Vector.h"
#include "../util/Pathfinder.h" #include "../util/Pathfinder.h"
#include "../util/Yaml.h" #include "../util/Yaml.h"
@ -42,7 +41,8 @@ public:
// Public functions. // Public functions.
public: public:
Player(const Instances& instances, const Vector2f& position, const Yaml& config); Player(World& world, Collection& collection, Pathfinder& pathfinder,
const Vector2f& position, const Yaml& config);
void setCrosshairPosition(const Vector2f& position); void setCrosshairPosition(const Vector2f& position);
void fire(); void fire();