예제 #1
0
    public void readMapFromFile(MapMP map)
    {
        string s = this.getSaveFileName();

        if (File.Exists(s))
        {
            NbtFile file = new NbtFile();
            file.LoadFromFile(s);

            NbtCompound rootTag = file.RootTag;
            map.readFromNbt(rootTag);
        }
    }
    public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    {
        //print("CM.OnServerAddPlayer()");
        if (this.isSinglePlayer)
        {
            GameObject playerGameObj = GameObject.Instantiate(this.playerPrefab, Vector3.zero, Quaternion.identity);
            Player     player        = playerGameObj.GetComponent <Player>();
            NetworkServer.AddPlayerForConnection(conn, playerGameObj, playerControllerId);

            // Set the Players Team.
            player.team = Team.SURVIVORS_1;
            player.RpcSetTeam(Team.SURVIVORS_1.getId());

            this.map.allPlayers.Add(new ConnectedPlayer(conn, player));

            // Hacky way to update resources for the player/client
            this.map.setResources(Team.SURVIVORS_1, this.map.getResources(Team.SURVIVORS_1));
        }
        else
        {
            MapMP mpMap = (MapMP)this.map;

            Team team   = mpMap.availibleTeams.getAvailableTeam();
            int  teamId = team.getId();

            GameObject playerGameObj = GameObject.Instantiate(this.playerPrefab, Vector3.zero, Quaternion.identity);
            Player     player        = playerGameObj.GetComponent <Player>();
            NetworkServer.AddPlayerForConnection(conn, playerGameObj, playerControllerId);

            // Set the Players Team.
            player.team = team;
            player.RpcSetTeam(teamId);

            ConnectedPlayer cp = new ConnectedPlayer(conn, player);

            if (mpMap.gameSaver.doesPlayerSaveExist(player))
            {
                mpMap.gameSaver.readPlayerFromFile(player);
            }
            else
            {
                // Setup a first time player
                mpMap.setResources(team, Constants.STARTING_RESOURCES);

                // TODO spawn starting units?
                //mpMap.mapGenerator.setupPlayerBase(team);
            }
            this.map.allPlayers.Add(cp);
        }
    }
예제 #3
0
    public void saveMapToFile(MapMP map)
    {
        #if UNITY_EDITOR
        if (this.dontWriteToDisk)
        {
            return;
        }
        #endif

        NbtCompound rootTag = new NbtCompound("map");
        map.writeToNbt(rootTag);

        NbtFile file = new NbtFile(rootTag);
        file.SaveToFile(this.getSaveFileName(), NbtCompression.None);
    }
    public override void OnServerDisconnect(NetworkConnection conn)
    {
        base.OnServerDisconnect(conn);

        if (!this.isSinglePlayer)
        {
            MapMP mpMap = (MapMP)this.map;

            //TODO use the method in Map to get the connected player.
            for (int i = this.map.allPlayers.Count - 1; i >= 0; i--)
            {
                ConnectedPlayer connectedPlayer = this.map.allPlayers[i];
                if (connectedPlayer.getConnection().connectionId == conn.connectionId)
                {
                    mpMap.allPlayers.RemoveAt(i);
                    mpMap.savePlayer(connectedPlayer);
                    mpMap.availibleTeams.freeTeam(connectedPlayer.getTeam());
                    return;
                }
            }
        }
    }