new mapactor init stuff

This commit is contained in:
Bob
2010-08-01 10:31:23 +12:00
parent f789aef892
commit 10b7ece62e
20 changed files with 149 additions and 49 deletions

View File

@@ -152,6 +152,7 @@ namespace OpenRA.Editor
try
{
var info = Rules.Info[a];
if( !info.Traits.Contains<RenderSimpleInfo>() ) continue;
var template = RenderActor(info, tileset, palette);
var ibox = new PictureBox
{

View File

@@ -17,10 +17,10 @@ namespace OpenRA.FileFormats
{
public class MapStub
{
public IFolder Package;
public readonly IFolder Package;
// Yaml map data
public string Uid;
public readonly string Uid;
public bool Selectable;
public string Title;
@@ -35,7 +35,6 @@ namespace OpenRA.FileFormats
public int2 BottomRight;
public int Width { get { return BottomRight.X - TopLeft.X; } }
public int Height { get { return BottomRight.Y - TopLeft.Y; } }
public Map Map { get { return new Map(Package); }}
static List<string> Fields = new List<string>() {
"Selectable", "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight"

View File

@@ -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">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -77,7 +77,6 @@
<Compile Include="Support\Timer.cs" />
<Compile Include="TypeDictionary.cs" />
<Compile Include="Map\ActorReference.cs" />
<Compile Include="Map\Map.cs" />
<Compile Include="Map\TileReference.cs" />
<Compile Include="Map\Terrain.cs" />
<Compile Include="Primitives\Cache.cs" />

View File

@@ -9,12 +9,13 @@
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.FileFormats
{
public class TypeDictionary
public class TypeDictionary : IEnumerable
{
Dictionary<Type, object> dataSingular = new Dictionary<Type, object>();
Dictionary<Type, List<object>> dataMultiple = new Dictionary<Type, List<object>>();
@@ -85,7 +86,7 @@ namespace OpenRA.FileFormats
return new T[ 0 ];
}
public IEnumerator<object> GetEnumerator()
public IEnumerator GetEnumerator()
{
return WithInterface<object>().GetEnumerator();
}

View File

@@ -35,13 +35,14 @@ namespace OpenRA
IActivity currentActivity;
public Group Group;
public Actor(World world, string name, int2 location, Player owner)
internal Actor(World world, string name, TypeDictionary initDict )
{
var init = new ActorInitializer( this, initDict );
World = world;
ActorID = world.NextAID();
Owner = owner;
var init = new ActorInitializer( this, location );
if( initDict.Contains<OwnerInit>() )
Owner = init.Get<OwnerInit,Player>();
if (name != null)
{
@@ -182,18 +183,10 @@ namespace OpenRA
var o = obj as Actor;
return ( o != null && o.ActorID == ActorID );
}
}
public class ActorInitializer
public override string ToString()
{
public readonly Actor self;
public World world { get { return self.World; } }
public readonly int2 location;
public ActorInitializer( Actor actor, int2 location )
{
this.self = actor;
this.location = location;
return "{0} {1}{2}".F( Info.Name, ActorID, IsInWorld ? "" : " (not in world)" );
}
}
}

83
OpenRA.Game/ActorInitializer.cs Executable file
View 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;
}
}
}

7
OpenRA.FileFormats/Map/Map.cs → OpenRA.Game/Map.cs Normal file → Executable file
View File

@@ -15,8 +15,9 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using OpenRA.FileFormats;
namespace OpenRA.FileFormats
namespace OpenRA
{
public class Map
{
@@ -274,8 +275,8 @@ namespace OpenRA.FileFormats
{
// UID is calculated by taking an SHA1 of the yaml and binary data
// Read the relevant data into a buffer
var data = Exts.ReadAllBytes(Package.GetContent("map.yaml"))
.Concat(Exts.ReadAllBytes(Package.GetContent("map.bin"))).ToArray();
var data = Package.GetContent("map.yaml").ReadAllBytes()
.Concat(Package.GetContent("map.bin").ReadAllBytes()).ToArray();
// Take the SHA1
using (var csp = SHA1.Create())

View File

@@ -227,6 +227,8 @@
<Project>{BDAEAB25-991E-46A7-AF1E-4F0E03358DAA}</Project>
<Name>OpenRA.FileFormats</Name>
</ProjectReference>
<Compile Include="ActorInitializer.cs" />
<Compile Include="Map.cs" />
<Compile Include="Widgets\Delegates\DeveloperModeDelegate.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -53,7 +53,7 @@ namespace OpenRA.Traits
public Building(ActorInitializer init)
{
this.self = init.self;
this.topLeft = init.location;
this.topLeft = init.Get<LocationInit,int2>();
Info = self.Info.Traits.Get<BuildingInfo>();
self.CenterLocation = Game.CellSize
* ((float2)topLeft + .5f * (float2)Info.Dimensions);

View File

@@ -76,7 +76,7 @@ namespace OpenRA.Traits
{
this.self = init.self;
this.Info = info;
this.__fromCell = this.__toCell = init.location;
this.__fromCell = this.__toCell = init.Get<LocationInit,int2>();
shroud = self.World.WorldActor.traits.Get<Shroud>();
uim = self.World.WorldActor.traits.Get<UnitInfluence>();

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Widgets
public Func<MapStub> Map = () => null;
public Action<int> OnSpawnClick = spawn => {};
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() { }
protected MapPreviewWidget(MapPreviewWidget other)

View File

@@ -91,7 +91,7 @@ namespace OpenRA
WorldRenderer = new WorldRenderer(this);
Timer.Time("renderer: {0}");
WorldActor = CreateActor("World", new int2(int.MaxValue, int.MaxValue), null);
WorldActor = CreateActor( "World", new TypeDictionary() );
// Add Map Players
int mapPlayerIndex = -1;
@@ -127,7 +127,28 @@ namespace OpenRA
public Actor CreateActor( string name, int2 location, Player owner )
{
var a = new Actor( this, name, location, owner );
return CreateActor( true, name, location, owner );
}
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;
}

View File

@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Cnc
a.traits.Get<IFacing>().Facing = 64;
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);
a.CancelActivity();

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA
public Aircraft( ActorInitializer init , AircraftInfo info)
{
this.Location = init.location;
this.Location = init.Get<LocationInit,int2>();
Info = info;
}

View File

@@ -49,7 +49,7 @@ namespace OpenRA.Mods.RA
public Crate(ActorInitializer init, CrateInfo info)
{
this.self = init.self;
this.Location = init.location;
this.Location = init.Get<LocationInit,int2>();
this.Info = info;
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);

View File

@@ -67,7 +67,7 @@ namespace OpenRA.Mods.RA
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);
var startPos = w.ChooseRandomEdgeCell();

View File

@@ -41,7 +41,7 @@ namespace OpenRA.Mods.RA.Crates
var location = ChooseEmptyCellNear(collector);
if (location != null)
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);
}

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.RA
public Husk(ActorInitializer init)
{
this.self = init.self;
this.location = init.location;
this.location = init.Get<LocationInit,int2>();
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
}

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.RA
{
this.self = init.self;
this.info = info;
this.location = init.location;
this.location = init.Get<LocationInit,int2>();
self.World.WorldActor.traits.Get<UnitInfluence>().Add(self, this);
}

View File

@@ -70,7 +70,7 @@ namespace OpenRA.Mods.RA
var cargo = a.traits.Get<Cargo>();
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));
});
}