From cdeac690f2144a8fb3918bb856c3cc163087a438 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 4 Oct 2012 19:10:12 +0200 Subject: [PATCH] Added Instances class to pass major objects in c'tors. --- source/Game.cpp | 8 +++++--- source/Instances.h | 35 +++++++++++++++++++++++++++++++++++ source/items/Weapon.cpp | 7 +++---- source/items/Weapon.h | 6 ++++-- source/sprite/Enemy.cpp | 9 ++++----- source/sprite/Enemy.h | 4 +++- source/sprite/Player.cpp | 9 ++++----- source/sprite/Player.h | 4 +++- 8 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 source/Instances.h diff --git a/source/Game.cpp b/source/Game.cpp index 43a0c68..470dd6c 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -9,6 +9,7 @@ #include +#include "Instances.h" #include "abstract/Character.h" #include "sprite/Cover.h" #include "sprite/Enemy.h" @@ -58,12 +59,13 @@ Game::generate() { for (int x = 1; x < 5; x++) mTileManager.setTile(TileManager::TilePosition(x, 4), TileManager::Type::WALL); - mCollection.insert(std::shared_ptr(new Enemy(mWorld, Vector2f(400.0f, 200.0f), - mCollection))); + Instances instances(mPathfinder, mTileManager, mCollection, mWorld); + + mCollection.insert(std::shared_ptr(new Enemy(instances, Vector2f(400.0f, 200.0f)))); mCollection.insert(std::shared_ptr(new Cover(Vector2f(300, 200), Vector2i(100, 150), mWorld))); - mPlayer = std::unique_ptr(new Player(mWorld, mCollection, Vector2f(200.0f, 100.0f), mPathfinder)); + mPlayer = std::unique_ptr(new Player(instances, Vector2f(200.0f, 100.0f))); } /** * Closes window. diff --git a/source/Instances.h b/source/Instances.h new file mode 100644 index 0000000..df5ff0b --- /dev/null +++ b/source/Instances.h @@ -0,0 +1,35 @@ +/* + * Instances.h + * + * Created on: 04.10.2012 + * Author: Felix + */ + +#ifndef DG_INSTANCES_H_ +#define DG_INSTANCES_H_ + +#include + +#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_ */ diff --git a/source/items/Weapon.cpp b/source/items/Weapon.cpp index 5773c3b..338d26f 100755 --- a/source/items/Weapon.cpp +++ b/source/items/Weapon.cpp @@ -18,13 +18,12 @@ const int Weapon::BULLET_DAMAGE = 10; -Weapon::Weapon(Physical& holder, Collection& collection, b2World& world, - const Vector2i& holderSize) : - Emitter(collection), +Weapon::Weapon(const Instances& instances, Physical& holder, const Vector2i& holderSize) : + Emitter(instances.collection), mHolder(holder), mBulletTexture(ResourceManager::i() .acquire(Loader::i().fromFile("bullet.png"))), - mWorld(world), + mWorld(instances.world), mOffset(0, std::max(holderSize.x, holderSize.y) / 2 + b2_linearSlop + std::max(Bullet::SIZE.x, Bullet::SIZE.y) / 2) { diff --git a/source/items/Weapon.h b/source/items/Weapon.h index 76d43ab..07a1e70 100755 --- a/source/items/Weapon.h +++ b/source/items/Weapon.h @@ -10,11 +10,13 @@ #include +#include "../Instances.h" #include "../abstract/Physical.h" #include "../particle/Emitter.h" -class Physical; class Emitter; +class Instances; +class Physical; /** * Loading mechanism: @@ -24,7 +26,7 @@ class Emitter; class Weapon : public Emitter { // Public functions. public: - Weapon(Physical& holder, Collection& collection, b2World& world, const Vector2i& holderSize); + Weapon(const Instances& instances, Physical& holder, const Vector2i& holderSize); ~Weapon(); void fire(); diff --git a/source/sprite/Enemy.cpp b/source/sprite/Enemy.cpp index 1015c6c..a694426 100644 --- a/source/sprite/Enemy.cpp +++ b/source/sprite/Enemy.cpp @@ -9,12 +9,11 @@ #include "Body.h" -Enemy::Enemy(b2World& world, const Vector2f& position, Collection& collection) : - Character("enemy.png", PhysicalData(position, Vector2i(50, 50), world, +Enemy::Enemy(const Instances& instances, const Vector2f& position) : + Character("enemy.png", PhysicalData(position, Vector2i(50, 50), instances.world, CATEGORY_ACTOR, MASK_ALL, true, false, true), 100), - mWorld(world), - mCollection(collection) { - + mWorld(instances.world), + mCollection(instances.collection) { } Enemy::~Enemy() { diff --git a/source/sprite/Enemy.h b/source/sprite/Enemy.h index ed98562..e262067 100644 --- a/source/sprite/Enemy.h +++ b/source/sprite/Enemy.h @@ -8,17 +8,19 @@ #ifndef DG_ENEMY_H_ #define DG_ENEMY_H +#include "../Instances.h" #include "../abstract/Character.h" #include "../util/Collection.h" #include "../util/Vector.h" class Character; class Collection; +class Instances; class Enemy : public Character { // Public functions. public: - Enemy(b2World& world, const Vector2f& position, Collection& collection); + Enemy(const Instances& instances, const Vector2f& position); ~Enemy(); // Private functions. diff --git a/source/sprite/Player.cpp b/source/sprite/Player.cpp index 93fd7f4..0190e45 100644 --- a/source/sprite/Player.cpp +++ b/source/sprite/Player.cpp @@ -19,13 +19,12 @@ const float Player::POINT_REACHED_DISTANCE = 1.0f; /** * Initializes Sprite. */ -Player::Player(b2World& world, Collection& collection, const Vector2f& position, - Pathfinder& pathfinder) : - Character("player.png", PhysicalData(position, SIZE, world, +Player::Player(const Instances& instances, const Vector2f& position) : + Character("player.png", PhysicalData(position, SIZE, instances.world, CATEGORY_ACTOR, MASK_ALL, true, false, true), 100), - mWeapon(*this, collection, world, SIZE), + mWeapon(instances, *this, SIZE), mDirection(0), - mPathfinder(pathfinder) { + mPathfinder(instances.pathfinder) { } /** diff --git a/source/sprite/Player.h b/source/sprite/Player.h index aafef6f..52183f3 100644 --- a/source/sprite/Player.h +++ b/source/sprite/Player.h @@ -11,12 +11,14 @@ #include #include +#include "../Instances.h" #include "../Pathfinder.h" #include "../abstract/Character.h" #include "../items/Weapon.h" #include "../util/Vector.h" class Character; +class Instances; class Pathfinder; class Weapon; @@ -38,7 +40,7 @@ public: // Public functions. public: - Player(b2World& world, Collection& collection, const Vector2f& position, Pathfinder& pathfinder); + Player(const Instances& instances, const Vector2f& position); void setCrosshairPosition(const Vector2f& position); void fire();