Fix ICustomTerrain to also work for resources
This commit is contained in:
@@ -36,14 +36,13 @@ namespace OpenRA.Traits
|
||||
public object Create(Actor self) { return new Bridge(self); }
|
||||
}
|
||||
|
||||
class Bridge: IRender, ICustomTerrain, INotifyDamage
|
||||
class Bridge: IRender, INotifyDamage
|
||||
{
|
||||
Dictionary<int2, int> Tiles;
|
||||
List<Dictionary<int2, Sprite>> TileSprites = new List<Dictionary<int2,Sprite>>();
|
||||
List<TileTemplate> Templates = new List<TileTemplate>();
|
||||
Actor self;
|
||||
int state;
|
||||
|
||||
Bridge northNeighbour, southNeighbour;
|
||||
|
||||
public Bridge(Actor self) { this.self = self; self.RemoveOnDeath = false; }
|
||||
@@ -57,6 +56,25 @@ namespace OpenRA.Traits
|
||||
yield return new Renderable(t.Value, Game.CellSize * t.Key, "terrain");
|
||||
}
|
||||
|
||||
public void FinalizeBridges(World world, Bridge[,] bridges)
|
||||
{
|
||||
// go looking for our neighbors, if this is a long bridge.
|
||||
var info = self.Info.Traits.Get<BridgeInfo>();
|
||||
if (info.NorthOffset != null)
|
||||
northNeighbour = GetNeighbor(world, info.NorthOffset, bridges);
|
||||
if (info.SouthOffset != null)
|
||||
southNeighbour = GetNeighbor(world, info.SouthOffset, bridges);
|
||||
}
|
||||
|
||||
public Bridge GetNeighbor(World world, int[] offset, Bridge[,] bridges)
|
||||
{
|
||||
if (offset == null) return null;
|
||||
var pos = self.Location + new int2(offset[0], offset[1]);
|
||||
if (!world.Map.IsInMap(pos.X, pos.Y)) return null;
|
||||
return bridges[pos.X, pos.Y];
|
||||
}
|
||||
|
||||
|
||||
public int StateFromTemplate(TileTemplate t)
|
||||
{
|
||||
var info = self.Info.Traits.Get<BridgeInfo>();
|
||||
@@ -83,10 +101,7 @@ namespace OpenRA.Traits
|
||||
{
|
||||
Tiles = replacedTiles;
|
||||
state = StateFromTemplate(template);
|
||||
|
||||
foreach (var t in replacedTiles.Keys)
|
||||
world.customTerrain[t.X, t.Y] = this;
|
||||
|
||||
|
||||
if (cachedTileset != world.Map.Tileset)
|
||||
{
|
||||
cachedTileset = world.Map.Tileset;
|
||||
@@ -109,24 +124,6 @@ namespace OpenRA.Traits
|
||||
self.Health = (int)(self.GetMaxHP() * template.HP);
|
||||
}
|
||||
|
||||
Bridge GetNeighbor(World world, int[] offset)
|
||||
{
|
||||
if (offset == null) return null;
|
||||
var pos = self.Location + new int2(offset[0], offset[1]);
|
||||
if (!world.Map.IsInMap(pos.X, pos.Y)) return null;
|
||||
return world.customTerrain[pos.X, pos.Y] as Bridge;
|
||||
}
|
||||
|
||||
public void FinalizeBridges(World world)
|
||||
{
|
||||
// go looking for our neighbors, if this is a long bridge.
|
||||
var info = self.Info.Traits.Get<BridgeInfo>();
|
||||
if (info.NorthOffset != null)
|
||||
northNeighbour = GetNeighbor(world, info.NorthOffset);
|
||||
if (info.SouthOffset != null)
|
||||
southNeighbour = GetNeighbor(world, info.SouthOffset);
|
||||
}
|
||||
|
||||
public float GetCost(int2 p, UnitMovementType umt)
|
||||
{
|
||||
return Rules.TerrainTypes[Templates[state].TerrainType[Tiles[p]]].GetCost(umt);
|
||||
|
||||
@@ -20,14 +20,18 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.GameRules;
|
||||
|
||||
namespace OpenRA.Traits
|
||||
{
|
||||
class BridgeLoadHookInfo : TraitInfo<BridgeLoadHook> { }
|
||||
class BridgeLayerInfo : TraitInfo<BridgeLayer> { }
|
||||
|
||||
class BridgeLoadHook : ILoadWorldHook
|
||||
class BridgeLayer : ILoadWorldHook, ICustomTerrain
|
||||
{
|
||||
static void MakeBridges(World w)
|
||||
// for tricky things like bridges.
|
||||
Bridge[,] customTerrain;
|
||||
|
||||
void MakeBridges(World w)
|
||||
{
|
||||
var tl = w.Map.TopLeft;
|
||||
var br = w.Map.BottomRight;
|
||||
@@ -38,10 +42,10 @@ namespace OpenRA.Traits
|
||||
ConvertBridgeToActor(w, i, j);
|
||||
|
||||
foreach (var b in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>()))
|
||||
b.FinalizeBridges(w);
|
||||
b.FinalizeBridges(w, customTerrain);
|
||||
}
|
||||
|
||||
static void ConvertBridgeToActor(World w, int i, int j)
|
||||
void ConvertBridgeToActor(World w, int i, int j)
|
||||
{
|
||||
Log.Write("Converting bridge at {0} {1}",i,j);
|
||||
|
||||
@@ -77,15 +81,30 @@ namespace OpenRA.Traits
|
||||
{
|
||||
var a = w.CreateActor(template.Bridge, new int2(ni, nj), w.NeutralPlayer);
|
||||
var br = a.traits.Get<Bridge>();
|
||||
|
||||
foreach (var t in replacedTiles.Keys)
|
||||
customTerrain[t.X, t.Y] = br;
|
||||
|
||||
br.SetTiles(w, template, replacedTiles);
|
||||
}
|
||||
}
|
||||
|
||||
public float GetCost(int2 p, UnitMovementType umt)
|
||||
{
|
||||
if (customTerrain[p.X, p.Y] != null)
|
||||
return customTerrain[p.X,p.Y].GetCost(p,umt);
|
||||
return 1f;
|
||||
}
|
||||
|
||||
static bool IsBridge(World w, ushort t)
|
||||
{
|
||||
return w.TileSet.walk[t].Bridge != null;
|
||||
}
|
||||
|
||||
public void WorldLoaded(World w) { MakeBridges(w); }
|
||||
public void WorldLoaded(World w)
|
||||
{
|
||||
customTerrain = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y];
|
||||
MakeBridges(w);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ namespace OpenRA.Traits
|
||||
public object Create(Actor self) { return new ResourceLayer(self); }
|
||||
}
|
||||
|
||||
public class ResourceLayer: IRenderOverlay, ILoadWorldHook
|
||||
public class ResourceLayer: IRenderOverlay, ILoadWorldHook, ICustomTerrain
|
||||
{
|
||||
SpriteRenderer sr;
|
||||
World world;
|
||||
@@ -88,18 +88,18 @@ namespace OpenRA.Traits
|
||||
content[x, y].density = GetIdealDensity(x, y);
|
||||
}
|
||||
|
||||
public float GetMovementCost(UnitMovementType umt, int2 p)
|
||||
public float GetSpeedMultiplier(UnitMovementType umt, int2 p)
|
||||
{
|
||||
if (content[p.X,p.Y].type == null)
|
||||
return 1.0f;
|
||||
return content[p.X,p.Y].type.GetMovementCost(umt);
|
||||
return content[p.X,p.Y].type.GetSpeedMultiplier(umt);
|
||||
}
|
||||
|
||||
public float GetPathCost(UnitMovementType umt, int2 p)
|
||||
public float GetCost(int2 p,UnitMovementType umt)
|
||||
{
|
||||
if (content[p.X,p.Y].type == null)
|
||||
return 1.0f;
|
||||
return content[p.X,p.Y].type.GetPathCost(umt);
|
||||
return content[p.X,p.Y].type.GetCost(umt);
|
||||
}
|
||||
|
||||
public Sprite[] ChooseContent(ResourceType t)
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace OpenRA.Traits
|
||||
int growthTicks;
|
||||
int spreadTicks;
|
||||
public ResourceTypeInfo info;
|
||||
float[] movementCost = new float[4];
|
||||
float[] movementSpeed = new float[4];
|
||||
float[] pathCost = new float[4];
|
||||
|
||||
public ResourceType(ResourceTypeInfo info)
|
||||
@@ -56,19 +56,20 @@ namespace OpenRA.Traits
|
||||
for (var umt = UnitMovementType.Foot; umt <= UnitMovementType.Float; umt++ )
|
||||
{
|
||||
// HACK: hardcode "ore" terraintype for now
|
||||
movementCost[(int)umt] = (info.MovementTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetCost(umt) : 1.0f;
|
||||
pathCost[(int)umt] = (info.PathingTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetCost(umt) : movementCost[(int)umt];
|
||||
movementSpeed[(int)umt] = (info.MovementTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetSpeedMultiplier(umt) : 1.0f;
|
||||
pathCost[(int)umt] = (info.PathingTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetCost(umt)
|
||||
: (info.MovementTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetCost(umt) : 1.0f;
|
||||
}
|
||||
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public float GetMovementCost(UnitMovementType umt)
|
||||
public float GetSpeedMultiplier(UnitMovementType umt)
|
||||
{
|
||||
return movementCost[(int)umt];
|
||||
return movementSpeed[(int)umt];
|
||||
}
|
||||
|
||||
public float GetPathCost(UnitMovementType umt)
|
||||
public float GetCost(UnitMovementType umt)
|
||||
{
|
||||
return pathCost[(int)umt];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user