From abdd7b9f081422f136e9edbff766df73a9d3bbd3 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Mon, 12 Apr 2010 21:21:23 +1200 Subject: [PATCH] wall hack --- .../Traits/Render/RenderBuildingWall.cs | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/OpenRA.Game/Traits/Render/RenderBuildingWall.cs b/OpenRA.Game/Traits/Render/RenderBuildingWall.cs index a55bd846a0..7188046913 100644 --- a/OpenRA.Game/Traits/Render/RenderBuildingWall.cs +++ b/OpenRA.Game/Traits/Render/RenderBuildingWall.cs @@ -19,6 +19,7 @@ #endregion using System.Linq; +using System; namespace OpenRA.Traits { @@ -33,6 +34,7 @@ namespace OpenRA.Traits string seqName; int damageStates; Actor self; + int adjacentWalls = 0; public RenderBuildingWall(Actor self) : base(self) @@ -40,6 +42,8 @@ namespace OpenRA.Traits seqName = "idle"; this.self = self; this.damageStates = self.Info.Traits.Get().DamageStates; + + anim.PlayFetchIndex(seqName, () => adjacentWalls); } enum ExtendedDamageState { Normal, ThreeQuarter, Half, Quarter, Dead }; @@ -91,37 +95,36 @@ namespace OpenRA.Traits } break; } + + anim.PlayFetchIndex(seqName, () => adjacentWalls); } - + + bool hasTicked = false; public override void Tick(Actor self) { base.Tick(self); - - // TODO: This only needs updating when a wall is built or destroyed - int index = NearbyWalls( self.Location ); - - anim.PlayFetchIndex(seqName, () => index); + if (!hasTicked) + { + var oneCell = new float2(Game.CellSize, Game.CellSize); + var adjWalls = self.World.FindUnits(self.CenterLocation - oneCell, self.CenterLocation + oneCell) + .Where(a => a.Info == self.Info && a != self); + + foreach (var w in adjWalls) + { + w.traits.Get().AddAdjacentWall(w, self); + AddAdjacentWall(self, w); + } + hasTicked = true; + } } - bool IsWall( int x, int y) + + void AddAdjacentWall(Actor self, Actor other) { - return self.World.Queries.WithTrait().Any(a => (a.Actor.Info.Name == self.Info.Name && a.Actor.Location.X == x && a.Actor.Location.Y == y)); + if (other.Location == self.Location + new int2(0, -1)) adjacentWalls |= 1; + if (other.Location == self.Location + new int2(+1, 0)) adjacentWalls |= 2; + if (other.Location == self.Location + new int2(0, +1)) adjacentWalls |= 4; + if (other.Location == self.Location + new int2(-1, 0)) adjacentWalls |= 8; } - - int NearbyWalls( int2 xy ) - { - int ret = 0; - - if( IsWall( xy.X, xy.Y - 1 ) ) - ret |= 1; - if( IsWall( xy.X + 1, xy.Y ) ) - ret |= 2; - if( IsWall( xy.X, xy.Y + 1 ) ) - ret |= 4; - if( IsWall( xy.X - 1, xy.Y ) ) - ret |= 8; - return ret; - } - } }