Commit 9bb2988c by Philipp Adolf

Fix indentation of LevelManagement

parent 9b187eac
...@@ -6,50 +6,50 @@ using System.IO; ...@@ -6,50 +6,50 @@ using System.IO;
[XmlRoot("Levels")] [XmlRoot("Levels")]
public class DeserializedLevels public class DeserializedLevels
{ {
[XmlElement ("Developer")] [XmlElement ("Developer")]
public Developer developer; public Developer developer;
public class Developer public class Developer
{ {
[XmlAttribute ("StartLevel")] [XmlAttribute ("StartLevel")]
public string startLevel; public string startLevel;
} }
[XmlElement ("Level")] [XmlElement ("Level")]
public Level[] levels; public Level[] levels;
public class Level public class Level
{ {
[XmlElement("Item")] [XmlElement("Item")]
public Item[] items; public Item[] items;
} }
public class PlayerStartPosition public class PlayerStartPosition
{ {
[XmlAttribute("x")] [XmlAttribute("x")]
public string x; public string x;
[XmlAttribute("y")] [XmlAttribute("y")]
public string y; public string y;
} }
public class Item public class Item
{ {
[XmlAttribute ("prefab")] [XmlAttribute ("prefab")]
public string prefab; public string prefab;
[XmlAttribute ("x")] [XmlAttribute ("x")]
public string x; public string x;
[XmlAttribute ("y")] [XmlAttribute ("y")]
public string y; public string y;
[XmlAttribute ("rot")] [XmlAttribute ("rot")]
public string rot; public string rot;
[XmlAttribute("scalex")] [XmlAttribute("scalex")]
public string scalex; public string scalex;
[XmlAttribute("scaley")] [XmlAttribute("scaley")]
public string scaley; public string scaley;
} }
} }
...@@ -7,42 +7,42 @@ using System.Xml; ...@@ -7,42 +7,42 @@ using System.Xml;
using System.Linq; using System.Linq;
public class DeserializedLevelsCrossChecker { public class DeserializedLevelsCrossChecker {
//Hallo Tim und Philipp. Die Datei kuckt nur ob irgendwas mit den Prefabs komisch ist. //Hallo Tim und Philipp. Die Datei kuckt nur ob irgendwas mit den Prefabs komisch ist.
// cross check /Resources/Prefabs and Levels.xml if there are any item prefabs that exist only in one but not the other // cross check /Resources/Prefabs and Levels.xml if there are any item prefabs that exist only in one but not the other
public void crossCheck () public void crossCheck ()
{
// create a list of /Resources/Prefabs for resources and Levels.Xml
List<string> resPrefabList = new List<string>();
List<string> xmlPrefabList = new List<string>();
// Get prefabs from Levels.xml
DeserializedLevels deserializedLevels = XmlIO.LoadXml<DeserializedLevels>("Levels");
foreach (DeserializedLevels.Level level in deserializedLevels.levels)
{ {
// create a list of /Resources/Prefabs for resources and Levels.Xml foreach (DeserializedLevels.Item item in level.items)
List<string> resPrefabList = new List<string>(); if (!xmlPrefabList.Contains(item.prefab))
List<string> xmlPrefabList = new List<string>(); xmlPrefabList.Add(item.prefab);
// Get prefabs from Levels.xml
DeserializedLevels deserializedLevels = XmlIO.LoadXml<DeserializedLevels>("Levels");
foreach (DeserializedLevels.Level level in deserializedLevels.levels)
{
foreach (DeserializedLevels.Item item in level.items)
if (!xmlPrefabList.Contains(item.prefab))
xmlPrefabList.Add(item.prefab);
}
// Get prefabs from the /Resources/Prefabs folder
// get all child items in the /Resources/Prefabs folder
DirectoryInfo dir = new DirectoryInfo("Assets/Resources/Prefabs");
FileInfo[] fileInfos = dir.GetFiles("*.prefab");
fileInfos.Select(f => f.FullName).ToArray();
// Add each prefab's file name to prefabList and truncate the .prefab extension from the end
foreach (FileInfo fileInfo in fileInfos)
resPrefabList.Add (fileInfo.Name.Substring(0, fileInfo.Name.Length - ".prefab".Length));
// Cross checks
foreach (string prefab in xmlPrefabList.Except(resPrefabList).ToList())
Debug.LogError (prefab + " is missing in the /Resorces/Prefabs folder but used in Levels.xml");
foreach (string prefab in resPrefabList.Except(xmlPrefabList).ToList())
Debug.Log (prefab + " exists in the /Resorces/Prefabs folder but not used in Levels.xml");
Debug.Log ("Cross Check Done");
} }
// Get prefabs from the /Resources/Prefabs folder
// get all child items in the /Resources/Prefabs folder
DirectoryInfo dir = new DirectoryInfo("Assets/Resources/Prefabs");
FileInfo[] fileInfos = dir.GetFiles("*.prefab");
fileInfos.Select(f => f.FullName).ToArray();
// Add each prefab's file name to prefabList and truncate the .prefab extension from the end
foreach (FileInfo fileInfo in fileInfos)
resPrefabList.Add (fileInfo.Name.Substring(0, fileInfo.Name.Length - ".prefab".Length));
// Cross checks
foreach (string prefab in xmlPrefabList.Except(resPrefabList).ToList())
Debug.LogError (prefab + " is missing in the /Resorces/Prefabs folder but used in Levels.xml");
foreach (string prefab in resPrefabList.Except(xmlPrefabList).ToList())
Debug.Log (prefab + " exists in the /Resorces/Prefabs folder but not used in Levels.xml");
Debug.Log ("Cross Check Done");
}
} }
...@@ -6,135 +6,135 @@ using UnityEngine; ...@@ -6,135 +6,135 @@ using UnityEngine;
public class DeserializedLevelsLoader public class DeserializedLevelsLoader
{ {
// Levels deserialized // Levels deserialized
private DeserializedLevels deserializedLevels; private DeserializedLevels deserializedLevels;
private int currentLevelIndex = 0; private int currentLevelIndex = 0;
private int currentSegmentIndex = 0; private int currentSegmentIndex = 0;
private const string prefabsFolder = "Prefabs/"; private const string prefabsFolder = "Prefabs/";
struct ItemStruct struct ItemStruct
{ {
public GameObject prefab; public GameObject prefab;
public float x; public float x;
public float y; public float y;
public float rot; public float rot;
public float scalex; public float scalex;
public float scaley; public float scaley;
} }
// Cache prefabs in prefabDict
Dictionary<string,GameObject> prefabPool;
// Cache all items with locations // Cache prefabs in prefabDict
List<ItemStruct> sceneItemsList; Dictionary<string,GameObject> prefabPool;
Transform parentOfXmlItems; // Cache all items with locations
List<ItemStruct> sceneItemsList;
public const string xmlItemsGOName = "XmlItems";
public void loadLevel (int levelIndex = 0) Transform parentOfXmlItems;
{
prefabPool = new Dictionary<string, GameObject>();
sceneItemsList = new List<ItemStruct>();
// if the XmlItems gameobject folder remained in the Hierarcy, then delete it public const string xmlItemsGOName = "XmlItems";
while (GameObject.Find (xmlItemsGOName) != null)
MonoBehaviour.DestroyImmediate(GameObject.Find (xmlItemsGOName));
parentOfXmlItems = new GameObject(xmlItemsGOName).transform;
deserializedLevels = XmlIO.LoadXml<DeserializedLevels>("Levels"); public void loadLevel (int levelIndex = 0)
{
prefabPool = new Dictionary<string, GameObject>();
sceneItemsList = new List<ItemStruct>();
// if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there // if the XmlItems gameobject folder remained in the Hierarcy, then delete it
// otherwise start with level 1 while (GameObject.Find (xmlItemsGOName) != null)
currentLevelIndex = (levelIndex > deserializedLevels.levels.Length - 1) ? 0 : levelIndex; MonoBehaviour.DestroyImmediate(GameObject.Find (xmlItemsGOName));
parentOfXmlItems = new GameObject(xmlItemsGOName).transform;
DeserializedLevels.Level currentLevel = deserializedLevels.levels[currentLevelIndex]; deserializedLevels = XmlIO.LoadXml<DeserializedLevels>("Levels");
sceneItemsList.Clear();
// if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there
// <Item prefab="Chair" x="1" y="10" rot="90" /> // otherwise start with level 1
foreach (DeserializedLevels.Item deserializedItem in currentLevel.items) currentLevelIndex = (levelIndex > deserializedLevels.levels.Length - 1) ? 0 : levelIndex;
{
// caching prefabString i.e. "phone"
string prefabString = deserializedItem.prefab;
// if the prefab in the item XmlNode has not been loaded then add it to the prefabsDict dictionary,
if (!prefabPool.ContainsKey(prefabString))
{
// load prefab
GameObject prefabObject = Resources.Load(prefabsFolder + prefabString, typeof(GameObject)) as GameObject;
// if unsuccesful, error message and jump to next in the foreach loop
if (prefabObject == null)
{
Debug.LogError("Prefab \"" + prefabString + "\" does not exists.");
continue;
}
// otherwise add to dictionary
prefabPool.Add(prefabString, prefabObject);
}
ItemStruct item;
item.prefab = prefabPool[prefabString];
item.x = toFloatZeroIfNull(deserializedItem.x);
item.y = toFloatZeroIfNull(deserializedItem.y);
item.rot = toFloatZeroIfNull(deserializedItem.rot);
item.scalex = toFloatOneIfNull(deserializedItem.scalex);
item.scaley = toFloatOneIfNull(deserializedItem.scaley);
sceneItemsList.Add(item);
}
// Finally instantiate all items
foreach (ItemStruct item in sceneItemsList)
{
// TODO load height coordinate from a directory DeserializedLevels.Level currentLevel = deserializedLevels.levels[currentLevelIndex];
GameObject newGameObject = MonoBehaviour.Instantiate(item.prefab) as GameObject; sceneItemsList.Clear();
// set position
setPos2D(newGameObject, new Vector2(item.x, item.y));
// set rotation // <Item prefab="Chair" x="1" y="10" rot="90" />
setRot2D(newGameObject, item.rot); foreach (DeserializedLevels.Item deserializedItem in currentLevel.items)
{
// caching prefabString i.e. "phone"
string prefabString = deserializedItem.prefab;
setScale2D(newGameObject, item.scalex, item.scaley); // if the prefab in the item XmlNode has not been loaded then add it to the prefabsDict dictionary,
if (!prefabPool.ContainsKey(prefabString))
{
// load prefab
GameObject prefabObject = Resources.Load(prefabsFolder + prefabString, typeof(GameObject)) as GameObject;
// set parent // if unsuccesful, error message and jump to next in the foreach loop
newGameObject.transform.parent = parentOfXmlItems; if (prefabObject == null)
{
Debug.LogError("Prefab \"" + prefabString + "\" does not exists.");
continue;
} }
}
// DONE, these are only helper functions below // otherwise add to dictionary
prefabPool.Add(prefabString, prefabObject);
}
// if no value then return zero or one, otherwise convert to float ItemStruct item;
float toFloatZeroIfNull (string value) { return value == null ? 0 : float.Parse(value); } item.prefab = prefabPool[prefabString];
float toFloatOneIfNull (string value) { return value == null ? 1 : float.Parse(value); } item.x = toFloatZeroIfNull(deserializedItem.x);
item.y = toFloatZeroIfNull(deserializedItem.y);
item.rot = toFloatZeroIfNull(deserializedItem.rot);
item.scalex = toFloatOneIfNull(deserializedItem.scalex);
item.scaley = toFloatOneIfNull(deserializedItem.scaley);
void setPos2D(GameObject g, Vector2 pos) sceneItemsList.Add(item);
{
g.transform.position = new Vector2 (
pos.x,
pos.y
);
} }
void setRot2D(GameObject g, float rot) // Finally instantiate all items
foreach (ItemStruct item in sceneItemsList)
{ {
//Debug.Log("Set rotation of " + g.name + " to " + rot);
Quaternion rotation = Quaternion.identity;
rotation.eulerAngles = new Vector3(0, 0, rot);
g.transform.localRotation = rotation;
}
void setScale2D(GameObject g, float scaleX, float scaleY) // TODO load height coordinate from a directory
{ GameObject newGameObject = MonoBehaviour.Instantiate(item.prefab) as GameObject;
g.transform.localScale = new Vector2(scaleX, scaleY);
// set position
setPos2D(newGameObject, new Vector2(item.x, item.y));
// set rotation
setRot2D(newGameObject, item.rot);
setScale2D(newGameObject, item.scalex, item.scaley);
// set parent
newGameObject.transform.parent = parentOfXmlItems;
} }
}
// DONE, these are only helper functions below
// if no value then return zero or one, otherwise convert to float
float toFloatZeroIfNull (string value) { return value == null ? 0 : float.Parse(value); }
float toFloatOneIfNull (string value) { return value == null ? 1 : float.Parse(value); }
void setPos2D(GameObject g, Vector2 pos)
{
g.transform.position = new Vector2 (
pos.x,
pos.y
);
}
void setRot2D(GameObject g, float rot)
{
//Debug.Log("Set rotation of " + g.name + " to " + rot);
Quaternion rotation = Quaternion.identity;
rotation.eulerAngles = new Vector3(0, 0, rot);
g.transform.localRotation = rotation;
}
void setScale2D(GameObject g, float scaleX, float scaleY)
{
g.transform.localScale = new Vector2(scaleX, scaleY);
}
} }
...@@ -4,68 +4,68 @@ using System.Xml; ...@@ -4,68 +4,68 @@ using System.Xml;
public class DeserializedLevelsSaver public class DeserializedLevelsSaver
{ {
public const string xmlItemsToExportGOName = "XmlItemsToExport"; public const string xmlItemsToExportGOName = "XmlItemsToExport";
public void saveExportItems () public void saveExportItems ()
{ {
// Create XmlItemsToExport if does not exist yet // Create XmlItemsToExport if does not exist yet
if (GameObject.Find (xmlItemsToExportGOName) == null) if (GameObject.Find (xmlItemsToExportGOName) == null)
new GameObject(xmlItemsToExportGOName); new GameObject(xmlItemsToExportGOName);
GameObject xmlItemsToExportGO = GameObject.Find (xmlItemsToExportGOName); GameObject xmlItemsToExportGO = GameObject.Find (xmlItemsToExportGOName);
var xmlItemsToExportGOchildren = xmlItemsToExportGO.GetComponentsInChildren<Transform>(); var xmlItemsToExportGOchildren = xmlItemsToExportGO.GetComponentsInChildren<Transform>();
// Check if any children exist // Check if any children exist
if (xmlItemsToExportGOchildren.Length == 0) if (xmlItemsToExportGOchildren.Length == 0)
Debug.LogError ("Add the prefabs to " + xmlItemsToExportGOName); Debug.LogError ("Add the prefabs to " + xmlItemsToExportGOName);
DeserializedLevels.Level levelXml = new DeserializedLevels.Level(); DeserializedLevels.Level levelXml = new DeserializedLevels.Level();
int n = 0; int n = 0;
// count number of children skipping sub-items // count number of children skipping sub-items
foreach (Transform item in xmlItemsToExportGOchildren) foreach (Transform item in xmlItemsToExportGOchildren)
if (item.parent == xmlItemsToExportGO.transform) n++; if (item.parent == xmlItemsToExportGO.transform) n++;
// the items array should have that many elements // the items array should have that many elements
levelXml.items = new DeserializedLevels.Item[n]; levelXml.items = new DeserializedLevels.Item[n];
// use i for counting items, i would be equal (one more to be precise) to n at the end of the cycle
int i = 0;
// cycle through the children again and add them to items
foreach (Transform item in xmlItemsToExportGOchildren)
{
// skip sub-items
if (item.parent != xmlItemsToExportGO.transform) continue;
levelXml.items[i] = new DeserializedLevels.Item();
levelXml.items[i].prefab = item.name;
levelXml.items[i].x = toStringNullIfZero(item.transform.position.x);
levelXml.items[i].y = toStringNullIfZero(item.transform.position.y);
levelXml.items[i].rot = toStringNullIfZero(item.localRotation.eulerAngles.z);
levelXml.items[i].scalex = toStringNullIfOne(item.localScale.x);
levelXml.items[i].scaley = toStringNullIfOne(item.localScale.y);
// increase i for the next cycle
i++;
}
// Export just one level
DeserializedLevels levelsXmlToExport = new DeserializedLevels();
levelsXmlToExport.levels = new DeserializedLevels.Level[1];
levelsXmlToExport.levels[0] = levelXml;
XmlIO.SaveXml<DeserializedLevels>(levelsXmlToExport, "./Assets/Resources/" + xmlItemsToExportGOName + ".xml");
}
string toStringNullIfZero (float num) { return num == 0 ? null : mathRound(num,2).ToString(); } // use i for counting items, i would be equal (one more to be precise) to n at the end of the cycle
string toStringNullIfOne (float num) { return num == 1 ? null : mathRound(num,2).ToString(); } int i = 0;
// cycle through the children again and add them to items
float mathRound (float round, int decimals) foreach (Transform item in xmlItemsToExportGOchildren)
{ {
return Mathf.Round(round * Mathf.Pow(10,decimals)) / Mathf.Pow(10,decimals); // skip sub-items
if (item.parent != xmlItemsToExportGO.transform) continue;
levelXml.items[i] = new DeserializedLevels.Item();
levelXml.items[i].prefab = item.name;
levelXml.items[i].x = toStringNullIfZero(item.transform.position.x);
levelXml.items[i].y = toStringNullIfZero(item.transform.position.y);
levelXml.items[i].rot = toStringNullIfZero(item.localRotation.eulerAngles.z);
levelXml.items[i].scalex = toStringNullIfOne(item.localScale.x);
levelXml.items[i].scaley = toStringNullIfOne(item.localScale.y);
// increase i for the next cycle
i++;
} }
// Export just one level
DeserializedLevels levelsXmlToExport = new DeserializedLevels();
levelsXmlToExport.levels = new DeserializedLevels.Level[1];
levelsXmlToExport.levels[0] = levelXml;
XmlIO.SaveXml<DeserializedLevels>(levelsXmlToExport, "./Assets/Resources/" + xmlItemsToExportGOName + ".xml");
}
string toStringNullIfZero (float num) { return num == 0 ? null : mathRound(num,2).ToString(); }
string toStringNullIfOne (float num) { return num == 1 ? null : mathRound(num,2).ToString(); }
float mathRound (float round, int decimals)
{
return Mathf.Round(round * Mathf.Pow(10,decimals)) / Mathf.Pow(10,decimals);
}
} }
...@@ -5,25 +5,25 @@ using UnityEngine; // necessary for TextAsset ...@@ -5,25 +5,25 @@ using UnityEngine; // necessary for TextAsset
public static class XmlIO public static class XmlIO
{ {
public static void SaveXml<T> (this object deserializedXml, string path) where T : class public static void SaveXml<T> (this object deserializedXml, string path) where T : class
{
using(var stream = new FileStream(path, FileMode.Create))
{ {
using(var stream = new FileStream(path, FileMode.Create)) var s = new XmlSerializer(typeof(T));
{ s.Serialize(stream, deserializedXml);
var s = new XmlSerializer(typeof(T));
s.Serialize(stream, deserializedXml);
}
} }
}
public static T LoadXml<T>(string textAssetName) where T : class public static T LoadXml<T>(string textAssetName) where T : class
{ {
TextAsset xmlTextAsset = (TextAsset) Resources.Load (textAssetName, typeof(TextAsset)); TextAsset xmlTextAsset = (TextAsset) Resources.Load (textAssetName, typeof(TextAsset));
using(var stream = new StringReader(xmlTextAsset.text)) using(var stream = new StringReader(xmlTextAsset.text))
{ {
var s = new XmlSerializer(typeof(T)); var s = new XmlSerializer(typeof(T));
T deserializedXml = s.Deserialize(stream) as T; T deserializedXml = s.Deserialize(stream) as T;
return deserializedXml; return deserializedXml;
}
} }
}
} }
Markdown is supported
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