Skip to content

Commit

Permalink
Fixed wall placing. Gradually removing all references to the old tile…
Browse files Browse the repository at this point in the history
… array from the client so we can switch completely to using world coordinates.
  • Loading branch information
ostaf committed Oct 19, 2013
1 parent bdf1bae commit e532c7d
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 107 deletions.
12 changes: 3 additions & 9 deletions ClientInterfaces/Map/IMapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ClientInterfaces.Map
{
public delegate void TileChangeEvent(Point tilePosition, PointF tileWorldPosition);
public delegate void TileChangeEvent(PointF tileWorldPosition);

public interface IMapManager
{
Expand All @@ -20,17 +20,11 @@ public interface IMapManager
/// Get Tile from World Position.
/// </summary>
ITile GetITileAt(Vector2D WorldPos);
ITile[] GetITilesIn(RectangleF Area);

/// <summary>
/// Get Tile from Array Position.
/// </summary>
ITile GetITileAt(int array_x, int array_y);

Vector2D GetTileArrayPositionFromWorldPosition(float x, float z);
Point GetTileArrayPositionFromWorldPosition(Vector2D pos);
int GetMapWidth();
int GetMapHeight();
byte SetSprite(int x, int y);
byte SetSprite(Vector2D position);

void Init();
Size GetMapSizeWorld();
Expand Down
1 change: 0 additions & 1 deletion ClientInterfaces/Map/ITile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace ClientInterfaces.Map
public interface ITile
{
Vector2D Position { get; }
Point TilePosition { get; }
bool Visible { get; set; }
bool ConnectSprite { get; set; }
bool Opaque { get; set; }
Expand Down
60 changes: 28 additions & 32 deletions ClientServices/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class MapManager : IMapManager
private bool _loaded;
private int _mapHeight; // Number of tiles up the map
private int _mapWidth; // Number of tiles across the map
//private Tile[][] _tileArray; // The array holding all the tiles that make up the map
private RectangleTree<Tile> _tileArray;

#endregion
Expand Down Expand Up @@ -100,6 +99,12 @@ private Rectangle TilePos(Tile T)
return new Rectangle((int)(T.Position.X), (int)(T.Position.Y), (int)(TileSpacing), (int)(TileSpacing));
}

public ITile[] GetITilesIn(RectangleF area)
{
return _tileArray.GetItems(new Rectangle((int)area.X, (int)area.Y, (int)area.Width, (int)area.Height));
}


private ITile GetITileAt(Point p)
{
return (Tile)_tileArray.GetItems(p).FirstOrDefault();
Expand All @@ -115,6 +120,11 @@ private Tile GetTileAt(Point p)
return (Tile)_tileArray.GetItems(p).FirstOrDefault();
}

private Tile GetTileAt(float x, float y)
{
return (Tile)_tileArray.GetItems(new Point((int)x, (int)y)).FirstOrDefault();
}

private Tile GetTileAt(int X, int Y)
{
return GetTileAt(new Point(X, Y));
Expand All @@ -126,32 +136,19 @@ public bool LoadTileMap(NetIncomingMessage message)
var _mapLoadWidth = message.ReadInt32();
var _mapLoadHeight = message.ReadInt32();

//_tileArray = new Tile[_mapHeight][];

_tileArray = new RectangleTree<Tile>(TilePos,
new Rectangle(-(_mapWidth/2)*TileSpacing, -(_mapHeight/2)*TileSpacing,
_mapWidth*TileSpacing, _mapHeight*TileSpacing));

/*for (int i = 0; i < _mapHeight; i++)
while (message.PositionInBytes < message.LengthBytes)
{
_tileArray[i] = new Tile[_mapWidth];
}*/

for (int x = 0; x < _mapLoadWidth; x++)
{
for (int y = 0; y < _mapLoadHeight; y++)
{
int posX = x*TileSpacing;
int posY = y*TileSpacing;

byte index = message.ReadByte();
if (index == 255) // No tile here
continue;
var state = (TileState) message.ReadByte();
float posX = message.ReadFloat();
float posY = message.ReadFloat();
byte index = message.ReadByte();
var state = (TileState)message.ReadByte();

Tile created = GenerateNewTile(GetTileString(index), state, new Vector2D(posX, posY));
_tileArray.Add(created);
}
Tile newTile = GenerateNewTile(GetTileString(index), state, new Vector2D(posX, posY));
_tileArray.Add(newTile);
}

for (int x = 0; x < _mapLoadWidth; x++)
Expand All @@ -163,7 +160,7 @@ public bool LoadTileMap(NetIncomingMessage message)
continue;
if (T.ConnectSprite) //Was wall check.
{
byte i = SetSprite(x, y);
byte i = SetSprite(T.Position);
}
if (y > 0)
{
Expand Down Expand Up @@ -455,23 +452,23 @@ public ITile GetITileAt(Vector2D worldPos)
// 8 = West
// So if we have one N and S, we return (N + S) or (1 + 4), so 5.

public byte SetSprite(int x, int y)
public byte SetSprite(Vector2D position)
{
byte i = 0;

if (GetTileAt(x * TileSpacing, (y - 1) * TileSpacing) != null && GetTileAt(x * TileSpacing, (y - 1) * TileSpacing).ConnectSprite) // N
if (GetTileAt(position.X, position.Y - TileSpacing) != null && GetTileAt(position.X, position.Y - TileSpacing).ConnectSprite) // N
{
i += 1;
}
if (GetTileAt((x + 1) * TileSpacing, y * TileSpacing) != null && GetTileAt((x + 1) * TileSpacing, y * TileSpacing).ConnectSprite) // E
if (GetTileAt(position.X + TileSpacing, position.Y) != null && GetTileAt(position.X + TileSpacing, position.Y).ConnectSprite) // E
{
i += 2;
}
if (GetTileAt(x * TileSpacing, (y + 1) * TileSpacing) != null && GetTileAt(x * TileSpacing, (y + 1) * TileSpacing).ConnectSprite) // S
if (GetTileAt(position.X, position.Y + TileSpacing) != null && GetTileAt(position.X, position.Y + TileSpacing).ConnectSprite) // S
{
i += 4;
}
if (GetTileAt((x - 1) * TileSpacing, y * TileSpacing) != null && GetTileAt((x - 1) * TileSpacing, y * TileSpacing).ConnectSprite) // W
if (GetTileAt(position.X - TileSpacing, position.Y) != null && GetTileAt(position.X - TileSpacing, position.Y).ConnectSprite) // W
{
i += 8;
}
Expand Down Expand Up @@ -516,13 +513,12 @@ public Type GetTileTypeFromArrayPosition(int x, int y)

public Tile GenerateNewTile(string typeName, TileState state, Vector2D pos)
{
var p = new Point((int)pos.X / TileSpacing, (int)pos.Y / TileSpacing);

Type tileType = Type.GetType("ClientServices.Tiles." + typeName, false);

if (tileType == null) throw new ArgumentException("Invalid Tile Type specified : '" + typeName + "' .");

var created = (Tile) Activator.CreateInstance(tileType, state, pos, p);
var created = (Tile) Activator.CreateInstance(tileType, state, pos);

if (tileType.GetInterface("ICollidable") != null)
_collisionManager.AddCollidable((ICollidable) created);
Expand Down Expand Up @@ -559,13 +555,13 @@ public void Shutdown()
private void TileChanged(Tile t)
{
if (OnTileChanged != null)
OnTileChanged(t.TilePosition, t.Position);
OnTileChanged(t.Position);

t.surroundDirs = SetSprite(t.TilePosition.X, t.TilePosition.Y);
t.surroundDirs = SetSprite(t.Position);
foreach (Tile T in t.surroundingTiles)
{
if (T == null) continue;
T.surroundDirs = SetSprite(T.TilePosition.X, T.TilePosition.Y);
T.surroundDirs = SetSprite(T.Position);
}
}

Expand Down
4 changes: 2 additions & 2 deletions ClientServices/Map/Tiles/Floor/Floor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace ClientServices.Tiles
{
public class Floor : Tile
{
public Floor(TileState state, Vector2D position, Point tilePosition)
: base(state, position, tilePosition)
public Floor(TileState state, Vector2D position)
: base(state, position)
{
ConnectSprite = false;
name = "Floor";
Expand Down
4 changes: 2 additions & 2 deletions ClientServices/Map/Tiles/Floor/Space.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace ClientServices.Tiles
{
public class Space : Tile
{
public Space(TileState state, Vector2D position, Point tilePosition)
: base(state, position, tilePosition)
public Space(TileState state, Vector2D position)
: base(state, position)
{
ConnectSprite = false;
name = "Space";
Expand Down
17 changes: 7 additions & 10 deletions ClientServices/Map/Tiles/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ public abstract class Tile : ITile
public Tile[] surroundingTiles;
public TileState tileState = TileState.Healthy;

protected Tile(TileState state, Vector2D position, Point tilePosition)
protected Tile(TileState state, Vector2D position)
{
_resourceManager = IoCManager.Resolve<IResourceManager>();
_lightManager = IoCManager.Resolve<ILightManager>();

tileState = state;

Position = position;
TilePosition = tilePosition;

Sprite = _resourceManager.GetSprite("space_texture");
Sprite.SetPosition(position.X, position.Y);
Expand All @@ -46,7 +45,6 @@ protected Tile(TileState state, Vector2D position, Point tilePosition)
#region ITile Members

public Vector2D Position { get; protected set; }
public Point TilePosition { get; protected set; }
public bool Visible { get; set; }

public bool Opaque { get; set; } //Does this block LOS etc?
Expand All @@ -56,8 +54,8 @@ protected Tile(TileState state, Vector2D position, Point tilePosition)
public virtual void Render(float xTopLeft, float yTopLeft, int tileSpacing, Batch batch)
{
Sprite.Color = Color.White;
Sprite.SetPosition((float) TilePosition.X*tileSpacing - xTopLeft,
(float) TilePosition.Y*tileSpacing - yTopLeft);
Sprite.SetPosition((float)Position.X - xTopLeft,
(float)Position.Y - yTopLeft);
batch.AddClone(Sprite);
}

Expand Down Expand Up @@ -91,8 +89,8 @@ public virtual void RenderGas(float xTopLeft, float yTopLeft, int tileSpacing, B
continue;
if (!spritepositionset)
{
gasSprite.SetPosition(TilePosition.X*tileSpacing - xTopLeft,
TilePosition.Y*tileSpacing - yTopLeft);
gasSprite.SetPosition(Position.X - xTopLeft,
Position.Y - yTopLeft);
spritepositionset = true;
}

Expand All @@ -109,7 +107,6 @@ public virtual void RenderGas(float xTopLeft, float yTopLeft, int tileSpacing, B
break;
}
gasBatch.AddClone(gasSprite);
//gasSprite.Draw();//UGH THIS IS SLOW AS FUCK
}
}
}
Expand Down Expand Up @@ -199,8 +196,8 @@ public TileDecal(Sprite _sprite, Vector2D _position, Tile _tile, Color color)
public void Draw(float xTopLeft, float yTopLeft, int tileSpacing, Batch decalBatch)
{
//Need to find a way to light it.
sprite.SetPosition(tile.TilePosition.X*tileSpacing - xTopLeft + position.X,
tile.TilePosition.Y*tileSpacing - yTopLeft + position.Y);
sprite.SetPosition(tile.Position.X - xTopLeft + position.X,
tile.Position.Y - yTopLeft + position.Y);
decalBatch.AddClone(sprite);
}
}
Expand Down
18 changes: 9 additions & 9 deletions ClientServices/Map/Tiles/Wall/Wall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class Wall : Tile, ICollidable
private readonly Sprite wallCorner1;
private readonly Sprite wallCorner2;

public Wall(TileState state, Vector2D position, Point tilePosition)
: base(state, position, tilePosition)
public Wall(TileState state, Vector2D position)
: base(state, position)
{
ConnectSprite = true;
Opaque = true;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void Bump(Entity collider)

public override void Render(float xTopLeft, float yTopLeft, int tileSpacing, Batch batch)
{
surroundDirs = mapMgr.SetSprite(TilePosition.X, TilePosition.Y); //Optimize.
surroundDirs = mapMgr.SetSprite(Position); //Optimize.

if (surroundDirs == 3 ||
surroundDirs == 2 &&
Expand All @@ -72,8 +72,8 @@ public override void Render(float xTopLeft, float yTopLeft, int tileSpacing, Bat
sideSprite = plainWall;
if (((surroundDirs & 4) == 0))
{
sideSprite.SetPosition((float) TilePosition.X*tileSpacing - xTopLeft,
(float) TilePosition.Y*tileSpacing - yTopLeft);
sideSprite.SetPosition((float) Position.X - xTopLeft,
(float) Position.Y - yTopLeft);
sideSprite.Color = Color.White;
batch.AddClone(sideSprite);
}
Expand Down Expand Up @@ -153,8 +153,8 @@ private void RenderOccluder(Direction d, Direction from, float x, float y, int t
public override void RenderPos(float x, float y, int tileSpacing, int lightSize)
{
//Not drawing occlusion for tiles on the edge. Fuck this. Looks better too since there isnt actually anything to hide behind them.
if ((TilePosition.X == (mapMgr.GetMapWidth() - 1) || TilePosition.X == 0) ||
(TilePosition.Y == (mapMgr.GetMapHeight() - 1) || TilePosition.Y == 0))
if ((Position.X == ((mapMgr.GetMapWidth() - 1) * mapMgr.GetTileSpacing()) || Position.X == 0) ||
(Position.Y == ((mapMgr.GetMapHeight() - 1) * mapMgr.GetTileSpacing()) || Position.Y == 0))
return;

int l = lightSize/2;
Expand Down Expand Up @@ -336,10 +336,10 @@ public override void DrawDecals(float xTopLeft, float yTopLeft, int tileSpacing,
public override void RenderTop(float xTopLeft, float yTopLeft, int tileSpacing, Batch wallTopsBatch)
{
Sprite =
_resourceManager.GetSprite("wall_texture" + mapMgr.SetSprite(TilePosition.X, TilePosition.Y).ToString());
_resourceManager.GetSprite("wall_texture" + mapMgr.SetSprite(Position).ToString());
//Optimize

Sprite.SetPosition(TilePosition.X*tileSpacing - xTopLeft, TilePosition.Y*tileSpacing - yTopLeft);
Sprite.SetPosition(Position.X - xTopLeft, Position.Y - yTopLeft);
Sprite.Position -= new Vector2D(0, tileSpacing);
Sprite.Color = Color.FromArgb(200, Color.White);

Expand Down
14 changes: 7 additions & 7 deletions ClientServices/Placement/Modes/AlignWallTops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ public override bool Update(Vector2D mouseS, IMapManager currentMap)
if (currentTile == null || !currentTile.IsSolidTile())
return false;

ITile wallTop = currentMap.GetITileAt(currentTile.TilePosition.X, currentTile.TilePosition.Y - 1);
ITile wallTop = currentMap.GetITileAt(new Vector2D(currentTile.Position.X, currentTile.Position.Y - currentMap.GetTileSpacing()));
if (wallTop == null)
return false;

ITile wallTopNorth = currentMap.GetITileAt(wallTop.TilePosition.X, wallTop.TilePosition.Y - 1);
ITile wallCurrentSouth = currentMap.GetITileAt(currentTile.TilePosition.X, currentTile.TilePosition.Y + 1);
ITile wallTopNorth = currentMap.GetITileAt(new Vector2D(wallTop.Position.X, wallTop.Position.Y - currentMap.GetTileSpacing()));
ITile wallCurrentSouth = currentMap.GetITileAt(new Vector2D(currentTile.Position.X, currentTile.Position.Y - currentMap.GetTileSpacing()));

ITile wallTopEast = currentMap.GetITileAt(wallTop.TilePosition.X + 1, wallTop.TilePosition.Y);
ITile wallCurrentEast = currentMap.GetITileAt(currentTile.TilePosition.X + 1, currentTile.TilePosition.Y);
ITile wallTopEast = currentMap.GetITileAt(new Vector2D(wallTop.Position.X + currentMap.GetTileSpacing(), wallTop.Position.Y));
ITile wallCurrentEast = currentMap.GetITileAt(new Vector2D(currentTile.Position.X + currentMap.GetTileSpacing(), currentTile.Position.Y));

ITile wallTopWest = currentMap.GetITileAt(wallTop.TilePosition.X - 1, wallTop.TilePosition.Y);
ITile wallCurrentWest = currentMap.GetITileAt(currentTile.TilePosition.X - 1, currentTile.TilePosition.Y);
ITile wallTopWest = currentMap.GetITileAt(new Vector2D(wallTop.Position.X - currentMap.GetTileSpacing(), wallTop.Position.Y));
ITile wallCurrentWest = currentMap.GetITileAt(new Vector2D(currentTile.Position.X - currentMap.GetTileSpacing(), currentTile.Position.Y));

switch (pManager.Direction)
{
Expand Down
4 changes: 2 additions & 2 deletions ClientServices/Placement/PlacementManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ private void RequestPlacement()

message.Write((byte) Direction);

message.Write(CurrentMode.currentTile.TilePosition.X);
message.Write(CurrentMode.currentTile.TilePosition.Y);
message.Write(CurrentMode.currentTile.Position.X);
message.Write(CurrentMode.currentTile.Position.Y);

NetworkManager.SendMessage(message, NetDeliveryMethod.ReliableUnordered);
}
Expand Down
Loading

0 comments on commit e532c7d

Please sign in to comment.