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().
|
||||
*/
|
||||
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() {
|
||||
}
|
||||
|
|
|
@ -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<Actor*> mInstances;
|
||||
|
||||
const int mMaxHealth;
|
||||
int mCurrentHealth; //< Current health. Between 0 and mMaxHealth.
|
||||
};
|
||||
|
||||
#endif /* DG_ACTOR_H_ */
|
||||
|
|
|
@ -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<Actor&>(other);
|
||||
a.onDamage(10);
|
||||
}
|
||||
setDelete(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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)) {
|
||||
}
|
||||
|
|
Reference in a new issue