From b0d2fe7a1fa3eecb2dcd811cc163b366bb61640c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 11 Sep 2012 16:48:28 +0200 Subject: [PATCH] Added health to actor, bullets can damage and kill. --- source/abstract/Actor.cpp | 25 ++++++++++++++++++++++++- source/abstract/Actor.h | 9 ++++++++- source/effects/Bullet.cpp | 5 +++++ source/sprite/Enemy.cpp | 5 +++-- source/sprite/Player.cpp | 1 + 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/source/abstract/Actor.cpp b/source/abstract/Actor.cpp index 2d4cc84..3e7318f 100755 --- a/source/abstract/Actor.cpp +++ b/source/abstract/Actor.cpp @@ -15,7 +15,9 @@ std::vector Actor::mInstances = std::vector(); /** * Saves pointer to this instance in static var for think(). */ -Actor::Actor() { +Actor::Actor(int health) : + mMaxHealth(health), + mCurrentHealth(health) { mInstances.push_back(this); } @@ -39,3 +41,24 @@ Actor::think(float elapsedTime) { i->onThink(elapsedTime); } } + +/** + * Subtracts health from Actor. + * + * @param damage Amount of health to subtract. + */ +void +Actor::onDamage(int damage) { + mCurrentHealth -= damage; + if (mCurrentHealth <= 0) { + mCurrentHealth = 0; + onDeath(); + } +} + +/** + * Called when health reaches zero. Does nothing by default. + */ +void +Actor::onDeath() { +} diff --git a/source/abstract/Actor.h b/source/abstract/Actor.h index d3781f8..50b9f68 100755 --- a/source/abstract/Actor.h +++ b/source/abstract/Actor.h @@ -16,11 +16,13 @@ class Actor { // Public functions. public: - Actor(); + Actor(int health); virtual ~Actor() = 0; static void think(float elapsedTime); + void onDamage(int damage); + // Protected functions. protected: /** @@ -30,9 +32,14 @@ protected: */ virtual void onThink(float elapsedTime) = 0; + virtual void onDeath(); + // Private variables. private: static std::vector mInstances; + + const int mMaxHealth; + int mCurrentHealth; //< Current health. Between 0 and mMaxHealth. }; #endif /* DG_ACTOR_H_ */ diff --git a/source/effects/Bullet.cpp b/source/effects/Bullet.cpp index 3abf1a5..224a5b0 100755 --- a/source/effects/Bullet.cpp +++ b/source/effects/Bullet.cpp @@ -7,6 +7,7 @@ #include "Bullet.h" +#include "../abstract/Actor.h" #include "../util/Log.h" const float Bullet::SPEED = 500.0f; @@ -35,6 +36,10 @@ Bullet::onCollide(Physical& other, uint16 type) { // Make sure we do not damage twice. if (!getDelete()) { // Call onShot on other, with damage as param. + if (type == CATEGORY_ACTOR) { + Actor& a = dynamic_cast(other); + a.onDamage(10); + } setDelete(true); } } diff --git a/source/sprite/Enemy.cpp b/source/sprite/Enemy.cpp index 24e74e2..2ce5aa7 100644 --- a/source/sprite/Enemy.cpp +++ b/source/sprite/Enemy.cpp @@ -8,8 +8,9 @@ #include "Enemy.h" Enemy::Enemy(b2World& world, const Vector2f& position) : -Sprite("enemy.png", PhysicalData(position, Vector2i(50, 50), world, - CATEGORY_ACTOR, MASK_ALL, true)) { + Sprite("enemy.png", PhysicalData(position, Vector2i(50, 50), world, + CATEGORY_ACTOR, MASK_ALL, true)), + Actor(100) { } diff --git a/source/sprite/Player.cpp b/source/sprite/Player.cpp index ec64937..62286be 100644 --- a/source/sprite/Player.cpp +++ b/source/sprite/Player.cpp @@ -20,6 +20,7 @@ const float Player::SPEED = 100.0f; Player::Player(b2World& world, Collection& collection, const Vector2f& position) : Sprite("player.png", PhysicalData(position, Vector2i(50, 50), world, CATEGORY_ACTOR, MASK_ALL, true)), + Actor(100), mWeapon(*this, collection, world), mDestination(Vector2i(50, 50)) { }