Added health to actor, bullets can damage and kill.
This commit is contained in:
parent
4419a76dfa
commit
b0d2fe7a1f
5 changed files with 41 additions and 4 deletions
|
@ -15,7 +15,9 @@ std::vector<Actor*> Actor::mInstances = std::vector<Actor*>();
|
||||||
/**
|
/**
|
||||||
* Saves pointer to this instance in static var for think().
|
* Saves pointer to this instance in static var for think().
|
||||||
*/
|
*/
|
||||||
Actor::Actor() {
|
Actor::Actor(int health) :
|
||||||
|
mMaxHealth(health),
|
||||||
|
mCurrentHealth(health) {
|
||||||
mInstances.push_back(this);
|
mInstances.push_back(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,3 +41,24 @@ Actor::think(float elapsedTime) {
|
||||||
i->onThink(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() {
|
||||||
|
}
|
||||||
|
|
|
@ -16,11 +16,13 @@
|
||||||
class Actor {
|
class Actor {
|
||||||
// Public functions.
|
// Public functions.
|
||||||
public:
|
public:
|
||||||
Actor();
|
Actor(int health);
|
||||||
virtual ~Actor() = 0;
|
virtual ~Actor() = 0;
|
||||||
|
|
||||||
static void think(float elapsedTime);
|
static void think(float elapsedTime);
|
||||||
|
|
||||||
|
void onDamage(int damage);
|
||||||
|
|
||||||
// Protected functions.
|
// Protected functions.
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
|
@ -30,9 +32,14 @@ protected:
|
||||||
*/
|
*/
|
||||||
virtual void onThink(float elapsedTime) = 0;
|
virtual void onThink(float elapsedTime) = 0;
|
||||||
|
|
||||||
|
virtual void onDeath();
|
||||||
|
|
||||||
// Private variables.
|
// Private variables.
|
||||||
private:
|
private:
|
||||||
static std::vector<Actor*> mInstances;
|
static std::vector<Actor*> mInstances;
|
||||||
|
|
||||||
|
const int mMaxHealth;
|
||||||
|
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* DG_ACTOR_H_ */
|
#endif /* DG_ACTOR_H_ */
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include "Bullet.h"
|
#include "Bullet.h"
|
||||||
|
|
||||||
|
#include "../abstract/Actor.h"
|
||||||
#include "../util/Log.h"
|
#include "../util/Log.h"
|
||||||
|
|
||||||
const float Bullet::SPEED = 500.0f;
|
const float Bullet::SPEED = 500.0f;
|
||||||
|
@ -35,6 +36,10 @@ Bullet::onCollide(Physical& other, uint16 type) {
|
||||||
// Make sure we do not damage twice.
|
// Make sure we do not damage twice.
|
||||||
if (!getDelete()) {
|
if (!getDelete()) {
|
||||||
// Call onShot on other, with damage as param.
|
// Call onShot on other, with damage as param.
|
||||||
|
if (type == CATEGORY_ACTOR) {
|
||||||
|
Actor& a = dynamic_cast<Actor&>(other);
|
||||||
|
a.onDamage(10);
|
||||||
|
}
|
||||||
setDelete(true);
|
setDelete(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,9 @@
|
||||||
#include "Enemy.h"
|
#include "Enemy.h"
|
||||||
|
|
||||||
Enemy::Enemy(b2World& world, const Vector2f& position) :
|
Enemy::Enemy(b2World& world, const Vector2f& position) :
|
||||||
Sprite("enemy.png", PhysicalData(position, Vector2i(50, 50), world,
|
Sprite("enemy.png", PhysicalData(position, Vector2i(50, 50), world,
|
||||||
CATEGORY_ACTOR, MASK_ALL, true)) {
|
CATEGORY_ACTOR, MASK_ALL, true)),
|
||||||
|
Actor(100) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ const float Player::SPEED = 100.0f;
|
||||||
Player::Player(b2World& world, Collection& collection, const Vector2f& position) :
|
Player::Player(b2World& world, Collection& collection, const Vector2f& position) :
|
||||||
Sprite("player.png", PhysicalData(position, Vector2i(50, 50), world,
|
Sprite("player.png", PhysicalData(position, Vector2i(50, 50), world,
|
||||||
CATEGORY_ACTOR, MASK_ALL, true)),
|
CATEGORY_ACTOR, MASK_ALL, true)),
|
||||||
|
Actor(100),
|
||||||
mWeapon(*this, collection, world),
|
mWeapon(*this, collection, world),
|
||||||
mDestination(Vector2i(50, 50)) {
|
mDestination(Vector2i(50, 50)) {
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue