Added teams so enemies don't attack each other.

This commit is contained in:
Felix Ableitner 2013-05-08 19:49:04 +02:00
parent 8ea278030d
commit 6418de7774
5 changed files with 32 additions and 8 deletions

View file

@ -5,3 +5,4 @@ health: 100
speed: 100.0 speed: 100.0
radius: 25.0 radius: 25.0
weapon: weapon.yaml weapon: weapon.yaml
faction: 1

View file

@ -5,3 +5,4 @@ health: 100
speed: 100.0 speed: 100.0
radius: 25.0 radius: 25.0
weapon: weapon.yaml weapon: weapon.yaml
faction: 0

View file

@ -32,7 +32,8 @@ Character::Character(const sf::Vector2f& position, Category category,
mMovementSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)), mMovementSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)),
mWeapon(new Weapon(world, *this, mWeapon(new Weapon(world, *this,
Yaml(config.get(YAML_KEY::WEAPON, YAML_DEFAULT::WEAPON)))), Yaml(config.get(YAML_KEY::WEAPON, YAML_DEFAULT::WEAPON)))),
mLastPosition(getPosition()) { mLastPosition(getPosition()),
mFaction((Faction) config.get(YAML_KEY::FACTION, YAML_DEFAULT::FACTION)) {
} }
Character::~Character() { Character::~Character() {
@ -66,6 +67,14 @@ Character::onThink(int elapsed) {
move(); move();
} }
/**
* Returns the faction this character belongs to.
*/
Character::Faction
Character::getFaction() const {
return mFaction;
}
/** /**
* Called when health reaches zero. Default implementation drops a corpse at * Called when health reaches zero. Default implementation drops a corpse at
* the current position. * the current position.
@ -163,5 +172,9 @@ Character::isVisible(const sf::Vector2f& target) const {
*/ */
std::vector<std::shared_ptr<Character> > std::vector<std::shared_ptr<Character> >
Character::getCharacters() const { Character::getCharacters() const {
return mWorld.getCharacters(getPosition(), VISION_DISTANCE); auto characters = mWorld.getCharacters(getPosition(), VISION_DISTANCE);
characters.erase(std::remove_if(characters.begin(), characters.end(),
[this](const std::shared_ptr<Character>& c)
{return c->getFaction() == getFaction();}), characters.end());
return characters;
} }

View file

@ -19,6 +19,15 @@ class Yaml;
* Provides think function for AI, manages health, drops body on death. * Provides think function for AI, manages health, drops body on death.
*/ */
class Character : public Circle { class Character : public Circle {
public:
enum Faction {
FACTION_PLAYER = 1,
FACTION_ENEMIES = 2
};
/// Maximum distance where an enemy will be detected.
static const float VISION_DISTANCE;
public: public:
explicit Character(const sf::Vector2f& position, Category category, explicit Character(const sf::Vector2f& position, Category category,
unsigned short mask, const Yaml& config, World& world, unsigned short mask, const Yaml& config, World& world,
@ -26,10 +35,7 @@ public:
virtual ~Character() = 0; virtual ~Character() = 0;
void onDamage(int damage); void onDamage(int damage);
Faction getFaction() const;
public:
/// Maximum distance where an enemy will be detected.
static const float VISION_DISTANCE;
protected: protected:
virtual void onThink(int elapsed); virtual void onThink(int elapsed);
@ -56,6 +62,7 @@ private:
std::unique_ptr<Weapon> mWeapon; std::unique_ptr<Weapon> mWeapon;
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination. std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
sf::Vector2f mLastPosition; sf::Vector2f mLastPosition;
Faction mFaction;
}; };
#endif /* DG_ACTOR_H_ */ #endif /* DG_ACTOR_H_ */

View file

@ -89,6 +89,7 @@ namespace YAML_KEY {
const std::string HEALTH = "health"; const std::string HEALTH = "health";
const std::string SPEED = "speed"; const std::string SPEED = "speed";
const std::string WEAPON = "weapon"; const std::string WEAPON = "weapon";
const std::string FACTION = "faction";
// Bullet // Bullet
const std::string DAMAGE = "damage"; const std::string DAMAGE = "damage";
@ -107,6 +108,7 @@ namespace YAML_DEFAULT {
const int INTERVAL = 250; const int INTERVAL = 250;
const std::string BULLET = "bullet.yaml"; const std::string BULLET = "bullet.yaml";
const bool AUTOMATIC = false; const bool AUTOMATIC = false;
const int FACTION = 1;
} }
#endif /* DG_YAML_H_ */ #endif /* DG_YAML_H_ */