summaryrefslogtreecommitdiffstats
path: root/src/Mobs/Behaviors
diff options
context:
space:
mode:
authorLogicParrot <LogicParrot@users.noreply.github.com>2017-09-02 19:37:01 +0200
committerLogicParrot <LogicParrot@users.noreply.github.com>2017-09-02 19:37:01 +0200
commit775d2edb6b606c3beb5c732bacb7e8a72fdff207 (patch)
tree891d7878d285f24272a6545553975410a398dd26 /src/Mobs/Behaviors
parentWorking spiders and cave spiders using behaviors (diff)
downloadcuberite-775d2edb6b606c3beb5c732bacb7e8a72fdff207.tar
cuberite-775d2edb6b606c3beb5c732bacb7e8a72fdff207.tar.gz
cuberite-775d2edb6b606c3beb5c732bacb7e8a72fdff207.tar.bz2
cuberite-775d2edb6b606c3beb5c732bacb7e8a72fdff207.tar.lz
cuberite-775d2edb6b606c3beb5c732bacb7e8a72fdff207.tar.xz
cuberite-775d2edb6b606c3beb5c732bacb7e8a72fdff207.tar.zst
cuberite-775d2edb6b606c3beb5c732bacb7e8a72fdff207.zip
Diffstat (limited to 'src/Mobs/Behaviors')
-rw-r--r--src/Mobs/Behaviors/BehaviorAttacker.h1
-rw-r--r--src/Mobs/Behaviors/BehaviorAttackerRanged.cpp37
-rw-r--r--src/Mobs/Behaviors/BehaviorAttackerRanged.h12
3 files changed, 41 insertions, 9 deletions
diff --git a/src/Mobs/Behaviors/BehaviorAttacker.h b/src/Mobs/Behaviors/BehaviorAttacker.h
index 82a377112..428e7a340 100644
--- a/src/Mobs/Behaviors/BehaviorAttacker.h
+++ b/src/Mobs/Behaviors/BehaviorAttacker.h
@@ -78,5 +78,6 @@ private:
// The mob we want to attack
cPawn * m_Target;
int m_StrikeTickCnt;
+ bool m_ShouldRetaliate; // Should we attack back whoever attacks us?
};
diff --git a/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp b/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp
index 366c77c68..888dc3497 100644
--- a/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp
+++ b/src/Mobs/Behaviors/BehaviorAttackerRanged.cpp
@@ -6,19 +6,38 @@
#include "../../BlockID.h"
#include "../../Entities/ArrowEntity.h"
+cBehaviorAttackerRanged::cBehaviorAttackerRanged(
+ RangedShootingFunction a_RangedShootingFunction, int a_ProjectileAmount,
+ int a_ShootingIntervals) :
+ m_ShootingFunction(a_RangedShootingFunction),
+ m_ProjectileAmount(a_ProjectileAmount),
+ m_ShootingIntervals(a_ShootingIntervals)
+{
+
+}
+
+
bool cBehaviorAttackerRanged::DoStrike(int a_StrikeTickCnt)
{
UNUSED(a_StrikeTickCnt);
- auto & Random = GetRandomProvider();
- if ((GetTarget() != nullptr) && (m_AttackCoolDownTicksLeft == 0))
+
+ // stop shooting if target is lost
+ if ((GetTarget() == nullptr))
{
- Vector3d Inaccuracy = Vector3d(Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25), Random.RandReal<double>(-0.25, 0.25));
- Vector3d Speed = (GetTarget()->GetPosition() + Inaccuracy - m_Parent->GetPosition()) * 5;
- Speed.y += Random.RandInt(-1, 1);
+ return true;
+ }
- auto Arrow = cpp14::make_unique<cArrowEntity>(m_Parent, m_Parent->GetPosX(), m_Parent->GetPosY() + 1, m_Parent->GetPosZ(), Speed);
- auto ArrowPtr = Arrow.get();
- ArrowPtr->Initialize(std::move(Arrow), *m_Parent->GetWorld());
+ // Stop shooting if we've shot m_ProjectileAmount times.
+ if (a_StrikeTickCnt - 1 == m_ShootingIntervals * m_ProjectileAmount)
+ {
+ return true;
+ }
+
+ // shoot once every m_ShootingIntervals.
+ // Starting immediately at first call to DoStrike
+ if ((a_StrikeTickCnt - 1) % m_ShootingIntervals == 0)
+ {
+ m_ShootingFunction(*this, *m_Parent, *GetTarget());
}
- return true; // Finish the strike. It only takes 1 tick.
+ return false;
}
diff --git a/src/Mobs/Behaviors/BehaviorAttackerRanged.h b/src/Mobs/Behaviors/BehaviorAttackerRanged.h
index 012590490..1d9e4fcd9 100644
--- a/src/Mobs/Behaviors/BehaviorAttackerRanged.h
+++ b/src/Mobs/Behaviors/BehaviorAttackerRanged.h
@@ -1,11 +1,23 @@
#pragma once
#include "BehaviorAttacker.h"
+#include <functional>
+class cBehaviorAttackerRanged;
/** Grants the mob that ability to approach a target and then melee attack it.
Use BehaviorAttackerMelee::SetTarget to attack. */
+typedef std::function<void(cBehaviorAttackerRanged & a_Behavior,
+ cMonster & a_Attacker, cPawn & a_Attacked)> RangedShootingFunction;
+
class cBehaviorAttackerRanged : public cBehaviorAttacker
{
public:
+ cBehaviorAttackerRanged(RangedShootingFunction a_RangedShootingFUnction,
+ int a_ProjectileAmount = 1, int a_ShootingIntervals = 1);
bool DoStrike(int a_StrikeTickCnt) override;
+
+private:
+ RangedShootingFunction m_ShootingFunction;
+ int m_ProjectileAmount;
+ int m_ShootingIntervals;
};