Skip to content

Instantly share code, notes, and snippets.

@LordNed
Created June 1, 2015 00:52
Show Gist options
  • Save LordNed/a52a726234747bfca9f1 to your computer and use it in GitHub Desktop.
Save LordNed/a52a726234747bfca9f1 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
public enum TileSides
{
None = 0,
BottomLeft,
BottomRight,
Bottom,
TopLeft,
TopRight,
Top,
Center,
VerticalWall,
HorizontalWall,
LeftUShape,
RightUShape,
LeftWall,
RightWall
}
public class TiledMap : MonoBehaviour
{
[System.Serializable]
private class PrefabContainer
{
#pragma warning disable 0649
public int Index;
public GameObject Prefab;
#pragma warning restore 0649
}
[SerializeField] private List<PrefabContainer> m_prefabs;
[SerializeField] private TextAsset m_selectedFile;
public GameObject GetPrefabForIndex(int value)
{
for(int i = 0; i < m_prefabs.Count; i++)
{
if (m_prefabs[i].Index == value)
return m_prefabs[i].Prefab;
}
return null;
}
public string GetJsonData()
{
if (m_selectedFile == null)
return string.Empty;
return m_selectedFile.text;
}
public GameObject GetTerrainPrefabForTile(TileSides tile)
{
Debug.Log("Tile: " + tile);
switch (tile)
{
case TileSides.None:
return null;
case TileSides.BottomLeft:
return GetPrefabForIndex(12);
case TileSides.Bottom:
return GetPrefabForIndex(13);
case TileSides.BottomRight:
return GetPrefabForIndex(14);
case TileSides.TopLeft:
return GetPrefabForIndex(15);
case TileSides.TopRight:
return GetPrefabForIndex(16);
case TileSides.Top:
return GetPrefabForIndex(17);
case TileSides.Center:
return GetPrefabForIndex(18);
case TileSides.VerticalWall:
return GetPrefabForIndex(19);
case TileSides.HorizontalWall:
return GetPrefabForIndex(20);
case TileSides.LeftUShape:
return GetPrefabForIndex(21);
case TileSides.RightUShape:
return GetPrefabForIndex(22);
case TileSides.RightWall:
return GetPrefabForIndex(23);
case TileSides.LeftWall:
return GetPrefabForIndex(24);
default:
break;
}
return null;
}
}
public void ImportTiledFile(TiledMap map, string jsonContents)
{
TiledMapJson tiledMap = JsonConvert.DeserializeObject<TiledMapJson>(jsonContents);
for (int l = 0; l < tiledMap.layers.Length; l++)
{
TiledMapImporter.Layer layer = tiledMap.layers[l];
Int64[] data = layer.data;
Int64[,] data2D = new Int64[layer.width, layer.height];
int layerWidth = layer.width;
int numRows = data.Length / layerWidth;
for(int y = 0; y < numRows; y++)
{
for(int x = 0; x < layer.width; x++)
{
int index = (y * layerWidth) + x;
data2D[x, y] = data[index];
}
}
for (int y = 0; y < numRows; y++)
{
for (int x = 0; x < layerWidth; x++)
{
Int64 fullValue = data2D[x, y];
Int64 trimmed = (int)(fullValue & 0x1FFFFFF);
int value = (int)trimmed;
if (value == 0)
continue;
GameObject prefab = null;
if (value == 1)
{
prefab = map.GetTerrainPrefabForTile(GetSidesForTile(data2D, x, y));
}
else
{
prefab = map.GetPrefabForIndex(value);
}
Debug.Log("value: " + value);
GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
instance.transform.SetParent(map.transform, false);
instance.transform.localPosition = new Vector3(x, -y, 0);
}
}
}
}
public TileSides GetSidesForTile(Int64[,] data, int xPos, int yPos)
{
bool tileLeft, tileRight, tileUp, tileDown;
tileLeft = IsTileInPosition(data, xPos - 1, yPos);
tileRight = IsTileInPosition(data, xPos + 1, yPos);
tileUp = IsTileInPosition(data, xPos, yPos - 1);
tileDown = IsTileInPosition(data, xPos, yPos + 1);
// Center Tile
if (tileLeft && tileRight && tileUp && tileDown)
return TileSides.Center;
// Bottom Left
if (tileUp && tileRight && !tileDown && !tileLeft)
return TileSides.BottomLeft;
// Bottom Right
if (tileUp && tileLeft && !tileDown && !tileRight)
return TileSides.BottomRight;
// Bottom
if (tileUp && tileLeft && tileRight && !tileDown)
return TileSides.Bottom;
// Top left
if (tileDown && tileRight && !tileUp && !tileLeft)
return TileSides.TopLeft;
// Top Right
if (tileDown && tileLeft && !tileUp && !tileRight)
return TileSides.TopRight;
// Top
if (tileDown && tileLeft && tileRight && !tileUp)
return TileSides.Top;
// Vertical Wall
if (tileUp && tileDown && !tileLeft && !tileRight)
return TileSides.VerticalWall;
// Horizonal Wall
if (tileLeft && tileRight && !tileUp && !tileDown)
return TileSides.HorizontalWall;
// Left U Shape
if (tileRight && !tileUp && !tileLeft && !tileDown)
return TileSides.LeftUShape;
// RIght U SHape
if (tileLeft && !tileUp && !tileRight && !tileDown)
return TileSides.RightUShape;
// Right Wall
if (tileLeft && tileUp && tileDown && !tileRight)
return TileSides.RightWall;
// Left Wall
if (tileRight && tileUp && tileDown && !tileLeft)
return TileSides.LeftWall;
return TileSides.Center;
}
private bool IsTileInPosition(Int64[,] data, int xPos, int yPos)
{
if (xPos < 0 || xPos >= data.GetLength(0) || yPos < 0 || yPos >= data.GetLength(1))
return false;
return data[xPos, yPos] == 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment