diff --git a/MeleeWeapon.cpp b/MeleeWeapon.cpp index 12e6585dfbdd5b4d265ac9c4a5ea76ced6c16392..41d323f6ba3b1e27e87b4acd1ccaad95379cde01 100644 --- a/MeleeWeapon.cpp +++ b/MeleeWeapon.cpp @@ -21,22 +21,29 @@ namespace weapons { return bl; } - MeleeWeapon::MeleeWeapon(render::ITexture& texture, float cooldownTime, int damage, const olc::vf2d& pos) - : Weapon(texture, cooldownTime, damage, pos) {} + MeleeWeapon::MeleeWeapon(render::ITexture& texture, const std::string& name, float cooldownTime, int damage, const olc::vf2d& pos) + : Weapon(texture, cooldownTime, damage, name, pos) {} bool MeleeWeapon::use(std::shared_ptr<LivingEntity> user, const olc::vf2d& direction) { if (this->cooldown != 0) return false; this->cooldown = this->cooldownTime; - damageIf() + return damageIf(user, this->getPredicator(direction)); } + + float MeleeWeapon::getHitOffset() const + { + return 0; + } + + void MeleeWeapon::setPos(const olc::vf2d& newPos) { this->pos = newPos; //I should test its pos... } - bool MeleeWeapon::predicateDistance::operator()(std::shared_ptr<Entity> entity, std::shared_ptr<Entity> other) const + bool MeleeWeapon::predicateDistance::operator()(const olc::vf2d& usePos, std::shared_ptr<Entity> other) const { - float d = (entity->getPos() - other->getPos()).mag(); + float d = (usePos - other->getPos()).mag(); return d > minDistance && d <= maxDistance; } } \ No newline at end of file diff --git a/MeleeWeapon.h b/MeleeWeapon.h index db481510f43bc8c2d932a686a7916482344e1740..97cfaaa6659cf887b478f1f5d54c330e1771fb7a 100644 --- a/MeleeWeapon.h +++ b/MeleeWeapon.h @@ -11,8 +11,9 @@ namespace weapons { virtual bool damageEntity(std::shared_ptr<LivingEntity> user, std::shared_ptr<Entity>& victim); virtual bool damageIf(std::shared_ptr<LivingEntity>& user, std::function<bool(std::shared_ptr<Entity> self, std::shared_ptr<Entity> other)> predicate); //virtual bool(*getPredicator())(std::shared_ptr<Entity>, std::shared_ptr<Entity>) = 0; - virtual std::function<bool(std::shared_ptr<Entity>, std::shared_ptr<Entity>)> getPredicator() = 0; - + virtual std::function<bool(std::shared_ptr<Entity>, std::shared_ptr<Entity>)> getPredicator(const olc::vf2d& direction) = 0; + + virtual float getHitOffset() const; /** * Functor to predicate entity distance. * You can also use lambda, if you want @@ -22,11 +23,11 @@ namespace weapons { public: float minDistance = 0; float maxDistance = 5; - bool operator()(std::shared_ptr<Entity> entity, std::shared_ptr<Entity> other) const; + bool operator()(const olc::vf2d& usePos, std::shared_ptr<Entity> other) const; }; public: - MeleeWeapon(render::ITexture& texture, float cooldownTime, int damage = 10, const olc::vf2d& pos = { 0, 0 }); + MeleeWeapon(render::ITexture& texture, const std::string& name, float cooldownTime, int damage = 10, const olc::vf2d& pos = { 0, 0 }); bool use(std::shared_ptr<LivingEntity> user, const olc::vf2d& direction) override; virtual void setPos(const olc::vf2d& newPos); diff --git a/TypicalMeleeWeapon.cpp b/TypicalMeleeWeapon.cpp index 61e2305830be8936f7054a95c68db4876da824a1..d5b97e53532b14a272fd569b04da3ffd15cdd2da 100644 --- a/TypicalMeleeWeapon.cpp +++ b/TypicalMeleeWeapon.cpp @@ -11,17 +11,26 @@ namespace weapons { { return false; } - std::function<bool(std::shared_ptr<Entity>, std::shared_ptr<Entity>)> TypicalMeleeWeapon::getPredicator() + std::function<bool(std::shared_ptr<Entity>, std::shared_ptr<Entity>)> TypicalMeleeWeapon::getPredicator(const olc::vf2d& direction) { predicateDistance p; p.maxDistance = this->maxRange; - return p; + return [p, direction, this](std::shared_ptr<Entity> user, std::shared_ptr<Entity> entity)->bool + { + if (user == entity) return false; + return p(user->getPos() + direction * this->getHitOffset(), entity); + }; } - TypicalMeleeWeapon::TypicalMeleeWeapon(render::ITexture& text, float cooldownTime, int damage, float range, const olc::vf2d& pos) - : MeleeWeapon(text, cooldownTime, damage, pos), maxRange(range) {} + float TypicalMeleeWeapon::getHitOffset() const + { + return this->attackOffset; + } + + TypicalMeleeWeapon::TypicalMeleeWeapon(render::ITexture& text, const std::string& name, float cooldownTime, int damage, float range, float hitOffset, const olc::vf2d& pos) + : MeleeWeapon(text, name, cooldownTime, damage, pos), maxRange(range), attackOffset(hitOffset) {} - TypicalMeleeWeapon TypicalMeleeWeapon::sword(textures::sword1, 1, 16, 1); - TypicalMeleeWeapon TypicalMeleeWeapon::longSword(textures::sword1, 3, 16, 3); - TypicalMeleeWeapon TypicalMeleeWeapon::spoon(textures::sword1, 3, 16, 3); + TypicalMeleeWeapon TypicalMeleeWeapon::sword(textures::sword1, "Sharp stick", 1, 16, 1); + TypicalMeleeWeapon TypicalMeleeWeapon::longSword(textures::sword1, "Long sharp stick", 3, 16, 3, 1); + TypicalMeleeWeapon TypicalMeleeWeapon::spoon(textures::sword1, "Spoon", 3, 32, 0.7f, 0.4f); } \ No newline at end of file diff --git a/TypicalMeleeWeapon.h b/TypicalMeleeWeapon.h index b714fd6ed593a3b35867c82314078f4ed6a81d9c..78eaae1998e4fb81de85e7e67788d021ba43d28f 100644 --- a/TypicalMeleeWeapon.h +++ b/TypicalMeleeWeapon.h @@ -15,9 +15,11 @@ namespace weapons { float maxRange; float attackOffset; - std::function<bool(std::shared_ptr<Entity>, std::shared_ptr<Entity>)> getPredicator() override; + std::function<bool(std::shared_ptr<Entity>, std::shared_ptr<Entity>)> getPredicator(const olc::vf2d& direction) override; + + float getHitOffset() const override; public: - TypicalMeleeWeapon(render::ITexture& text, float cooldownTime, int damage, float range, const olc::vf2d& pos = {0, 0}); + TypicalMeleeWeapon(render::ITexture& text, const std::string& name, float cooldownTime, int damage, float range, float hitOffset = .5f, const olc::vf2d& pos = { 0, 0 }); }; } diff --git a/Weapon.cpp b/Weapon.cpp index c456e72fc4a32e3d88f16b6e414ad5626e3c2e2b..6721d604b5b20059e918e9e11fa517dfd767cf4b 100644 --- a/Weapon.cpp +++ b/Weapon.cpp @@ -7,8 +7,8 @@ namespace weapons return this->texture; } - Weapon::Weapon(render::ITexture& texture, float cooldownTime, int damage, const olc::vf2d& pos) - : Entity(pos), texture(texture), cooldown(0), cooldownTime(cooldownTime), baseDamage(damage) {} + Weapon::Weapon(render::ITexture& texture, float cooldownTime, int damage, const std::string& name, const olc::vf2d& pos) + : Entity(pos), texture(texture), cooldown(0), cooldownTime(cooldownTime), baseDamage(damage), name(name) {} bool Weapon::update(float dTick) { diff --git a/Weapon.h b/Weapon.h index 4f903846b354ae9b45df028a255917f5e7f4368f..0f03c393659fbf1339c8e6b9c31151eaf34444e4 100644 --- a/Weapon.h +++ b/Weapon.h @@ -18,9 +18,10 @@ namespace weapons { float cooldown; float cooldownTime; int baseDamage; + std::string name; public: - Weapon(render::ITexture& texture, float cooldownTime, int damage = 10, const olc::vf2d& pos = { 0, 0 }); + Weapon(render::ITexture& texture, float cooldownTime, int damage = 10, const std::string& name = "Weapon", const olc::vf2d& pos = { 0, 0 }); /** * @return true, if can use