Commit cfcb801a by Alisa Jung

added game state tracking:

if all (except one) players are dead: TimeScale is set to 0, ranking is logged.
parent 99feb492
fileFormatVersion: 2
guid: 3d31e8f63f3ce8f44943e5e4cca3b056
timeCreated: 1433457524
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameStateTracker : MonoBehaviour {
private int numberOfPlayers;
/// <summary>
/// Numbers of Player ranked 4th to winner
/// </summary>
private int fourthPlayer;
private int thirdPlayer;
private int secondPlayer;
private int winnerPlayer;
private List<string> ranking;
void Awake()
{
ranking = new List<string>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void setNumberOfPlayers(int players)
{
numberOfPlayers = players;
}
public void playerDied(string name)
{
if (!ranking.Contains(name))
{
ranking.Add(name);
}
if (ranking.Count >= numberOfPlayers - 1)
{
gameOver();
}
}
public void gameOver()
{
Time.timeScale = 0.0f;
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
string loser = "";
foreach (GameObject p in players){
if (p.GetComponent<PlayerHealth>().getCurrentHealth() > 0)
{
loser = p.name;
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.
}
}
fileFormatVersion: 2
guid: dc91e7f992ec0f845bc2a960c166515b
timeCreated: 1433456333
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -16,6 +16,9 @@ public class PlayerHealth : MonoBehaviour { ...@@ -16,6 +16,9 @@ public class PlayerHealth : MonoBehaviour {
if (alive && currentHealth <= 0) if (alive && currentHealth <= 0)
{ {
Debug.Log("Player " + name + " dead."); Debug.Log("Player " + name + " dead.");
alive = false;
GameObject.Find("GameStateTracker").GetComponent<GameStateTracker>().playerDied(name);
Destroy(gameObject);
} }
} }
......
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