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 "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<Sprite>(new Enemy(mWorld, Vector2f(400.0f, 200.0f),
mCollection)));
Instances instances(mPathfinder, mTileManager, mCollection, mWorld);
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),
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.

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;
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<sf::Texture>("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) {

View file

@ -10,11 +10,13 @@
#include <Thor/Particles.hpp>
#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();

View file

@ -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() {

View file

@ -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.

View file

@ -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) {
}
/**

View file

@ -11,12 +11,14 @@
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#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();