From da84b664242bdfc7e784be2167f1b8b3ec2329ff Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sun, 29 Nov 2009 15:26:55 +1300 Subject: [PATCH] moved inner classes out of UnitInfo for brevity. added money-up and money-down sounds. added slow view of money changes, like real-ra. --- OpenRa.Game/Chrome.cs | 4 +- OpenRa.Game/Controller.cs | 12 ++-- OpenRa.Game/Game.cs | 6 +- OpenRa.Game/GameRules/Footprint.cs | 8 +-- OpenRa.Game/GameRules/Rules.cs | 12 ++-- OpenRa.Game/GameRules/TechTree.cs | 4 +- OpenRa.Game/GameRules/UnitInfo.cs | 80 +++++++++++++-------------- OpenRa.Game/PlaceBuilding.cs | 4 +- OpenRa.Game/Player.cs | 21 +++++++ OpenRa.Game/Traits/Activities/Move.cs | 2 +- OpenRa.Game/Traits/Building.cs | 4 +- OpenRa.Game/Traits/Helicopter.cs | 2 +- OpenRa.Game/Traits/InfantrySquad.cs | 4 +- OpenRa.Game/Traits/McvDeploy.cs | 2 +- OpenRa.Game/Traits/Mobile.cs | 4 +- OpenRa.Game/UiOverlay.cs | 2 +- OpenRa.Game/UnitOrders.cs | 2 +- 17 files changed, 96 insertions(+), 77 deletions(-) diff --git a/OpenRa.Game/Chrome.cs b/OpenRa.Game/Chrome.cs index 07d40fd1cc..270ef53c6e 100644 --- a/OpenRa.Game/Chrome.cs +++ b/OpenRa.Game/Chrome.cs @@ -91,7 +91,7 @@ namespace OpenRa.Game Game.orderManager.FrameNumber, PerfHistory.items["render"].LastValue, PerfHistory.items["tick_time"].LastValue, - Game.LocalPlayer.Cash, + Game.LocalPlayer.DisplayCash, Game.LocalPlayer.GetTotalPower() ), new int2(140, 5), Color.White); @@ -100,7 +100,7 @@ namespace OpenRa.Game chromeRenderer.DrawSprite(specialBinSprite, float2.Zero, 0); chromeRenderer.DrawSprite(moneyBinSprite, new float2(Game.viewport.Width - 320, 0), 0); - var moneyDigits = Game.LocalPlayer.Cash.ToString(); + var moneyDigits = Game.LocalPlayer.DisplayCash.ToString(); var x = Game.viewport.Width - 155; foreach (var d in moneyDigits.Reverse()) { diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index 174f61b43c..f5b7db954c 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -27,7 +27,7 @@ namespace OpenRa.Game if (order.Subject != null && order.Player == Game.LocalPlayer) doVoice = order.Subject; } - if (doVoice != null && doVoice.traits.Contains()) + if (doVoice != null && doVoice.traits.Contains()) Game.PlaySound(Game.SovietVoices.First.GetNext() + GetVoiceSuffix(doVoice), false); } @@ -43,7 +43,7 @@ namespace OpenRa.Game static string GetVoiceSuffix(Actor unit) { var suffixes = new[] { ".r01", ".r03" }; - return suffixes[unit.traits.Get().Voice]; + return suffixes[unit.ActorID % suffixes.Length]; } float2 dragStart, dragEnd; @@ -69,12 +69,12 @@ namespace OpenRa.Game Game.SelectActorsInBox(Game.CellSize * dragStart, Game.CellSize * xy)); var voicedUnit = ((UnitOrderGenerator)orderGenerator).selection - .Select(a => a.traits.GetOrDefault()) - .Where(m => m != null && m.self.Owner == Game.LocalPlayer) + .Where(a => a.traits.Contains() + && a.Owner == Game.LocalPlayer) .FirstOrDefault(); if (voicedUnit != null) - Game.PlaySound(Game.SovietVoices.Second.GetNext() + GetVoiceSuffix(voicedUnit.self), false); + Game.PlaySound(Game.SovietVoices.Second.GetNext() + GetVoiceSuffix(voicedUnit), false); } dragStart = dragEnd = xy; @@ -124,7 +124,7 @@ namespace OpenRa.Game else return Cursor.MoveBlocked; case "DeployMcv": - var factBuildingInfo = (UnitInfo.BuildingInfo)Rules.UnitInfo[ "fact" ]; + var factBuildingInfo = (BuildingInfo)Rules.UnitInfo[ "fact" ]; if( Game.CanPlaceBuilding( factBuildingInfo, a.Location - new int2( 1, 1 ), a, false ) ) return Cursor.Deploy; else diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index c18b9c2cc4..7513a2f51d 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -303,7 +303,7 @@ namespace OpenRa.Game return null; } - public static bool CanPlaceBuilding(UnitInfo.BuildingInfo building, int2 xy, Actor toIgnore, bool adjust) + public static bool CanPlaceBuilding(BuildingInfo building, int2 xy, Actor toIgnore, bool adjust) { return !Footprint.Tiles(building, xy, adjust).Any( t => Rules.Map.ContainsResource(t) || !Game.IsCellBuildable(t, @@ -311,12 +311,12 @@ namespace OpenRa.Game toIgnore)); } - public static bool CanPlaceBuilding(UnitInfo.BuildingInfo building, int2 xy, bool adjust) + public static bool CanPlaceBuilding(BuildingInfo building, int2 xy, bool adjust) { return CanPlaceBuilding(building, xy, null, adjust); } - public static bool IsCloseEnoughToBase(Player p, UnitInfo.BuildingInfo bi, int2 position) + public static bool IsCloseEnoughToBase(Player p, BuildingInfo bi, int2 position) { var maxDistance = bi.Adjacent + 2; /* real-ra is weird. this is 1 GAP. */ diff --git a/OpenRa.Game/GameRules/Footprint.cs b/OpenRa.Game/GameRules/Footprint.cs index 8842af3d2c..2438f4755f 100644 --- a/OpenRa.Game/GameRules/Footprint.cs +++ b/OpenRa.Game/GameRules/Footprint.cs @@ -9,12 +9,12 @@ namespace OpenRa.Game.GameRules { static class Footprint { - public static IEnumerable Tiles( UnitInfo.BuildingInfo buildingInfo, int2 position ) + public static IEnumerable Tiles( BuildingInfo buildingInfo, int2 position ) { return Tiles(buildingInfo, position, true); } - public static IEnumerable Tiles( UnitInfo.BuildingInfo buildingInfo, int2 position, bool adjustForPlacement ) + public static IEnumerable Tiles( BuildingInfo buildingInfo, int2 position, bool adjustForPlacement ) { var dim = buildingInfo.Dimensions; @@ -36,7 +36,7 @@ namespace OpenRa.Game.GameRules return Tiles( building.unitInfo, a.Location, false ); } - public static IEnumerable UnpathableTiles( UnitInfo.BuildingInfo buildingInfo, int2 position ) + public static IEnumerable UnpathableTiles( BuildingInfo buildingInfo, int2 position ) { var footprint = buildingInfo.Footprint.Where( x => !char.IsWhiteSpace( x ) ).ToArray(); foreach( var tile in TilesWhere( buildingInfo.Name, buildingInfo.Dimensions, footprint, a => a == 'x' ) ) @@ -55,7 +55,7 @@ namespace OpenRa.Game.GameRules yield return new int2( x, y ); } - public static int2 AdjustForBuildingSize( UnitInfo.BuildingInfo unitInfo ) + public static int2 AdjustForBuildingSize( BuildingInfo unitInfo ) { var dim = unitInfo.Dimensions; return new int2( dim.X / 2, dim.Y > 1 ? ( dim.Y + 1 ) / 2 : 0 ); diff --git a/OpenRa.Game/GameRules/Rules.cs b/OpenRa.Game/GameRules/Rules.cs index 8ee2c516e8..817786ae1a 100755 --- a/OpenRa.Game/GameRules/Rules.cs +++ b/OpenRa.Game/GameRules/Rules.cs @@ -53,12 +53,12 @@ namespace OpenRa.Game UnitCategory = Categories.SelectMany(x => x.Value.Select(y => new KeyValuePair(y, x.Key))).ToDictionary(x => x.Key, x => x.Value); UnitInfo = new InfoLoader( - Pair.New>("Building", s => new UnitInfo.BuildingInfo(s)), - Pair.New>("Defense", s => new UnitInfo.BuildingInfo(s)), - Pair.New>("Infantry", s => new UnitInfo.InfantryInfo(s)), - Pair.New>("Vehicle", s => new UnitInfo.VehicleInfo(s)), - Pair.New>("Ship", s => new UnitInfo.VehicleInfo(s)), - Pair.New>("Plane", s => new UnitInfo.VehicleInfo(s))); + Pair.New>("Building", s => new BuildingInfo(s)), + Pair.New>("Defense", s => new BuildingInfo(s)), + Pair.New>("Infantry", s => new InfantryInfo(s)), + Pair.New>("Vehicle", s => new VehicleInfo(s)), + Pair.New>("Ship", s => new VehicleInfo(s)), + Pair.New>("Plane", s => new VehicleInfo(s))); LoadCategories( "Weapon", diff --git a/OpenRa.Game/GameRules/TechTree.cs b/OpenRa.Game/GameRules/TechTree.cs index 3376d98a60..bfd2a04e08 100755 --- a/OpenRa.Game/GameRules/TechTree.cs +++ b/OpenRa.Game/GameRules/TechTree.cs @@ -14,7 +14,7 @@ namespace OpenRa.Game.GameRules { foreach( var b in Rules.Categories[ "Building" ] ) { - var info = (UnitInfo.BuildingInfo)Rules.UnitInfo[ b ]; + var info = (BuildingInfo)Rules.UnitInfo[ b ]; foreach( var p in info.Produces ) producesIndex[ p ].Add( info ); } @@ -23,7 +23,7 @@ namespace OpenRa.Game.GameRules public Cache> GatherBuildings( Player player ) { var ret = new Cache>( x => new List() ); - foreach( var b in Game.world.Actors.Where( x => x.Owner == player && x.unitInfo is UnitInfo.BuildingInfo ) ) + foreach( var b in Game.world.Actors.Where( x => x.Owner == player && x.unitInfo is BuildingInfo ) ) ret[ b.unitInfo.Name ].Add( b ); return ret; } diff --git a/OpenRa.Game/GameRules/UnitInfo.cs b/OpenRa.Game/GameRules/UnitInfo.cs index 4acef1a740..573c7adc52 100755 --- a/OpenRa.Game/GameRules/UnitInfo.cs +++ b/OpenRa.Game/GameRules/UnitInfo.cs @@ -56,54 +56,54 @@ namespace OpenRa.Game.GameRules public readonly int InitialFacing = 128; public UnitInfo(string name) { Name = name; } + } - public class MobileInfo : UnitInfo - { - public readonly int Passengers = 0; - public readonly int Speed = 0; - public readonly bool NoMovingFire = false; + public class MobileInfo : UnitInfo + { + public readonly int Passengers = 0; + public readonly int Speed = 0; + public readonly bool NoMovingFire = false; - public MobileInfo(string name) : base(name) { } - } + public MobileInfo(string name) : base(name) { } + } - public class InfantryInfo : MobileInfo - { - public readonly bool Crushable = true; // also on VehicleInfo, but with a different default - public readonly bool C4 = false; - public readonly bool FraidyCat = false; - public readonly bool Infiltrate = false; - public readonly bool IsCanine = false; - public readonly int SquadSize = 1; + public class InfantryInfo : MobileInfo + { + public readonly bool Crushable = true; // also on VehicleInfo, but with a different default + public readonly bool C4 = false; + public readonly bool FraidyCat = false; + public readonly bool Infiltrate = false; + public readonly bool IsCanine = false; + public readonly int SquadSize = 1; - public InfantryInfo(string name) : base(name) { } - } + public InfantryInfo(string name) : base(name) { } + } - public class VehicleInfo : MobileInfo - { - public readonly bool Crushable = false; - public readonly bool Tracked = false; + public class VehicleInfo : MobileInfo + { + public readonly bool Crushable = false; + public readonly bool Tracked = false; - public VehicleInfo(string name) : base(name) { } - } + public VehicleInfo(string name) : base(name) { } + } - public class BuildingInfo : UnitInfo - { - public readonly int2 Dimensions = new int2( 1, 1 ); - public readonly string Footprint = "x"; - public readonly string[] Produces = { }; + public class BuildingInfo : UnitInfo + { + public readonly int2 Dimensions = new int2(1, 1); + public readonly string Footprint = "x"; + public readonly string[] Produces = { }; - public readonly bool BaseNormal = true; - public readonly int Adjacent = 1; - public readonly bool Bib = false; - public readonly bool Capturable = false; - public readonly int Power = 0; - public readonly bool Powered = false; - public readonly bool Repairable = true; - public readonly int Storage = 0; - public readonly bool Unsellable = false; - public readonly int[] RallyPoint = { 1, 3 }; + public readonly bool BaseNormal = true; + public readonly int Adjacent = 1; + public readonly bool Bib = false; + public readonly bool Capturable = false; + public readonly int Power = 0; + public readonly bool Powered = false; + public readonly bool Repairable = true; + public readonly int Storage = 0; + public readonly bool Unsellable = false; + public readonly int[] RallyPoint = { 1, 3 }; - public BuildingInfo(string name) : base(name) { } - } + public BuildingInfo(string name) : base(name) { } } } diff --git a/OpenRa.Game/PlaceBuilding.cs b/OpenRa.Game/PlaceBuilding.cs index 29cb5da425..057e68f71a 100644 --- a/OpenRa.Game/PlaceBuilding.cs +++ b/OpenRa.Game/PlaceBuilding.cs @@ -9,12 +9,12 @@ namespace OpenRa.Game class PlaceBuilding : IOrderGenerator { public readonly Player Owner; - public readonly UnitInfo.BuildingInfo Building; + public readonly BuildingInfo Building; public PlaceBuilding(Player owner, string name) { Owner = owner; - Building = (UnitInfo.BuildingInfo)Rules.UnitInfo[ name ]; + Building = (BuildingInfo)Rules.UnitInfo[ name ]; } public IEnumerable Order(int2 xy, bool lmb) diff --git a/OpenRa.Game/Player.cs b/OpenRa.Game/Player.cs index 82723abffc..80bd86a7da 100644 --- a/OpenRa.Game/Player.cs +++ b/OpenRa.Game/Player.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System; namespace OpenRa.Game { @@ -10,6 +11,7 @@ namespace OpenRa.Game public Race Race; public readonly int Index; public int Cash; + public int DisplayCash; int powerProvided; int powerDrained; @@ -20,6 +22,7 @@ namespace OpenRa.Game this.PlayerName = playerName; this.Race = race; this.Cash = 10000; + this.DisplayCash = 0; this.powerProvided = this.powerDrained = 0; foreach( var cat in Rules.Categories.Keys ) @@ -56,11 +59,29 @@ namespace OpenRa.Game return true; } + const int displayCashDeltaPerFrame = 50; + public void Tick() { foreach( var p in production ) if( p.Value != null ) p.Value.Tick( this ); + + if (this == Game.LocalPlayer) + { + if (DisplayCash < Cash) + { + DisplayCash += Math.Min(displayCashDeltaPerFrame, + Cash - DisplayCash); + Game.PlaySound("cashup1.aud", false); + } + else if (DisplayCash > Cash) + { + DisplayCash -= Math.Min(displayCashDeltaPerFrame, + DisplayCash - Cash); + Game.PlaySound("cashdn1.aud", false); + } + } } // Key: Production category. Categories are: Building, Infantry, Vehicle, Ship, Plane (and one per super, if they're done in here) diff --git a/OpenRa.Game/Traits/Activities/Move.cs b/OpenRa.Game/Traits/Activities/Move.cs index 903e2b5629..a484557bdd 100755 --- a/OpenRa.Game/Traits/Activities/Move.cs +++ b/OpenRa.Game/Traits/Activities/Move.cs @@ -180,7 +180,7 @@ namespace OpenRa.Game.Traits.Activities var oldFraction = moveFraction; var oldTotal = moveFractionTotal; - moveFraction += ( self.unitInfo as UnitInfo.MobileInfo ).Speed; + moveFraction += ( self.unitInfo as MobileInfo ).Speed; UpdateCenterLocation( self, mobile ); if( moveFraction >= moveFractionTotal ) { diff --git a/OpenRa.Game/Traits/Building.cs b/OpenRa.Game/Traits/Building.cs index d6585296f8..acd2ab866f 100644 --- a/OpenRa.Game/Traits/Building.cs +++ b/OpenRa.Game/Traits/Building.cs @@ -8,11 +8,11 @@ namespace OpenRa.Game.Traits { class Building : ITick, INotifyBuildComplete { - public readonly UnitInfo.BuildingInfo unitInfo; + public readonly BuildingInfo unitInfo; public Building(Actor self) { - unitInfo = (UnitInfo.BuildingInfo)self.unitInfo; + unitInfo = (BuildingInfo)self.unitInfo; } bool first = true; diff --git a/OpenRa.Game/Traits/Helicopter.cs b/OpenRa.Game/Traits/Helicopter.cs index dd63b63ed7..96ba1316d2 100644 --- a/OpenRa.Game/Traits/Helicopter.cs +++ b/OpenRa.Game/Traits/Helicopter.cs @@ -52,7 +52,7 @@ namespace OpenRa.Game.Traits self.unitInfo.ROT); // .6f going the wrong way; .8f going sideways, 1f going forward. - var rawSpeed = .2f * (self.unitInfo as UnitInfo.VehicleInfo).Speed; + var rawSpeed = .2f * (self.unitInfo as VehicleInfo).Speed; var angle = (unit.Facing - desiredFacing) / 128f * Math.PI; var scale = .4f + .6f * (float)Math.Cos(angle); diff --git a/OpenRa.Game/Traits/InfantrySquad.cs b/OpenRa.Game/Traits/InfantrySquad.cs index 68ca9674de..fb51ce2a1d 100644 --- a/OpenRa.Game/Traits/InfantrySquad.cs +++ b/OpenRa.Game/Traits/InfantrySquad.cs @@ -22,7 +22,7 @@ namespace OpenRa.Game.Traits public InfantrySquad(Actor self) { - var ii = (UnitInfo.InfantryInfo)self.unitInfo; + var ii = (InfantryInfo)self.unitInfo; for (int i = 0; i < ii.SquadSize; i++) elements.Add(new Soldier(self.unitInfo.Name, self.CenterLocation.ToInt2() + elementOffsets[ii.SquadSize][i])); @@ -72,7 +72,7 @@ namespace OpenRa.Game.Traits anim.PlayFetchIndex("stand", () => Util.QuantizeFacing(facing, anim.CurrentSequence.Length)); location = initialLocation; - speed = ((UnitInfo.InfantryInfo)Rules.UnitInfo[name]).Speed / 2; + speed = ((InfantryInfo)Rules.UnitInfo[name]).Speed / 2; } public void Tick( int2 desiredLocation, Actor self ) diff --git a/OpenRa.Game/Traits/McvDeploy.cs b/OpenRa.Game/Traits/McvDeploy.cs index c3052ba33b..1465a72dce 100644 --- a/OpenRa.Game/Traits/McvDeploy.cs +++ b/OpenRa.Game/Traits/McvDeploy.cs @@ -22,7 +22,7 @@ namespace OpenRa.Game.Traits { if( order.OrderString == "DeployMcv" ) { - var factBuildingInfo = (UnitInfo.BuildingInfo)Rules.UnitInfo[ "fact" ]; + var factBuildingInfo = (BuildingInfo)Rules.UnitInfo[ "fact" ]; if( Game.CanPlaceBuilding( factBuildingInfo, self.Location - new int2( 1, 1 ), self, false ) ) { self.CancelActivity(); diff --git a/OpenRa.Game/Traits/Mobile.cs b/OpenRa.Game/Traits/Mobile.cs index 70e47ca712..709450d712 100644 --- a/OpenRa.Game/Traits/Mobile.cs +++ b/OpenRa.Game/Traits/Mobile.cs @@ -16,8 +16,6 @@ namespace OpenRa.Game.Traits public int2 fromCell { get { return __fromCell; } set { Game.UnitInfluence.Remove( this ); __fromCell = value; Game.UnitInfluence.Add( this ); } } public int2 toCell { get { return self.Location; } set { Game.UnitInfluence.Remove( this ); self.Location = value; Game.UnitInfluence.Add( this ); } } - public int Voice = Game.CosmeticRandom.Next(2); - public Mobile(Actor self) { this.self = self; @@ -62,7 +60,7 @@ namespace OpenRa.Game.Traits case "Infantry": return UnitMovementType.Foot; case "Vehicle": - return ( self.unitInfo as UnitInfo.VehicleInfo ).Tracked ? UnitMovementType.Track : UnitMovementType.Wheel; + return ( self.unitInfo as VehicleInfo ).Tracked ? UnitMovementType.Track : UnitMovementType.Wheel; case "Ship": return UnitMovementType.Float; case "Plane": diff --git a/OpenRa.Game/UiOverlay.cs b/OpenRa.Game/UiOverlay.cs index b88a06d518..3f90ddab59 100644 --- a/OpenRa.Game/UiOverlay.cs +++ b/OpenRa.Game/UiOverlay.cs @@ -43,7 +43,7 @@ namespace OpenRa.Game spriteRenderer.DrawSprite(unitDebug, Game.CellSize * new float2(i, j), 0); } - public void DrawBuildingGrid( UnitInfo.BuildingInfo bi ) + public void DrawBuildingGrid( BuildingInfo bi ) { var position = Game.controller.MousePosition.ToInt2(); var isCloseEnough = Game.IsCloseEnoughToBase(Game.LocalPlayer, bi, position); diff --git a/OpenRa.Game/UnitOrders.cs b/OpenRa.Game/UnitOrders.cs index e8ccf325fc..ba8ecca999 100755 --- a/OpenRa.Game/UnitOrders.cs +++ b/OpenRa.Game/UnitOrders.cs @@ -28,7 +28,7 @@ namespace OpenRa.Game { Game.world.AddFrameEndTask( _ => { - var building = (UnitInfo.BuildingInfo)Rules.UnitInfo[ order.TargetString ]; + var building = (BuildingInfo)Rules.UnitInfo[ order.TargetString ]; var producing = order.Player.Producing(Rules.UnitCategory[order.TargetString]); if( producing == null || producing.Item != order.TargetString || producing.RemainingTime != 0 ) return;