Skip to content

Commit

Permalink
Merge pull request #8171 from reaperrr/withunitbody1
Browse files Browse the repository at this point in the history
Introduce WithFacingSpriteBody and related With*Animation traits
  • Loading branch information
Mailaender committed Jun 7, 2015
2 parents e445cc4 + d529061 commit 9882a6e
Show file tree
Hide file tree
Showing 41 changed files with 754 additions and 284 deletions.
1 change: 0 additions & 1 deletion OpenRA.Mods.Cnc/OpenRA.Mods.Cnc.csproj
Expand Up @@ -72,7 +72,6 @@
<Compile Include="Traits\SupportPowers\IonCannonPower.cs" />
<Compile Include="Widgets\Logic\CncMainMenuLogic.cs" />
<Compile Include="Widgets\Logic\ProductionTabsLogic.cs" />
<Compile Include="Traits\RenderUnitFlying.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">
Expand Down
4 changes: 4 additions & 0 deletions OpenRA.Mods.Common/OpenRA.Mods.Common.csproj
Expand Up @@ -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" />
Expand All @@ -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" />
Expand All @@ -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" />
Expand Down
31 changes: 31 additions & 0 deletions OpenRA.Mods.Common/Traits/Render/AutoSelectionSize.cs
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/Render/RenderSimple.cs
Expand Up @@ -16,7 +16,7 @@

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";
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/Render/RenderSprites.cs
Expand Up @@ -236,8 +236,8 @@ public static string NormalizeSequence(Animation anim, DamageState state, string
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)
Expand Down
3 changes: 2 additions & 1 deletion OpenRA.Mods.Common/Traits/Render/RenderUnit.cs
Expand Up @@ -13,7 +13,8 @@

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); }
Expand Down
73 changes: 73 additions & 0 deletions OpenRA.Mods.Common/Traits/Render/WithAttackAnimation.cs
@@ -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);
}
}
}
46 changes: 46 additions & 0 deletions OpenRA.Mods.Common/Traits/Render/WithFacingSpriteBody.cs
@@ -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)) { }
}
}
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/Render/WithHarvestAnimation.cs
Expand Up @@ -40,7 +40,7 @@ public WithHarvestAnimation(Actor self, WithHarvestAnimationInfo info)
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,
Expand Down
Expand Up @@ -8,47 +8,47 @@
*/
#endregion

using OpenRA.Mods.Common.Traits;
using System.Linq;
using OpenRA.Traits;

namespace OpenRA.Mods.Cnc.Traits
namespace OpenRA.Mods.Common.Traits
{
class RenderUnitFlyingInfo : RenderUnitInfo, Requires<IMoveInfo>
public class WithMoveAnimationInfo : ITraitInfo, Requires<WithFacingSpriteBodyInfo>, Requires<IMoveInfo>
{
[Desc("Displayed while moving.")]
public readonly string MoveSequence = "move";

public override object Create(ActorInitializer init) { return new RenderUnitFlying(init, this); }
public object Create(ActorInitializer init) { return new WithMoveAnimation(init, this); }
}

class RenderUnitFlying : RenderUnit, ITick
public class WithMoveAnimation : ITick
{
readonly RenderUnitFlyingInfo info;
readonly WithMoveAnimationInfo info;
readonly IMove movement;
readonly WithFacingSpriteBody wfsb;

WPos cachedPosition;

public RenderUnitFlying(ActorInitializer init, RenderUnitFlyingInfo info)
: base(init, info)
public WithMoveAnimation(ActorInitializer init, WithMoveAnimationInfo info)
{
this.info = info;
movement = init.Self.Trait<IMove>();
wfsb = init.Self.Trait<WithFacingSpriteBody>();

cachedPosition = init.Self.CenterPosition;
}

public override void Tick(Actor self)
public void Tick(Actor self)
{
base.Tick(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 ^ (DefaultAnimation.CurrentSequence.Name != info.MoveSequence))
if (isMoving ^ (wfsb.DefaultAnimation.CurrentSequence.Name != info.MoveSequence))
return;

DefaultAnimation.ReplaceAnim(isMoving ? info.MoveSequence : info.Sequence);
wfsb.DefaultAnimation.ReplaceAnim(isMoving ? info.MoveSequence : wfsb.Info.Sequence);
}
}
}
62 changes: 45 additions & 17 deletions OpenRA.Mods.Common/Traits/Render/WithSpriteBody.cs
Expand Up @@ -9,59 +9,87 @@
#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;

[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); }

public virtual IEnumerable<IActorPreview> RenderPreviewSprites(ActorPreviewInitializer init, RenderSpritesInfo rs, string image, int facings, PaletteReference p)
{
var anim = new Animation(init.World, image);
anim.PlayRepeating(RenderSprites.NormalizeSequence(anim, init.GetDamageState(), Sequence));

yield return new SpriteActorPreview(anim, WVec.Zero, 0, p, rs.Scale);
}

public virtual int QuantizedBodyFacings(ActorInfo ai, SequenceProvider sequenceProvider, string race)
{
return 1;
}
}

class WithSpriteBody : UpgradableTrait<WithSpriteBodyInfo>, ISpriteBody
public class WithSpriteBody : UpgradableTrait<WithSpriteBodyInfo>, ISpriteBody
{
readonly Animation body;
readonly WithSpriteBodyInfo info;
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)
{
this.info = info;

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));

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 newAnimation, Action after)
public void PlayCustomAnimation(Actor self, string name, Action after = null)
{
body.PlayThen(newAnimation, () =>
DefaultAnimation.PlayThen(NormalizeSequence(self, name), () =>
{
body.Play(info.Sequence);
DefaultAnimation.Play(NormalizeSequence(self, Info.Sequence));
if (after != null)
after();
});
}

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();
});
Expand Down

0 comments on commit 9882a6e

Please sign in to comment.