Add Map.GetNamedActor for map actor lookup

This commit is contained in:
ScottNZ
2013-12-20 19:05:24 +13:00
parent 3cf06119f7
commit 09b694095c
3 changed files with 15 additions and 2 deletions

View File

@@ -10,6 +10,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLua; using NLua;
using OpenRA.Effects; using OpenRA.Effects;
@@ -34,6 +35,7 @@ namespace OpenRA.Mods.RA.Scripting
public class LuaScriptInterface : IWorldLoaded, ITick public class LuaScriptInterface : IWorldLoaded, ITick
{ {
World world; World world;
Dictionary<string, Actor> mapActors;
readonly LuaScriptContext context = new LuaScriptContext(); readonly LuaScriptContext context = new LuaScriptContext();
readonly LuaScriptInterfaceInfo info; readonly LuaScriptInterfaceInfo info;
@@ -45,6 +47,7 @@ namespace OpenRA.Mods.RA.Scripting
public void WorldLoaded(World w, WorldRenderer wr) public void WorldLoaded(World w, WorldRenderer wr)
{ {
world = w; world = w;
mapActors = world.WorldActor.Trait<SpawnMapActors>().Actors;
context.Lua["World"] = w; context.Lua["World"] = w;
context.Lua["WorldRenderer"] = wr; context.Lua["WorldRenderer"] = wr;
@@ -72,7 +75,7 @@ namespace OpenRA.Mods.RA.Scripting
void AddMapActorGlobals() void AddMapActorGlobals()
{ {
foreach (var kv in world.WorldActor.Trait<SpawnMapActors>().Actors) foreach (var kv in mapActors)
{ {
if (context.Lua[kv.Key] != null) if (context.Lua[kv.Key] != null)
context.ShowErrorMessage("{0}: The global name '{1}' is reserved and may not be used by map actor {2}".F(GetType().Name, kv.Key, kv.Value), null); context.ShowErrorMessage("{0}: The global name '{1}' is reserved and may not be used by map actor {2}".F(GetType().Name, kv.Key, kv.Value), null);
@@ -315,5 +318,11 @@ namespace OpenRA.Mods.RA.Scripting
{ {
return world.ChooseRandomEdgeCell(); return world.ChooseRandomEdgeCell();
} }
[LuaGlobal]
public Actor GetNamedActor(string actorName)
{
return mapActors[actorName];
}
} }
} }

View File

@@ -12,6 +12,10 @@ Map.GetRandomEdgeCell = function()
return Internal.GetRandomEdgeCell() return Internal.GetRandomEdgeCell()
end end
Map.GetNamedActor = function(actorName)
return Internal.GetNamedActor(actorName)
end
CPos.New = function(x, y) CPos.New = function(x, y)
return OpenRA.New("CPos", { { x, "Int32" }, { y, "Int32" } }) return OpenRA.New("CPos", { { x, "Int32" }, { y, "Int32" } })
end end

View File

@@ -72,7 +72,7 @@ end
SendCruisers = function() SendCruisers = function()
for i, cruiser in ipairs(Cruisers) do for i, cruiser in ipairs(Cruisers) do
local ca = Actor.Create(cruiser, { Owner = england, Location = SouthReinforcementsPoint.Location }) local ca = Actor.Create(cruiser, { Owner = england, Location = SouthReinforcementsPoint.Location })
Actor.Move(ca, _G["CruiserPoint" .. i].Location) Actor.Move(ca, Map.GetNamedActor("CruiserPoint" .. i).Location)
end end
end end