Added Instances class to pass major objects in c'tors.

This commit is contained in:
Felix Ableitner 2012-10-04 19:10:12 +02:00
parent f4bb9a5dae
commit cdeac690f2
8 changed files with 61 additions and 21 deletions

View file

@ -9,6 +9,7 @@
#include <Thor/Graphics.hpp> #include <Thor/Graphics.hpp>
#include "Instances.h"
#include "abstract/Character.h" #include "abstract/Character.h"
#include "sprite/Cover.h" #include "sprite/Cover.h"
#include "sprite/Enemy.h" #include "sprite/Enemy.h"
@ -58,12 +59,13 @@ 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);
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f), Instances instances(mPathfinder, mTileManager, mCollection, mWorld);
mCollection)));
mCollection.insert(std::shared_ptr<Sprite>(new Enemy(instances, Vector2f(400.0f, 200.0f))));
mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(300, 200), Vector2i(100, 150), mCollection.insert(std::shared_ptr<Sprite>(new Cover(Vector2f(300, 200), Vector2i(100, 150),
mWorld))); mWorld)));
mPlayer = std::unique_ptr<Player>(new Player(mWorld, mCollection, Vector2f(200.0f, 100.0f), mPathfinder)); mPlayer = std::unique_ptr<Player>(new Player(instances, Vector2f(200.0f, 100.0f)));
} }
/** /**
* Closes window. * Closes window.

35
source/Instances.h Normal file
View file

@ -0,0 +1,35 @@
/*
* Instances.h
*
* Created on: 04.10.2012
* Author: Felix
*/
#ifndef DG_INSTANCES_H_
#define DG_INSTANCES_H_
#include <Box2D/Box2D.h>
#include "Pathfinder.h"
#include "TileManager.h"
#include "util/Collection.h"
class Pathfinder;
class TileManager;
class Collection;
/**
* POD class that holds instances of major classes used by other objects.
*/
struct Instances {
Instances() = default;
Instances(Pathfinder& p, TileManager& t, Collection& c, b2World& w) :
pathfinder(p), tilemanager(t), collection(c), world(w) {};
Pathfinder& pathfinder;
TileManager& tilemanager;
Collection& collection;
b2World& world;
};
#endif /* DG_INSTANCES_H_ */

View file

@ -18,13 +18,12 @@
const int Weapon::BULLET_DAMAGE = 10; const int Weapon::BULLET_DAMAGE = 10;
Weapon::Weapon(Physical& holder, Collection& collection, b2World& world, Weapon::Weapon(const Instances& instances, Physical& holder, const Vector2i& holderSize) :
const Vector2i& holderSize) : Emitter(instances.collection),
Emitter(collection),
mHolder(holder), mHolder(holder),
mBulletTexture(ResourceManager::i() mBulletTexture(ResourceManager::i()
.acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))), .acquire(Loader::i().fromFile<sf::Texture>("bullet.png"))),
mWorld(world), mWorld(instances.world),
mOffset(0, std::max(holderSize.x, holderSize.y) / 2 + mOffset(0, std::max(holderSize.x, holderSize.y) / 2 +
b2_linearSlop + b2_linearSlop +
std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2) { std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2) {

View file

@ -10,11 +10,13 @@
#include <Thor/Particles.hpp> #include <Thor/Particles.hpp>
#include "../Instances.h"
#include "../abstract/Physical.h" #include "../abstract/Physical.h"
#include "../particle/Emitter.h" #include "../particle/Emitter.h"
class Physical;
class Emitter; class Emitter;
class Instances;
class Physical;
/** /**
* Loading mechanism: * Loading mechanism:
@ -24,7 +26,7 @@ class Emitter;
class Weapon : public Emitter { class Weapon : public Emitter {
// Public functions. // Public functions.
public: public:
Weapon(Physical& holder, Collection& collection, b2World& world, const Vector2i& holderSize); Weapon(const Instances& instances, Physical& holder, const Vector2i& holderSize);
~Weapon(); ~Weapon();
void fire(); void fire();

View file

@ -9,12 +9,11 @@
#include "Body.h" #include "Body.h"
Enemy::Enemy(b2World& world, const Vector2f& position, Collection& collection) : Enemy::Enemy(const Instances& instances, const Vector2f& position) :
Character("enemy.png", PhysicalData(position, Vector2i(50, 50), world, Character("enemy.png", PhysicalData(position, Vector2i(50, 50), instances.world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), 100), CATEGORY_ACTOR, MASK_ALL, true, false, true), 100),
mWorld(world), mWorld(instances.world),
mCollection(collection) { mCollection(instances.collection) {
} }
Enemy::~Enemy() { Enemy::~Enemy() {

View file

@ -8,17 +8,19 @@
#ifndef DG_ENEMY_H_ #ifndef DG_ENEMY_H_
#define DG_ENEMY_H #define DG_ENEMY_H
#include "../Instances.h"
#include "../abstract/Character.h" #include "../abstract/Character.h"
#include "../util/Collection.h" #include "../util/Collection.h"
#include "../util/Vector.h" #include "../util/Vector.h"
class Character; class Character;
class Collection; class Collection;
class Instances;
class Enemy : public Character { class Enemy : public Character {
// Public functions. // Public functions.
public: public:
Enemy(b2World& world, const Vector2f& position, Collection& collection); Enemy(const Instances& instances, const Vector2f& position);
~Enemy(); ~Enemy();
// Private functions. // Private functions.

View file

@ -19,13 +19,12 @@ const float Player::POINT_REACHED_DISTANCE = 1.0f;
/** /**
* Initializes Sprite. * Initializes Sprite.
*/ */
Player::Player(b2World& world, Collection& collection, const Vector2f& position, Player::Player(const Instances& instances, const Vector2f& position) :
Pathfinder& pathfinder) : Character("player.png", PhysicalData(position, SIZE, instances.world,
Character("player.png", PhysicalData(position, SIZE, world,
CATEGORY_ACTOR, MASK_ALL, true, false, true), 100), CATEGORY_ACTOR, MASK_ALL, true, false, true), 100),
mWeapon(*this, collection, world, SIZE), mWeapon(instances, *this, SIZE),
mDirection(0), mDirection(0),
mPathfinder(pathfinder) { mPathfinder(instances.pathfinder) {
} }
/** /**

View file

@ -11,12 +11,14 @@
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "../Instances.h"
#include "../Pathfinder.h" #include "../Pathfinder.h"
#include "../abstract/Character.h" #include "../abstract/Character.h"
#include "../items/Weapon.h" #include "../items/Weapon.h"
#include "../util/Vector.h" #include "../util/Vector.h"
class Character; class Character;
class Instances;
class Pathfinder; class Pathfinder;
class Weapon; class Weapon;
@ -38,7 +40,7 @@ public:
// Public functions. // Public functions.
public: public:
Player(b2World& world, Collection& collection, const Vector2f& position, Pathfinder& pathfinder); Player(const Instances& instances, const Vector2f& position);
void setCrosshairPosition(const Vector2f& position); void setCrosshairPosition(const Vector2f& position);
void fire(); void fire();