Skip to content
Snippets Groups Projects
Commit 1e44d414 authored by mclar053's avatar mclar053
Browse files

Import tiles and entities from xml file. Layout of rooms are displayed as they...

Import tiles and entities from xml file. Layout of rooms are displayed as they are in room editor. Generates random dungeon by selecting room ids for rooms and creates the tiles and enemies from those templates
parent 8511da73
Branches basic-enemies
No related merge requests found
......@@ -7,6 +7,7 @@ public class GameManager : MonoBehaviour {
public RoomManager roomScript;
public LoadXmlData roomData;
private GUIStyle guiStyle = new GUIStyle();
public int numberOfRooms = 18;
private int level = 3;
......
......@@ -80,11 +80,11 @@ public class RoomManager : MonoBehaviour {
Debug.Log (System.String.Format("{0} : {1} : {2} : {3} : {4}",i, doors[0],doors[1],doors[2],doors[3]));
roomHolder[i] = new GameObject ("Room"+i).transform;
createRoom (i, doors, GameManager.instance.roomData.getRoom(currentNode.getRoomID()));
if(i!=0) addEnemies (i); //First room is safe
if(i!=0)
createRoom (i, doors, GameManager.instance.roomData.getRoomLayout(currentNode.getRoomID()));
if (i != 0) {
addEnemies (i, GameManager.instance.roomData.getRoomEntities (currentNode.getRoomID ())); //First room is safe
roomHolder [i].gameObject.SetActive (false);
}
}
}
......@@ -145,9 +145,10 @@ public class RoomManager : MonoBehaviour {
}
}
void addEnemies(int _room){
for(int i=0; i<3; i++){
GameObject instance = Instantiate (enemy[Random.Range(0,4)], new Vector3 (Random.Range (1, columns-2), Random.Range (1, rows-2), 0f), Quaternion.identity) as GameObject;
void addEnemies(int _room, int[,] _entities){
for(int i=0; i<_entities.GetLength(0); i++){
Debug.Log (System.String.Format("{0}, {1}, {2}, {3}",i,_entities[i,0],_entities[i,1],_entities[i,2]));
GameObject instance = Instantiate (enemy[_entities[i,0]], new Vector3 (_entities[i,1],_entities[i,2], 0f), Quaternion.identity) as GameObject;
instance.transform.SetParent (roomHolder [_room]);
}
}
......
No preview for this file type
......@@ -8,33 +8,45 @@ using System.IO;
public class LoadXmlData : MonoBehaviour{ // the Class
public TextAsset GameAsset;
static string Cube_Character = "";
static string Cylinder_Character = "";
static string Capsule_Character = "";
static string Sphere_Character = "";
List<int[,]> rooms = new List<int[,]>();
List<Room> rooms = new List<Room>();
public void loadRooms(){
XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.
xmlDoc.LoadXml(GameAsset.text); // load the file.
XmlNodeList roomsList = xmlDoc.GetElementsByTagName("room"); // array of the level nodes.
XmlNodeList roomsList = xmlDoc.GetElementsByTagName("room"); // array of the room nodes.
for (int i=0; i<roomsList.Count; i++){
int[,] roomLayout = new int[15,9];
XmlNodeList rows = roomsList [i].ChildNodes;
XmlNodeList rows = roomsList[i].ChildNodes[0].ChildNodes;
XmlNodeList entities = roomsList[i].ChildNodes[1].ChildNodes;
Room newRoom = new Room (15,9,entities.Count);
Debug.Log (rows.Count+" "+roomsList.Count);
//Room Layout
for (int j=0; j<rows.Count; j++){ // levels itens nodes.
XmlNodeList columns = rows [j].ChildNodes;
for (int k=0; k<columns.Count; k++){ // levels itens nodes.
roomLayout[j,k] = int.Parse(columns[k].InnerText);
newRoom.layout[j,k] = int.Parse(columns[k].InnerText);
}
}
rooms.Add(roomLayout);
//Entity Positions
Debug.Log(entities.Count);
for(int j=0; j<entities.Count; j++){
Debug.Log (entities[j].Attributes["column"].Value);
newRoom.setEntity (j, int.Parse(entities[j].Attributes["column"].Value), int.Parse(entities[j].Attributes["row"].Value), int.Parse(entities[j].Attributes["type"].Value));
}
rooms.Add(newRoom);
}
}
public int[,] getRoom(int _room){
return rooms[_room];
public int[,] getRoomLayout(int _room){
Debug.Log (_room);
return rooms[_room].layout;
}
public int[,] getRoomEntities(int _room){
return rooms[_room].entities;
}
}
\ No newline at end of file
......@@ -3,14 +3,17 @@ using System.Collections;
public class Room : MonoBehaviour {
public int roomId;
public bool complete;
void Start(){
public int[,] layout;
public int[,] entities; //[0]-ID [1]-X [2]-Y
public Room(int _w, int _h, int _entityNum){
layout = new int[_w,_h];
entities = new int[_entityNum,3];
}
void Update(){
public void setEntity(int _pos, int _x, int _y, int _id){
entities [_pos, 0] = _id-2;
entities [_pos, 1] = _x;
entities [_pos, 2] = _y;
}
}
......@@ -105,7 +105,7 @@ public class mTree{
//For each child node
for(int i=0; i<currentNode.getChildren().Length; i++){
//Create a new node in the nodes arraylist
nodes.Add(new Node(_nodeNum,(int)directions[i].x,(int)directions[i].y, Random.Range(0,19)));
nodes.Add(new Node(_nodeNum,(int)directions[i].x,(int)directions[i].y, Random.Range(0,GameManager.instance.numberOfRooms)));
//Set the child node's index in the current node
currentNode.setChildNode(i,nodes.Count-1);
childNodeNumbers.Add (nodes.Count-1);
......
No preview for this file type
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment