Added teams so enemies don't attack each other.
This commit is contained in:
parent
8ea278030d
commit
6418de7774
5 changed files with 32 additions and 8 deletions
|
@ -4,4 +4,5 @@ texture: enemy.png
|
|||
health: 100
|
||||
speed: 100.0
|
||||
radius: 25.0
|
||||
weapon: weapon.yaml
|
||||
weapon: weapon.yaml
|
||||
faction: 1
|
|
@ -4,4 +4,5 @@ texture: player.png
|
|||
health: 100
|
||||
speed: 100.0
|
||||
radius: 25.0
|
||||
weapon: weapon.yaml
|
||||
weapon: weapon.yaml
|
||||
faction: 0
|
|
@ -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<std::shared_ptr<Character> >
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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<Weapon> mWeapon;
|
||||
std::vector<sf::Vector2f> mPath; //< Contains nodes to reach a set destination.
|
||||
sf::Vector2f mLastPosition;
|
||||
Faction mFaction;
|
||||
};
|
||||
|
||||
#endif /* DG_ACTOR_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_ */
|
||||
|
|
Reference in a new issue