using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

public class CustomNetworkManager : NetworkManager {
    public Transform blueSpawner;
    public Transform redSpawner;
    public int playerCount = 0;

    // called when a new player is added for a client
    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    {
		Debug.Log("OnServerAddPlayer " + playerCount + ", " + playerControllerId);

        Vector3 playerSpawnPos = blueSpawner.position;
        if (playerCount == 1)
        {
            playerSpawnPos = redSpawner.position;
        }
		
		var player = (GameObject)GameObject.Instantiate(playerPrefab, playerSpawnPos, Quaternion.identity);
        NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
		
		int playerId = playerCount;
		player.GetComponent<Player>().side = playerId;
		Game.GetInstance().localPlayerSide = playerId;
		Game.GetInstance().remotePlayerSide = 1 - playerId;

		Game.GetInstance().GetGameForceBySide(playerId).Init(playerId);

		playerCount++;

		if(playerCount == 2)
		{
			Game.GetInstance().isGameStart = true;
		}
    }
}
