From b24327a2940539ff568fd8d7b841a99682b2d488 Mon Sep 17 00:00:00 2001
From: KosmX <kosmx.mc@gmail.com>
Date: Wed, 12 May 2021 12:12:57 +0200
Subject: [PATCH] some sharp stuff

---
 MeleeWeapon.cpp        | 17 ++++++++++++-----
 MeleeWeapon.h          |  9 +++++----
 TypicalMeleeWeapon.cpp | 23 ++++++++++++++++-------
 TypicalMeleeWeapon.h   |  6 ++++--
 Weapon.cpp             |  4 ++--
 Weapon.h               |  3 ++-
 6 files changed, 41 insertions(+), 21 deletions(-)

diff --git a/MeleeWeapon.cpp b/MeleeWeapon.cpp
index 12e6585..41d323f 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 db48151..97cfaaa 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 61e2305..d5b97e5 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 b714fd6..78eaae1 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 c456e72..6721d60 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 4f90384..0f03c39 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
-- 
GitLab