Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
merge updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rluders committed Apr 17, 2014
1 parent b3d8e0a commit ecced2c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
6 changes: 5 additions & 1 deletion src/UniTiled/Tilemap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void LoadTilesets(XmlNodeList nodes) {

void LoadLayers(XmlNodeList nodes) {

int depth = 0;
foreach (XmlNode layerNode in nodes) {

Layer layer = new Layer(
Expand All @@ -104,7 +105,10 @@ void LoadLayers(XmlNodeList nodes) {
float.Parse(layerNode.Attributes["height"].Value)),
layerNode.SelectNodes("/map/layer[@name='"
+ layerNode.Attributes["name"].Value
+ "']/data/tile"));
+ "']/data/tile"),
depth);

depth--;

Layers.Add(layer);
}
Expand Down
20 changes: 11 additions & 9 deletions src/UniTiled/Tilemap/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ public class Layer {
public string Name { get; private set; }
public Vector2 Size { get; private set; }
public List<Tile> Tiles { get; private set; }
public int Depth { get; set; }

public Layer(string name, Vector2 size, XmlNodeList tileNodes) {
public Layer(string name, Vector2 size, XmlNodeList tileNodes, int depth = 0) {

Name = name;
Size = size;
Depth = depth;

layerObject = new GameObject(name);

LoadTiles(tileNodes);
Expand Down Expand Up @@ -47,7 +50,7 @@ public void AttachTo(GameObject map) {
public void Render() {

Transform go = layerObject.transform;

// @TODO put it into Tile or/and TilemapComponent properties
float x_size = .3f,
y_size = .3f;
Expand All @@ -56,15 +59,12 @@ public void Render() {
row = (int)Size.y;

foreach (Tile tile in Tiles) {

Vector3 position = new Vector3(
go.position.x + (col * x_size),
go.position.y + (row * y_size),
go.position.z);

tile.SetPosition(
position,
go.rotation);
new Vector3(
go.position.x + (col * x_size),
go.position.y + (row * y_size),
0), go.rotation);
tile.AttachTo(layerObject);

col++;
Expand All @@ -76,6 +76,8 @@ public void Render() {

}

go.transform.position = new Vector3(0, 0, Depth);

}

}
Expand Down
51 changes: 39 additions & 12 deletions src/UniTiled/Tilemap/Tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace UniTiled {

// @todo optimize this class
public class Tile {

GameObject tileObject;
Expand All @@ -16,12 +17,19 @@ public Tile(int gid) {
Gid = gid;
tileObject = new GameObject("tile_" + gid);

// GameObject go = GameObject.Find("_Tilemap");
// Tileset tileset = go.GetComponent<TilemapComponent>().tilemap.Tilesets.GetFromGid(gid);
Tileset tileset = Tilemap.Tilesets.GetFromGid(gid);
Debug.Log("Gid: " + gid);
// if it have no texture so we destroy the tile
if (!LoadTexture()) {
// @todo make it better
GameObject.DestroyImmediate(tileObject);
tileObject = null;
}

}

protected bool LoadTexture() {

Tileset tileset = Tilemap.Tilesets.GetFromGid(Gid);

// @TEST
tileObject.AddComponent("SpriteRenderer");
SpriteRenderer sr = (SpriteRenderer)tileObject.GetComponent("SpriteRenderer");

Expand All @@ -32,22 +40,41 @@ public Tile(int gid) {
names[i] = sprites[i].name;
}

if (gid > 0) {
sr.sprite = sprites[Array.IndexOf(names, tileset.Name + "_" + (gid))];
if (Gid > 0) {

// every tileset for unity starts with index zero, so I neet to calculate the tile index based on gid
int first = tileset.FirstGid == 0 ? 1 : tileset.FirstGid,
tile_gid = Math.Abs(first - Gid),
index = Array.IndexOf(names, tileset.Name + "_" + tile_gid);

if (index <= sprites.Length) {

sr.sprite = sprites[index];
return true;

}

}

return false;

}

public void SetPosition(Vector2 position, Quaternion rotation) {
public void SetPosition(Vector3 position, Quaternion rotation) {

tileObject.transform.position = position;
tileObject.transform.rotation = rotation;
if (tileObject) {
tileObject.transform.position = position;
tileObject.transform.rotation = rotation;
}

}

public void AttachTo(GameObject map) {
public void AttachTo(GameObject layer) {

tileObject.transform.parent = map.transform;
// attach only if tile object exists
if (tileObject) {
tileObject.transform.parent = layer.transform;
}

}

Expand Down

0 comments on commit ecced2c

Please sign in to comment.