moved bridge support into ra

This commit is contained in:
Chris Forbes
2010-05-20 18:16:00 +12:00
parent 4585addff2
commit e5951b1502
5 changed files with 9 additions and 7 deletions

View File

@@ -22,15 +22,15 @@ using System.Drawing;
namespace OpenRA.Graphics
{
class SheetBuilder
public class SheetBuilder
{
public static SheetBuilder SharedInstance;
public static void Initialize(Renderer r)
internal static void Initialize(Renderer r)
{
SharedInstance = new SheetBuilder(r, TextureChannel.Red);
}
public SheetBuilder(Renderer r, TextureChannel ch)
internal SheetBuilder(Renderer r, TextureChannel ch)
{
renderer = r;
current = null;

View File

@@ -214,7 +214,6 @@
<Compile Include="Traits\AI\AutoHeal.cs" />
<Compile Include="Traits\AI\AutoTarget.cs" />
<Compile Include="Traits\Modifiers\BelowUnits.cs" />
<Compile Include="Traits\Bridge.cs" />
<Compile Include="Traits\Buildable.cs" />
<Compile Include="Traits\Building.cs" />
<Compile Include="Traits\World\BuildingInfluence.cs" />
@@ -298,7 +297,6 @@
<Compile Include="Widgets\PerfGraphWidget.cs" />
<Compile Include="Widgets\Delegates\PerfDebugDelegate.cs" />
<Compile Include="Widgets\BuildPaletteWidget.cs" />
<Compile Include="Traits\World\BridgeLayer.cs" />
<Compile Include="Widgets\Delegates\LobbyDelegate.cs" />
<Compile Include="Widgets\ColorBlockWidget.cs" />
<Compile Include="GameRules\MusicInfo.cs" />

View File

@@ -1,175 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.GameRules;
using OpenRA.Graphics;
namespace OpenRA.Traits
{
class BridgeInfo : ITraitInfo
{
public readonly bool Long = false;
public readonly bool UseAlternateNames = false;
public readonly int[] NorthOffset = null;
public readonly int[] SouthOffset = null;
public object Create(Actor self) { return new Bridge(self); }
}
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; }
static string cachedTileset;
static Cache<TileReference<ushort,byte>, Sprite> sprites;
public IEnumerable<Renderable> Render(Actor self)
{
foreach (var t in TileSprites[state])
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>();
if (info.UseAlternateNames)
{
if (t.Name.EndsWith("d")) return 2;
if (t.Name.EndsWith("h")) return 1;
return 0;
}
else
return t.Name[t.Name.Length - 1] - 'a';
}
public string NameFromState(TileTemplate t, int state)
{
var info = self.Info.Traits.Get<BridgeInfo>();
if (info.UseAlternateNames)
return t.Bridge + new[] { "", "h", "d" }[state];
else
return t.Bridge + (char)(state + 'a');
}
public void SetTiles(World world, TileTemplate template, Dictionary<int2, int> replacedTiles)
{
Tiles = replacedTiles;
state = StateFromTemplate(template);
if (cachedTileset != world.Map.Tileset)
{
cachedTileset = world.Map.Tileset;
sprites = new Cache<TileReference<ushort,byte>, Sprite>(
x => SheetBuilder.SharedInstance.Add(world.TileSet.GetBytes(x),
new Size(Game.CellSize, Game.CellSize)));
}
var numStates = self.Info.Traits.Get<BridgeInfo>().Long ? 6 : 3;
for (var n = 0; n < numStates; n++)
{
var stateTemplate = world.TileSet.Walkability.GetTileTemplate(NameFromState(template, n));
Templates.Add( stateTemplate );
TileSprites.Add(replacedTiles.ToDictionary(
a => a.Key,
a => sprites[new TileReference<ushort,byte>((ushort)stateTemplate.Index, (byte)a.Value)]));
}
self.Health = (int)(self.GetMaxHP() * template.HP);
}
public float GetCost(int2 p, UnitMovementType umt)
{
return Rules.TerrainTypes[Templates[state].TerrainType[Tiles[p]]].GetCost(umt);
}
public float GetSpeedModifier(int2 p, UnitMovementType umt)
{
return Rules.TerrainTypes[Templates[state].TerrainType[Tiles[p]]].GetSpeedModifier(umt);
}
static bool IsIntact(Bridge b)
{
return b != null && b.self.IsInWorld && b.self.Health > 0;
}
static bool IsLong(Bridge b)
{
return b != null && b.self.IsInWorld && b.self.Info.Traits.Get<BridgeInfo>().Long;
}
void UpdateState()
{
var ds = self.GetDamageState();
if (!self.Info.Traits.Get<BridgeInfo>().Long)
{
state = (int)ds;
return;
}
bool waterToSouth = !IsIntact(southNeighbour) && (!IsLong(southNeighbour) || !IsIntact(this));
bool waterToNorth = !IsIntact(northNeighbour) && (!IsLong(northNeighbour) || !IsIntact(this));
if (waterToSouth && waterToNorth) { state = 5; return; }
if (waterToNorth) { state = 4; return; }
if (waterToSouth) { state = 3; return; }
state = (int)ds;
}
public void Damaged(Actor self, AttackInfo e)
{
if (e.DamageStateChanged)
{
UpdateState();
if (northNeighbour != null) northNeighbour.UpdateState();
if (southNeighbour != null) southNeighbour.UpdateState();
}
}
}
}

View File

@@ -1,116 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.GameRules;
namespace OpenRA.Traits
{
class BridgeLayerInfo : TraitInfo<BridgeLayer> { }
class BridgeLayer : ILoadWorldHook, ICustomTerrain
{
// for tricky things like bridges.
Bridge[,] customTerrain;
void MakeBridges(World w)
{
var tl = w.Map.TopLeft;
var br = w.Map.BottomRight;
for (int i = tl.X; i < br.X; i++)
for (int j = tl.Y; j < br.Y; j++)
if (IsBridge(w, w.Map.MapTiles[i, j].type))
ConvertBridgeToActor(w, i, j);
foreach (var b in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>()))
b.FinalizeBridges(w, customTerrain);
}
void ConvertBridgeToActor(World w, int i, int j)
{
Log.Write("Converting bridge at {0} {1}",i,j);
var tile = w.Map.MapTiles[i, j].type;
var image = w.Map.MapTiles[i, j].image;
var template = w.TileSet.walk[tile];
// base position of the tile
var ni = i - image % template.Size.X;
var nj = j - image / template.Size.X;
var replacedTiles = new Dictionary<int2, int>();
for (var x = ni; x < ni + template.Size.X; x++)
for (var y = nj; y < nj + template.Size.Y; y++)
{
var n = (x - ni) + template.Size.X * (y - nj);
if (!template.TerrainType.ContainsKey(n)) continue;
if (w.Map.IsInMap(x, y))
if (w.Map.MapTiles[x, y].type == tile
&& w.Map.MapTiles[x, y].index == n)
{
// stash it
replacedTiles[new int2(x, y)] = w.Map.MapTiles[x, y].index;
// remove the tile from the actual map
w.Map.MapTiles[x, y].type = 0xfffe;
w.Map.MapTiles[x, y].index = 0;
w.Map.MapTiles[x, y].image = 0;
}
}
if (replacedTiles.Any())
{
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;
}
public float GetSpeedModifier(int2 p, UnitMovementType umt)
{
if (customTerrain[p.X, p.Y] != null)
return customTerrain[p.X,p.Y].GetSpeedModifier(p,umt);
return 1f;
}
static bool IsBridge(World w, ushort t)
{
return w.TileSet.walk[t].Bridge != null;
}
public void WorldLoaded(World w)
{
customTerrain = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y];
MakeBridges(w);
}
}
}