From 6418de77743a75fd1fc0c491374b62b7e90473ab Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 8 May 2013 19:49:04 +0200 Subject: [PATCH] Added teams so enemies don't attack each other. --- resources/yaml/enemy.yaml | 3 ++- resources/yaml/player.yaml | 3 ++- source/abstract/Character.cpp | 17 +++++++++++++++-- source/abstract/Character.h | 15 +++++++++++---- source/util/Yaml.h | 2 ++ 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/resources/yaml/enemy.yaml b/resources/yaml/enemy.yaml index bfb16b3..92571ac 100644 --- a/resources/yaml/enemy.yaml +++ b/resources/yaml/enemy.yaml @@ -4,4 +4,5 @@ texture: enemy.png health: 100 speed: 100.0 radius: 25.0 -weapon: weapon.yaml \ No newline at end of file +weapon: weapon.yaml +faction: 1 \ No newline at end of file diff --git a/resources/yaml/player.yaml b/resources/yaml/player.yaml index 9b300be..3c22bbd 100644 --- a/resources/yaml/player.yaml +++ b/resources/yaml/player.yaml @@ -4,4 +4,5 @@ texture: player.png health: 100 speed: 100.0 radius: 25.0 -weapon: weapon.yaml \ No newline at end of file +weapon: weapon.yaml +faction: 0 \ No newline at end of file diff --git a/source/abstract/Character.cpp b/source/abstract/Character.cpp index f864862..a15c77f 100644 --- a/source/abstract/Character.cpp +++ b/source/abstract/Character.cpp @@ -32,7 +32,8 @@ Character::Character(const sf::Vector2f& position, Category category, mMovementSpeed(config.get(YAML_KEY::SPEED, YAML_DEFAULT::SPEED)), mWeapon(new Weapon(world, *this, 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() { @@ -66,6 +67,14 @@ Character::onThink(int elapsed) { 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 * the current position. @@ -163,5 +172,9 @@ Character::isVisible(const sf::Vector2f& target) const { */ std::vector > 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& c) + {return c->getFaction() == getFaction();}), characters.end()); + return characters; } diff --git a/source/abstract/Character.h b/source/abstract/Character.h index 89ae6cd..c4af4b0 100644 --- a/source/abstract/Character.h +++ b/source/abstract/Character.h @@ -19,6 +19,15 @@ class Yaml; * Provides think function for AI, manages health, drops body on death. */ 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: explicit Character(const sf::Vector2f& position, Category category, unsigned short mask, const Yaml& config, World& world, @@ -26,10 +35,7 @@ public: virtual ~Character() = 0; void onDamage(int damage); - -public: - /// Maximum distance where an enemy will be detected. - static const float VISION_DISTANCE; + Faction getFaction() const; protected: virtual void onThink(int elapsed); @@ -56,6 +62,7 @@ private: std::unique_ptr mWeapon; std::vector mPath; //< Contains nodes to reach a set destination. sf::Vector2f mLastPosition; + Faction mFaction; }; #endif /* DG_ACTOR_H_ */ diff --git a/source/util/Yaml.h b/source/util/Yaml.h index 1f5b1b3..cd3b7c9 100644 --- a/source/util/Yaml.h +++ b/source/util/Yaml.h @@ -89,6 +89,7 @@ namespace YAML_KEY { const std::string HEALTH = "health"; const std::string SPEED = "speed"; const std::string WEAPON = "weapon"; + const std::string FACTION = "faction"; // Bullet const std::string DAMAGE = "damage"; @@ -107,6 +108,7 @@ namespace YAML_DEFAULT { const int INTERVAL = 250; const std::string BULLET = "bullet.yaml"; const bool AUTOMATIC = false; + const int FACTION = 1; } #endif /* DG_YAML_H_ */