Created
November 25, 2015 03:41
-
-
Save poemdexter/e1fdd6247acbaa3a80d3 to your computer and use it in GitHub Desktop.
method for reading in the json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Room LoadRoom(int roomNumber) | |
{ | |
Room room = new Room(); | |
TextAsset roomAsset = levelsData[roomNumber]; | |
Dictionary<string, object> hash = roomAsset.text.dictionaryFromJson(); | |
int roomWidth = int.Parse(hash["width"].ToString()); | |
int roomHeight = int.Parse(hash["height"].ToString()); | |
// tile properties for cells | |
List<object> tilesetsList = (List<object>)hash["tilesets"]; | |
Dictionary<string, object> tilesetHash = (Dictionary<string, object>)tilesetsList[0]; | |
Dictionary<string, object> tilePropsHash = (Dictionary<string, object>)tilesetHash["tileproperties"]; | |
Dictionary<int, Cell> cellTypes = new Dictionary<int, Cell>(); | |
foreach(var cellType in tilePropsHash) | |
{ | |
int key = int.Parse(cellType.Key) + 1; // tileproperties are "data" values - 1 for some reason | |
Dictionary<string, object> cellProps = (Dictionary<string, object>)cellType.Value; | |
cellTypes.Add(key, CreateCellFromProps(cellProps)); | |
} | |
// layers | |
List<object> layersList = (List<object>)hash["layers"]; | |
foreach (Dictionary<string, object> layerHash in layersList) | |
{ | |
if (layerHash["name"].Equals("TilesBottom")) // load all the room data for barriers and such | |
{ | |
List<object> objectList = (List<object>)layerHash["data"]; | |
int[,] dataCells = new int[roomWidth, roomHeight]; | |
// grab the tile id from "data" | |
for (int y = roomHeight - 1; y >= 0; y--) | |
{ | |
for (int x = 0; x < roomWidth; x++) | |
{ | |
dataCells[x, y] = int.Parse(objectList[(roomHeight - y - 1) * roomWidth + x].ToString()); | |
} | |
} | |
// fill roomCells with Cells based of dataCells + cellTypes | |
Cell[,] roomCells = new Cell[roomWidth, roomHeight]; | |
for (int y = 0; y < roomHeight; y++) | |
{ | |
for (int x = 0; x < roomWidth; x++) | |
{ | |
if (dataCells[x, y] != 0 && cellTypes.ContainsKey(dataCells[x, y])) | |
{ | |
roomCells[x, y] = cellTypes[dataCells[x, y]]; | |
} | |
else | |
{ | |
roomCells[x, y] = new Cell(); | |
} | |
} | |
} | |
room.SetRoomCells(roomCells); | |
} | |
if (layerHash["name"].Equals("TilesTop")) // load all the room data for barriers and such | |
{ | |
List<object> objectList = (List<object>)layerHash["data"]; | |
int[,] dataCells = new int[roomWidth, roomHeight]; | |
// grab the tile id from "data" | |
for (int y = roomHeight - 1; y >= 0; y--) | |
{ | |
for (int x = 0; x < roomWidth; x++) | |
{ | |
dataCells[x, y] = int.Parse(objectList[(roomHeight - y - 1) * roomWidth + x].ToString()); | |
} | |
} | |
// fill roomCells with Cells based of dataCells + cellTypes | |
Cell[,] roomCells = room.GetRoomCells(); | |
for (int y = 0; y < roomHeight; y++) | |
{ | |
for (int x = 0; x < roomWidth; x++) | |
{ | |
if (dataCells[x, y] != 0 && cellTypes.ContainsKey(dataCells[x, y])) | |
{ | |
roomCells[x, y] = cellTypes[dataCells[x, y]]; | |
} | |
} | |
} | |
} | |
else if (layerHash["name"].Equals("EnemySpawn")) // load enemy spawn positions | |
{ | |
List<object> objectList = (List<object>)layerHash["objects"]; | |
foreach (Dictionary<string, object> spawn in objectList) | |
{ | |
int x = int.Parse(spawn["x"].ToString()) / 32; | |
int y = roomHeight - (int.Parse(spawn["y"].ToString()) / 32) - 1; | |
room.AddEnemySpawn(new Vector2(x, y)); | |
} | |
} | |
} | |
// spawn initial set of enemies for each room | |
List<Vector2> spawns = room.GetEnemySpawns(); | |
foreach (Vector2 spawn in spawns) | |
{ | |
GameObject enemy = Instantiate(enemyPrefab, spawn, Quaternion.identity) as GameObject; | |
enemy.GetComponent<EnemyAttack>().Spawn = spawn; | |
room.AddEnemy(enemy.GetComponent<EnemyAttack>(), spawn); | |
room.EnemyAmount++; | |
} | |
return room; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ "height":12, | |
"layers":[ | |
{ | |
"data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 10, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 8, 51, 16, 17, 8, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 10, 15, 16, 1, 2, 33, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 32, 2, 3, 8, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 9, 9, 10, 8, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 9, 9, 9, 5, 8, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 9, 9, 39, 12, 8, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 9, 9, 9, 19, 8, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 9, 9, 10, 15, 16, 26, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 25, 16, 17, 2, 3, 8, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 10, 1, 2, 51, 10, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 8, 51], | |
"height":12, | |
"name":"TilesBottom", | |
"opacity":1, | |
"type":"tilelayer", | |
"visible":true, | |
"width":16, | |
"x":0, | |
"y":0 | |
}, | |
{ | |
"data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | |
"height":12, | |
"name":"TilesTop", | |
"opacity":1, | |
"type":"tilelayer", | |
"visible":true, | |
"width":16, | |
"x":0, | |
"y":0 | |
}, | |
{ | |
"color":"#ff0000", | |
"draworder":"topdown", | |
"height":12, | |
"name":"EnemySpawn", | |
"objects":[ | |
{ | |
"height":32, | |
"id":10, | |
"name":"enemy", | |
"properties": | |
{ | |
}, | |
"rotation":0, | |
"type":"", | |
"visible":true, | |
"width":32, | |
"x":352, | |
"y":96 | |
}, | |
{ | |
"height":32, | |
"id":12, | |
"name":"enemy", | |
"properties": | |
{ | |
}, | |
"rotation":0, | |
"type":"", | |
"visible":true, | |
"width":32, | |
"x":352, | |
"y":288 | |
}, | |
{ | |
"height":32, | |
"id":15, | |
"name":"enemy", | |
"properties": | |
{ | |
}, | |
"rotation":0, | |
"type":"", | |
"visible":true, | |
"width":32, | |
"x":416, | |
"y":192 | |
}], | |
"opacity":1, | |
"type":"objectgroup", | |
"visible":false, | |
"width":16, | |
"x":0, | |
"y":0 | |
}], | |
"nextobjectid":16, | |
"orientation":"orthogonal", | |
"properties": | |
{ | |
}, | |
"renderorder":"left-up", | |
"tileheight":32, | |
"tilesets":[ | |
{ | |
"firstgid":1, | |
"image":"tileset.png", | |
"imageheight":256, | |
"imagewidth":224, | |
"margin":0, | |
"name":"tileset", | |
"properties": | |
{ | |
}, | |
"spacing":0, | |
"tileheight":32, | |
"tileproperties": | |
{ | |
"0": | |
{ | |
"barrier":"true" | |
}, | |
"1": | |
{ | |
"barrier":"true" | |
}, | |
"10": | |
{ | |
"barrier":"true" | |
}, | |
"11": | |
{ | |
"barrier":"true" | |
}, | |
"12": | |
{ | |
"barrier":"true" | |
}, | |
"13": | |
{ | |
"barrier":"true" | |
}, | |
"14": | |
{ | |
"barrier":"true" | |
}, | |
"15": | |
{ | |
"barrier":"true" | |
}, | |
"16": | |
{ | |
"barrier":"true" | |
}, | |
"17": | |
{ | |
"barrier":"true" | |
}, | |
"18": | |
{ | |
"barrier":"true" | |
}, | |
"19": | |
{ | |
"barrier":"true" | |
}, | |
"2": | |
{ | |
"barrier":"true" | |
}, | |
"20": | |
{ | |
"barrier":"true" | |
}, | |
"21": | |
{ | |
"barrier":"true" | |
}, | |
"22": | |
{ | |
"barrier":"true" | |
}, | |
"23": | |
{ | |
"barrier":"true" | |
}, | |
"24": | |
{ | |
"barrier":"true" | |
}, | |
"25": | |
{ | |
"barrier":"true" | |
}, | |
"27": | |
{ | |
"barrier":"true" | |
}, | |
"28": | |
{ | |
"barrier":"true" | |
}, | |
"29": | |
{ | |
"barrier":"true" | |
}, | |
"3": | |
{ | |
"barrier":"true" | |
}, | |
"30": | |
{ | |
"barrier":"true" | |
}, | |
"31": | |
{ | |
"barrier":"true" | |
}, | |
"32": | |
{ | |
"barrier":"true" | |
}, | |
"34": | |
{ | |
"barrier":"true" | |
}, | |
"35": | |
{ | |
"barrier":"true" | |
}, | |
"36": | |
{ | |
"barrier":"true" | |
}, | |
"37": | |
{ | |
"barrier":"true" | |
}, | |
"4": | |
{ | |
"barrier":"true" | |
}, | |
"41": | |
{ | |
"barrier":"true" | |
}, | |
"42": | |
{ | |
"barrier":"true" | |
}, | |
"43": | |
{ | |
"barrier":"true" | |
}, | |
"44": | |
{ | |
"barrier":"true" | |
}, | |
"45": | |
{ | |
"barrier":"true" | |
}, | |
"47": | |
{ | |
"barrier":"true" | |
}, | |
"48": | |
{ | |
"barrier":"true" | |
}, | |
"5": | |
{ | |
"barrier":"true" | |
}, | |
"6": | |
{ | |
"barrier":"true" | |
}, | |
"7": | |
{ | |
"barrier":"true" | |
}, | |
"9": | |
{ | |
"barrier":"true" | |
} | |
}, | |
"tilewidth":32 | |
}], | |
"tilewidth":32, | |
"version":1, | |
"width":16 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment