initial bridges hack

This commit is contained in:
Chris Forbes
2010-01-18 22:53:39 +13:00
parent b3ea880ca5
commit 3101ffa671
11 changed files with 183 additions and 21 deletions

63
OpenRa.Game/Bridges.cs Normal file
View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Traits;
namespace OpenRa
{
static class Bridges
{
public static void MakeBridges(World w)
{
var mini = w.Map.XOffset; var maxi = w.Map.XOffset + w.Map.Width;
var minj = w.Map.YOffset; var maxj = w.Map.YOffset + w.Map.Height;
for (var j = minj; j < maxj; j++)
for (var i = mini; i < maxi; i++)
if (IsBridge(w, w.Map.MapTiles[i, j].tile))
ConvertBridgeToActor(w, i, j);
}
static void ConvertBridgeToActor(World w, int i, int j)
{
var tile = w.Map.MapTiles[i, j].tile;
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 y = nj; y < nj + template.Size.Y; y++)
for (var x = ni; x < ni + template.Size.X; x++)
{
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].tile == tile)
{
// stash it
replacedTiles[new int2(x, y)] = w.Map.MapTiles[x, y].image;
// remove the tile from the actual map
w.Map.MapTiles[x, y].tile = 0xff;
w.Map.MapTiles[x, y].image = 0;
}
}
if (replacedTiles.Any())
{
var a = w.CreateActor("Bridge", new int2(ni, nj), null);
var br = a.traits.Get<Bridge>();
br.SetTiles(template, replacedTiles);
}
}
static bool IsBridge(World w, ushort t)
{
return w.TileSet.walk[t].IsBridge;
}
}
}

View File

@@ -107,8 +107,6 @@ namespace OpenRa.Graphics
public static Sprite GetImageFromCollection(Renderer renderer,string collection, string image)
{
// Cached sprite
if (cachedSprites.ContainsKey(collection) && cachedSprites[collection].ContainsKey(image))
return cachedSprites[collection][image];

View File

@@ -77,6 +77,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Bridges.cs" />
<Compile Include="Chat.cs" />
<Compile Include="Chrome.cs" />
<Compile Include="Combat.cs" />
@@ -210,6 +211,7 @@
<Compile Include="Traits\AutoHeal.cs" />
<Compile Include="Traits\AutoTarget.cs" />
<Compile Include="Traits\BelowUnits.cs" />
<Compile Include="Traits\Bridge.cs" />
<Compile Include="Traits\Buildable.cs" />
<Compile Include="Traits\Building.cs" />
<Compile Include="Traits\Cargo.cs" />

View File

@@ -137,6 +137,8 @@ namespace OpenRa
internal void Draw(SpriteRenderer r)
{
return;
if (dirty)
{
dirty = false;

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Graphics;
using OpenRa.FileFormats;
namespace OpenRa.Traits
{
class BridgeInfo : ITraitInfo
{
public object Create(Actor self) { return new Bridge(); }
}
class Bridge : IRender, ITick
{
Animation anim;
public Bridge() {}
public IEnumerable<Renderable> Render(Actor self)
{
if (anim != null)
return new[] { Util.Centered(self, anim.Image, self.CenterLocation) };
else
return new Renderable[] { };
}
public void Tick(Actor self)
{
if (anim == null)
{
anim = new Animation("3tnk");
anim.PlayRepeating("idle");
}
}
public void SetTiles(TileTemplate template, Dictionary<int2, int> replacedTiles)
{
/* todo: stash these, etc */
}
}
}

View File

@@ -44,6 +44,8 @@ namespace OpenRa
CreateActor("World", new int2(int.MaxValue, int.MaxValue), null);
Bridges.MakeBridges(this);
WorldRenderer = new WorldRenderer(this, Game.renderer);
Minimap = new Minimap(this, Game.renderer);
}