From 0703f3f164e225a7bf2eeda797c6390901e07060 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 24 Feb 2013 13:11:43 +1300 Subject: [PATCH] Extract auto-selection-size into its own interface. --- OpenRA.Game/Actor.cs | 14 +++++--------- OpenRA.Game/Traits/Render/RenderSimple.cs | 11 ++++++++++- OpenRA.Game/Traits/TraitsInterfaces.cs | 1 + 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/OpenRA.Game/Actor.cs b/OpenRA.Game/Actor.cs index f431ad149a..f61623c4da 100755 --- a/OpenRA.Game/Actor.cs +++ b/OpenRA.Game/Actor.cs @@ -74,7 +74,7 @@ namespace OpenRA AddTrait(trait.Create(init)); } - Move = Lazy.New( () => TraitOrDefault() ); + Move = Lazy.New(() => TraitOrDefault()); Size = Lazy.New(() => { @@ -82,27 +82,23 @@ namespace OpenRA if (si != null && si.Bounds != null) return new int2(si.Bounds[0], si.Bounds[1]); - // auto size from render - var firstSprite = TraitsImplementing().SelectMany(ApplyIRender).FirstOrDefault(); - if (firstSprite.Sprite == null) return int2.Zero; - return (firstSprite.Sprite.size * firstSprite.Scale).ToInt2(); + return TraitsImplementing().Select(x => x.SelectionSize(this)).FirstOrDefault(); }); - if(this.HasTrait()) + if (this.HasTrait()) { Sight = new Shroud.ActorVisibility { range = this.Trait().RevealRange, vis = Shroud.GetVisOrigins(this).ToArray() }; - } ApplyIRender = x => x.Render(this); ApplyRenderModifier = (m, p, wr) => p.ModifyRender(this, wr, m); - Bounds = Cached.New( () => CalculateBounds(false) ); - ExtendedBounds = Cached.New( () => CalculateBounds(true) ); + Bounds = Cached.New(() => CalculateBounds(false)); + ExtendedBounds = Cached.New(() => CalculateBounds(true)); } public void Tick() diff --git a/OpenRA.Game/Traits/Render/RenderSimple.cs b/OpenRA.Game/Traits/Render/RenderSimple.cs index 3ada42970c..28cdff3aee 100755 --- a/OpenRA.Game/Traits/Render/RenderSimple.cs +++ b/OpenRA.Game/Traits/Render/RenderSimple.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; +using System.Linq; using OpenRA.Graphics; namespace OpenRA.Traits @@ -32,7 +33,7 @@ namespace OpenRA.Traits } } - public class RenderSimple : IRender, ITick + public class RenderSimple : IRender, IAutoSelectionSize, ITick { public Dictionary anims = new Dictionary(); @@ -94,6 +95,14 @@ namespace OpenRA.Traits } } + public int2 SelectionSize(Actor self) + { + return anims.Values.Where(b => (b.DisableFunc == null || !b.DisableFunc()) + && b.Animation.CurrentSequence != null) + .Select(a => (a.Animation.Image.size*Info.Scale).ToInt2()) + .FirstOrDefault(); + } + public virtual void Tick(Actor self) { foreach (var a in anims.Values) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 2b100763a3..06ed8b92aa 100755 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -35,6 +35,7 @@ namespace OpenRA.Traits public interface ITick { void Tick(Actor self); } public interface IRender { IEnumerable Render(Actor self); } + public interface IAutoSelectionSize { int2 SelectionSize(Actor self); } public interface IIssueOrder {