use the new actorinit stuff when loading map yaml

This commit is contained in:
Bob
2010-08-01 15:25:57 +12:00
parent 10b7ece62e
commit 970eb4d6e1
8 changed files with 104 additions and 28 deletions

View File

@@ -49,8 +49,11 @@ namespace OpenRA
public class LocationInit : IActorInit<int2>
{
[FieldFromYamlKey]
public readonly int2 value = int2.Zero;
public LocationInit() { }
public LocationInit( int2 init )
{
value = init;
@@ -64,9 +67,17 @@ namespace OpenRA
public class OwnerInit : IActorInit<Player>
{
[FieldFromYamlKey]
public readonly string PlayerName = "Neutral";
Player player;
public OwnerInit() { }
public OwnerInit( string playerName )
{
this.PlayerName = playerName;
}
public OwnerInit( Player player )
{
this.player = player;

53
OpenRA.Game/ActorReference.cs Executable file
View File

@@ -0,0 +1,53 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System.Collections.Generic;
using System.Collections;
namespace OpenRA.FileFormats
{
public class ActorReference : IEnumerable
{
public readonly string Type;
public readonly TypeDictionary InitDict;
public ActorReference( string type ) : this( type, new Dictionary<string, MiniYaml>() ) { }
public ActorReference( string type, Dictionary<string, MiniYaml> inits )
{
Type = type;
InitDict = new TypeDictionary();
foreach( var i in inits )
InitDict.Add( LoadInit( i.Key, i.Value ) );
}
static IActorInit LoadInit(string traitName, MiniYaml my)
{
var info = Game.CreateObject<IActorInit>(traitName + "Init");
FieldLoader.Load(info, my);
return info;
}
public MiniYaml Save()
{
var ret = new MiniYaml( Type );
foreach( var init in InitDict )
{
var initName = init.GetType().Name;
ret.Nodes.Add( initName.Substring( 0, initName.Length - 4 ), FieldSaver.Save( init ) );
}
return ret;
}
// for initialization syntax
public void Add( object o ) { InitDict.Add( o ); }
public IEnumerator GetEnumerator() { return InitDict.GetEnumerator(); }
}
}

View File

@@ -14,7 +14,7 @@ using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Traits;
namespace OpenRA.GameRules
namespace OpenRA
{
public class ActorInfo
{

View File

@@ -123,20 +123,19 @@ namespace OpenRA
int actors = 0;
foreach (var kv in yaml["Actors"].Nodes)
{
string[] vals = kv.Value.Value.Split(' ');
string[] loc = vals[2].Split(',');
var a = new ActorReference("Actor"+actors++, vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), "Neutral");
Actors.Add(a.Id, a);
string[] vals = kv.Value.Value.Split(' ');
string[] loc = vals[2].Split(',');
Actors.Add( "Actor" + actors++, new ActorReference( vals[ 0 ] )
{
new LocationInit( new int2( int.Parse( loc[ 0 ] ), int.Parse( loc[ 1 ] ) ) ),
new OwnerInit( "Neutral" ),
} );
}
}
else
{
foreach (var kv in yaml["Actors"].Nodes)
{
kv.Value.Nodes.Add( "Type", new MiniYaml( kv.Value.Value ) );
var a = new ActorReference(kv.Key, kv.Value);
Actors.Add(a.Id, a);
}
{
foreach( var kv in yaml[ "Actors" ].Nodes )
Actors.Add( kv.Key, new ActorReference( kv.Value.Value, kv.Value.Nodes ) );
}
// Smudges
@@ -173,10 +172,10 @@ namespace OpenRA
p => FieldSaver.Save(p.Value))));
root.Add("Actors",
new MiniYaml(null, Actors.ToDictionary(
a => "{0}".F(a.Key),
a => FieldSaver.Save(a.Value))));
new MiniYaml( null, Actors.ToDictionary(
x => x.Key,
x => x.Value.Save() ) ) );
root.Add("Waypoints", MiniYaml.FromDictionary<string, int2>(Waypoints));
root.Add("Smudges", MiniYaml.FromList<SmudgeReference>(Smudges));
root.Add("Rules", new MiniYaml(null, Rules));

View File

@@ -228,6 +228,7 @@
<Name>OpenRA.FileFormats</Name>
</ProjectReference>
<Compile Include="ActorInitializer.cs" />
<Compile Include="ActorReference.cs" />
<Compile Include="Map.cs" />
<Compile Include="Widgets\Delegates\DeveloperModeDelegate.cs" />
</ItemGroup>