2019-12-07 15:17:40 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-09-18 14:09:15 +02:00
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using MonoGame.Extended;
|
|
|
|
using MonoGame.Extended.Tiled;
|
|
|
|
|
|
|
|
namespace MLEM.Extended.Tiled {
|
|
|
|
public static class TiledExtensions {
|
|
|
|
|
2019-12-07 15:17:40 +01:00
|
|
|
private static readonly Dictionary<int, TiledMapTilesetTile> StubTilesetTiles = new Dictionary<int, TiledMapTilesetTile>();
|
|
|
|
|
2019-09-18 14:09:15 +02:00
|
|
|
public static string Get(this TiledMapProperties properties, string key) {
|
|
|
|
properties.TryGetValue(key, out var val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool GetBool(this TiledMapProperties properties, string key) {
|
|
|
|
bool.TryParse(properties.Get(key), out var val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Color GetColor(this TiledMapProperties properties, string key) {
|
|
|
|
return ColorHelper.FromHex(properties.Get(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static float GetFloat(this TiledMapProperties properties, string key) {
|
|
|
|
float.TryParse(properties.Get(key), out var val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int GetInt(this TiledMapProperties properties, string key) {
|
|
|
|
int.TryParse(properties.Get(key), out var val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TiledMapTileset GetTileset(this TiledMapTile tile, TiledMap map) {
|
|
|
|
return map.GetTilesetByTileGlobalIdentifier(tile.GlobalIdentifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int GetLocalIdentifier(this TiledMapTile tile, TiledMapTileset tileset, TiledMap map) {
|
|
|
|
return tile.GlobalIdentifier - map.GetTilesetFirstGlobalIdentifier(tileset);
|
|
|
|
}
|
|
|
|
|
2019-12-07 15:17:40 +01:00
|
|
|
public static TiledMapTilesetTile GetTilesetTile(this TiledMapTileset tileset, TiledMapTile tile, TiledMap map, bool createStub = true) {
|
2019-09-24 12:21:12 +02:00
|
|
|
if (tile.IsBlank)
|
|
|
|
return null;
|
2019-09-18 14:09:15 +02:00
|
|
|
var localId = tile.GetLocalIdentifier(tileset, map);
|
2019-12-07 15:17:40 +01:00
|
|
|
var tilesetTile = tileset.Tiles.FirstOrDefault(t => t.LocalTileIdentifier == localId);
|
|
|
|
if (tilesetTile == null && createStub) {
|
|
|
|
var id = tile.GetLocalIdentifier(tileset, map);
|
|
|
|
if (!StubTilesetTiles.TryGetValue(id, out tilesetTile)) {
|
|
|
|
tilesetTile = new TiledMapTilesetTile(id);
|
|
|
|
StubTilesetTiles.Add(id, tilesetTile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tilesetTile;
|
2019-09-18 14:09:15 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 15:17:40 +01:00
|
|
|
public static TiledMapTilesetTile GetTilesetTile(this TiledMapTile tile, TiledMap map, bool createStub = true) {
|
2019-09-24 12:21:12 +02:00
|
|
|
if (tile.IsBlank)
|
|
|
|
return null;
|
2019-09-18 14:09:15 +02:00
|
|
|
var tileset = tile.GetTileset(map);
|
2019-12-07 15:17:40 +01:00
|
|
|
return tileset.GetTilesetTile(tile, map, createStub);
|
2019-09-18 14:09:15 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 12:56:01 +01:00
|
|
|
public static int GetTileLayerIndex(this TiledMap map, string layerName) {
|
|
|
|
var layer = map.GetLayer<TiledMapTileLayer>(layerName);
|
|
|
|
return map.TileLayers.IndexOf(layer);
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:09:15 +02:00
|
|
|
public static TiledMapTile GetTile(this TiledMap map, string layerName, int x, int y) {
|
|
|
|
var layer = map.GetLayer<TiledMapTileLayer>(layerName);
|
2019-09-24 12:26:38 +02:00
|
|
|
return layer != null ? layer.GetTile(x, y) : default;
|
|
|
|
}
|
|
|
|
|
2019-12-17 21:02:09 +01:00
|
|
|
public static IEnumerable<TiledMapTile> GetTiles(this TiledMap map, int x, int y) {
|
|
|
|
foreach (var layer in map.TileLayers)
|
|
|
|
yield return layer.GetTile(x, y);
|
|
|
|
}
|
|
|
|
|
2019-09-24 12:26:38 +02:00
|
|
|
public static TiledMapTile GetTile(this TiledMapTileLayer layer, int x, int y) {
|
2019-12-06 16:12:57 +01:00
|
|
|
return !layer.IsInBounds(x, y) ? default : layer.GetTile((ushort) x, (ushort) y);
|
2019-09-18 14:09:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:37:20 +02:00
|
|
|
public static RectangleF GetArea(this TiledMapObject obj, TiledMap map, Vector2? position = null) {
|
2019-09-19 20:23:18 +02:00
|
|
|
var tileSize = map.GetTileSize();
|
2019-09-25 20:37:20 +02:00
|
|
|
var pos = position ?? Vector2.Zero;
|
2019-12-01 22:55:17 +01:00
|
|
|
return new RectangleF(obj.Position / tileSize + pos, obj.Size / tileSize);
|
2019-09-18 14:09:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 20:23:18 +02:00
|
|
|
public static Vector2 GetTileSize(this TiledMap map) {
|
|
|
|
return new Vector2(map.TileWidth, map.TileHeight);
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:12:57 +01:00
|
|
|
public static bool IsInBounds(this TiledMapTileLayer layer, int x, int y) {
|
|
|
|
return x >= 0 && y >= 0 && x < layer.Width && y < layer.Height;
|
|
|
|
}
|
|
|
|
|
2019-12-19 00:27:56 +01:00
|
|
|
public static IEnumerable<TiledMapObject> GetObjects(this TiledMapObjectLayer layer, string id, bool searchName = true, bool searchType = false) {
|
|
|
|
foreach (var obj in layer.Objects) {
|
|
|
|
if (searchName && obj.Name == id || searchType && obj.Type == id)
|
|
|
|
yield return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IEnumerable<TiledMapObject> GetObjects(this TiledMap map, string name, bool searchName = true, bool searchType = false) {
|
|
|
|
foreach (var layer in map.ObjectLayers) {
|
|
|
|
foreach (var obj in layer.GetObjects(name, searchName, searchType))
|
|
|
|
yield return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 14:09:15 +02:00
|
|
|
}
|
|
|
|
}
|