Esempio n. 1
0
    private SpwanInfo convertToObject(String info)
    {
        SpwanInfo spwanInfo = new SpwanInfo();

        spwanInfo.networkPlayer = new NetworkPlayer(info);
        spwanInfo.spawn_index   = Int32.Parse(info.Split(";")[3]);
        return(spwanInfo);
    }
Esempio n. 2
0
    private void spwanPlayer(String info)
    {
        SpwanInfo     spwanInfo  = convertToObject(info);
        NetworkPlayer pininfo    = new NetworkPlayer(info);
        int           spawnIndex = spwanInfo.spawn_index;

        // If the spawn_index is -1 then we define it based on the size of the player list
        if (spawnIndex == -1)
        {
            spawnIndex = network.networkPlayers.Count;
        }

        if (GetTree().IsNetworkServer() && pininfo.net_id != 1)
        {
            // We are on the server and the requested spawn does not belong to the server
            // Iterate through the connected players
            int s_index = 1; // Will be used as spawn index

            foreach (KeyValuePair <int, NetworkPlayer> item in network.networkPlayers)
            {
                // Spawn currently iterated player within the new player's scene, skipping the new player for now
                if (item.Key != pininfo.net_id)
                {
                    RpcId(pininfo.net_id, nameof(spwanPlayer), convertToString(item.Value, s_index));
                }

                // Spawn the new player within the currently iterated player as long it's not the server
                // Because the server's list already contains the new player, that one will also get itself!
                if (item.Key != 1)
                {
                    RpcId(item.Key, nameof(spwanPlayer), convertToString(pininfo, spawnIndex));
                }

                s_index++;
            }

            // Add current bot info to new player
            foreach (SpawnBot spawnBot in spawnBots.Values)
            {
                RpcId(pininfo.net_id, nameof(addBot), spawnBot.name);
            }

            // Sync the destoryed obstacles
            foreach (String obstacle in obstaclesDestroyed)
            {
                RpcId(pininfo.net_id, nameof(destroyObstacle), obstacle);
            }
        }

        // Load the scene and create an instance
        Player client;

        client = (Player)((PackedScene)GD.Load("res://tanks/Player.tscn")).Instance();

        // Get spawn position, -1 as to utilize 0 spawn point
        Node2D nodeSpawnPoint = (Node2D)GetNode("SpawnPoints/SpawnPoint_" + getNextSpawnIndex(spawnIndex - 1));

        client.Position = nodeSpawnPoint.GlobalPosition;

        client.Name = "client_" + pininfo.net_id;
        client.setUnitName(pininfo.name);
        client.setTeamIdentifier(pininfo.team);

        // If this actor does not belong to the server, change the network master accordingly
        if (pininfo.net_id != 1)
        {
            client.SetNetworkMaster(pininfo.net_id);
        }

        AddChild(client);

        // If this actor is the current client controlled, add camera and attach HUD
        if (pininfo.net_id == network.gamestateNetworkPlayer.net_id)
        {
            Camera2D camera2D = new Camera2D();
            camera2D.Name = "Camera2D";
            client.AddChild(camera2D);
            client.Connect("PrimaryWeaponChangeSignal", GetNode("HUD"), "_updatePrimaryWeapon");
            client.Connect("PrimaryWeaponChangeSignal", GetNode("HUD"), "_updateSecondaryWeapon");
            client.Connect("HealthChangedSignal", GetNode("HUD"), "_updateHealthBar");
            client.Connect("DefeatedAgentChangedSignal", GetNode("HUD"), "_updateDefeatedAgentBar");

            // Notify HUD about weapon 2
            client.changePrimaryWeapon(2);

            _setCameraLimit();
        }
    }