1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 03:23:37 +02:00

added flipping to tile collisions, as well as an option to make custom collisions

This commit is contained in:
Ellpeck 2020-01-01 12:41:48 +01:00
parent 45c725f5ef
commit 56fbc77df7
3 changed files with 51 additions and 9 deletions

View file

@ -12,14 +12,27 @@ namespace MLEM.Extended.Tiled {
private TiledMap map;
private TileCollisionInfo[,,] collisionInfos;
private CollectCollisions collisionFunction;
public TiledMapCollisions(TiledMap map = null) {
if (map != null)
this.SetMap(map);
}
public void SetMap(TiledMap map) {
public void SetMap(TiledMap map, CollectCollisions collisionFunction = null) {
this.map = map;
this.collisionFunction = collisionFunction ?? ((collisions, tile) => {
foreach (var obj in tile.TilesetTile.Objects) {
var area = obj.GetArea(tile.Map);
if (tile.Tile.IsFlippedHorizontally)
area.X = 1 - area.X - area.Width;
if (tile.Tile.IsFlippedVertically)
area.Y = 1 - area.Y - area.Height;
area.Offset(tile.Position);
collisions.Add(area);
}
});
this.collisionInfos = new TileCollisionInfo[map.Layers.Count, map.Width, map.Height];
for (var i = 0; i < map.TileLayers.Count; i++) {
for (var x = 0; x < map.Width; x++) {
@ -38,7 +51,7 @@ namespace MLEM.Extended.Tiled {
return;
}
var tilesetTile = tile.GetTilesetTile(this.map);
this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, layer, tilesetTile);
this.collisionInfos[layerIndex, x, y] = new TileCollisionInfo(this.map, new Vector2(x, y), tile, layer, tilesetTile, this.collisionFunction);
}
public IEnumerable<TileCollisionInfo> GetCollidingTiles(RectangleF area, Func<TileCollisionInfo, bool> included = null) {
@ -64,22 +77,26 @@ namespace MLEM.Extended.Tiled {
return this.GetCollidingTiles(area, included).Any();
}
public delegate void CollectCollisions(List<RectangleF> collisions, TileCollisionInfo tile);
public class TileCollisionInfo {
public readonly TiledMap Map;
public readonly Vector2 Position;
public readonly TiledMapTile Tile;
public readonly TiledMapTileLayer Layer;
public readonly TiledMapTilesetTile TilesetTile;
public readonly ReadOnlyCollection<RectangleF> Collisions;
public readonly List<RectangleF> Collisions;
public TileCollisionInfo(TiledMap map, Vector2 position, TiledMapTile tile, TiledMapTileLayer layer, TiledMapTilesetTile tilesetTile) {
public TileCollisionInfo(TiledMap map, Vector2 position, TiledMapTile tile, TiledMapTileLayer layer, TiledMapTilesetTile tilesetTile, CollectCollisions collisionFunction) {
this.TilesetTile = tilesetTile;
this.Layer = layer;
this.Tile = tile;
this.Map = map;
this.Position = position;
var collisions = new List<RectangleF>();
foreach (var obj in tilesetTile.Objects)
collisions.Add(obj.GetArea(map, position));
this.Collisions = collisions.AsReadOnly();
this.Collisions = new List<RectangleF>();
collisionFunction(this.Collisions, this);
}
}

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.2.4" name="TownTiles" tilewidth="16" tileheight="16" tilecount="512" columns="32">
<tileset version="1.2" tiledversion="1.3.1" name="TownTiles" tilewidth="16" tileheight="16" tilecount="512" columns="32">
<image source="Tiles.png" width="512" height="256"/>
<tile id="3">
<properties>
@ -16,6 +16,16 @@
<property name="Walkability" type="int" value="30"/>
</properties>
</tile>
<tile id="7">
<objectgroup draworder="index" id="5">
<object id="4" x="8" y="3" width="5" height="10"/>
</objectgroup>
</tile>
<tile id="8">
<objectgroup draworder="index" id="2">
<object id="1" x="3" y="2" width="10" height="5"/>
</objectgroup>
</tile>
<tile id="15">
<animation>
<frame tileid="15" duration="1000"/>
@ -72,6 +82,11 @@
<property name="Walkability" type="int" value="30"/>
</properties>
</tile>
<tile id="72">
<objectgroup draworder="index" id="2">
<object id="1" x="12" y="0" width="4" height="4"/>
</objectgroup>
</tile>
<tile id="132">
<properties>
<property name="Walkability" type="int" value="60"/>

View file

@ -12,6 +12,7 @@ using MLEM.Textures;
using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
using MonoGame.Extended;
using MonoGame.Extended.Tiled;
namespace Sandbox {
@ -20,6 +21,7 @@ namespace Sandbox {
private Camera camera;
private TiledMap map;
private IndividualTiledMapRenderer mapRenderer;
private TiledMapCollisions collisions;
public GameImpl() {
this.IsMouseVisible = true;
@ -33,6 +35,7 @@ namespace Sandbox {
this.map = LoadContent<TiledMap>("Tiled/Map");
this.mapRenderer = new IndividualTiledMapRenderer(this.map);
this.collisions = new TiledMapCollisions(this.map);
this.camera = new Camera(this.GraphicsDevice) {
AutoScaleWithScreen = true,
@ -74,6 +77,13 @@ namespace Sandbox {
this.GraphicsDevice.Clear(Color.Black);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, this.camera.ViewMatrix);
this.mapRenderer.Draw(this.SpriteBatch, this.camera.GetVisibleRectangle().ToExtended());
foreach (var tile in this.collisions.GetCollidingTiles(new RectangleF(0, 0, this.map.Width, this.map.Height))) {
foreach (var area in tile.Collisions) {
this.SpriteBatch.DrawRectangle(area.Position * this.map.GetTileSize(), area.Size * this.map.GetTileSize(), Color.Red);
}
}
this.SpriteBatch.End();
base.DoDraw(gameTime);
}