From 073cdc202d19363095e41071012290fe77266ba1 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Tue, 8 Dec 2009 20:21:24 +1300 Subject: [PATCH] reuse CenterOfCell --- OpenRa.Game/Game.cs | 4 ++-- OpenRa.Game/Traits/Activities/Move.cs | 24 +++++++----------------- OpenRa.Game/Traits/Helicopter.cs | 2 +- OpenRa.Game/Traits/RallyPoint.cs | 4 ++-- OpenRa.Game/Traits/Util.cs | 10 ++++++++++ 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index d8c54c6ded..efe5d1610b 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -70,7 +70,7 @@ namespace OpenRa.Game BuildingInfluence = new BuildingInfluenceMap(); UnitInfluence = new UnitInfluenceMap(); - foreach (TreeReference treeReference in Rules.Map.Trees) + foreach (var treeReference in Rules.Map.Trees) world.Add(new Actor(Rules.UnitInfo[treeReference.Image], new int2(treeReference.Location), null)); @@ -104,7 +104,7 @@ namespace OpenRa.Game foreach (var s in toLoad) { - //num=owner,type,health,location,facing,action,trigger + //num=owner,type,health,location,facing,... var parts = s.Value.Split( ',' ); var loc = int.Parse(parts[3]); world.Add(new Actor(Rules.UnitInfo[parts[1].ToLowerInvariant()], new int2(loc % 128, loc / 128), diff --git a/OpenRa.Game/Traits/Activities/Move.cs b/OpenRa.Game/Traits/Activities/Move.cs index 746906b44d..603b73232e 100755 --- a/OpenRa.Game/Traits/Activities/Move.cs +++ b/OpenRa.Game/Traits/Activities/Move.cs @@ -93,8 +93,8 @@ namespace OpenRa.Game.Traits.Activities { mobile.toCell = nextCell.Value; move = new MoveFirstHalf( - CenterOfCell( mobile.fromCell ), - BetweenCells( mobile.fromCell, mobile.toCell ), + Util.CenterOfCell( mobile.fromCell ), + Util.BetweenCells( mobile.fromCell, mobile.toCell ), unit.Facing, unit.Facing, 0 ); @@ -142,16 +142,6 @@ namespace OpenRa.Game.Traits.Activities return nextCell; } - static float2 CenterOfCell( int2 loc ) - { - return new float2( 12, 12 ) + Game.CellSize * (float2)loc; - } - - static float2 BetweenCells( int2 from, int2 to ) - { - return 0.5f * ( CenterOfCell( from ) + CenterOfCell( to ) ); - } - public void Cancel( Actor self ) { path = new List(); @@ -222,8 +212,8 @@ namespace OpenRa.Game.Traits.Activities if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) ) { var ret = new MoveFirstHalf( - BetweenCells( mobile.fromCell, mobile.toCell ), - BetweenCells( mobile.toCell, nextCell.Value ), + Util.BetweenCells( mobile.fromCell, mobile.toCell ), + Util.BetweenCells( mobile.toCell, nextCell.Value ), unit.Facing, Util.GetNearestFacing( unit.Facing, Util.GetFacing( nextCell.Value - mobile.toCell, unit.Facing ) ), moveFraction - moveFractionTotal ); @@ -236,8 +226,8 @@ namespace OpenRa.Game.Traits.Activities parent.path.Add( nextCell.Value ); } var ret2 = new MoveSecondHalf( - BetweenCells( mobile.fromCell, mobile.toCell ), - CenterOfCell( mobile.toCell ), + Util.BetweenCells( mobile.fromCell, mobile.toCell ), + Util.CenterOfCell( mobile.toCell ), unit.Facing, unit.Facing, moveFraction - moveFractionTotal ); @@ -256,7 +246,7 @@ namespace OpenRa.Game.Traits.Activities protected override MovePart OnComplete( Actor self, Mobile mobile, Move parent ) { - self.CenterLocation = CenterOfCell( mobile.toCell ); + self.CenterLocation = Util.CenterOfCell( mobile.toCell ); mobile.fromCell = mobile.toCell; return null; } diff --git a/OpenRa.Game/Traits/Helicopter.cs b/OpenRa.Game/Traits/Helicopter.cs index 22a512f417..36a0bfa69d 100644 --- a/OpenRa.Game/Traits/Helicopter.cs +++ b/OpenRa.Game/Traits/Helicopter.cs @@ -44,7 +44,7 @@ namespace OpenRa.Game.Traits if (self.Location != targetLocation) { - var dist = Game.CellSize * (targetLocation + new float2(.5f,.5f)) - self.CenterLocation; + var dist = Util.CenterOfCell(targetLocation) - self.CenterLocation; var desiredFacing = Util.GetFacing(dist, unit.Facing); Util.TickFacing(ref unit.Facing, desiredFacing, self.Info.ROT); diff --git a/OpenRa.Game/Traits/RallyPoint.cs b/OpenRa.Game/Traits/RallyPoint.cs index 63eb7906bd..3d2a046e37 100644 --- a/OpenRa.Game/Traits/RallyPoint.cs +++ b/OpenRa.Game/Traits/RallyPoint.cs @@ -20,8 +20,8 @@ namespace OpenRa.Game.Traits { var uog = Game.controller.orderGenerator as UnitOrderGenerator; if (uog != null && self.Owner == Game.LocalPlayer && uog.selection.Contains(self)) - yield return Util.Centered( self, - anim.Image, Game.CellSize * (new float2(.5f, .5f) + rallyPoint.ToFloat2())); + yield return Util.Centered(self, + anim.Image, Util.CenterOfCell(rallyPoint)); } public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor) diff --git a/OpenRa.Game/Traits/Util.cs b/OpenRa.Game/Traits/Util.cs index b1604e0b5c..df0cd9aef4 100755 --- a/OpenRa.Game/Traits/Util.cs +++ b/OpenRa.Game/Traits/Util.cs @@ -90,6 +90,16 @@ namespace OpenRa.Game.Traits return RotateVectorByFacing(new float2(0, recoil * self.Info.Recoil), quantizedFacing, .7f); } + public static float2 CenterOfCell(int2 loc) + { + return new float2(12, 12) + Game.CellSize * (float2)loc; + } + + public static float2 BetweenCells(int2 from, int2 to) + { + return 0.5f * (CenterOfCell(from) + CenterOfCell(to)); + } + public static float2 GetTurretPosition(Actor self, Unit unit, int[] offset, float recoil) { if( unit == null ) return int2.Zero; /* things that don't have a rotating base don't need the turrets repositioned */