prep for mod trait support

This commit is contained in:
Chris Forbes
2009-12-28 09:38:11 +13:00
parent 5e21f6393b
commit fc030fae5c
2 changed files with 14 additions and 11 deletions

View File

@@ -13,9 +13,9 @@ namespace OpenRa
inner.Add( t, val ); inner.Add( t, val );
} }
public void Add<T>( T val ) public void Add( object val )
{ {
Add( typeof( T ), val ); Add( val.GetType(), val );
} }
public void Remove<T>() public void Remove<T>()

View File

@@ -19,6 +19,16 @@ namespace OpenRa.Game
public int Health; public int Health;
IActivity currentActivity; IActivity currentActivity;
object ConstructTrait(string traitName)
{
/* todo: allow mods to introduce traits */
var type = typeof(Mobile).Assembly.GetType(typeof(Mobile).Namespace + "." + traitName, true, false);
var ctor = type.GetConstructor(new[] { typeof(Actor) });
if (ctor == null)
throw new InvalidOperationException("Trait {0} does not have the correct constructor: {0}(Actor self)".F(type.Name));
return ctor.Invoke(new object[] { this });
}
public Actor( ActorInfo info, int2 location, Player owner ) public Actor( ActorInfo info, int2 location, Player owner )
{ {
ActorID = Game.world.NextAID(); ActorID = Game.world.NextAID();
@@ -29,20 +39,13 @@ namespace OpenRa.Game
if (Info == null) return; if (Info == null) return;
Health = Info.Strength; /* todo: handle cases where this is not true! */ Health = Info.Strength; /* todo: fix walls, etc so this is always true! */
if( Info.Traits == null ) if( Info.Traits == null )
throw new InvalidOperationException( "No Actor traits for {0}; add Traits= to units.ini for appropriate unit".F(Info.Name) ); throw new InvalidOperationException( "No Actor traits for {0}; add Traits= to units.ini for appropriate unit".F(Info.Name) );
foreach (var traitName in Info.Traits) foreach (var traitName in Info.Traits)
{ /* todo: a better solution than `the assembly Mobile lives in`, for mod support & sanity. */ traits.Add(ConstructTrait(traitName));
var type = typeof(Mobile).Assembly.GetType(typeof(Mobile).Namespace + "." + traitName, true, false);
var ctor = type.GetConstructor(new[] { typeof(Actor) });
if (ctor == null)
throw new InvalidOperationException("Trait {0} does not have the correct constructor: {0}(Actor self)".F(type.Name));
traits.Add(type, ctor.Invoke(new object[] { this }));
}
} }
public void Tick() public void Tick()