Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
Updated Tileset and Tile
Browse files Browse the repository at this point in the history
  • Loading branch information
craftersmine committed Mar 31, 2019
1 parent 9458c6c commit c238e7f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 28 deletions.
20 changes: 17 additions & 3 deletions craftersmine.EtherEngine.Core/Objects/Tile.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
namespace craftersmine.EtherEngine.Core
{
/// <summary>
/// Represents a world tile
/// </summary>
public class Tile : GameObject
{
/// <summary>
/// Gets or sets tile position along X axis relative to <see cref="Tileset"/>
/// </summary>
public int TilesetX { get; set; }
/// <summary>
/// Gets or sets tile position along Y axis relative to <see cref="Tileset"/>
/// </summary>
public int TilesetY { get; set; }
/// <summary>
/// Gets or sets internal Tile id. May be useful for specific actions
/// </summary>
public string TileId { get; set; }

public Tile(int tilesetX, int tilesetY)
/// <summary>
/// Creates a new instance of <see cref="Tile"/>
/// </summary>
public Tile()
{
TilesetX = tilesetX;
TilesetY = tilesetY;

}
}
}
86 changes: 61 additions & 25 deletions craftersmine.EtherEngine.Core/Objects/Tileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,100 @@
using System.Text;
using System.Threading.Tasks;
using craftersmine.EtherEngine.Content;
using craftersmine.EtherEngine.Utilities;

namespace craftersmine.EtherEngine.Core
{
/// <summary>
/// Represents a world tileset. This class cannot be inherited
/// </summary>
public sealed class Tileset : GameObject
{
private List<Tile> _tiles = new List<Tile>();
private Dictionary<string, Tile> _tilesPresets = new Dictionary<string, Tile>();

public TextureSet TilesTextures { get; private set; }
/// <summary>
/// Gets current tiles width
/// </summary>
public int TileWidth { get; private set; }
/// <summary>
/// Gets current tiles height
/// </summary>
public int TileHeight { get; private set; }
/// <summary>
/// Gets or sets <see cref="Tile"/> OnUpdate action
/// </summary>
public TileOnUpdateAction TileOnUpdateAction { get; set; }

public Tileset(TextureSet tilesTextures, int tileWidth, int tileHeight)
/// <summary>
/// Creates a new instance of <see cref="Tileset"/> with specific tiles size and global tiles OnUpdate action
/// </summary>
/// <param name="tileWidth">Width of adding tiles</param>
/// <param name="tileHeight">Height of adding tiles</param>
/// <param name="onTileUpdateAction">Tiles global OnUpdate action</param>
public Tileset(int tileWidth, int tileHeight, TileOnUpdateAction onTileUpdateAction)
{
TilesTextures = tilesTextures;
TileWidth = tileWidth;
TileHeight = tileHeight;
TileOnUpdateAction = onTileUpdateAction;
}

/// <summary>
/// Occurs when <see cref="Tileset"/> being updated
/// </summary>
public override void OnUpdate()
{
foreach (Tile tile in _tiles)
for (int i = 0; i < _tiles.Count; i++)
{
TileOnUpdateAction?.Invoke(tile);
_tiles[i].OnUpdate();
TileOnUpdateAction?.Invoke(_tiles[i]);
}
}

/// <summary>
/// Checks for a tile under specific world coordinates and returns <see cref="Tile"/> if it exists or <code>null</code> if it doesn't exist at this coordinates
/// </summary>
/// <param name="x">Coordinate along X axis to check <see cref="Tile"/></param>
/// <param name="y">Coordinate along Y axis to check <see cref="Tile"/></param>
/// <returns></returns>
public Tile CheckTile(int x, int y)
{
throw new NotImplementedException("[WIP] CheckTile is not implemented yet");
}

public void AddTile(string tilePresetId, int x, int y)
{
try
int tileX = x / TileWidth;
int tileY = y / TileHeight;

foreach (var tile in _tiles)
{
Tile clonedTile = _tilesPresets[tilePresetId].Clone<Tile>();
clonedTile.TilesetX = x;
clonedTile.TilesetY = y;
_tiles.Add(clonedTile);
if (tile.X == tileX && tile.Y == tileY)
return tile;
else continue;
}
catch { }
return null;
}

/// <summary>
/// Tries to add preset of tile and returns true if success, otherwise false
/// Adds specific <see cref="Tile"/> at specified location in tileset
/// </summary>
/// <param name="tile"></param>
/// <returns></returns>
public bool AddTilePreset(Tile tile)
/// <param name="tile">Tile to add</param>
/// <param name="x">Coordinate along X axis (all tiles have coordinates like 1,1; 1,2; 3,10 etc.)</param>
/// <param name="y">Coordinate along Y axis (all tiles have coordinates like 1,1; 1,2; 3,10 etc.)</param>
public void AddTile(Tile tile, int x, int y)
{
if (!_tilesPresets.ContainsKey(tile.TileId))
try
{
tile.TilesetX = x;
tile.TilesetY = y;
_tiles.Add(tile);
}
catch (Exception ex)
{
_tilesPresets.Add(tile.TileId, tile);
return true;
Debugging.Log(LogEntryType.Warning, "Unable to add tile " + tile.TileId + " @" + x + "," + y);
Debugging.LogException(LogEntryType.Warning, ex);
}
else return false;
}
}

/// <summary>
/// Represents <see cref="Tile"/> OnUpdate behaviour
/// </summary>
/// <param name="updatingTile"></param>
public delegate void TileOnUpdateAction(Tile updatingTile);
}

0 comments on commit c238e7f

Please sign in to comment.