Merge pull request #8090 from penev92/bleed_upgradableTraits
Make some traits upgradable.
This commit is contained in:
@@ -16,7 +16,8 @@ using OpenRA.Traits;
|
|||||||
|
|
||||||
namespace OpenRA.Mods.Common.Traits
|
namespace OpenRA.Mods.Common.Traits
|
||||||
{
|
{
|
||||||
public class WithInfantryBodyInfo : ITraitInfo, IQuantizeBodyOrientationInfo, IRenderActorPreviewSpritesInfo, Requires<IMoveInfo>, Requires<RenderSpritesInfo>
|
public class WithInfantryBodyInfo : UpgradableTraitInfo, ITraitInfo, IQuantizeBodyOrientationInfo, IRenderActorPreviewSpritesInfo,
|
||||||
|
Requires<IMoveInfo>, Requires<RenderSpritesInfo>
|
||||||
{
|
{
|
||||||
public readonly int MinIdleWaitTicks = 30;
|
public readonly int MinIdleWaitTicks = 30;
|
||||||
public readonly int MaxIdleWaitTicks = 110;
|
public readonly int MaxIdleWaitTicks = 110;
|
||||||
@@ -46,29 +47,28 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WithInfantryBody : ITick, INotifyAttack, INotifyIdle
|
public class WithInfantryBody : UpgradableTrait<WithInfantryBodyInfo>, ITick, INotifyAttack, INotifyIdle
|
||||||
{
|
{
|
||||||
readonly WithInfantryBodyInfo info;
|
|
||||||
readonly IMove move;
|
readonly IMove move;
|
||||||
|
readonly IRenderInfantrySequenceModifier rsm;
|
||||||
protected readonly Animation DefaultAnimation;
|
protected readonly Animation DefaultAnimation;
|
||||||
|
|
||||||
bool dirty = false;
|
bool dirty;
|
||||||
string idleSequence;
|
string idleSequence;
|
||||||
int idleDelay;
|
int idleDelay;
|
||||||
AnimationState state;
|
AnimationState state;
|
||||||
|
|
||||||
IRenderInfantrySequenceModifier rsm;
|
|
||||||
bool IsModifyingSequence { get { return rsm != null && rsm.IsModifyingSequence; } }
|
bool IsModifyingSequence { get { return rsm != null && rsm.IsModifyingSequence; } }
|
||||||
bool wasModifying;
|
bool wasModifying;
|
||||||
|
|
||||||
public WithInfantryBody(ActorInitializer init, WithInfantryBodyInfo info)
|
public WithInfantryBody(ActorInitializer init, WithInfantryBodyInfo info)
|
||||||
|
: base(info)
|
||||||
{
|
{
|
||||||
this.info = info;
|
|
||||||
var self = init.Self;
|
var self = init.Self;
|
||||||
var rs = self.Trait<RenderSprites>();
|
var rs = self.Trait<RenderSprites>();
|
||||||
|
|
||||||
DefaultAnimation = new Animation(init.World, rs.GetImage(self), RenderSprites.MakeFacingFunc(self));
|
DefaultAnimation = new Animation(init.World, rs.GetImage(self), RenderSprites.MakeFacingFunc(self));
|
||||||
rs.Add(DefaultAnimation);
|
rs.Add(new AnimationWithOffset(DefaultAnimation, null, () => IsTraitDisabled));
|
||||||
|
|
||||||
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(init.Self, info.StandSequences.Random(Game.CosmeticRandom)), () => 0);
|
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(init.Self, info.StandSequences.Random(Game.CosmeticRandom)), () => 0);
|
||||||
state = AnimationState.Waiting;
|
state = AnimationState.Waiting;
|
||||||
@@ -94,8 +94,8 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
public void Attacking(Actor self, Target target)
|
public void Attacking(Actor self, Target target)
|
||||||
{
|
{
|
||||||
state = AnimationState.Attacking;
|
state = AnimationState.Attacking;
|
||||||
if (DefaultAnimation.HasSequence(NormalizeInfantrySequence(self, info.AttackSequence)))
|
if (DefaultAnimation.HasSequence(NormalizeInfantrySequence(self, Info.AttackSequence)))
|
||||||
DefaultAnimation.PlayThen(NormalizeInfantrySequence(self, info.AttackSequence), () => state = AnimationState.Idle);
|
DefaultAnimation.PlayThen(NormalizeInfantrySequence(self, Info.AttackSequence), () => state = AnimationState.Idle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
|
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
|
||||||
@@ -116,12 +116,12 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if ((state == AnimationState.Moving || dirty) && !move.IsMoving)
|
if ((state == AnimationState.Moving || dirty) && !move.IsMoving)
|
||||||
{
|
{
|
||||||
state = AnimationState.Waiting;
|
state = AnimationState.Waiting;
|
||||||
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandSequences.Random(Game.CosmeticRandom)), () => 0);
|
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom)), () => 0);
|
||||||
}
|
}
|
||||||
else if ((state != AnimationState.Moving || dirty) && move.IsMoving)
|
else if ((state != AnimationState.Moving || dirty) && move.IsMoving)
|
||||||
{
|
{
|
||||||
state = AnimationState.Moving;
|
state = AnimationState.Moving;
|
||||||
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.MoveSequence));
|
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, Info.MoveSequence));
|
||||||
}
|
}
|
||||||
|
|
||||||
dirty = false;
|
dirty = false;
|
||||||
@@ -131,13 +131,13 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
{
|
{
|
||||||
if (state != AnimationState.Idle && state != AnimationState.IdleAnimating && state != AnimationState.Attacking)
|
if (state != AnimationState.Idle && state != AnimationState.IdleAnimating && state != AnimationState.Attacking)
|
||||||
{
|
{
|
||||||
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandSequences.Random(Game.CosmeticRandom)), () => 0);
|
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom)), () => 0);
|
||||||
state = AnimationState.Idle;
|
state = AnimationState.Idle;
|
||||||
|
|
||||||
if (info.IdleSequences.Length > 0)
|
if (Info.IdleSequences.Length > 0)
|
||||||
{
|
{
|
||||||
idleSequence = info.IdleSequences.Random(self.World.SharedRandom);
|
idleSequence = Info.IdleSequences.Random(self.World.SharedRandom);
|
||||||
idleDelay = self.World.SharedRandom.Next(info.MinIdleWaitTicks, info.MaxIdleWaitTicks);
|
idleDelay = self.World.SharedRandom.Next(Info.MinIdleWaitTicks, Info.MaxIdleWaitTicks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (AllowIdleAnimation(self))
|
else if (AllowIdleAnimation(self))
|
||||||
@@ -149,14 +149,14 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
state = AnimationState.IdleAnimating;
|
state = AnimationState.IdleAnimating;
|
||||||
DefaultAnimation.PlayThen(idleSequence, () =>
|
DefaultAnimation.PlayThen(idleSequence, () =>
|
||||||
{
|
{
|
||||||
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.StandSequences.Random(Game.CosmeticRandom)));
|
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom)));
|
||||||
state = AnimationState.Waiting;
|
state = AnimationState.Waiting;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, info.StandSequences.Random(Game.CosmeticRandom)));
|
DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom)));
|
||||||
state = AnimationState.Waiting;
|
state = AnimationState.Waiting;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,13 @@
|
|||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using OpenRA.Mods.Common.Traits;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
|
||||||
namespace OpenRA.Mods.D2k.Traits
|
namespace OpenRA.Mods.D2k.Traits
|
||||||
{
|
{
|
||||||
[Desc("This actor makes noise, which causes them to be targeted by actors with the Sandworm trait.")]
|
[Desc("This actor makes noise, which causes them to be targeted by actors with the Sandworm trait.")]
|
||||||
public class AttractsWormsInfo : ITraitInfo
|
public class AttractsWormsInfo : UpgradableTraitInfo, ITraitInfo
|
||||||
{
|
{
|
||||||
[Desc("How much noise this actor produces.")]
|
[Desc("How much noise this actor produces.")]
|
||||||
public readonly int Intensity = 0;
|
public readonly int Intensity = 0;
|
||||||
@@ -27,22 +28,23 @@ namespace OpenRA.Mods.D2k.Traits
|
|||||||
[Desc("Ranges at which each Falloff step is defined. Overrides Spread.")]
|
[Desc("Ranges at which each Falloff step is defined. Overrides Spread.")]
|
||||||
public WRange[] Range = null;
|
public WRange[] Range = null;
|
||||||
|
|
||||||
public object Create(ActorInitializer init) { return new AttractsWorms(this); }
|
public object Create(ActorInitializer init) { return new AttractsWorms(init, this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AttractsWorms
|
public class AttractsWorms : UpgradableTrait<AttractsWormsInfo>
|
||||||
{
|
{
|
||||||
public readonly AttractsWormsInfo Info;
|
readonly Actor self;
|
||||||
|
|
||||||
public AttractsWorms(AttractsWormsInfo info)
|
public AttractsWorms(ActorInitializer init, AttractsWormsInfo info)
|
||||||
|
: base(info)
|
||||||
{
|
{
|
||||||
Info = info;
|
self = init.Self;
|
||||||
|
|
||||||
if (info.Range == null)
|
if (info.Range == null)
|
||||||
info.Range = Exts.MakeArray(info.Falloff.Length, i => i * info.Spread);
|
info.Range = Exts.MakeArray(info.Falloff.Length, i => i * info.Spread);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetNoisePercentageAtDistance(int distance)
|
int GetNoisePercentageAtDistance(int distance)
|
||||||
{
|
{
|
||||||
var inner = Info.Range[0].Range;
|
var inner = Info.Range[0].Range;
|
||||||
for (var i = 1; i < Info.Range.Length; i++)
|
for (var i = 1; i < Info.Range.Length; i++)
|
||||||
@@ -56,5 +58,23 @@ namespace OpenRA.Mods.D2k.Traits
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WVec AttractionAtPosition(WPos pos)
|
||||||
|
{
|
||||||
|
if (IsTraitDisabled)
|
||||||
|
return WVec.Zero;
|
||||||
|
|
||||||
|
var distance = self.CenterPosition - pos;
|
||||||
|
var length = distance.Length;
|
||||||
|
|
||||||
|
// Actor is too far to hear anything.
|
||||||
|
if (length > Info.Range[Info.Range.Length - 1].Range)
|
||||||
|
return WVec.Zero;
|
||||||
|
|
||||||
|
var direction = 1024 * distance / length;
|
||||||
|
var percentage = GetNoisePercentageAtDistance(length);
|
||||||
|
|
||||||
|
return direction * Info.Intensity * percentage / 100;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using OpenRA.Mods.Common.Traits;
|
using OpenRA.Mods.Common.Traits;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
|
||||||
@@ -91,43 +92,28 @@ namespace OpenRA.Mods.D2k.Traits
|
|||||||
{
|
{
|
||||||
targetCountdown = Info.TargetRescanInterval;
|
targetCountdown = Info.TargetRescanInterval;
|
||||||
|
|
||||||
var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, Info.MaxSearchRadius);
|
// If close enough, we don't care about other actors.
|
||||||
var noiseDirection = WVec.Zero;
|
var target = self.World.FindActorsInCircle(self.CenterPosition, Info.IgnoreNoiseAttackRange).FirstOrDefault(x => x.HasTrait<AttractsWorms>());
|
||||||
|
if (target != null)
|
||||||
foreach (var actor in actorsInRange)
|
|
||||||
{
|
{
|
||||||
if (!actor.IsInWorld)
|
self.CancelActivity();
|
||||||
continue;
|
attackTrait.Value.ResolveOrder(self, new Order("Attack", target, true) { TargetActor = target });
|
||||||
|
return;
|
||||||
// TODO: Test if we really want to ignore actors that are on rock
|
|
||||||
if (!mobile.Value.CanEnterCell(actor.Location, null, false))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var noise = actor.TraitOrDefault<AttractsWorms>();
|
|
||||||
if (noise == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var distance = actor.CenterPosition - self.CenterPosition;
|
|
||||||
var length = distance.Length;
|
|
||||||
|
|
||||||
// Actor is too far to be heard
|
|
||||||
if (noise.Info.Range[noise.Info.Range.Length - 1].Range < length)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// If close enough, we don't care about other actors
|
|
||||||
if (length <= Info.IgnoreNoiseAttackRange.Range)
|
|
||||||
{
|
|
||||||
self.CancelActivity();
|
|
||||||
attackTrait.Value.ResolveOrder(self, new Order("Attack", actor, true) { TargetActor = actor });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var direction = 1024 * distance / length;
|
|
||||||
var percentage = noise.GetNoisePercentageAtDistance(length);
|
|
||||||
|
|
||||||
noiseDirection += direction * noise.Info.Intensity * percentage / 100;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Func<Actor, bool> isValidTarget = a =>
|
||||||
|
{
|
||||||
|
if (!a.HasTrait<AttractsWorms>())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return mobile.Value.CanEnterCell(a.Location, null, false);
|
||||||
|
};
|
||||||
|
|
||||||
|
var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, Info.MaxSearchRadius)
|
||||||
|
.Where(isValidTarget).SelectMany(a => a.TraitsImplementing<AttractsWorms>());
|
||||||
|
|
||||||
|
var noiseDirection = actorsInRange.Aggregate(WVec.Zero, (a, b) => a + b.AttractionAtPosition(self.CenterPosition));
|
||||||
|
|
||||||
// No target was found
|
// No target was found
|
||||||
if (noiseDirection == WVec.Zero)
|
if (noiseDirection == WVec.Zero)
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user