Health as an ActorInit

This commit is contained in:
Paul Chote
2010-08-02 20:12:04 +12:00
parent 578d42614b
commit bc93e8cac0
4 changed files with 43 additions and 27 deletions

View File

@@ -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<HealthInit>() ? (int)(init.Get<HealthInit, float>()*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<float>
{
[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)

View File

@@ -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<Health>();
var newHealth = a.traits.GetOrDefault<Health>();
if (oldHealth != null && newHealth != null)
newHealth.HPFraction = oldHealth.HPFraction;
};
if (self.traits.Contains<Health>())
init.Add( new HealthInit( self.traits.Get<Health>().HPFraction ));
var a = w.CreateActor( actor, init );
if (selected)
w.Selection.Add(w, a);

View File

@@ -37,27 +37,27 @@ namespace OpenRA.Mods.RA
public object Create(ActorInitializer init) { return new Bridge(init.self, this); }
public IEnumerable<ushort> Templates
public IEnumerable<Pair<ushort, float>> 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<int2, byte> 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<ushort,byte>(t, (byte)a.Value)]));
a => sprites[new TileReference<ushort,byte>(t.First, (byte)a.Value)]));
}
}

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.RA
{
readonly BridgeLayerInfo Info;
readonly World world;
Dictionary<ushort, string> BridgeTypes = new Dictionary<ushort, string>();
Dictionary<ushort, Pair<string, float>> BridgeTypes = new Dictionary<ushort, Pair<string,float>>();
Bridge[,] Bridges;
public BridgeLayer(Actor self, BridgeLayerInfo Info)
@@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA
var bi = Rules.Info[bridge].Traits.Get<BridgeInfo>();
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<Bridge>();
Dictionary<int2, byte> subTiles = new Dictionary<int2, byte>();