Merge pull request #8838 from reaperrr/withbarrel2
Fixed WithBarrel and made it upgradable
This commit is contained in:
@@ -17,24 +17,25 @@ using OpenRA.Traits;
|
|||||||
namespace OpenRA.Mods.Common.Traits
|
namespace OpenRA.Mods.Common.Traits
|
||||||
{
|
{
|
||||||
[Desc("Renders barrels for units with the Turreted trait.")]
|
[Desc("Renders barrels for units with the Turreted trait.")]
|
||||||
class WithBarrelInfo : ITraitInfo, IRenderActorPreviewSpritesInfo, Requires<RenderSpritesInfo>, Requires<IBodyOrientationInfo>
|
public class WithBarrelInfo : UpgradableTraitInfo, IRenderActorPreviewSpritesInfo, Requires<TurretedInfo>,
|
||||||
|
Requires<ArmamentInfo>, Requires<RenderSpritesInfo>, Requires<IBodyOrientationInfo>
|
||||||
{
|
{
|
||||||
[Desc("Sequence name to use")]
|
[Desc("Sequence name to use.")]
|
||||||
[SequenceReference] public readonly string Sequence = "barrel";
|
[SequenceReference] public readonly string Sequence = "barrel";
|
||||||
|
|
||||||
[Desc("Armament to use for recoil")]
|
[Desc("Armament to use for recoil.")]
|
||||||
public readonly string Armament = "primary";
|
public readonly string Armament = "primary";
|
||||||
|
|
||||||
[Desc("Turreted 'Barrel' key to display")]
|
[Desc("Visual offset.")]
|
||||||
public readonly string Barrel = "first";
|
|
||||||
|
|
||||||
[Desc("Visual offset")]
|
|
||||||
public readonly WVec LocalOffset = WVec.Zero;
|
public readonly WVec LocalOffset = WVec.Zero;
|
||||||
|
|
||||||
public object Create(ActorInitializer init) { return new WithBarrel(init.Self, this); }
|
public override object Create(ActorInitializer init) { return new WithBarrel(init.Self, this); }
|
||||||
|
|
||||||
public IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
|
public IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
|
||||||
{
|
{
|
||||||
|
if (UpgradeMinEnabledLevel > 0)
|
||||||
|
yield break;
|
||||||
|
|
||||||
var body = init.Actor.Traits.Get<BodyOrientationInfo>();
|
var body = init.Actor.Traits.Get<BodyOrientationInfo>();
|
||||||
var armament = init.Actor.Traits.WithInterface<ArmamentInfo>()
|
var armament = init.Actor.Traits.WithInterface<ArmamentInfo>()
|
||||||
.First(a => a.Name == Armament);
|
.First(a => a.Name == Armament);
|
||||||
@@ -51,38 +52,43 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class WithBarrel
|
public class WithBarrel : UpgradableTrait<WithBarrelInfo>
|
||||||
{
|
{
|
||||||
WithBarrelInfo info;
|
public readonly Animation DefaultAnimation;
|
||||||
Actor self;
|
readonly RenderSprites rs;
|
||||||
Armament armament;
|
readonly Actor self;
|
||||||
Turreted turreted;
|
readonly Armament armament;
|
||||||
IBodyOrientation body;
|
readonly Turreted turreted;
|
||||||
Animation anim;
|
readonly IBodyOrientation body;
|
||||||
|
|
||||||
public WithBarrel(Actor self, WithBarrelInfo info)
|
public WithBarrel(Actor self, WithBarrelInfo info)
|
||||||
|
: base(info)
|
||||||
{
|
{
|
||||||
this.self = self;
|
this.self = self;
|
||||||
this.info = info;
|
|
||||||
body = self.Trait<IBodyOrientation>();
|
body = self.Trait<IBodyOrientation>();
|
||||||
armament = self.TraitsImplementing<Armament>()
|
armament = self.TraitsImplementing<Armament>()
|
||||||
.First(a => a.Info.Name == info.Armament);
|
.First(a => a.Info.Name == Info.Armament);
|
||||||
turreted = self.TraitsImplementing<Turreted>()
|
turreted = self.TraitsImplementing<Turreted>()
|
||||||
.First(tt => tt.Name == armament.Info.Turret);
|
.First(tt => tt.Name == armament.Info.Turret);
|
||||||
|
|
||||||
var rs = self.Trait<RenderSprites>();
|
rs = self.Trait<RenderSprites>();
|
||||||
anim = new Animation(self.World, rs.GetImage(self), () => turreted.TurretFacing);
|
DefaultAnimation = new Animation(self.World, rs.GetImage(self), () => turreted.TurretFacing);
|
||||||
anim.Play(info.Sequence);
|
DefaultAnimation.PlayRepeating(NormalizeSequence(self, Info.Sequence));
|
||||||
rs.Add(new AnimationWithOffset(
|
rs.Add(new AnimationWithOffset(
|
||||||
anim, () => BarrelOffset(), null, () => false, p => WithTurret.ZOffsetFromCenter(self, p, 0)));
|
DefaultAnimation, () => BarrelOffset(), () => IsTraitDisabled, () => false, p => WithTurret.ZOffsetFromCenter(self, p, 0)));
|
||||||
|
|
||||||
// Restrict turret facings to match the sprite
|
// Restrict turret facings to match the sprite
|
||||||
turreted.QuantizedFacings = anim.CurrentSequence.Facings;
|
turreted.QuantizedFacings = DefaultAnimation.CurrentSequence.Facings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string NormalizeSequence(Actor self, string sequence)
|
||||||
|
{
|
||||||
|
return RenderSprites.NormalizeSequence(DefaultAnimation, self.GetDamageState(), sequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
WVec BarrelOffset()
|
WVec BarrelOffset()
|
||||||
{
|
{
|
||||||
var localOffset = info.LocalOffset + new WVec(-armament.Recoil, WDist.Zero, WDist.Zero);
|
var localOffset = Info.LocalOffset + new WVec(-armament.Recoil, WDist.Zero, WDist.Zero);
|
||||||
var turretOffset = turreted != null ? turreted.Position(self) : WVec.Zero;
|
var turretOffset = turreted != null ? turreted.Position(self) : WVec.Zero;
|
||||||
var turretOrientation = turreted != null ? turreted.LocalOrientation(self) : WRot.Zero;
|
var turretOrientation = turreted != null ? turreted.LocalOrientation(self) : WRot.Zero;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user