Hello Guest

Author Topic: Tiled Support  (Read 1840 times)

0 Members and 1 Guest are viewing this topic.

Cross

  • Member
  • Posts: 4
    • View Profile
Tiled Support
« on: September 21, 2015, 02:09:54 PM »
Hey,

i would appreciate it if otter2D has TiledMap-Support.

Best regards,
Cross

(The Engine rocks!)

Commanderstitch

  • Member
  • Posts: 37
  • Personal Text
    • View Profile
    • commanderstitch Homepage (coming someday)
Re: Tiled Support
« Reply #1 on: September 22, 2015, 05:18:49 PM »
http://screencast.com/t/Z285dQwZVXm

 tiled sharp works. Floatvoid has a better understanding of it, however. My line only does a single layer. I have the other one on my machine somewhere that does the layers too.
- Commanderstitch

misthema

  • Member
  • Posts: 3
    • View Profile
Re: Tiled Support
« Reply #2 on: December 28, 2016, 06:10:12 AM »
http://screencast.com/t/Z285dQwZVXm

 tiled sharp works. Floatvoid has a better understanding of it, however. My line only does a single layer. I have the other one on my machine somewhere that does the layers too.

How do you set your collisions? I only see setting tile IDs for your Tilemap and I'm so lost with collisions...

EDIT: Here's a quick and nasty (but working) solution for Tiled maps (uses TiledSharp):
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using Otter;
using TiledSharp;

public class TiledMap : Entity
{
class _Tileset
{
public string Name;
public TmxTileset TmxTileset;
public ImageSet TilesetImage;
}

readonly string pathMap = string.Empty;

public TmxMap TmxMap;
public int FullWidth, FullHeight;

GridCollider _gc;
List<_Tileset> _tilesets;

public TiledMap(string path) : base()
{
pathMap = path;
_tilesets = new List<_Tileset>();
Load();
}

public void SetCollisionLayer(string layer, Enum tag, params Enum[] tags)
{
_gc = new GridCollider(FullWidth, FullHeight, TmxMap.TileWidth, TmxMap.TileHeight, tag, tags);
setupGridCollider(layer);
AddCollider(_gc);
}

public void SetCollisionLayer(string layer, params int[] tags)
{
_gc = new GridCollider(FullWidth, FullHeight, TmxMap.TileWidth, TmxMap.TileHeight, tags);
setupGridCollider(layer);
AddCollider(_gc);
}

void setupGridCollider(string layerName)
{
var layer = TmxMap.Layers.FirstOrDefault( x => x.Name.Equals(layerName) );

for( int y = 0; y < TmxMap.Height; y++ )
{
for( int x = 0; x < TmxMap.Width; x++ )
{
int tileID = y * TmxMap.Width + x;
_gc.SetTile(x, y, layer.Tiles[tileID].Gid > 0);
}
}
}

public void Load()
{
TmxMap = new TmxMap(pathMap);

if( TmxMap.Orientation != OrientationType.Orthogonal )
throw new FormatException("Tilemap Orientation invalid! Only [Orthogonal] supported.");

FullWidth = TmxMap.Width * TmxMap.TileWidth;
FullHeight = TmxMap.Height * TmxMap.TileHeight;

// Load tileset images
foreach( var tileset in TmxMap.Tilesets )
{
var imgSet = new ImageSet(
tileset.Image.Source,
tileset.TileWidth,
tileset.TileHeight
);

_tilesets.Add(
new _Tileset{
Name = tileset.Name,
TmxTileset = tileset,
TilesetImage = imgSet
}
);
}
}

public override void Render()
{
foreach( var tmxLayer in TmxMap.Layers )
{
// Render visible layers only!
if( !tmxLayer.Visible )
continue;

// Layer tiles
var tiles = tmxLayer.Tiles;

for( int y = 0; y < TmxMap.Height; y++ )
{
for( int x = 0; x < TmxMap.Width; x++ )
{
var i = y * TmxMap.Width + x;
var tileID = tiles[i].Gid;
var tileset = tilesetForGid(tileID); // get right tileset for this tile!

if( tileset == null )
continue;

var firstGid = tileset.TmxTileset.FirstGid;
var frame = tileID - firstGid;

tileset.TilesetImage.Frame = frame;

var tx = x * TmxMap.TileWidth;
var ty = y * TmxMap.TileHeight;

tileset.TilesetImage.Render(tx, ty);
}
}
}

_gc.Render();
}

_Tileset tilesetForGid(int gid)
{
foreach( var tileset in _tilesets )
{
var first = tileset.TmxTileset.FirstGid;
var last = first + tileset.TmxTileset.TileCount;

if( gid >= first && gid < last )
return tileset;
}

return null;
}
}


USAGE:
Code: [Select]
public class TestScene : Scene
{
public TestScene()
{
var tm = new TiledMap("path/to/your/map.tmx");
tm.SetCollisionLayer("Name of your Collision layer", YourCollisionTagEnumerator.Value);
Add(tm);

this.UseCameraBounds = true;
this.CameraBounds = new Rectangle(0,0, tm.FullWidth, tm.FullHeight);
}
}

Supports multi-tilesets too, but lacks all the other cool stuff you could have. It's very minimalistic.

...But it works:

« Last Edit: December 29, 2016, 02:57:49 AM by misthema »