Merge pull request #8171 from reaperrr/withunitbody1

Introduce WithFacingSpriteBody and related With*Animation traits
This commit is contained in:
Matthias Mailänder
2015-06-07 17:11:48 +02:00
41 changed files with 759 additions and 289 deletions

View File

@@ -364,6 +364,7 @@
<Compile Include="Traits\ProximityCaptor.cs" />
<Compile Include="Traits\ProximityCapturable.cs" />
<Compile Include="Traits\RadarColorFromTerrain.cs" />
<Compile Include="Traits\Render\AutoSelectionSize.cs" />
<Compile Include="Traits\Render\Hovers.cs" />
<Compile Include="Traits\Render\LeavesTrails.cs" />
<Compile Include="Traits\Render\RenderBuilding.cs" />
@@ -386,6 +387,8 @@
<Compile Include="Traits\Render\WithBarrel.cs" />
<Compile Include="Traits\Render\WithBuildingExplosion.cs" />
<Compile Include="Traits\Render\WithActiveAnimation.cs" />
<Compile Include="Traits\Render\WithAttackAnimation.cs" />
<Compile Include="Traits\Render\WithMoveAnimation.cs" />
<Compile Include="Traits\Render\WithBuildingPlacedAnimation.cs" />
<Compile Include="Traits\Render\WithMakeAnimation.cs" />
<Compile Include="Traits\Render\WithChargeOverlay.cs" />
@@ -405,6 +408,7 @@
<Compile Include="Traits\Render\WithSmoke.cs" />
<Compile Include="Traits\Render\WithSpriteBody.cs" />
<Compile Include="Traits\Render\WithTurret.cs" />
<Compile Include="Traits\Render\WithFacingSpriteBody.cs" />
<Compile Include="Traits\Render\WithBuildingPlacedOverlay.cs" />
<Compile Include="Traits\Render\WithProductionDoorOverlay.cs" />
<Compile Include="Traits\Render\WithProductionOverlay.cs" />

View File

@@ -0,0 +1,31 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class AutoSelectionSizeInfo : ITraitInfo, Requires<RenderSpritesInfo>
{
public object Create(ActorInitializer init) { return new AutoSelectionSize(this); }
}
public class AutoSelectionSize : IAutoSelectionSize
{
public AutoSelectionSize(AutoSelectionSizeInfo info) { }
public int2 SelectionSize(Actor self)
{
var rs = self.Trait<RenderSprites>();
return rs.AutoSelectionSize(self);
}
}
}

View File

@@ -16,7 +16,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Basic render trait for immobile actors.")]
[Desc("Basic render trait for immobile actors. Deprecated, use RenderSprites + WithSpriteBody instead.")]
public class RenderSimpleInfo : RenderSpritesInfo, IRenderActorPreviewSpritesInfo, IQuantizeBodyOrientationInfo, Requires<IBodyOrientationInfo>
{
public readonly string Sequence = "idle";

View File

@@ -236,8 +236,8 @@ namespace OpenRA.Mods.Common.Traits
return sequence;
}
// Required by RenderSimple
protected int2 AutoSelectionSize(Actor self)
// Required by RenderSimple, WithSpriteBody and WithInfantryBody
public int2 AutoSelectionSize(Actor self)
{
return anims.Where(b => b.IsVisible
&& b.Animation.Animation.CurrentSequence != null)

View File

@@ -13,7 +13,8 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Render trait for non-animated actors that have sprites facing into each direction.")]
[Desc("Render trait for non-animated actors that have sprites facing into each direction.",
"Deprecated. This will soon be removed, use RenderSprites + WithFacingSpriteBody instead.")]
public class RenderUnitInfo : RenderSimpleInfo, Requires<IFacingInfo>
{
public override object Create(ActorInitializer init) { return new RenderUnit(init, this); }

View File

@@ -0,0 +1,73 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
public class WithAttackAnimationInfo : ITraitInfo, Requires<WithFacingSpriteBodyInfo>, Requires<ArmamentInfo>, Requires<AttackBaseInfo>
{
[Desc("Armament name")]
public readonly string Armament = "primary";
[Desc("Displayed while attacking.")]
public readonly string AttackSequence = null;
[Desc("Displayed while targeting.")]
public readonly string AimSequence = null;
[Desc("Shown while reloading.")]
public readonly string ReloadPrefix = null;
public object Create(ActorInitializer init) { return new WithAttackAnimation(init, this); }
}
public class WithAttackAnimation : ITick, INotifyAttack
{
readonly WithAttackAnimationInfo info;
readonly AttackBase attack;
readonly Armament armament;
readonly WithFacingSpriteBody wfsb;
public WithAttackAnimation(ActorInitializer init, WithAttackAnimationInfo info)
{
this.info = info;
attack = init.Self.Trait<AttackBase>();
armament = init.Self.TraitsImplementing<Armament>()
.Single(a => a.Info.Name == info.Armament);
wfsb = init.Self.Trait<WithFacingSpriteBody>();
}
public void Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
if (!string.IsNullOrEmpty(info.AttackSequence))
wfsb.PlayCustomAnimation(self, info.AttackSequence);
}
public void Tick(Actor self)
{
if (string.IsNullOrEmpty(info.AimSequence) && string.IsNullOrEmpty(info.ReloadPrefix))
return;
var sequence = wfsb.Info.Sequence;
if (!string.IsNullOrEmpty(info.AimSequence) && attack.IsAttacking)
sequence = info.AimSequence;
var prefix = (armament.IsReloading && !string.IsNullOrEmpty(info.ReloadPrefix)) ? info.ReloadPrefix : "";
if (!string.IsNullOrEmpty(prefix) && sequence != (prefix + sequence))
sequence = prefix + sequence;
wfsb.DefaultAnimation.ReplaceAnim(sequence);
}
}
}

View File

@@ -0,0 +1,46 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class WithFacingSpriteBodyInfo : WithSpriteBodyInfo, Requires<IBodyOrientationInfo>, Requires<IFacingInfo>
{
public override object Create(ActorInitializer init) { return new WithFacingSpriteBody(init, this); }
public override IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
{
var ifacing = init.Actor.Traits.GetOrDefault<IFacingInfo>();
var facing = ifacing != null ? init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : ifacing.GetInitialFacing() : 0;
var anim = new Animation(init.World, image, () => facing);
anim.PlayRepeating(RenderSprites.NormalizeSequence(anim, init.GetDamageState(), Sequence));
yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale);
}
public override int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race)
{
var rsi = ai.Traits.Get<RenderSpritesInfo>();
return sequenceProvider.GetSequence(rsi.GetImage(ai, sequenceProvider, race), Sequence).Facings;
}
}
public class WithFacingSpriteBody : WithSpriteBody
{
public WithFacingSpriteBody(ActorInitializer init, WithFacingSpriteBodyInfo info)
: base(init, info, RenderSprites.MakeFacingFunc(init.Self)) { }
}
}

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Traits
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
anim = new Animation(self.World, rs.GetImage(self), RenderSimple.MakeFacingFunc(self));
anim = new Animation(self.World, rs.GetImage(self), RenderSprites.MakeFacingFunc(self));
anim.IsDecoration = true;
anim.Play(info.Sequence);
rs.Add(new AnimationWithOffset(anim,

View File

@@ -0,0 +1,54 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class WithMoveAnimationInfo : ITraitInfo, Requires<WithFacingSpriteBodyInfo>, Requires<IMoveInfo>
{
[Desc("Displayed while moving.")]
public readonly string MoveSequence = "move";
public object Create(ActorInitializer init) { return new WithMoveAnimation(init, this); }
}
public class WithMoveAnimation : ITick
{
readonly WithMoveAnimationInfo info;
readonly IMove movement;
readonly WithFacingSpriteBody wfsb;
WPos cachedPosition;
public WithMoveAnimation(ActorInitializer init, WithMoveAnimationInfo info)
{
this.info = info;
movement = init.Self.Trait<IMove>();
wfsb = init.Self.Trait<WithFacingSpriteBody>();
cachedPosition = init.Self.CenterPosition;
}
public void Tick(Actor self)
{
var oldCachedPosition = cachedPosition;
cachedPosition = self.CenterPosition;
// Flying units set IsMoving whenever they are airborne, which isn't enough for our purposes
var isMoving = movement.IsMoving && !self.IsDead && (oldCachedPosition - cachedPosition).HorizontalLengthSquared != 0;
if (isMoving ^ (wfsb.DefaultAnimation.CurrentSequence.Name != info.MoveSequence))
return;
wfsb.DefaultAnimation.ReplaceAnim(isMoving ? info.MoveSequence : wfsb.Info.Sequence);
}
}
}

View File

@@ -9,13 +9,16 @@
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Default trait for rendering sprite-based actors.")]
class WithSpriteBodyInfo : UpgradableTraitInfo, ITraitInfo, Requires<RenderSpritesInfo>
public class WithSpriteBodyInfo : UpgradableTraitInfo, ITraitInfo, IRenderActorPreviewSpritesInfo, IQuantizeBodyOrientationInfo,
Requires<RenderSpritesInfo>
{
[Desc("Animation to play when the actor is created.")]
public readonly string StartSequence = null;
@@ -23,30 +26,54 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Animation to play when the actor is idle.")]
public readonly string Sequence = "idle";
public object Create(ActorInitializer init) { return new WithSpriteBody(init, this); }
}
public virtual object Create(ActorInitializer init) { return new WithSpriteBody(init, this); }
class WithSpriteBody : UpgradableTrait<WithSpriteBodyInfo>, ISpriteBody
{
readonly Animation body;
readonly WithSpriteBodyInfo info;
public WithSpriteBody(ActorInitializer init, WithSpriteBodyInfo info)
: base(info)
public virtual IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
{
this.info = info;
var anim = new Animation(init.World, image);
anim.PlayRepeating(RenderSprites.NormalizeSequence(anim, init.GetDamageState(), Sequence));
var rs = init.Self.Trait<RenderSprites>();
body = new Animation(init.Self.World, rs.GetImage(init.Self));
PlayCustomAnimation(init.Self, info.StartSequence, () => body.PlayRepeating(info.Sequence));
rs.Add(new AnimationWithOffset(body, null, () => IsTraitDisabled));
yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale);
}
public void PlayCustomAnimation(Actor self, string newAnimation, Action after)
public virtual int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race)
{
body.PlayThen(newAnimation, () =>
return 1;
}
}
public class WithSpriteBody : UpgradableTrait<WithSpriteBodyInfo>, ISpriteBody
{
public readonly Animation DefaultAnimation;
public WithSpriteBody(ActorInitializer init, WithSpriteBodyInfo info)
: this(init, info, () => 0) { }
protected WithSpriteBody(ActorInitializer init, WithSpriteBodyInfo info, Func<int> baseFacing)
: base(info)
{
var rs = init.Self.Trait<RenderSprites>();
DefaultAnimation = new Animation(init.World, rs.GetImage(init.Self), baseFacing);
rs.Add(new AnimationWithOffset(DefaultAnimation, null, () => IsTraitDisabled));
if (Info.StartSequence != null)
PlayCustomAnimation(init.Self, Info.StartSequence,
() => DefaultAnimation.PlayRepeating(NormalizeSequence(init.Self, Info.Sequence)));
else
DefaultAnimation.PlayRepeating(NormalizeSequence(init.Self, Info.Sequence));
}
public string NormalizeSequence(Actor self, string sequence)
{
return RenderSprites.NormalizeSequence(DefaultAnimation, self.GetDamageState(), sequence);
}
public void PlayCustomAnimation(Actor self, string name, Action after = null)
{
DefaultAnimation.PlayThen(NormalizeSequence(self, name), () =>
{
body.Play(info.Sequence);
DefaultAnimation.Play(NormalizeSequence(self, Info.Sequence));
if (after != null)
after();
});
@@ -54,14 +81,15 @@ namespace OpenRA.Mods.Common.Traits
public void PlayCustomAnimationRepeating(Actor self, string name)
{
body.PlayThen(name, () => PlayCustomAnimationRepeating(self, name));
DefaultAnimation.PlayThen(name,
() => PlayCustomAnimationRepeating(self, name));
}
public void PlayCustomAnimationBackwards(Actor self, string name, Action after)
public void PlayCustomAnimationBackwards(Actor self, string name, Action after = null)
{
body.PlayBackwardsThen(name, () =>
DefaultAnimation.PlayBackwardsThen(NormalizeSequence(self, name), () =>
{
body.PlayRepeating(info.Sequence);
DefaultAnimation.PlayRepeating(NormalizeSequence(self, Info.Sequence));
if (after != null)
after();
});

View File

@@ -13,7 +13,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
class ThrowsParticleInfo : ITraitInfo, Requires<RenderSimpleInfo>, Requires<IBodyOrientationInfo>
class ThrowsParticleInfo : ITraitInfo, Requires<WithSpriteBodyInfo>, Requires<IBodyOrientationInfo>
{
public readonly string Anim = null;
@@ -57,7 +57,7 @@ namespace OpenRA.Mods.Common.Traits
public ThrowsParticle(ActorInitializer init, ThrowsParticleInfo info)
{
var self = init.Self;
var rs = self.Trait<RenderSimple>();
var rs = self.Trait<RenderSprites>();
var body = self.Trait<IBodyOrientation>();
// TODO: Carry orientation over from the parent instead of just facing

View File

@@ -1016,6 +1016,75 @@ namespace OpenRA.Mods.Common.UtilityCommands
}
}
if (engineVersion < 20150528)
{
// Note (stolen from WithInfantryBody upgrade rule):
// These rules are set up to do approximately the right thing for maps, but
// mods need additional manual tweaks. This is the best we can do without having
// much smarter rules parsing, because we currently can't reason about inherited traits.
if (depth == 0)
{
var childKeys = new[] { "Sequence" };
var ru = node.Value.Nodes.FirstOrDefault(n => n.Key == "RenderUnit");
if (ru != null)
{
ru.Key = "WithFacingSpriteBody";
node.Value.Nodes.Add(new MiniYamlNode("AutoSelectionSize", ""));
var rsNodes = ru.Value.Nodes.Where(n => !childKeys.Contains(n.Key)).ToList();
if (rsNodes.Any())
node.Value.Nodes.Add(new MiniYamlNode("RenderSprites", new MiniYaml("", rsNodes)));
else
node.Value.Nodes.Add(new MiniYamlNode("RenderSprites", ""));
ru.Value.Nodes.RemoveAll(n => rsNodes.Contains(n));
}
var rru = node.Value.Nodes.FirstOrDefault(n => n.Key == "-RenderUnit");
if (rru != null)
rru.Key = "-WithFacingSpriteBody";
}
// For RenderUnitReload
var rur = node.Value.Nodes.Where(x => x.Key == "RenderUnitReload");
if (rur.Any())
{
rur.Do(x => x.Key = "RenderSprites");
node.Value.Nodes.Add(new MiniYamlNode("AutoSelectionSize", ""));
node.Value.Nodes.Add(new MiniYamlNode("WithFacingSpriteBody", "", new List<MiniYamlNode>
{
new MiniYamlNode("Sequence", "idle")
}));
node.Value.Nodes.Add(new MiniYamlNode("WithAttackAnimation", "", new List<MiniYamlNode>
{
new MiniYamlNode("AimSequence", "aim"),
new MiniYamlNode("ReloadPrefix", "empty-")
}));
var rrur = node.Value.Nodes.FirstOrDefault(n => n.Key == "-RenderUnitReload");
if (rrur != null)
rrur.Key = "-WithFacingSpriteBody";
}
// For RenderUnitFlying
var ruf = node.Value.Nodes.Where(x => x.Key == "RenderUnitFlying");
if (ruf.Any())
{
ruf.Do(x => x.Key = "RenderSprites");
node.Value.Nodes.Add(new MiniYamlNode("AutoSelectionSize", ""));
node.Value.Nodes.Add(new MiniYamlNode("WithFacingSpriteBody", ""));
node.Value.Nodes.Add(new MiniYamlNode("WithMoveAnimation", "", new List<MiniYamlNode>
{
new MiniYamlNode("MoveSequence", "move")
}));
var rruf = node.Value.Nodes.FirstOrDefault(n => n.Key == "-RenderUnitFlying");
if (rruf != null)
rruf.Key = "-WithFacingSpriteBody";
}
}
UpgradeActorRules(engineVersion, ref node.Value.Nodes, node, depth + 1);
}
}