new mapactor init stuff
This commit is contained in:
@@ -152,6 +152,7 @@ namespace OpenRA.Editor
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var info = Rules.Info[a];
|
var info = Rules.Info[a];
|
||||||
|
if( !info.Traits.Contains<RenderSimpleInfo>() ) continue;
|
||||||
var template = RenderActor(info, tileset, palette);
|
var template = RenderActor(info, tileset, palette);
|
||||||
var ibox = new PictureBox
|
var ibox = new PictureBox
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -17,10 +17,10 @@ namespace OpenRA.FileFormats
|
|||||||
{
|
{
|
||||||
public class MapStub
|
public class MapStub
|
||||||
{
|
{
|
||||||
public IFolder Package;
|
public readonly IFolder Package;
|
||||||
|
|
||||||
// Yaml map data
|
// Yaml map data
|
||||||
public string Uid;
|
public readonly string Uid;
|
||||||
public bool Selectable;
|
public bool Selectable;
|
||||||
|
|
||||||
public string Title;
|
public string Title;
|
||||||
@@ -35,7 +35,6 @@ namespace OpenRA.FileFormats
|
|||||||
public int2 BottomRight;
|
public int2 BottomRight;
|
||||||
public int Width { get { return BottomRight.X - TopLeft.X; } }
|
public int Width { get { return BottomRight.X - TopLeft.X; } }
|
||||||
public int Height { get { return BottomRight.Y - TopLeft.Y; } }
|
public int Height { get { return BottomRight.Y - TopLeft.Y; } }
|
||||||
public Map Map { get { return new Map(Package); }}
|
|
||||||
|
|
||||||
static List<string> Fields = new List<string>() {
|
static List<string> Fields = new List<string>() {
|
||||||
"Selectable", "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight"
|
"Selectable", "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
@@ -77,7 +77,6 @@
|
|||||||
<Compile Include="Support\Timer.cs" />
|
<Compile Include="Support\Timer.cs" />
|
||||||
<Compile Include="TypeDictionary.cs" />
|
<Compile Include="TypeDictionary.cs" />
|
||||||
<Compile Include="Map\ActorReference.cs" />
|
<Compile Include="Map\ActorReference.cs" />
|
||||||
<Compile Include="Map\Map.cs" />
|
|
||||||
<Compile Include="Map\TileReference.cs" />
|
<Compile Include="Map\TileReference.cs" />
|
||||||
<Compile Include="Map\Terrain.cs" />
|
<Compile Include="Map\Terrain.cs" />
|
||||||
<Compile Include="Primitives\Cache.cs" />
|
<Compile Include="Primitives\Cache.cs" />
|
||||||
|
|||||||
@@ -9,12 +9,13 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace OpenRA.FileFormats
|
namespace OpenRA.FileFormats
|
||||||
{
|
{
|
||||||
public class TypeDictionary
|
public class TypeDictionary : IEnumerable
|
||||||
{
|
{
|
||||||
Dictionary<Type, object> dataSingular = new Dictionary<Type, object>();
|
Dictionary<Type, object> dataSingular = new Dictionary<Type, object>();
|
||||||
Dictionary<Type, List<object>> dataMultiple = new Dictionary<Type, List<object>>();
|
Dictionary<Type, List<object>> dataMultiple = new Dictionary<Type, List<object>>();
|
||||||
@@ -85,7 +86,7 @@ namespace OpenRA.FileFormats
|
|||||||
return new T[ 0 ];
|
return new T[ 0 ];
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<object> GetEnumerator()
|
public IEnumerator GetEnumerator()
|
||||||
{
|
{
|
||||||
return WithInterface<object>().GetEnumerator();
|
return WithInterface<object>().GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,13 +35,14 @@ namespace OpenRA
|
|||||||
IActivity currentActivity;
|
IActivity currentActivity;
|
||||||
public Group Group;
|
public Group Group;
|
||||||
|
|
||||||
public Actor(World world, string name, int2 location, Player owner)
|
internal Actor(World world, string name, TypeDictionary initDict )
|
||||||
{
|
{
|
||||||
World = world;
|
var init = new ActorInitializer( this, initDict );
|
||||||
ActorID = world.NextAID();
|
|
||||||
Owner = owner;
|
|
||||||
|
|
||||||
var init = new ActorInitializer( this, location );
|
World = world;
|
||||||
|
ActorID = world.NextAID();
|
||||||
|
if( initDict.Contains<OwnerInit>() )
|
||||||
|
Owner = init.Get<OwnerInit,Player>();
|
||||||
|
|
||||||
if (name != null)
|
if (name != null)
|
||||||
{
|
{
|
||||||
@@ -182,18 +183,10 @@ namespace OpenRA
|
|||||||
var o = obj as Actor;
|
var o = obj as Actor;
|
||||||
return ( o != null && o.ActorID == ActorID );
|
return ( o != null && o.ActorID == ActorID );
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public override string ToString()
|
||||||
public class ActorInitializer
|
|
||||||
{
|
|
||||||
public readonly Actor self;
|
|
||||||
public World world { get { return self.World; } }
|
|
||||||
public readonly int2 location;
|
|
||||||
|
|
||||||
public ActorInitializer( Actor actor, int2 location )
|
|
||||||
{
|
{
|
||||||
this.self = actor;
|
return "{0} {1}{2}".F( Info.Name, ActorID, IsInWorld ? "" : " (not in world)" );
|
||||||
this.location = location;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
83
OpenRA.Game/ActorInitializer.cs
Executable file
83
OpenRA.Game/ActorInitializer.cs
Executable file
@@ -0,0 +1,83 @@
|
|||||||
|
#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.Linq;
|
||||||
|
using OpenRA.FileFormats;
|
||||||
|
|
||||||
|
namespace OpenRA
|
||||||
|
{
|
||||||
|
public class ActorInitializer
|
||||||
|
{
|
||||||
|
public readonly Actor self;
|
||||||
|
public World world { get { return self.World; } }
|
||||||
|
internal TypeDictionary dict;
|
||||||
|
|
||||||
|
public ActorInitializer( Actor actor, TypeDictionary dict )
|
||||||
|
{
|
||||||
|
this.self = actor;
|
||||||
|
this.dict = dict;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Get<T>()
|
||||||
|
where T : IActorInit
|
||||||
|
{
|
||||||
|
return dict.Get<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public U Get<T,U>()
|
||||||
|
where T : IActorInit<U>
|
||||||
|
{
|
||||||
|
return dict.Get<T>().Value( world );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IActorInit
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IActorInit<T> : IActorInit
|
||||||
|
{
|
||||||
|
T Value( World world );
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LocationInit : IActorInit<int2>
|
||||||
|
{
|
||||||
|
public readonly int2 value = int2.Zero;
|
||||||
|
|
||||||
|
public LocationInit( int2 init )
|
||||||
|
{
|
||||||
|
value = init;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int2 Value( World world )
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class OwnerInit : IActorInit<Player>
|
||||||
|
{
|
||||||
|
public readonly string PlayerName = "Neutral";
|
||||||
|
Player player;
|
||||||
|
|
||||||
|
public OwnerInit( Player player )
|
||||||
|
{
|
||||||
|
this.player = player;
|
||||||
|
this.PlayerName = player.InternalName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player Value( World world )
|
||||||
|
{
|
||||||
|
if( player != null )
|
||||||
|
return player;
|
||||||
|
return world.players.First( x => x.Value.InternalName == PlayerName ).Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
OpenRA.FileFormats/Map/Map.cs → OpenRA.Game/Map.cs
Normal file → Executable file
17
OpenRA.FileFormats/Map/Map.cs → OpenRA.Game/Map.cs
Normal file → Executable file
@@ -15,8 +15,9 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using OpenRA.FileFormats;
|
||||||
|
|
||||||
namespace OpenRA.FileFormats
|
namespace OpenRA
|
||||||
{
|
{
|
||||||
public class Map
|
public class Map
|
||||||
{
|
{
|
||||||
@@ -118,14 +119,14 @@ namespace OpenRA.FileFormats
|
|||||||
|
|
||||||
// Actors
|
// Actors
|
||||||
if (MapFormat == 1)
|
if (MapFormat == 1)
|
||||||
{
|
{
|
||||||
int actors = 0;
|
int actors = 0;
|
||||||
foreach (var kv in yaml["Actors"].Nodes)
|
foreach (var kv in yaml["Actors"].Nodes)
|
||||||
{
|
{
|
||||||
string[] vals = kv.Value.Value.Split(' ');
|
string[] vals = kv.Value.Value.Split(' ');
|
||||||
string[] loc = vals[2].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");
|
var a = new ActorReference("Actor"+actors++, vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), "Neutral");
|
||||||
Actors.Add(a.Id, a);
|
Actors.Add(a.Id, a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -274,8 +275,8 @@ namespace OpenRA.FileFormats
|
|||||||
{
|
{
|
||||||
// UID is calculated by taking an SHA1 of the yaml and binary data
|
// UID is calculated by taking an SHA1 of the yaml and binary data
|
||||||
// Read the relevant data into a buffer
|
// Read the relevant data into a buffer
|
||||||
var data = Exts.ReadAllBytes(Package.GetContent("map.yaml"))
|
var data = Package.GetContent("map.yaml").ReadAllBytes()
|
||||||
.Concat(Exts.ReadAllBytes(Package.GetContent("map.bin"))).ToArray();
|
.Concat(Package.GetContent("map.bin").ReadAllBytes()).ToArray();
|
||||||
|
|
||||||
// Take the SHA1
|
// Take the SHA1
|
||||||
using (var csp = SHA1.Create())
|
using (var csp = SHA1.Create())
|
||||||
@@ -227,6 +227,8 @@
|
|||||||
<Project>{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}</Project>
|
<Project>{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}</Project>
|
||||||
<Name>OpenRA.FileFormats</Name>
|
<Name>OpenRA.FileFormats</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
|
<Compile Include="ActorInitializer.cs" />
|
||||||
|
<Compile Include="Map.cs" />
|
||||||
<Compile Include="Widgets\Delegates\DeveloperModeDelegate.cs" />
|
<Compile Include="Widgets\Delegates\DeveloperModeDelegate.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ namespace OpenRA.Traits
|
|||||||
public Building(ActorInitializer init)
|
public Building(ActorInitializer init)
|
||||||
{
|
{
|
||||||
this.self = init.self;
|
this.self = init.self;
|
||||||
this.topLeft = init.location;
|
this.topLeft = init.Get<LocationInit,int2>();
|
||||||
Info = self.Info.Traits.Get<BuildingInfo>();
|
Info = self.Info.Traits.Get<BuildingInfo>();
|
||||||
self.CenterLocation = Game.CellSize
|
self.CenterLocation = Game.CellSize
|
||||||
* ((float2)topLeft + .5f * (float2)Info.Dimensions);
|
* ((float2)topLeft + .5f * (float2)Info.Dimensions);
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ namespace OpenRA.Traits
|
|||||||
{
|
{
|
||||||
this.self = init.self;
|
this.self = init.self;
|
||||||
this.Info = info;
|
this.Info = info;
|
||||||
this.__fromCell = this.__toCell = init.location;
|
this.__fromCell = this.__toCell = init.Get<LocationInit,int2>();
|
||||||
|
|
||||||
shroud = self.World.WorldActor.traits.Get<Shroud>();
|
shroud = self.World.WorldActor.traits.Get<Shroud>();
|
||||||
uim = self.World.WorldActor.traits.Get<UnitInfluence>();
|
uim = self.World.WorldActor.traits.Get<UnitInfluence>();
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace OpenRA.Widgets
|
|||||||
public Func<MapStub> Map = () => null;
|
public Func<MapStub> Map = () => null;
|
||||||
public Action<int> OnSpawnClick = spawn => {};
|
public Action<int> OnSpawnClick = spawn => {};
|
||||||
public Func<Dictionary<int2, Color>> SpawnColors = () => new Dictionary<int2, Color>();
|
public Func<Dictionary<int2, Color>> SpawnColors = () => new Dictionary<int2, Color>();
|
||||||
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview(stub.Map));
|
static Cache<MapStub,Bitmap> PreviewCache = new Cache<MapStub, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Package )));
|
||||||
|
|
||||||
public MapPreviewWidget() : base() { }
|
public MapPreviewWidget() : base() { }
|
||||||
protected MapPreviewWidget(MapPreviewWidget other)
|
protected MapPreviewWidget(MapPreviewWidget other)
|
||||||
|
|||||||
@@ -90,8 +90,8 @@ namespace OpenRA
|
|||||||
|
|
||||||
WorldRenderer = new WorldRenderer(this);
|
WorldRenderer = new WorldRenderer(this);
|
||||||
Timer.Time("renderer: {0}");
|
Timer.Time("renderer: {0}");
|
||||||
|
|
||||||
WorldActor = CreateActor("World", new int2(int.MaxValue, int.MaxValue), null);
|
WorldActor = CreateActor( "World", new TypeDictionary() );
|
||||||
|
|
||||||
// Add Map Players
|
// Add Map Players
|
||||||
int mapPlayerIndex = -1;
|
int mapPlayerIndex = -1;
|
||||||
@@ -124,11 +124,32 @@ namespace OpenRA
|
|||||||
|
|
||||||
Timer.Time( "----end World.ctor" );
|
Timer.Time( "----end World.ctor" );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Actor CreateActor( string name, int2 location, Player owner )
|
public Actor CreateActor( string name, int2 location, Player owner )
|
||||||
{
|
{
|
||||||
var a = new Actor( this, name, location, owner );
|
return CreateActor( true, name, location, owner );
|
||||||
Add( a );
|
}
|
||||||
|
|
||||||
|
public Actor CreateActor( bool addToWorld, string name, int2 location, Player owner )
|
||||||
|
{
|
||||||
|
var initDict = new TypeDictionary
|
||||||
|
{
|
||||||
|
new LocationInit( location ),
|
||||||
|
new OwnerInit( owner ),
|
||||||
|
};
|
||||||
|
return CreateActor( addToWorld, name, initDict );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Actor CreateActor( string name, TypeDictionary initDict )
|
||||||
|
{
|
||||||
|
return CreateActor( true, name, initDict );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Actor CreateActor( bool addToWorld, string name, TypeDictionary initDict )
|
||||||
|
{
|
||||||
|
var a = new Actor( this, name, initDict );
|
||||||
|
if( addToWorld )
|
||||||
|
Add( a );
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Cnc
|
|||||||
a.traits.Get<IFacing>().Facing = 64;
|
a.traits.Get<IFacing>().Facing = 64;
|
||||||
a.traits.Get<IMove>().Altitude = a.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
|
a.traits.Get<IMove>().Altitude = a.Info.Traits.Get<PlaneInfo>().CruiseAltitude;
|
||||||
|
|
||||||
var newUnit = new Actor(self.World, producee.Name, new int2(0, 0), self.Owner);
|
var newUnit = self.World.CreateActor(false, producee.Name, new int2(0, 0), self.Owner);
|
||||||
cargo.Load(a, newUnit);
|
cargo.Load(a, newUnit);
|
||||||
|
|
||||||
a.CancelActivity();
|
a.CancelActivity();
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA
|
|||||||
|
|
||||||
public Aircraft( ActorInitializer init , AircraftInfo info)
|
public Aircraft( ActorInitializer init , AircraftInfo info)
|
||||||
{
|
{
|
||||||
this.Location = init.location;
|
this.Location = init.Get<LocationInit,int2>();
|
||||||
Info = info;
|
Info = info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace OpenRA.Mods.RA
|
|||||||
public Crate(ActorInitializer init, CrateInfo info)
|
public Crate(ActorInitializer init, CrateInfo info)
|
||||||
{
|
{
|
||||||
this.self = init.self;
|
this.self = init.self;
|
||||||
this.Location = init.location;
|
this.Location = init.Get<LocationInit,int2>();
|
||||||
this.Info = info;
|
this.Info = info;
|
||||||
|
|
||||||
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ namespace OpenRA.Mods.RA
|
|||||||
|
|
||||||
self.World.AddFrameEndTask(w =>
|
self.World.AddFrameEndTask(w =>
|
||||||
{
|
{
|
||||||
var crate = new Actor(w, "crate", new int2(0, 0), w.WorldActor.Owner);
|
var crate = w.CreateActor(false, "crate", new int2(0, 0), w.WorldActor.Owner);
|
||||||
crates.Add(crate);
|
crates.Add(crate);
|
||||||
|
|
||||||
var startPos = w.ChooseRandomEdgeCell();
|
var startPos = w.ChooseRandomEdgeCell();
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA.Crates
|
|||||||
var location = ChooseEmptyCellNear(collector);
|
var location = ChooseEmptyCellNear(collector);
|
||||||
if (location != null)
|
if (location != null)
|
||||||
collector.World.AddFrameEndTask(
|
collector.World.AddFrameEndTask(
|
||||||
w => w.Add(new Actor(w, Info.Unit, location.Value, collector.Owner)));
|
w => w.CreateActor(Info.Unit, location.Value, collector.Owner));
|
||||||
|
|
||||||
base.Activate(collector);
|
base.Activate(collector);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ namespace OpenRA.Mods.RA
|
|||||||
public Husk(ActorInitializer init)
|
public Husk(ActorInitializer init)
|
||||||
{
|
{
|
||||||
this.self = init.self;
|
this.self = init.self;
|
||||||
this.location = init.location;
|
this.location = init.Get<LocationInit,int2>();
|
||||||
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ namespace OpenRA.Mods.RA
|
|||||||
{
|
{
|
||||||
this.self = init.self;
|
this.self = init.self;
|
||||||
this.info = info;
|
this.info = info;
|
||||||
this.location = init.location;
|
this.location = init.Get<LocationInit,int2>();
|
||||||
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ namespace OpenRA.Mods.RA
|
|||||||
|
|
||||||
var cargo = a.traits.Get<Cargo>();
|
var cargo = a.traits.Get<Cargo>();
|
||||||
foreach (var i in items)
|
foreach (var i in items)
|
||||||
cargo.Load(a, new Actor(owner.World, i.ToLowerInvariant(),
|
cargo.Load(a, owner.World.CreateActor(false, i.ToLowerInvariant(),
|
||||||
new int2(0,0), a.Owner));
|
new int2(0,0), a.Owner));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user