Removed Instances class.
This commit is contained in:
parent
d923e94541
commit
402cd138d3
9 changed files with 47 additions and 34 deletions
|
@ -11,7 +11,6 @@
|
|||
|
||||
#include "abstract/Character.h"
|
||||
#include "sprites/Enemy.h"
|
||||
#include "types/Instances.h"
|
||||
#include "types/String.h"
|
||||
#include "util/Loader.h"
|
||||
#include "util/ResourceManager.h"
|
||||
|
@ -50,11 +49,11 @@ Game::generate() {
|
|||
for (int x = 1; x < 5; x++)
|
||||
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(instances, Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
|
||||
mPlayer = std::unique_ptr<Player>(new Player(mWorld, mCollection, mPathfinder,
|
||||
Vector2f(200.0f, 100.0f), Yaml("player.yaml")));
|
||||
}
|
||||
/**
|
||||
* Closes window.
|
||||
|
|
|
@ -25,28 +25,29 @@ std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>
|
|||
/**
|
||||
* Saves pointer to this instance in static var for think().
|
||||
*/
|
||||
Character::Character(const Instances& instances, const String& texturePath,
|
||||
const PhysicalData& data, const Yaml& config) :
|
||||
Character::Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const String& texturePath, const PhysicalData& data, const Yaml& config) :
|
||||
Sprite(config, data),
|
||||
mCollection(collection),
|
||||
mPathfinder(pathfinder),
|
||||
mWorld(world),
|
||||
mMaxHealth(config.get(KEY_HEALTH, DEFAULT_HEALTH)),
|
||||
mCurrentHealth(mMaxHealth),
|
||||
mMovementSpeed(config.get(KEY_SPEED, DEFAULT_SPEED)),
|
||||
mWeapon(instances, *this, Yaml("weapon.yaml")),
|
||||
mInstances(instances),
|
||||
mWeapon(world, collection, *this, Yaml("weapon.yaml")),
|
||||
mStartPathfinding(false) {
|
||||
mCharacterInstances.push_back(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes pointer from static variable mCharacterInstances, inserts body into world
|
||||
* (done here to avoid altering Box2D data during timestep).
|
||||
* Deletes pointer from static variable mCharacterInstances, inserts body into world.
|
||||
*/
|
||||
Character::~Character() {
|
||||
auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this);
|
||||
assert(it != mCharacterInstances.end());
|
||||
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"))));
|
||||
}
|
||||
|
||||
|
@ -118,7 +119,7 @@ Character::fire() {
|
|||
*/
|
||||
bool
|
||||
Character::setDestination(const Vector2f& destination) {
|
||||
mPath = mInstances.pathfinder.getPath(*this, destination);
|
||||
mPath = mPathfinder.getPath(*this, destination);
|
||||
// Make sure we found a path.
|
||||
if (mPath.empty()) {
|
||||
LOG_I("No path found to destination.");
|
||||
|
|
|
@ -11,14 +11,17 @@
|
|||
#include <vector>
|
||||
|
||||
#include "Sprite.h"
|
||||
#include "../World.h"
|
||||
#include "../items/Weapon.h"
|
||||
#include "../types/Instances.h"
|
||||
#include "../types/String.h"
|
||||
#include "../util/Pathfinder.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
||||
class World;
|
||||
class Weapon;
|
||||
class Instances;
|
||||
class Sprite;
|
||||
class Pathfinder;
|
||||
class Yaml;
|
||||
|
||||
/**
|
||||
|
@ -27,8 +30,8 @@ class Yaml;
|
|||
class Character : public Sprite {
|
||||
// Public functions.
|
||||
public:
|
||||
Character(const Instances& instances, const String& texturePath,
|
||||
const PhysicalData& data, const Yaml& config);
|
||||
Character(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const String& texturePath, const PhysicalData& data, const Yaml& config);
|
||||
virtual ~Character() = 0;
|
||||
|
||||
static void think(float elapsedTime);
|
||||
|
@ -55,11 +58,14 @@ private:
|
|||
|
||||
static std::vector<Character*> mCharacterInstances;
|
||||
|
||||
Collection& mCollection;
|
||||
Pathfinder& mPathfinder;
|
||||
World& mWorld;
|
||||
|
||||
const int mMaxHealth;
|
||||
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
|
||||
const float mMovementSpeed;
|
||||
Weapon mWeapon;
|
||||
Instances mInstances;
|
||||
std::vector<Vector2f> mPath; //< Contains nodes to reach a set destination.
|
||||
bool mStartPathfinding; //< True if a movement destination was just set.
|
||||
};
|
||||
|
|
|
@ -19,10 +19,10 @@ const String Weapon::DEFAULT_BULLET = "bullet.yaml";
|
|||
const String Weapon::KEY_INTERVAL = "interval";
|
||||
const int Weapon::DEFAULT_INTERVAL = 250;
|
||||
|
||||
Weapon::Weapon(const Instances& instances, Body& holder, const Yaml& config) :
|
||||
Emitter(instances.collection),
|
||||
Weapon::Weapon(World& world, Collection& collection, Body& holder, const Yaml& config) :
|
||||
Emitter(collection),
|
||||
mHolder(holder),
|
||||
mWorld(instances.world),
|
||||
mWorld(world),
|
||||
mBullet(config.get(KEY_BULLET, DEFAULT_BULLET)),
|
||||
mTimer(sf::milliseconds(config.get(KEY_INTERVAL, DEFAULT_INTERVAL))) {
|
||||
Vector2f holderSize = mHolder.getSize();
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
|
||||
#include "../abstract/Body.h"
|
||||
#include "../particle/Emitter.h"
|
||||
#include "../types/Instances.h"
|
||||
#include "../util/Timer.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
||||
|
@ -30,7 +29,7 @@ class Yaml;
|
|||
class Weapon : public Emitter {
|
||||
// Public functions.
|
||||
public:
|
||||
Weapon(const Instances& instances, Body& holder, const Yaml& config);
|
||||
Weapon(World& world, Collection& collection, Body& holder, const Yaml& config);
|
||||
|
||||
void fire();
|
||||
|
||||
|
|
|
@ -9,11 +9,14 @@
|
|||
|
||||
#include "Corpse.h"
|
||||
|
||||
Enemy::Enemy(const Instances& instances, const Vector2f& position, const Yaml& config) :
|
||||
Character(instances, "enemy.png", PhysicalData(position, instances.world,
|
||||
CATEGORY_ACTOR, MASK_ALL, true, false, true), config),
|
||||
mWorld(instances.world),
|
||||
mCollection(instances.collection) {
|
||||
Enemy::Enemy(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config) :
|
||||
Character(world, collection, pathfinder, "enemy.png",
|
||||
PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL,
|
||||
true, false, true),
|
||||
config),
|
||||
mWorld(world),
|
||||
mCollection(collection) {
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -8,12 +8,13 @@
|
|||
#ifndef DG_ENEMY_H_
|
||||
#define DG_ENEMY_H
|
||||
|
||||
#include "../types/Instances.h"
|
||||
#include "../World.h"
|
||||
#include "../abstract/Character.h"
|
||||
#include "../util/Collection.h"
|
||||
#include "../types/Vector.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
||||
class World;
|
||||
class Character;
|
||||
class Collection;
|
||||
class Instances;
|
||||
|
@ -22,7 +23,8 @@ class Yaml;
|
|||
class Enemy : public Character {
|
||||
// Public functions.
|
||||
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:
|
||||
|
|
|
@ -16,9 +16,12 @@
|
|||
/**
|
||||
* Initializes Sprite.
|
||||
*/
|
||||
Player::Player(const Instances& instances, const Vector2f& position, const Yaml& config) :
|
||||
Character(instances, "player.png", PhysicalData(position, instances.world,
|
||||
CATEGORY_ACTOR, MASK_ALL, true, false, true), config),
|
||||
Player::Player(World& world, Collection& collection, Pathfinder& pathfinder,
|
||||
const Vector2f& position, const Yaml& config) :
|
||||
Character(world, collection, pathfinder, "player.png",
|
||||
PhysicalData(position, world, CATEGORY_ACTOR, MASK_ALL, true,
|
||||
false, true),
|
||||
config),
|
||||
mDirection(0) {
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
|
||||
#include "../abstract/Character.h"
|
||||
#include "../items/Weapon.h"
|
||||
#include "../types/Instances.h"
|
||||
#include "../types/Vector.h"
|
||||
#include "../util/Pathfinder.h"
|
||||
#include "../util/Yaml.h"
|
||||
|
@ -42,7 +41,8 @@ public:
|
|||
|
||||
// Public functions.
|
||||
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 fire();
|
||||
|
|
Reference in a new issue