From 4ea66ea30970591dfdee68b26cf8756913391034 Mon Sep 17 00:00:00 2001 From: alzeih Date: Mon, 2 Aug 2010 00:49:26 +1200 Subject: [PATCH] Added FacingInit and AltitudeInit. Used in Mobile/Aircraft/Husks. Refactored Production, SpyPlane, Paratroopers and LeavesHusk --- OpenRA.Game/ActorInitializer.cs | 48 +++++++++++++++++-- OpenRA.Game/Traits/Mobile.cs | 2 + OpenRA.Game/Traits/Production.cs | 8 +++- OpenRA.Mods.RA/Aircraft.cs | 2 + OpenRA.Mods.RA/Husk.cs | 3 +- OpenRA.Mods.RA/LeavesHusk.cs | 11 +++-- .../SupportPowers/ParatroopersPower.cs | 17 ++++--- OpenRA.Mods.RA/SupportPowers/SpyPlanePower.cs | 11 +++-- 8 files changed, 83 insertions(+), 19 deletions(-) diff --git a/OpenRA.Game/ActorInitializer.cs b/OpenRA.Game/ActorInitializer.cs index 8653ec1bfc..0798b45055 100755 --- a/OpenRA.Game/ActorInitializer.cs +++ b/OpenRA.Game/ActorInitializer.cs @@ -36,15 +36,55 @@ namespace OpenRA { return dict.Get().Value( world ); } - } - - public interface IActorInit - { + + public bool Contains() + where T : IActorInit + { + return dict.Contains(); + } } + + public interface IActorInit {} public interface IActorInit : IActorInit { T Value( World world ); + } + + public class FacingInit : IActorInit + { + [FieldFromYamlKey] + public readonly int value = 128; + + public FacingInit() { } + + public FacingInit( int init ) + { + value = init; + } + + public int Value( World world ) + { + return value; + } + } + + public class AltitudeInit : IActorInit + { + [FieldFromYamlKey] + public readonly int value = 0; + + public AltitudeInit() { } + + public AltitudeInit( int init ) + { + value = init; + } + + public int Value( World world ) + { + return value; + } } public class LocationInit : IActorInit diff --git a/OpenRA.Game/Traits/Mobile.cs b/OpenRA.Game/Traits/Mobile.cs index 53dafb47b7..cf58134c27 100644 --- a/OpenRA.Game/Traits/Mobile.cs +++ b/OpenRA.Game/Traits/Mobile.cs @@ -77,6 +77,8 @@ namespace OpenRA.Traits this.self = init.self; this.Info = info; this.__fromCell = this.__toCell = init.Get(); + this.Facing = init.Contains() ? init.Get() : info.InitialFacing; + this.Altitude = init.Contains() ? init.Get() : 0; shroud = self.World.WorldActor.traits.Get(); uim = self.World.WorldActor.traits.Get(); diff --git a/OpenRA.Game/Traits/Production.cs b/OpenRA.Game/Traits/Production.cs index 8436eda603..2f11c8f686 100755 --- a/OpenRA.Game/Traits/Production.cs +++ b/OpenRA.Game/Traits/Production.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Linq; using OpenRA.GameRules; using System.Drawing; +using OpenRA.FileFormats; namespace OpenRA.Traits { @@ -55,8 +56,11 @@ namespace OpenRA.Traits if( location == null || self.World.WorldActor.traits.Get().GetUnitsAt( location.Value ).Any() ) return false; - var newUnit = self.World.CreateActor( producee.Name, location.Value, self.Owner ); - newUnit.traits.Get().Facing = CreationFacing( self, newUnit ); ; + var newUnit = self.World.CreateActor( producee.Name, new TypeDictionary + { + new LocationInit( location.Value ), + new OwnerInit( self.Owner ), + }); var pi = self.Info.Traits.Get(); var rp = self.traits.GetOrDefault(); diff --git a/OpenRA.Mods.RA/Aircraft.cs b/OpenRA.Mods.RA/Aircraft.cs index 60c2f580cc..9dc5821131 100755 --- a/OpenRA.Mods.RA/Aircraft.cs +++ b/OpenRA.Mods.RA/Aircraft.cs @@ -42,6 +42,8 @@ namespace OpenRA.Mods.RA public Aircraft( ActorInitializer init , AircraftInfo info) { this.Location = init.Get(); + this.Facing = init.Contains() ? init.Get() : info.InitialFacing; + this.Altitude = init.Contains() ? init.Get() : 0; Info = info; } diff --git a/OpenRA.Mods.RA/Husk.cs b/OpenRA.Mods.RA/Husk.cs index 64eff0e1ae..f0d5cbddd4 100644 --- a/OpenRA.Mods.RA/Husk.cs +++ b/OpenRA.Mods.RA/Husk.cs @@ -27,12 +27,13 @@ namespace OpenRA.Mods.RA [Sync] public int Facing { get; set; } public int ROT { get { return 0; } } - public int InitialFacing { get { return 0; } } + public int InitialFacing { get { return 128; } } public Husk(ActorInitializer init) { this.self = init.self; this.location = init.Get(); + this.Facing = init.Contains() ? init.Get() : 128; self.World.WorldActor.traits.Get().Add(self, this); } diff --git a/OpenRA.Mods.RA/LeavesHusk.cs b/OpenRA.Mods.RA/LeavesHusk.cs index c8a525c37c..d162cb727b 100644 --- a/OpenRA.Mods.RA/LeavesHusk.cs +++ b/OpenRA.Mods.RA/LeavesHusk.cs @@ -9,6 +9,7 @@ #endregion using OpenRA.Traits; +using OpenRA.FileFormats; namespace OpenRA.Mods.RA { @@ -26,10 +27,14 @@ namespace OpenRA.Mods.RA self.World.AddFrameEndTask(w => { var info = self.Info.Traits.Get(); - var husk = w.CreateActor(info.HuskActor, self.Location, self.Owner); - husk.CenterLocation = self.CenterLocation; - husk.traits.Get().Facing = self.traits.Get().Facing; + var husk = w.CreateActor(info.HuskActor, new TypeDictionary + { + new LocationInit( self.Location ), + new OwnerInit( self.Owner ), + new FacingInit( self.traits.Get().Facing ), + }); + var turreted = self.traits.GetOrDefault(); if (turreted != null) foreach (var p in husk.traits.WithInterface()) diff --git a/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs b/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs index c924667023..bb5c051996 100755 --- a/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs +++ b/OpenRA.Mods.RA/SupportPowers/ParatroopersPower.cs @@ -11,6 +11,7 @@ using OpenRA.Mods.RA.Activities; using OpenRA.Orders; using OpenRA.Traits; +using OpenRA.FileFormats; namespace OpenRA.Mods.RA { @@ -57,13 +58,17 @@ namespace OpenRA.Mods.RA var startPos = owner.World.ChooseRandomEdgeCell(); owner.World.AddFrameEndTask(w => { - var flareType = (Info as ParatroopersPowerInfo).FlareType; - var flare = flareType != null ? w.CreateActor(flareType, p, owner) : null; - - var a = w.CreateActor((Info as ParatroopersPowerInfo).UnitType, startPos, owner); - a.traits.Get().Facing = Util.GetFacing(p - startPos, 0); - a.traits.Get().Altitude = a.Info.Traits.Get().CruiseAltitude; + var info = (Info as ParatroopersPowerInfo); + var flare = info.FlareType != null ? w.CreateActor(info.FlareType, p, owner) : null; + var a = w.CreateActor(info.UnitType, new TypeDictionary + { + new LocationInit( startPos ), + new OwnerInit( owner ), + new FacingInit( Util.GetFacing(p - startPos, 0) ), + new AltitudeInit( Rules.Info[info.UnitType].Traits.Get().CruiseAltitude ), + }); + a.CancelActivity(); a.QueueActivity(new FlyCircle(p)); a.traits.Get().SetLZ(p, flare); diff --git a/OpenRA.Mods.RA/SupportPowers/SpyPlanePower.cs b/OpenRA.Mods.RA/SupportPowers/SpyPlanePower.cs index 16c068ddab..3c7b2af3fe 100755 --- a/OpenRA.Mods.RA/SupportPowers/SpyPlanePower.cs +++ b/OpenRA.Mods.RA/SupportPowers/SpyPlanePower.cs @@ -12,6 +12,7 @@ using OpenRA.Mods.RA.Activities; using OpenRA.Orders; using OpenRA.Traits; using OpenRA.Traits.Activities; +using OpenRA.FileFormats; namespace OpenRA.Mods.RA { @@ -45,9 +46,13 @@ namespace OpenRA.Mods.RA var enterCell = self.World.ChooseRandomEdgeCell(); - var plane = self.World.CreateActor("U2", enterCell, self.Owner); - plane.traits.Get().Altitude = plane.Info.Traits.Get().CruiseAltitude; - plane.traits.Get().Facing = Util.GetFacing(order.TargetLocation - enterCell, 0); + var plane = self.World.CreateActor("u2", new TypeDictionary + { + new LocationInit( enterCell ), + new OwnerInit( self.Owner ), + new FacingInit( Util.GetFacing(order.TargetLocation - enterCell, 0) ), + new AltitudeInit( Rules.Info["u2"].Traits.Get().CruiseAltitude ), + }); plane.CancelActivity(); plane.QueueActivity(new Fly(Util.CenterOfCell(order.TargetLocation)));