Commit 13072770 by Tim Reiter

started working on game over panel

parent 3c8982aa
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class GameMaster : MonoBehaviour { public class GameMaster : MonoBehaviour {
DeserializedLevelsLoader levelLoader; DeserializedLevelsLoader levelLoader;
public GameObject gameOverPanel;
public Text rankingText;
// Use this for initialization // Use this for initialization
void Start () { void Start () {
...@@ -11,9 +15,26 @@ public class GameMaster : MonoBehaviour { ...@@ -11,9 +15,26 @@ public class GameMaster : MonoBehaviour {
levelLoader.loadLevel(); levelLoader.loadLevel();
} }
public void gameOver(List<string> ranking)
{
gameOverPanel.SetActive(true);
// Update is called once per frame Debug.Log(ranking.Count);
void Update () { if (ranking.Count > 0)
{
rankingText.text = "1st: " + ranking[0];
}
if (ranking.Count > 1)
{
rankingText.text += "\n2nd: " + ranking[1];
}
if (ranking.Count > 2)
{
rankingText.text += "\n3rd: " + ranking[2];
}
if (ranking.Count > 3)
{
rankingText.text += "\n4th: " + ranking[3];
}
} }
} }
...@@ -15,6 +15,7 @@ public class GameStateTracker : MonoBehaviour { ...@@ -15,6 +15,7 @@ public class GameStateTracker : MonoBehaviour {
private int winnerPlayer; private int winnerPlayer;
private List<string> ranking; private List<string> ranking;
private GameMaster gameMaster;
void Awake() void Awake()
...@@ -24,13 +25,9 @@ public class GameStateTracker : MonoBehaviour { ...@@ -24,13 +25,9 @@ public class GameStateTracker : MonoBehaviour {
// Use this for initialization // Use this for initialization
void Start () { void Start () {
gameMaster = GetComponent<GameMaster>();
} }
// Update is called once per frame
void Update () {
}
public void setNumberOfPlayers(int players) public void setNumberOfPlayers(int players)
{ {
...@@ -52,33 +49,22 @@ public class GameStateTracker : MonoBehaviour { ...@@ -52,33 +49,22 @@ public class GameStateTracker : MonoBehaviour {
public void gameOver() public void gameOver()
{ {
Time.timeScale = 0.0f; Time.timeScale = 0.0f; //stops the game
//find the player who is still alive:
string playerStillAlive = "";
GameObject[] players = GameObject.FindGameObjectsWithTag("Player"); GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
string loser = ""; foreach (GameObject p in players)
foreach (GameObject p in players){ {
if (p.GetComponent<PlayerHealth>().getCurrentHealth() > 0) if (p.GetComponent<PlayerHealth>().getCurrentHealth() > 0)
{ {
loser = p.name; playerStillAlive = p.name;
ranking.Add(playerStillAlive);
break; break;
} }
} }
if (string.IsNullOrEmpty(loser))
{
Debug.LogError("No loser found");
loser = "None";
}
else
{
ranking.Add(loser);
}
Debug.Log("Ranking: ");
for (int i = 0; i < ranking.Count; i++)
{
Debug.Log((i+1) + "th: " + ranking[i]);
}
//Ranking enthält an erster stelle den gewinner (der als erster gestorben ist) und dann absteigend die danach als String.
}
gameMaster.gameOver(ranking); //shows the game over screen
}
} }
...@@ -13,12 +13,19 @@ public class PlayerHealth : MonoBehaviour { ...@@ -13,12 +13,19 @@ public class PlayerHealth : MonoBehaviour {
private GameStateTracker gameStateTracker; private GameStateTracker gameStateTracker;
private bool hasShield = false; private bool hasShield = false;
void Start() void Start()
{ {
gameStateTracker = GameObject.Find("GameMaster").GetComponent<GameStateTracker>(); //warning: geht kaputt wenn der name "Game Master" sich aendert. gameStateTracker = GameObject.Find("GameMaster").GetComponent<GameStateTracker>(); //warning: geht kaputt wenn der name "Game Master" sich aendert.
playerControl = gameObject.GetComponent<PlayerControl>(); playerControl = gameObject.GetComponent<PlayerControl>();
string healthbarName = "HealthBar" + playerControl.playerNumber.ToString(); string healthbarName = "HealthBar" + playerControl.playerNumber.ToString();
healthbar = GameObject.Find(healthbarName).GetComponent<Scrollbar>(); healthbar = GameObject.Find(healthbarName).GetComponent<Scrollbar>();
if (name.EndsWith("(Clone)"))
{
name = name.Split(new char[] { '(' })[0];
}
} }
// Update is called once per frame // Update is called once per frame
......
fileFormatVersion: 2
guid: c07127002e5da3d4f8e6c8df3baacab8
folderAsset: yes
timeCreated: 1433459937
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
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