Make speedModifiers Lazy

This commit is contained in:
penev92
2015-05-30 22:56:20 +03:00
parent 3666fabf1e
commit fcd9906683

View File

@@ -68,7 +68,7 @@ namespace OpenRA.Mods.Common.Traits
var ret = new Dictionary<string, TerrainInfo>(); var ret = new Dictionary<string, TerrainInfo>();
foreach (var t in y.ToDictionary()["TerrainSpeeds"].Nodes) foreach (var t in y.ToDictionary()["TerrainSpeeds"].Nodes)
{ {
var speed = FieldLoader.GetValue<decimal>("speed", t.Value.Value); var speed = FieldLoader.GetValue<int>("speed", t.Value.Value);
var nodesDict = t.Value.ToDictionary(); var nodesDict = t.Value.ToDictionary();
var cost = nodesDict.ContainsKey("PathingCost") var cost = nodesDict.ContainsKey("PathingCost")
? FieldLoader.GetValue<int>("cost", nodesDict["PathingCost"].Value) ? FieldLoader.GetValue<int>("cost", nodesDict["PathingCost"].Value)
@@ -100,7 +100,7 @@ namespace OpenRA.Mods.Common.Traits
public static readonly TerrainInfo Impassable = new TerrainInfo(); public static readonly TerrainInfo Impassable = new TerrainInfo();
public readonly int Cost; public readonly int Cost;
public readonly decimal Speed; public readonly int Speed;
public TerrainInfo() public TerrainInfo()
{ {
@@ -108,7 +108,7 @@ namespace OpenRA.Mods.Common.Traits
Speed = 0; Speed = 0;
} }
public TerrainInfo(decimal speed, int cost) public TerrainInfo(int speed, int cost)
{ {
Speed = speed; Speed = speed;
Cost = cost; Cost = cost;
@@ -311,7 +311,7 @@ namespace OpenRA.Mods.Common.Traits
internal int TicksBeforePathing = 0; internal int TicksBeforePathing = 0;
readonly Actor self; readonly Actor self;
readonly ISpeedModifier[] speedModifiers; readonly Lazy<ISpeedModifier[]> speedModifiers;
public readonly MobileInfo Info; public readonly MobileInfo Info;
public bool IsMoving { get; set; } public bool IsMoving { get; set; }
@@ -351,7 +351,7 @@ namespace OpenRA.Mods.Common.Traits
self = init.Self; self = init.Self;
Info = info; Info = info;
speedModifiers = self.TraitsImplementing<ISpeedModifier>().ToArray(); speedModifiers = Exts.Lazy(() => self.TraitsImplementing<ISpeedModifier>().ToArray());
ToSubCell = FromSubCell = info.SharesCell ? init.World.Map.DefaultSubCell : SubCell.FullCell; ToSubCell = FromSubCell = info.SharesCell ? init.World.Map.DefaultSubCell : SubCell.FullCell;
if (init.Contains<SubCellInit>()) if (init.Contains<SubCellInit>())
@@ -593,16 +593,13 @@ namespace OpenRA.Mods.Common.Traits
if (index == byte.MaxValue) if (index == byte.MaxValue)
return 0; return 0;
// TODO: Convert to integers var terrainSpeed = Info.TilesetTerrainInfo[self.World.TileSet][index].Speed;
var speed = Info.TilesetTerrainInfo[self.World.TileSet][index].Speed; if (terrainSpeed == 0)
if (speed == decimal.Zero)
return 0; return 0;
speed *= Info.Speed; var modifiers = speedModifiers.Value.Select(x => x.GetSpeedModifier()).Append(terrainSpeed);
foreach (var t in speedModifiers)
speed *= t.GetSpeedModifier() / 100m;
return (int)(speed / 100); return Util.ApplyPercentageModifiers(Info.Speed, modifiers);
} }
public void AddInfluence() public void AddInfluence()