Commit 3c8982aa by Tim Reiter

players are now loaded from xml (+ fixed a bug when the player picked up the portable fire)

parent 83bf8ea8
...@@ -4,8 +4,12 @@ ...@@ -4,8 +4,12 @@
<Developer StartLevel="0" /> <Developer StartLevel="0" />
<Level> <Level>
<Item prefab="Trap" x="-2.2" y="-0.1" rot="90" /> <Item prefab="Player Two" x="-3.05" y="2.06" />
<Item prefab="ground" x="9.3" y="-0.7" scalex="200" /> <Item prefab="Player One" x="-0.23" y="2.06" />
<Item prefab="Trap" x="-5.02" y="-0.57" rot="90" />
<Item prefab="ground" x="1.51" y="-0.8" scalex="58.39" />
<Item prefab="Turret" x="4.47" y="0.71" />
<Item prefab="PortableFire" x="4.04" y="-0.22" scalex="0.2" scaley="0.2" />
</Level> </Level>
<Level> <Level>
<Item prefab="PlayerStartPosition" x="5.11" y="-0.92" /> <Item prefab="PlayerStartPosition" x="5.11" y="-0.92" />
......
fileFormatVersion: 2 fileFormatVersion: 2
guid: 8d9c84360d70c1b44af690dc244612a0 guid: 329ca4a74de8c8a438a191940544e5e1
timeCreated: 1433461451 timeCreated: 1433500438
licenseType: Free licenseType: Free
NativeFormatImporter: NativeFormatImporter:
userData: userData:
......
fileFormatVersion: 2
guid: 6c6daf85837b9f542bcb18f819b9d8a8
timeCreated: 1433500442
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2 fileFormatVersion: 2
guid: e0d90fce50c24884c8d83b9d365a5fe3 guid: 3ad7c167097946b408dbef72e2814b8f
timeCreated: 1433446163 timeCreated: 1433501024
licenseType: Free licenseType: Free
NativeFormatImporter: NativeFormatImporter:
userData: userData:
......
<?xml version="1.0" encoding="Windows-1252"?> <?xml version="1.0" encoding="Windows-1252"?>
<Levels xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Levels xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Level> <Level>
<Item prefab="Trap" x="-2.2" y="-0.1" rot="90" /> <Item prefab="Player Two" x="-3.05" y="2.06" />
<Item prefab="ground" x="9.3" y="-0.7" scalex="200" /> <Item prefab="Player One" x="-0.23" y="2.06" />
<Item prefab="Trap" x="-5.02" y="-0.57" rot="90" />
<Item prefab="ground" x="1.51" y="-0.8" scalex="58.39" />
<Item prefab="Turret" x="4.47" y="0.71" />
<Item prefab="PortableFire" x="4.04" y="-0.22" scalex="0.2" scaley="0.2" />
</Level> </Level>
</Levels> </Levels>
\ No newline at end of file
...@@ -7,20 +7,23 @@ public class FireScript : MonoBehaviour { ...@@ -7,20 +7,23 @@ public class FireScript : MonoBehaviour {
public float constantDamagepoints = 0.1f; public float constantDamagepoints = 0.1f;
public float burningTime = 10; public float burningTime = 10f;
// Use this for initialization private Vector3 startScale; //fixes a bug: scale of fire was 0 when attached to the player.
void Start () {
Invoke("stopBurning", burningTime);
} void Start()
{
// Update is called once per frame startScale = transform.localScale;
void Update () { }
if (parentHealth != null)
// Update is called once per frame
void Update () {
if (parentHealth)
{ {
parentHealth.changeHealthBy(-constantDamagepoints); parentHealth.changeHealthBy(-constantDamagepoints * Time.deltaTime);
} }
} }
void OnTriggerEnter2D(Collider2D other) void OnTriggerEnter2D(Collider2D other)
{ {
...@@ -29,6 +32,8 @@ public class FireScript : MonoBehaviour { ...@@ -29,6 +32,8 @@ public class FireScript : MonoBehaviour {
parentHealth = other.GetComponent<PlayerHealth>(); parentHealth = other.GetComponent<PlayerHealth>();
transform.position = other.transform.position; //hier schon position setzen transform.position = other.transform.position; //hier schon position setzen
transform.parent = other.transform; transform.parent = other.transform;
transform.localScale = startScale;
Invoke("stopBurning", burningTime);
} }
} }
......
using UnityEngine;
using System.Collections;
public class GameMaster : MonoBehaviour {
DeserializedLevelsLoader levelLoader;
// Use this for initialization
void Start () {
levelLoader = new DeserializedLevelsLoader();
levelLoader.loadLevel();
}
// Update is called once per frame
void Update () {
}
}
fileFormatVersion: 2 fileFormatVersion: 2
guid: 01cdeb3258bc3446ba8846c11515fc68 guid: 50166dd06214cc844822e30d279ffe94
timeCreated: 1433499967
licenseType: Free
MonoImporter: MonoImporter:
serializedVersion: 2 serializedVersion: 2
defaultReferences: [] defaultReferences: []
executionOrder: 0 executionOrder: 0
icon: {instanceID: 0} icon: {instanceID: 0}
userData: userData:
assetBundleName:
assetBundleVariant:
...@@ -22,15 +22,15 @@ public class GameStateTracker : MonoBehaviour { ...@@ -22,15 +22,15 @@ public class GameStateTracker : MonoBehaviour {
ranking = new List<string>(); ranking = new List<string>();
} }
// Use this for initialization // Use this for initialization
void Start () { void Start () {
} }
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
} }
public void setNumberOfPlayers(int players) public void setNumberOfPlayers(int players)
{ {
......
...@@ -6,13 +6,13 @@ using UnityEngine; ...@@ -6,13 +6,13 @@ 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
{ {
...@@ -24,32 +24,32 @@ public class DeserializedLevelsLoader ...@@ -24,32 +24,32 @@ public class DeserializedLevelsLoader
public float scaley; public float scaley;
} }
// Cache prefabs in prefabDict // Cache prefabs in prefabDict
Dictionary<string,GameObject> prefabPool; Dictionary<string,GameObject> prefabPool;
// Cache all items with locations // Cache all items with locations
List<ItemStruct> sceneItemsList; List<ItemStruct> sceneItemsList;
Transform parentOfXmlItems; Transform parentOfXmlItems;
public const string xmlItemsGOName = "XmlItems"; public const string xmlItemsGOName = "XmlItems";
public void loadLevel (int levelIndex = 0) public void loadLevel (int levelIndex = 0)
{ {
prefabPool = new Dictionary<string, GameObject>(); prefabPool = new Dictionary<string, GameObject>();
sceneItemsList = new List<ItemStruct>(); sceneItemsList = new List<ItemStruct>();
// if the XmlItems gameobject folder remained in the Hierarcy, then delete it // if the XmlItems gameobject folder remained in the Hierarcy, then delete it
while (GameObject.Find (xmlItemsGOName) != null) while (GameObject.Find (xmlItemsGOName) != null)
MonoBehaviour.DestroyImmediate(GameObject.Find (xmlItemsGOName)); MonoBehaviour.DestroyImmediate(GameObject.Find (xmlItemsGOName));
parentOfXmlItems = new GameObject(xmlItemsGOName).transform; parentOfXmlItems = new GameObject(xmlItemsGOName).transform;
deserializedLevels = XmlIO.LoadXml<DeserializedLevels>("Levels"); deserializedLevels = XmlIO.LoadXml<DeserializedLevels>("Levels");
// if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there // if startlevel is in the XML i.e. <Developer StartLevel="3" /> then get level from there
// otherwise start with level 1 // otherwise start with level 1
currentLevelIndex = (levelIndex > deserializedLevels.levels.Length - 1) ? 0 : levelIndex; currentLevelIndex = (levelIndex > deserializedLevels.levels.Length - 1) ? 0 : levelIndex;
DeserializedLevels.Level currentLevel = deserializedLevels.levels[currentLevelIndex]; DeserializedLevels.Level currentLevel = deserializedLevels.levels[currentLevelIndex];
...@@ -108,30 +108,30 @@ public class DeserializedLevelsLoader ...@@ -108,30 +108,30 @@ public class DeserializedLevelsLoader
// set parent // set parent
newGameObject.transform.parent = parentOfXmlItems; newGameObject.transform.parent = parentOfXmlItems;
} }
} }
// DONE, these are only helper functions below // DONE, these are only helper functions below
// if no value then return zero or one, otherwise convert to float // if no value then return zero or one, otherwise convert to float
float toFloatZeroIfNull (string value) { return value == null ? 0 : float.Parse(value); } float toFloatZeroIfNull (string value) { return value == null ? 0 : float.Parse(value); }
float toFloatOneIfNull (string value) { return value == null ? 1 : float.Parse(value); } float toFloatOneIfNull (string value) { return value == null ? 1 : float.Parse(value); }
void setPos2D(GameObject g, Vector2 pos) void setPos2D(GameObject g, Vector2 pos)
{ {
g.transform.position = new Vector2 ( g.transform.position = new Vector2 (
pos.x, pos.x,
pos.y pos.y
); );
} }
void setRot2D(GameObject g, float rot) void setRot2D(GameObject g, float rot)
{ {
//Debug.Log("Set rotation of " + g.name + " to " + rot); //Debug.Log("Set rotation of " + g.name + " to " + rot);
Quaternion rotation = Quaternion.identity; Quaternion rotation = Quaternion.identity;
rotation.eulerAngles = new Vector3(0, 0, rot); rotation.eulerAngles = new Vector3(0, 0, rot);
g.transform.localRotation = rotation; g.transform.localRotation = rotation;
} }
void setScale2D(GameObject g, float scaleX, float scaleY) void setScale2D(GameObject g, float scaleX, float scaleY)
{ {
......
using UnityEngine;
using System.Collections;
public class PlayScript : MonoBehaviour {
DeserializedLevelsLoader d;
void Start ()
{
d = new DeserializedLevelsLoader();
d.loadLevel();
}
}
...@@ -8,21 +8,29 @@ public class PlayerHealth : MonoBehaviour { ...@@ -8,21 +8,29 @@ public class PlayerHealth : MonoBehaviour {
public float currentHealth = 100; public float currentHealth = 100;
private bool alive = true; private bool alive = true;
public Scrollbar healthbar; private Scrollbar healthbar;
private PlayerControl playerControl;
private GameStateTracker gameStateTracker;
private bool hasShield = false; private bool hasShield = false;
void Start()
{
gameStateTracker = GameObject.Find("GameMaster").GetComponent<GameStateTracker>(); //warning: geht kaputt wenn der name "Game Master" sich aendert.
playerControl = gameObject.GetComponent<PlayerControl>();
string healthbarName = "HealthBar" + playerControl.playerNumber.ToString();
healthbar = GameObject.Find(healthbarName).GetComponent<Scrollbar>();
}
// Update is called once per frame // Update is called once per frame
void Update () { void Update () {
if (alive && currentHealth <= 0) if (alive && currentHealth <= 0)
{ {
Debug.Log("Player " + name + " dead."); Debug.Log("Player " + name + " dead.");
alive = false; alive = false;
GameObject.Find("GameStateTracker").GetComponent<GameStateTracker>().playerDied(name); gameStateTracker.playerDied(name);
Destroy(gameObject); Destroy(gameObject);
} }
} }
public float getCurrentHealth() public float getCurrentHealth()
{ {
......
No preview for this file type
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