diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 29cf8054bf..d917cb4b66 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -225,6 +225,7 @@ + diff --git a/OpenRA.Game/Traits/Render/RenderSimple.cs b/OpenRA.Game/Traits/Render/RenderSimple.cs index f26e73ea2a..4cad396ad6 100755 --- a/OpenRA.Game/Traits/Render/RenderSimple.cs +++ b/OpenRA.Game/Traits/Render/RenderSimple.cs @@ -16,23 +16,14 @@ using OpenRA.FileFormats; namespace OpenRA.Traits { - public class RenderSimpleInfo : ITraitInfo, LocalCoordinatesModelInfo + public class RenderSimpleInfo : RenderSpritesInfo, LocalCoordinatesModelInfo { - [Desc("Defaults to the actor name.")] - public readonly string Image = null; - [Desc("custom palette name")] - public readonly string Palette = null; - [Desc("custom PlayerColorPalette: BaseName")] - public readonly string PlayerPalette = "player"; - [Desc("Change the sprite image size.")] - public readonly float Scale = 1f; - [Desc("Number of facings for gameplay calculations. -1 indiciates auto-detection from sequence")] public readonly int QuantizedFacings = -1; [Desc("Camera pitch the sprite was rendered with. Used to determine rotation ellipses")] public readonly WAngle CameraPitch = WAngle.FromDegrees(40); - public virtual object Create(ActorInitializer init) { return new RenderSimple(init.self); } + public override object Create(ActorInitializer init) { return new RenderSimple(init.self); } public virtual IEnumerable RenderPreview(ActorInfo ai, PaletteReference pr) { @@ -43,75 +34,23 @@ namespace OpenRA.Traits } } - public class RenderSimple : IRender, ILocalCoordinatesModel, IAutoSelectionSize, ITick, INotifyOwnerChanged + public class RenderSimple : RenderSprites, ILocalCoordinatesModel, IAutoSelectionSize { - public Dictionary anims = new Dictionary(); - - public static Func MakeFacingFunc(Actor self) - { - var facing = self.TraitOrDefault(); - if (facing == null) return () => 0; - return () => facing.Facing; - } - - public Animation anim - { - get { return anims[""].Animation; } - protected set { anims[""] = new AnimationWithOffset(value, - anims[""].OffsetFunc, anims[""].DisableFunc, anims[""].ZOffset); } - } - - public static string GetImage(ActorInfo actor) - { - var Info = actor.Traits.Get(); - return Info.Image ?? actor.Name; - } - - public string GetImage(Actor self) - { - if (cachedImage != null) - return cachedImage; - - return cachedImage = GetImage(self.Info); - } - RenderSimpleInfo Info; - string cachedImage = null; - bool initializePalette = true; - protected PaletteReference palette; public RenderSimple(Actor self, Func baseFacing) + : base(self, baseFacing) { anims.Add("", new Animation(GetImage(self), baseFacing)); Info = self.Info.Traits.Get(); } - public RenderSimple(Actor self) : this( self, MakeFacingFunc(self) ) + public RenderSimple(Actor self) + : this(self, MakeFacingFunc(self)) { anim.PlayRepeating("idle"); } - protected virtual string PaletteName(Actor self) - { - return Info.Palette ?? Info.PlayerPalette + self.Owner.InternalName; - } - - protected void UpdatePalette() { initializePalette = true; } - public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) { UpdatePalette(); } - - public virtual IEnumerable Render(Actor self, WorldRenderer wr) - { - if (initializePalette) - { - palette = wr.Palette(PaletteName(self)); - initializePalette = false; - } - - foreach (var a in anims.Values) - if (a.DisableFunc == null || !a.DisableFunc()) - yield return a.Image(self, wr, palette, Info.Scale); - } - public int2 SelectionSize(Actor self) { return anims.Values.Where(b => (b.DisableFunc == null || !b.DisableFunc()) @@ -120,12 +59,6 @@ namespace OpenRA.Traits .FirstOrDefault(); } - public virtual void Tick(Actor self) - { - foreach (var a in anims.Values) - a.Animation.Tick(); - } - protected virtual string NormalizeSequence(Actor self, string baseSequence) { string damageState = self.GetDamageState() >= DamageState.Heavy ? "damaged-" : ""; diff --git a/OpenRA.Game/Traits/Render/RenderSprites.cs b/OpenRA.Game/Traits/Render/RenderSprites.cs new file mode 100755 index 0000000000..9e6fd1a429 --- /dev/null +++ b/OpenRA.Game/Traits/Render/RenderSprites.cs @@ -0,0 +1,107 @@ +#region Copyright & License Information +/* + * Copyright 2007-2011 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 System.Linq; +using OpenRA.Graphics; +using OpenRA.FileFormats; + +namespace OpenRA.Traits +{ + public class RenderSpritesInfo : ITraitInfo + { + [Desc("Defaults to the actor name.")] + public readonly string Image = null; + + [Desc("Custom palette name")] + public readonly string Palette = null; + [Desc("Custom PlayerColorPalette: BaseName")] + public readonly string PlayerPalette = "player"; + [Desc("Change the sprite image size.")] + public readonly float Scale = 1f; + + public virtual object Create(ActorInitializer init) { return new RenderSprites(init.self); } + } + + public class RenderSprites : IRender, ITick, INotifyOwnerChanged + { + public Dictionary anims = new Dictionary(); + + public static Func MakeFacingFunc(Actor self) + { + var facing = self.TraitOrDefault(); + if (facing == null) return () => 0; + return () => facing.Facing; + } + + public Animation anim + { + get { return anims[""].Animation; } + protected set { anims[""] = new AnimationWithOffset(value, + anims[""].OffsetFunc, anims[""].DisableFunc, anims[""].ZOffset); } + } + + RenderSpritesInfo Info; + string cachedImage = null; + bool initializePalette = true; + protected PaletteReference palette; + + public RenderSprites(Actor self, Func baseFacing) + { + Info = self.Info.Traits.Get(); + } + + public RenderSprites(Actor self) + : this(self, MakeFacingFunc(self)) {} + + public static string GetImage(ActorInfo actor) + { + var Info = actor.Traits.Get(); + return Info.Image ?? actor.Name; + } + + public string GetImage(Actor self) + { + if (cachedImage != null) + return cachedImage; + + return cachedImage = GetImage(self.Info); + } + + protected virtual string PaletteName(Actor self) + { + return Info.Palette ?? Info.PlayerPalette + self.Owner.InternalName; + } + + protected void UpdatePalette() { initializePalette = true; } + public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) { UpdatePalette(); } + + public virtual IEnumerable Render(Actor self, WorldRenderer wr) + { + if (initializePalette) + { + palette = wr.Palette(PaletteName(self)); + initializePalette = false; + } + + foreach (var a in anims.Values) + if (a.DisableFunc == null || !a.DisableFunc()) + yield return a.Image(self, wr, palette, Info.Scale); + } + + public virtual void Tick(Actor self) + { + foreach (var a in anims.Values) + a.Animation.Tick(); + } + + } +}