using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using TNet;
public class ZoneManager : TNBehaviour
{
//Singleton
private static ZoneManager instance = null;
//Other Managers
private GameManager gm;
//Monster Plate
[HideInInspector]
public Monster monsterSpawn;
//List of Monsters
public Zone[] zones;
[Space]
public Zone currentZone
= new Zone
();
//Singleton Check
void Awake()
{
// First we check if there are any other instances conflicting
if(instance != null && instance != this)
{
// If that is the case, we destroy other instances
Destroy(gameObject);
}
// Here we save our singleton instance
instance = this;
// Furthermore we make sure that we don't destroy between scenes (this is optional)
DontDestroyOnLoad(gameObject);
//Get Zone Manager
gm = GameObject.Find("_GameManager").GetComponent<GameManager>();
}
//Chooses a Zone then updates it to all
public void pickZone()
{
//Chooses a random Zone
int zoneNum = Random.Range (0, zones.Length);
//Apply the settings to everyone
tno.Send ("zoneApply", TNet.Target.AllSaved, zoneNum);
}
[RFC]
public void zoneApply(int zoneNumber)
{
//Debug
Debug.Log ("Zone(" + zoneNumber + ") Chosen");
//Apply Zone
currentZone = zones [zoneNumber];
//Change Background
GameObject.Find ("Canvas").transform.FindChild ("BG").GetComponent<Image> ().color = currentZone.zoneColor;
}
//Chooses a Monster then updates him to all clients
public void pickMonster()
{
//Choose random Monster
int monsterNum = Random.Range(0, currentZone.monsters.Count);
//Apply the setting to everyone
tno.Send("monsterApply", TNet.Target.AllSaved, currentZone.monsters[monsterNum]);
}
//Update Monster on all clients
[RFC]
public void monsterApply(string monsterName)
{
//Get New Monster
GameObject monsterObj = Resources.Load("Monsters/" + monsterName) as GameObject;
Monster newMonster = monsterObj.GetComponent<Monster>();
//Apply Stats
monsterSpawn.Name = newMonster.Name;
monsterSpawn.Health = newMonster.Health;
monsterSpawn.image = newMonster.image;
monsterSpawn.updateMonster();
//Debug
Debug.Log(monsterName + " has been spawned.");
}
//Channel Joined Callback
void OnNetworkJoinChannel(bool success, string message)
{
if(success)
{
StartCoroutine(serverLoad());
}
}
IEnumerator serverLoad()
{
//Get Monster Spawn
monsterSpawn = GameObject.Find("Monster").GetComponent<Monster>();
//Delay 5 seconds
float waitTime = TNManager.ping / 250f;
yield return new WaitForSeconds
(waitTime
);
//Update Monster
monsterSpawn.updateMonster();
//Debug
Debug.Log ("Current Zone Name: " + currentZone.zoneName);
Debug.Log ("Current Monster Name: " + monsterSpawn.Name);
//Check if zone needs to be chosen
if(currentZone.zoneName == "Name")
{
//Choose a zone
pickZone();
}
//Spawn First Monster if has not been spawned yet
if(monsterSpawn.Name == "Name")
{
pickMonster();
Debug.Log("Generating First Monster....");
}
}
}
[System.Serializable]
public class Zone
{
public string zoneName = "Name";
public Color zoneColor = Color.cyan;
public int monstersToKill = 5;
public System.Collections.Generic.List<string> bosses;
public System.Collections.Generic.List<string> monsters;
}