2012-10-01 09:02:44 +00:00
|
|
|
/*
|
|
|
|
* Actor.cpp
|
|
|
|
*
|
|
|
|
* Created on: 02.09.2012
|
|
|
|
* Author: Felix
|
2012-09-12 12:21:57 +00:00
|
|
|
*/
|
2012-10-01 09:02:44 +00:00
|
|
|
|
|
|
|
#include "Character.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2012-10-04 17:24:46 +00:00
|
|
|
#include "../sprite/Body.h"
|
|
|
|
|
2012-10-12 17:34:07 +00:00
|
|
|
const String Character::KEY_HEALTH = "health";
|
2012-10-04 17:24:46 +00:00
|
|
|
std::vector<Character*> Character::mCharacterInstances = std::vector<Character*>();
|
2012-10-01 09:02:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves pointer to this instance in static var for think().
|
|
|
|
*/
|
2012-10-11 21:24:22 +00:00
|
|
|
Character::Character(const Instances& instances, const String& texturePath,
|
2012-10-12 17:34:07 +00:00
|
|
|
const PhysicalData& data, const Yaml& config) :
|
2012-10-12 18:59:33 +00:00
|
|
|
Sprite(config, data),
|
2012-10-12 17:34:07 +00:00
|
|
|
mMaxHealth(config.get<int>(KEY_HEALTH)),
|
|
|
|
mCurrentHealth(mMaxHealth),
|
2012-10-04 17:24:46 +00:00
|
|
|
mInstances(instances) {
|
|
|
|
mCharacterInstances.push_back(this);
|
2012-10-01 09:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-04 17:24:46 +00:00
|
|
|
* Deletes pointer from static variable mCharacterInstances, inserts body into world
|
|
|
|
* (done here to avoid altering Box2D data during timestep).
|
2012-10-01 09:02:44 +00:00
|
|
|
*/
|
|
|
|
Character::~Character() {
|
2012-10-04 17:24:46 +00:00
|
|
|
auto it = std::find(mCharacterInstances.begin(), mCharacterInstances.end(), this);
|
|
|
|
assert(it != mCharacterInstances.end());
|
|
|
|
mCharacterInstances.erase(it);
|
|
|
|
|
|
|
|
mInstances.collection.insert(std::shared_ptr<Sprite>(new Body(mInstances.world,
|
2012-10-12 17:34:07 +00:00
|
|
|
getPosition(), Yaml("body.yaml"))));
|
2012-10-01 09:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls onThink on all Actor instances.
|
|
|
|
*
|
|
|
|
* @param elapsedTime Amount of time to simulate.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::think(float elapsedTime) {
|
2012-10-04 17:24:46 +00:00
|
|
|
for (auto i : mCharacterInstances) {
|
2012-10-01 09:02:44 +00:00
|
|
|
i->onThink(elapsedTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-04 17:30:50 +00:00
|
|
|
* Subtracts health from Actor. Calls onDeath() when health reaches zero and marks
|
|
|
|
* object for deletion.
|
2012-10-01 09:02:44 +00:00
|
|
|
*
|
|
|
|
* @param damage Amount of health to subtract.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onDamage(int damage) {
|
|
|
|
mCurrentHealth -= damage;
|
|
|
|
if (mCurrentHealth <= 0) {
|
|
|
|
mCurrentHealth = 0;
|
|
|
|
onDeath();
|
2012-10-04 17:30:50 +00:00
|
|
|
setDelete(true);
|
2012-10-01 09:02:44 +00:00
|
|
|
}
|
2012-09-12 12:21:57 +00:00
|
|
|
}
|
2012-10-01 09:02:44 +00:00
|
|
|
|
2012-10-04 17:36:05 +00:00
|
|
|
/**
|
|
|
|
* Implement this function for any (regular) AI computations. Default implementation does nothing.
|
|
|
|
*
|
|
|
|
* @param elapsedTime Amount of time to simulate.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onThink(float elapsedTime) {
|
|
|
|
}
|
|
|
|
|
2012-10-01 09:02:44 +00:00
|
|
|
/**
|
2012-10-04 17:30:50 +00:00
|
|
|
* Called when health reaches zero. Default immplementation does nothing.
|
2012-10-01 09:02:44 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
Character::onDeath() {
|
2012-09-12 12:21:57 +00:00
|
|
|
}
|