Refactor Leap attack logic
This commit is contained in:
@@ -9,53 +9,71 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Cnc.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Cnc.Traits
|
||||
{
|
||||
[Desc("Dogs use this attack model.")]
|
||||
class AttackLeapInfo : AttackFrontalInfo
|
||||
[Desc("Move onto the target then execute the attack.")]
|
||||
public class AttackLeapInfo : AttackFrontalInfo, Requires<MobileInfo>
|
||||
{
|
||||
[Desc("Leap speed (in units/tick).")]
|
||||
[Desc("Leap speed (in WDist units/tick).")]
|
||||
public readonly WDist Speed = new WDist(426);
|
||||
|
||||
public readonly WAngle Angle = WAngle.FromDegrees(20);
|
||||
|
||||
[Desc("Types of damage that this trait causes. Leave empty for no damage types.")]
|
||||
public readonly BitSet<DamageType> DamageTypes = default(BitSet<DamageType>);
|
||||
[Desc("Conditions that last from start of the leap until the attack.")]
|
||||
[GrantedConditionReference]
|
||||
public readonly string LeapCondition = "attacking";
|
||||
|
||||
public override object Create(ActorInitializer init) { return new AttackLeap(init.Self, this); }
|
||||
}
|
||||
|
||||
class AttackLeap : AttackFrontal
|
||||
public class AttackLeap : AttackFrontal
|
||||
{
|
||||
readonly AttackLeapInfo info;
|
||||
|
||||
ConditionManager conditionManager;
|
||||
int leapToken = ConditionManager.InvalidConditionToken;
|
||||
|
||||
public AttackLeap(Actor self, AttackLeapInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public override void DoAttack(Actor self, Target target, IEnumerable<Armament> armaments = null)
|
||||
protected override void Created(Actor self)
|
||||
{
|
||||
if (target.Type != TargetType.Actor || !CanAttack(self, target))
|
||||
return;
|
||||
conditionManager = self.TraitOrDefault<ConditionManager>();
|
||||
base.Created(self);
|
||||
}
|
||||
|
||||
var a = ChooseArmamentsForTarget(target, true).FirstOrDefault();
|
||||
if (a == null)
|
||||
return;
|
||||
protected override bool CanAttack(Actor self, Target target)
|
||||
{
|
||||
if (target.Type != TargetType.Actor)
|
||||
return false;
|
||||
|
||||
if (!target.IsInRange(self.CenterPosition, a.MaxRange()))
|
||||
return;
|
||||
if (self.Location == target.Actor.Location && HasAnyValidWeapons(target))
|
||||
return true;
|
||||
|
||||
self.CancelActivity();
|
||||
self.QueueActivity(new Leap(self, target.Actor, a, info.Speed, info.Angle, info.DamageTypes));
|
||||
return base.CanAttack(self, target);
|
||||
}
|
||||
|
||||
public void GrantLeapCondition(Actor self)
|
||||
{
|
||||
if (conditionManager != null && !string.IsNullOrEmpty(info.LeapCondition))
|
||||
leapToken = conditionManager.GrantCondition(self, info.LeapCondition);
|
||||
}
|
||||
|
||||
public void RevokeLeapCondition(Actor self)
|
||||
{
|
||||
if (leapToken != ConditionManager.InvalidConditionToken)
|
||||
leapToken = conditionManager.RevokeCondition(self, leapToken);
|
||||
}
|
||||
|
||||
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack)
|
||||
{
|
||||
return new LeapAttack(self, newTarget, allowMove, this, info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user