From bc93e8cac0279f444028bf81f1c3736fa639000b Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 2 Aug 2010 20:12:04 +1200 Subject: [PATCH] Health as an ActorInit --- OpenRA.Game/Traits/Health.cs | 25 ++++++++++++++++++++++--- OpenRA.Mods.RA/Activities/Transform.cs | 14 +++++++------- OpenRA.Mods.RA/Bridge.cs | 24 ++++++++++-------------- OpenRA.Mods.RA/BridgeLayer.cs | 7 ++++--- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/OpenRA.Game/Traits/Health.cs b/OpenRA.Game/Traits/Health.cs index 81d4d3b83f..570ad7ff11 100644 --- a/OpenRA.Game/Traits/Health.cs +++ b/OpenRA.Game/Traits/Health.cs @@ -15,13 +15,13 @@ using System.Linq; using OpenRA.Effects; using OpenRA.Traits.Activities; using OpenRA.GameRules; +using OpenRA.FileFormats; namespace OpenRA.Traits { public class HealthInfo : ITraitInfo { public readonly int HP = 0; - public readonly int InitialHP = 0; public readonly ArmorType Armor = ArmorType.none; public virtual object Create(ActorInitializer init) { return new Health(init, this); } } @@ -39,7 +39,7 @@ namespace OpenRA.Traits { Info = info; MaxHP = info.HP; - hp = (info.InitialHP == 0) ? MaxHP : info.InitialHP; + hp = init.Contains() ? (int)(init.Get()*MaxHP) : MaxHP; } public int HP { get { return hp; } } @@ -47,7 +47,6 @@ namespace OpenRA.Traits public float HPFraction { get { return hp * 1f / MaxHP; } - set { hp = (int)(value * MaxHP); } } public bool IsDead { get { return hp <= 0; } } @@ -117,6 +116,26 @@ namespace OpenRA.Traits } } + + public class HealthInit : IActorInit + { + [FieldFromYamlKey] + public readonly float value = 1f; + + public HealthInit() { } + + public HealthInit( float init ) + { + value = init; + } + + public float Value( World world ) + { + return value; + } + } + + public static class HealthExts { public static bool IsDead(this Actor self) diff --git a/OpenRA.Mods.RA/Activities/Transform.cs b/OpenRA.Mods.RA/Activities/Transform.cs index b4fada1bf7..1c49508675 100644 --- a/OpenRA.Mods.RA/Activities/Transform.cs +++ b/OpenRA.Mods.RA/Activities/Transform.cs @@ -52,17 +52,17 @@ namespace OpenRA.Mods.RA.Activities self.World.Remove(self); foreach (var s in sounds) Sound.PlayToPlayer(self.Owner, s, self.CenterLocation); - - var a = w.CreateActor( actor, new TypeDictionary + + var init = new TypeDictionary { new LocationInit( self.Location + offset ), new OwnerInit( self.Owner ), new FacingInit( facing ), - }); - var oldHealth = self.traits.GetOrDefault(); - var newHealth = a.traits.GetOrDefault(); - if (oldHealth != null && newHealth != null) - newHealth.HPFraction = oldHealth.HPFraction; + }; + if (self.traits.Contains()) + init.Add( new HealthInit( self.traits.Get().HPFraction )); + + var a = w.CreateActor( actor, init ); if (selected) w.Selection.Add(w, a); diff --git a/OpenRA.Mods.RA/Bridge.cs b/OpenRA.Mods.RA/Bridge.cs index 3b3beb90bf..31380cebc7 100644 --- a/OpenRA.Mods.RA/Bridge.cs +++ b/OpenRA.Mods.RA/Bridge.cs @@ -37,27 +37,27 @@ namespace OpenRA.Mods.RA public object Create(ActorInitializer init) { return new Bridge(init.self, this); } - public IEnumerable Templates + public IEnumerable> Templates { get { if (Template != 0) - yield return Template; + yield return Pair.New(Template, 1f); if (DamagedTemplate != 0) - yield return DamagedTemplate; + yield return Pair.New(DamagedTemplate, .5f); if (DestroyedTemplate != 0) - yield return DestroyedTemplate; + yield return Pair.New(DestroyedTemplate, 0f); if (DestroyedPlusNorthTemplate != 0) - yield return DestroyedPlusNorthTemplate; + yield return Pair.New(DestroyedPlusNorthTemplate, 0f); if (DestroyedPlusSouthTemplate != 0) - yield return DestroyedPlusSouthTemplate; + yield return Pair.New(DestroyedPlusSouthTemplate, 0f); if (DestroyedPlusBothTemplate != 0) - yield return DestroyedPlusBothTemplate; + yield return Pair.New(DestroyedPlusBothTemplate, 0f); } } } @@ -89,10 +89,6 @@ namespace OpenRA.Mods.RA public void Create(ushort template, Dictionary subtiles) { currentTemplate = template; - if (template == Info.DamagedTemplate) - Health.HPFraction = .5f; - else if (template != Info.Template) - Health.HPFraction = 0f; // Create a new cache to store the tile data if (cachedTileset != self.World.Map.Tileset) @@ -106,10 +102,10 @@ namespace OpenRA.Mods.RA // Cache templates and tiles for the different states foreach (var t in Info.Templates) { - Templates.Add(t,self.World.TileSet.Templates[t]); - TileSprites.Add(t,subtiles.ToDictionary( + Templates.Add(t.First,self.World.TileSet.Templates[t.First]); + TileSprites.Add(t.First,subtiles.ToDictionary( a => a.Key, - a => sprites[new TileReference(t, (byte)a.Value)])); + a => sprites[new TileReference(t.First, (byte)a.Value)])); } } diff --git a/OpenRA.Mods.RA/BridgeLayer.cs b/OpenRA.Mods.RA/BridgeLayer.cs index ec1484f9dc..5c1334a4f4 100644 --- a/OpenRA.Mods.RA/BridgeLayer.cs +++ b/OpenRA.Mods.RA/BridgeLayer.cs @@ -27,7 +27,7 @@ namespace OpenRA.Mods.RA { readonly BridgeLayerInfo Info; readonly World world; - Dictionary BridgeTypes = new Dictionary(); + Dictionary> BridgeTypes = new Dictionary>(); Bridge[,] Bridges; public BridgeLayer(Actor self, BridgeLayerInfo Info) @@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA var bi = Rules.Info[bridge].Traits.Get(); foreach (var template in bi.Templates) { - BridgeTypes.Add(template, bridge); + BridgeTypes.Add(template.First, Pair.New(bridge, template.Second)); Log.Write("debug", "Adding template {0} for bridge {1}", template, bridge); } } @@ -81,10 +81,11 @@ namespace OpenRA.Mods.RA var nj = j - image / template.Size.X; // Create a new actor for this bridge and keep track of which subtiles this bridge includes - var bridge = w.CreateActor(BridgeTypes[tile], new TypeDictionary + var bridge = w.CreateActor(BridgeTypes[tile].First, new TypeDictionary { new LocationInit( new int2(ni, nj) ), new OwnerInit( w.WorldActor.Owner ), + new HealthInit( BridgeTypes[tile].Second ), }).traits.Get(); Dictionary subTiles = new Dictionary();