From 69eca24bd2cd5f9fb17c1e51019fe2176fc5be81 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 14 Sep 2012 20:33:32 +0200 Subject: [PATCH] Added Body class to be dropped by Enemy on death. --- source/Game.cpp | 2 +- source/abstract/Physical.cpp | 1 - source/abstract/Physical.h | 2 +- source/sprite/Body.cpp | 14 +++++++++ source/sprite/Body.h | 19 ++++++++++++ source/sprite/Enemy.cpp | 57 ++++++++++++++++++++-------------- source/sprite/Enemy.h | 60 ++++++++++++++++++++---------------- 7 files changed, 102 insertions(+), 53 deletions(-) create mode 100755 source/sprite/Body.cpp create mode 100755 source/sprite/Body.h diff --git a/source/Game.cpp b/source/Game.cpp index f97de06..2dbd85b 100644 --- a/source/Game.cpp +++ b/source/Game.cpp @@ -43,7 +43,7 @@ Game::Game(const Vector2i& resolution) : mCollection.insert(std::shared_ptr(new Cover(Vector2f(i, i), Vector2i(20, 20), mWorld)), Collection::LEVEL_STATIC); } - mCollection.insert(std::shared_ptr(new Enemy(mWorld, Vector2f(400.0f, 200.0f))), + mCollection.insert(std::shared_ptr(new Enemy(mWorld, Vector2f(400.0f, 200.0f), mCollection)), Collection::LEVEL_ACTOR); } diff --git a/source/abstract/Physical.cpp b/source/abstract/Physical.cpp index 2e50735..f285c10 100755 --- a/source/abstract/Physical.cpp +++ b/source/abstract/Physical.cpp @@ -19,7 +19,6 @@ Physical::Physical(const PhysicalData& data) : mDelete(false) { assert(data.size != Vector2i()); - assert(data.category); b2BodyDef bodyDef; bodyDef.type = (data.moving) diff --git a/source/abstract/Physical.h b/source/abstract/Physical.h index 08c8b47..dc35d1c 100755 --- a/source/abstract/Physical.h +++ b/source/abstract/Physical.h @@ -76,7 +76,7 @@ public: Vector2f getSpeed() const; float getAngle() const; bool getDelete() const; - uint16 getCategory() const; + uint16 getCategory() const; virtual bool doesCollide(Physical& other); virtual void onCollide(Physical& other, uint16 category); diff --git a/source/sprite/Body.cpp b/source/sprite/Body.cpp new file mode 100755 index 0000000..ef87618 --- /dev/null +++ b/source/sprite/Body.cpp @@ -0,0 +1,14 @@ +/* + * Body.cpp + * + * Created on: 13.09.2012 + * Author: Felix + */ + +#include "Body.h" + +Body::Body(b2World& world, const Vector2f& position) : + Sprite("body.png", PhysicalData(position, Vector2i(50, 50), world, + CATEGORY_NONSOLID, MASK_NONE, false)) { +} + diff --git a/source/sprite/Body.h b/source/sprite/Body.h new file mode 100755 index 0000000..2697886 --- /dev/null +++ b/source/sprite/Body.h @@ -0,0 +1,19 @@ +/* + * Body.h + * + * Created on: 13.09.2012 + * Author: Felix + */ + +#ifndef DG_BODY_H_ +#define DG_BODY_H_ + +#include "../abstract/Sprite.h" + +class Body : public Sprite { +// Public functions. +public: + Body(b2World& world, const Vector2f& position); +}; + +#endif /* DG_BODY_H_ */ diff --git a/source/sprite/Enemy.cpp b/source/sprite/Enemy.cpp index b061231..dd81dc7 100644 --- a/source/sprite/Enemy.cpp +++ b/source/sprite/Enemy.cpp @@ -1,23 +1,34 @@ -/* - * Enemy.cpp - * - * Created on: 10.09.2012 - * Author: Felix - */ - -#include "Enemy.h" - -Enemy::Enemy(b2World& world, const Vector2f& position) : - Sprite("enemy.png", PhysicalData(position, Vector2i(50, 50), world, - CATEGORY_ACTOR, MASK_ALL, true, false, true)), - Actor(100) { - -} - -Enemy::~Enemy() { -} - - -void -Enemy::onThink(float elapsedTime) { -} +/* + * Enemy.cpp + * + * Created on: 10.09.2012 + * Author: Felix + */ + +#include "Enemy.h" + +#include "Body.h" + +Enemy::Enemy(b2World& world, const Vector2f& position, Collection& collection) : + Sprite("enemy.png", PhysicalData(position, Vector2i(50, 50), world, + CATEGORY_ACTOR, MASK_ALL, true, false, true)), + Actor(100), + mWorld(world), + mCollection(collection) { + +} + +Enemy::~Enemy() { + // Insert here to avoid altering b2d data during timestep. + mCollection.insert(std::shared_ptr(new Body(mWorld, getPosition())), + Collection::LEVEL_STATIC); +} + +void +Enemy::onThink(float elapsedTime) { +} + +void +Enemy::onDeath() { + setDelete(true); +} diff --git a/source/sprite/Enemy.h b/source/sprite/Enemy.h index bf6aaf7..fd5e53d 100644 --- a/source/sprite/Enemy.h +++ b/source/sprite/Enemy.h @@ -1,27 +1,33 @@ -/* - * Enemy.h - * - * Created on: 10.09.2012 - * Author: Felix - */ - -#ifndef DG_ENEMY_H_ -#define DG_ENEMY_H - -#include "../abstract/Actor.h" -#include "../abstract/Sprite.h" - -#include "../util/Vector.h" - -class Enemy : public Sprite, public Actor { -// Public functions. -public: - Enemy(b2World& world, const Vector2f& position); - ~Enemy(); - -// Private functions. -private: - void onThink(float elapsedTime); -}; - -#endif /* DG_ENEMY_H_ */ +/* + * Enemy.h + * + * Created on: 10.09.2012 + * Author: Felix + */ + +#ifndef DG_ENEMY_H_ +#define DG_ENEMY_H + +#include "../abstract/Actor.h" +#include "../abstract/Sprite.h" +#include "../util/Collection.h" +#include "../util/Vector.h" + +class Enemy : public Sprite, public Actor { +// Public functions. +public: + Enemy(b2World& world, const Vector2f& position, Collection& collection); + ~Enemy(); + +// Private functions. +private: + void onThink(float elapsedTime); + void onDeath(); + +// Private variablese. +private: + b2World& mWorld; + Collection& mCollection; +}; + +#endif /* DG_ENEMY_H_ */