1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01: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 TiledMap map;
private TileCollisionInfo[,,] collisionInfos; private TileCollisionInfo[,,] collisionInfos;
private CollectCollisions collisionFunction;
public TiledMapCollisions(TiledMap map = null) { public TiledMapCollisions(TiledMap map = null) {
if (map != null) if (map != null)
this.SetMap(map); this.SetMap(map);
} }
public void SetMap(TiledMap map) { public void SetMap(TiledMap map, CollectCollisions collisionFunction = null) {
this.map = map; 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]; this.collisionInfos = new TileCollisionInfo[map.Layers.Count, map.Width, map.Height];
for (var i = 0; i < map.TileLayers.Count; i++) { for (var i = 0; i < map.TileLayers.Count; i++) {
for (var x = 0; x < map.Width; x++) { for (var x = 0; x < map.Width; x++) {
@ -38,7 +51,7 @@ namespace MLEM.Extended.Tiled {
return; return;
} }
var tilesetTile = tile.GetTilesetTile(this.map); 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) { 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(); return this.GetCollidingTiles(area, included).Any();
} }
public delegate void CollectCollisions(List<RectangleF> collisions, TileCollisionInfo tile);
public class TileCollisionInfo { public class TileCollisionInfo {
public readonly TiledMap Map;
public readonly Vector2 Position;
public readonly TiledMapTile Tile; public readonly TiledMapTile Tile;
public readonly TiledMapTileLayer Layer; public readonly TiledMapTileLayer Layer;
public readonly TiledMapTilesetTile TilesetTile; 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.TilesetTile = tilesetTile;
this.Layer = layer; this.Layer = layer;
this.Tile = tile; this.Tile = tile;
this.Map = map;
this.Position = position;
var collisions = new List<RectangleF>(); this.Collisions = new List<RectangleF>();
foreach (var obj in tilesetTile.Objects) collisionFunction(this.Collisions, this);
collisions.Add(obj.GetArea(map, position));
this.Collisions = collisions.AsReadOnly();
} }
} }

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?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"/> <image source="Tiles.png" width="512" height="256"/>
<tile id="3"> <tile id="3">
<properties> <properties>
@ -16,6 +16,16 @@
<property name="Walkability" type="int" value="30"/> <property name="Walkability" type="int" value="30"/>
</properties> </properties>
</tile> </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"> <tile id="15">
<animation> <animation>
<frame tileid="15" duration="1000"/> <frame tileid="15" duration="1000"/>
@ -72,6 +82,11 @@
<property name="Walkability" type="int" value="30"/> <property name="Walkability" type="int" value="30"/>
</properties> </properties>
</tile> </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"> <tile id="132">
<properties> <properties>
<property name="Walkability" type="int" value="60"/> <property name="Walkability" type="int" value="60"/>

View file

@ -12,6 +12,7 @@ using MLEM.Textures;
using MLEM.Ui; using MLEM.Ui;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
using MLEM.Ui.Style; using MLEM.Ui.Style;
using MonoGame.Extended;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
namespace Sandbox { namespace Sandbox {
@ -20,6 +21,7 @@ namespace Sandbox {
private Camera camera; private Camera camera;
private TiledMap map; private TiledMap map;
private IndividualTiledMapRenderer mapRenderer; private IndividualTiledMapRenderer mapRenderer;
private TiledMapCollisions collisions;
public GameImpl() { public GameImpl() {
this.IsMouseVisible = true; this.IsMouseVisible = true;
@ -33,6 +35,7 @@ namespace Sandbox {
this.map = LoadContent<TiledMap>("Tiled/Map"); this.map = LoadContent<TiledMap>("Tiled/Map");
this.mapRenderer = new IndividualTiledMapRenderer(this.map); this.mapRenderer = new IndividualTiledMapRenderer(this.map);
this.collisions = new TiledMapCollisions(this.map);
this.camera = new Camera(this.GraphicsDevice) { this.camera = new Camera(this.GraphicsDevice) {
AutoScaleWithScreen = true, AutoScaleWithScreen = true,
@ -74,6 +77,13 @@ namespace Sandbox {
this.GraphicsDevice.Clear(Color.Black); this.GraphicsDevice.Clear(Color.Black);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, this.camera.ViewMatrix); this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, this.camera.ViewMatrix);
this.mapRenderer.Draw(this.SpriteBatch, this.camera.GetVisibleRectangle().ToExtended()); 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(); this.SpriteBatch.End();
base.DoDraw(gameTime); base.DoDraw(gameTime);
} }