ITraitInfo, plumbing in NewUnitInfo

This commit is contained in:
Chris Forbes
2010-01-10 12:34:17 +13:00
parent fe8b13591f
commit 5fb2f91281
8 changed files with 44 additions and 12 deletions

View File

@@ -16,6 +16,15 @@ namespace OpenRa.Game.GameRules
}
}
public static void Load(object self, MiniYaml my)
{
foreach (var x in my.Nodes)
{
var field = self.GetType().GetField(x.Key.Trim());
field.SetValue(self, GetValue(field.FieldType, x.Value.Value.Trim()));
}
}
public static void CheckYaml( object self, Dictionary<string, MiniYaml> d )
{
//foreach( var x in d )

View File

@@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using OpenRa.FileFormats;
using OpenRa.Game.Traits;
namespace OpenRa.Game.GameRules
{
class NewUnitInfo
{
public readonly string Parent;
public readonly Dictionary<string, MiniYaml> Traits = new Dictionary<string, MiniYaml>();
public readonly Dictionary<string, ITraitInfo> Traits;
public NewUnitInfo( MiniYaml node )
{
@@ -18,7 +19,22 @@ namespace OpenRa.Game.GameRules
Parent = inherit.Value;
node.Nodes.Remove( "Inherits" );
}
Traits = node.Nodes;
Traits = node.Nodes.ToDictionary(
a => a.Key,
a => LoadTraitInfo( a.Key, a.Value ));
}
static ITraitInfo LoadTraitInfo(string traitName, MiniYaml my)
{
var fullTypeName = typeof(ITraitInfo).Namespace + "." + traitName + "Info";
var info = (ITraitInfo)typeof(ITraitInfo).Assembly.CreateInstance(fullTypeName);
if (info == null)
throw new NotImplementedException("Missing traitinfo type `{0}`".F(fullTypeName));
FieldLoader.Load(info, my);
return info;
}
}
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using IjwFramework.Types;
using OpenRa.FileFormats;
using OpenRa.Game.GameRules;
using OpenRa.Game.Traits;
namespace OpenRa.Game
{
@@ -97,10 +98,6 @@ namespace OpenRa.Game
NewUnitInfo = new Dictionary<string, NewUnitInfo>();
foreach( var kv in MiniYaml.FromFile( "ra.yaml" ) )
NewUnitInfo.Add( kv.Key.ToLowerInvariant(), new NewUnitInfo( kv.Value ) );
foreach( var unit in NewUnitInfo )
foreach( var trait in unit.Value.Traits.Values )
FieldLoader.CheckYaml( UnitInfo[ unit.Key.ToLowerInvariant() ], trait.Nodes );
}
static void LoadCategories(params string[] types)

View File

@@ -67,16 +67,16 @@ namespace OpenRa.Game.GameRules
public UnitInfo(string name) { Name = name; }
}
public class MobileInfo : UnitInfo
public class LegacyMobileInfo : UnitInfo
{
public readonly int Speed = 0;
public readonly bool NoMovingFire = false;
public readonly string Voice = "GenericVoice";
public MobileInfo(string name) : base(name) { }
public LegacyMobileInfo(string name) : base(name) { }
}
public class InfantryInfo : MobileInfo
public class InfantryInfo : LegacyMobileInfo
{
public readonly bool C4 = false;
public readonly bool FraidyCat = false;
@@ -87,7 +87,7 @@ namespace OpenRa.Game.GameRules
public InfantryInfo(string name) : base(name) { }
}
public class VehicleInfo : MobileInfo
public class VehicleInfo : LegacyMobileInfo
{
public readonly bool Tracked = false;

View File

@@ -92,7 +92,7 @@ namespace OpenRa.Game
{
if (voicedUnit == null) return;
var mi = voicedUnit.Info as MobileInfo;
var mi = voicedUnit.Info as LegacyMobileInfo;
if (mi == null) return;
var vi = Rules.VoiceInfo[mi.Voice];

View File

@@ -5,6 +5,14 @@ using OpenRa.Game.GameRules;
namespace OpenRa.Game.Traits
{
class MobileInfo : ITraitInfo
{
public readonly int Sight;
public readonly int ROT;
public readonly int Speed;
public object Create(Actor self) { return new Mobile(self); }
}
class Mobile : IIssueOrder, IResolveOrder, IOccupySpace, IMovement
{
readonly Actor self;

View File

@@ -69,4 +69,6 @@ namespace OpenRa.Game.Traits
public Renderable WithZOffset(int newOffset) { return new Renderable(Sprite, Pos, Palette, newOffset); }
public Renderable WithPos(float2 newPos) { return new Renderable(Sprite, newPos, Palette, ZOffset); }
}
interface ITraitInfo { object Create(Actor self); }
}

View File

@@ -127,7 +127,7 @@ namespace OpenRa.Game.Traits
public static float GetEffectiveSpeed(Actor self)
{
var mi = self.Info as MobileInfo;
var mi = self.Info as LegacyMobileInfo;
if (mi == null) return 0f;
var modifier = self.traits