private void Update_Clients(Protocol.BaseProtocol proto)
    {
        Protocol.LobbyClientList clientList = proto.AsType <Protocol.LobbyClientList>();

        ClientCount = clientList.client_ids.Length;
        name_text.SetText(string.Join("\n", clientList.client_nicknames));
    }
    /// <summary>
    /// Updates the objects position when the server says so. :D
    /// </summary>
    /// <param name="proto"></param>
    public void UpdateServerObject(Protocol.BaseProtocol proto)
    {
        Protocol.ServerObject obj = proto.AsType <Protocol.ServerObject>();

        if (obj.Type != serverObjectType || obj.object_id != serverObjectId)
        {
            return;
        }

        print(obj.Action + "==" + Protocol.ServerObject.ObjectAction.Destroy + " && " + obj.Type + "==" + serverObjectType + " && " + obj.object_id + "==" + serverObjectId);
        if (obj.Action == Protocol.ServerObject.ObjectAction.Destroy)
        {
            Destroy(gameObject);
        }

        if (obj.Action != Protocol.ServerObject.ObjectAction.Defualt)
        {
            return;
        }

        // stop the nav agent if present.
        ClientAgent agent = GetComponent <ClientAgent>();

        if (agent != null && !agent.FindingPath && agent.Naving)
        {
            print("Stop Agent.");
            agent?.CancelAction();
        }

        print("TAG " + gameObject.tag + " transform.position " + transform.position);

        // update the position of the object.
        transform.position    = lastPosition = obj.Position;
        transform.eulerAngles = lastRotation = obj.Rotation;
    }
    private void UpdateLobbyInfo(Protocol.BaseProtocol proto)
    {
        Protocol.LobbyInfo lobbyInfo = proto.AsType <Protocol.LobbyInfo>();

        min_players = lobbyInfo.min_players;
        max_players = lobbyInfo.max_players;

        level_name_text.text    = lobbyInfo.level_name;
        level_players_text.text = string.Format("{0} of {1}", clientList.ClientCount, max_players);

        float timeTillStart = lobbyInfo.starts_in;

        if (lobbyInfo.starts_in <= 0)
        {
            level_start_in_text.text = string.Format("Requires {0} more players", (min_players - clientList.ClientCount));
            if (countdownTimer != null)
            {
                StopCoroutine(countdownTimer);
            }
        }
        else
        {
            if (countdownTimer != null)
            {
                StopCoroutine(countdownTimer);
            }

            countdownTimer = StartCoroutine(CountdownTimer(timeTillStart));
        }
    }
    private void UpdateLobbyList(Protocol.BaseProtocol proto)
    {
        print("Identity l processed................");

        Protocol.LobbyList lobbyList = proto.AsType <Protocol.LobbyList>();

        // go thorough every button (that we need or pre existing)
        // if there currently more items than needed will just switch them off
        // otherwise we add new items if neeed
        for (int i = 0; i < Mathf.Max(lobbyList.lobby_ids.Length, lobbyListGroups.Count); i++)
        {
            if (i >= lobbyListGroups.Count)
            {   // Add new buttons
                UI_LobbyButtonGroup button = Instantiate <UI_LobbyButtonGroup>(baseButtonGroup, parent);
                lobbyListGroups.Add(button);
            }
            else if (i >= lobbyList.lobby_ids.Length)
            {   // switch off existing buttons
                lobbyListGroups[i].gameObject.SetActive(false);
            }
            else
            {   // update info
                lobbyListGroups[i].gameObject.SetActive(true);
            }

            // update position and info
            if (i < lobbyList.lobby_ids.Length)
            {
                (lobbyListGroups[i].transform as RectTransform).anchoredPosition = startPosition - (buttonOffset * i);
                lobbyListGroups[i].SetData(lobbyList.lobby_ids[i], lobbyList.lobby_names[i], "Level Name", lobbyList.current_clients[i], lobbyList.max_clients[i]);
            }
        }
    }
    private void UpdateGameClients(Protocol.BaseProtocol proto)
    {
        // TODO: prevent game clients from being set again.

        Protocol.GameClientList clientList = proto.AsType <Protocol.GameClientList>();
        clientData = new Client[clientList.client_ids.Length];

        for (int i = 0; i < clientList.client_ids.Length; i++)
        {
            if (clientList.client_ids[i] == playerData.clientId)
            {
                playerData.playerId = clientList.client_player_ids[i];
            }

            clientData[i] = new Client(clientList.client_ids[i], clientList.client_nicknames[i], clientList.client_player_ids[i]);
        }
        print(Inst.clientData.Length + " :: " + clientData.Length);
        gameClientsSet?.Invoke(clientData);

        // now that the final list of active clients have arrived
        // we can now notify the server that the scene is ready to play
        // Also to be far, we should make sure that the object list has been received as well
        // but thats a task for later. for now we'll just hope its all setup correctly :)
        // TODO: ^^^
        // TODO: Uncomment... Im going to get the relix to work first. ( it will be easier to test :) )
        Protocol.ClientStatus clientStatus = new Protocol.ClientStatus()
        {
            StatusType = Protocol.ClientStatusType.GameReady,
            ok         = true
        };

        clientStatus.Send();
    }
    private void ProcessesPing(Protocol.BaseProtocol proto)
    {
        Protocol.PingTime ping = proto.AsType <Protocol.PingTime>();

        System.TimeSpan t = System.DateTime.UtcNow - new System.DateTime(1970, 1, 1);
        double          millisSinceEpoch = t.TotalMilliseconds;

        double total_time     = millisSinceEpoch - ping.client_send_time;
        double time_to_server = ping.server_receive_time - ping.client_send_time;
        double return_time    = millisSinceEpoch - ping.server_receive_time;

        pingTimeText.text = string.Format("ping: {0:f3}ms", total_time);

        /* CSV HEADERS
         * saveLoadFile.AddRow( new string[] {
         * "Client send time",
         * "ServerRecevie time",
         * "client receive time",
         * "total time",
         * "time to server",
         * "return time"
         * } );
         */
        csvFile.AddRow(new string[] {
            ping.client_send_time.ToString(),
            ping.server_receive_time.ToString(),
            millisSinceEpoch.ToString(),
            total_time.ToString(),
            time_to_server.ToString(),
            return_time.ToString()
        });

        csvFile.SaveCSV();
    }
    private void PrintServerStatus(Protocol.BaseProtocol proto)
    {
        Protocol.ServerStatus serverStatus = proto.AsType <Protocol.ServerStatus>();

        if (!serverStatus.ok)
        {
            AddMessage("Error: " + serverStatus.message, 30);
        }
    }
Exemple #8
0
    private void CollectItem(Protocol.BaseProtocol proto)
    {
        Protocol.CollectItem collectItem = proto.AsType <Protocol.CollectItem>();

        if (collectItem.player_id == playerId)
        {
            itemHold.CollectItem(collectItem.object_id);
        }
    }
    private void UpdateMessages(Protocol.BaseProtocol proto)
    {
        Protocol.Message message = proto.AsType <Protocol.Message>();

        string receivedTime = System.DateTime.Now.ToShortTimeString();

        string outStr = string.Format("{0}\n{1} | {2}: {3}", output.text, receivedTime, message.from_client_name, message.message);

        output.text = outStr;
    }
Exemple #10
0
    public void MovePlayer(Protocol.BaseProtocol proto)
    {
        Protocol.MovePlayer movePlayer = proto.AsType <Protocol.MovePlayer>();

        if (movePlayer.player_id == playerId)
        {
            clientAgent.MoveAgent(movePlayer.Position);
            currentAction = clientAgent;
        }
    }
Exemple #11
0
    public void BuildObject(Protocol.BaseProtocol protocol)
    {
        Protocol.BuildObject buildObj = protocol.AsType <Protocol.BuildObject>();

        if (buildObj.player_id != playerId)
        {
            return;
        }

        build.BuildObject(buildObj.obj_id);
    }
Exemple #12
0
    private void UpdateRelicCount(Protocol.BaseProtocol proto)
    {
        Protocol.RelicCount relicCount = proto.AsType <Protocol.RelicCount>();

        // make sure that the count belongs to the player
        if (relicCount.player_id != GameCtrl.Inst.playerData.playerId)
        {
            return;
        }

        relicCountText.text = string.Format("{0} of {1}", relicCount.count.ToString(), 4);
    }
Exemple #13
0
    public void LookAt(Protocol.BaseProtocol protocol)
    {
        Protocol.LookAtPosition lookAtPos = protocol.AsType <Protocol.LookAtPosition>();

        if (lookAtPos.player_id != playerId)
        {
            return;
        }

        lookAtPosition.LookAtPosition(lookAtPos.Position);
        currentAction = lookAtPosition;
    }
    private void IdentityStatus(Protocol.BaseProtocol prto)
    {
        Protocol.IdentityStatus status = prto.AsType <Protocol.IdentityStatus>();

        if (!status.ok)
        {
            Debug.LogError("Error Bad Identity status");
            return;
        }

        playerData.clientId = status.client_id;
        playerData.reg_key  = status.reg_key;

        UIAct_MessageList.MessageList.AddMessage(string.Format("Wellcom, {0} ({1} :: {2})", playerData.nickname, playerData.clientId, playerData.reg_key), 20);
    }
    private void IdentityRequest(Protocol.BaseProtocol prto)
    {
        Protocol.IdentityRequest request = prto.AsType <Protocol.IdentityRequest>();

        if (string.IsNullOrWhiteSpace(playerData.nickname))      // only set a nickname if we dont have one already.
        {
            playerData.nickname = request.nickname;
        }
        else                                                     // otherwise return our current nickname
        {
            request.nickname = playerData.nickname;
        }

        request.client_id = playerData.clientId;
        request.reg_key   = playerData.reg_key;

        ClientSocket.ActiveSocket.SendMsg(request);
    }
    private void UpdateGameLoop(Protocol.BaseProtocol proto)
    {
        Protocol.GameLoop gameLoop = proto.AsType <Protocol.GameLoop>();

        currentGameLoopState = gameLoop.Action;

        if (gameLoop.Action == Protocol.GameLoop.Actions.Change)
        {
            for (int i = 0; i < clientData.Length; i++)
            {
                if (clientData[i].playerId == gameLoop.player_id)
                {
                    currentClientId = i;
                }
            }
        }

        gameLoopEvent?.Invoke(gameLoop.Action, gameLoop.t);
    }
Exemple #17
0
    public void SetHealth(Protocol.BaseProtocol proto)
    {
        Protocol.ApplyDamage damage = proto.AsType <Protocol.ApplyDamage>();

        if (damage.player_id != playerId)
        {
            return;
        }

        if (damage.kill)
        {
            health.Kill();
            GameCtrl.Inst.KillPlayer(playerId);
        }
        else            // let's do some damage boys....
        {
            health.SetHealth(damage.health);
        }
    }
    private void SpawnObject(Protocol.BaseProtocol proto)
    {
        Protocol.ServerObject servObj = proto.AsType <Protocol.ServerObject>();

        if (servObj.Action != Protocol.ServerObject.ObjectAction.Add)
        {
            return;
        }

        // check that the object does not not exist.
        SelectedObject = null;
        ServerObject.InvokeSelectObject(servObj.object_id, this);

        if (SelectedObject != null)
        {
            Debug.LogError("Unable to spwan object, already exist :( ");
            return;
        }

        // find the object to spawn and spawn it :D
        ServerObject spawnObj = null;

        switch (servObj.Type)
        {
        case Protocol.ServerObject.ObjectType.Block:
            spawnObj = blockPrefab;
            break;

        default:
            Debug.LogErrorFormat("Unable to spwan server object of type {0}", servObj.Type);
            return;
        }

        if (spawnObj != null)
        {
            Quaternion quat = Quaternion.identity;
            quat.eulerAngles = servObj.Rotation;

            ServerObject so = Instantiate(spawnObj, servObj.Position, quat);
            so.serverObjectId = servObj.object_id;
        }
    }
Exemple #19
0
    private void Action(Protocol.BaseProtocol proto)
    {
        Protocol.GameAction gameAct = proto.AsType <Protocol.GameAction>();

        if (gameAct.player_id != playerId)
        {
            return;
        }

        switch (gameAct.Action)
        {
        case Protocol.GameAction.Actions.DropItem:
            itemHold.DropItem();
            break;

        case Protocol.GameAction.Actions.LaunchProjectile:
            projectileLauncher.LaunchProjectile();
            break;
        }
    }
    private void ToggleUI(Protocol.BaseProtocol proto)
    {
        Protocol.GameAction action = proto.AsType <Protocol.GameAction>();

        if (action.Action != Protocol.GameAction.Actions.EndGame)
        {
            return;
        }

        string winnerNickname = GameCtrl.Inst.GetPlayerIdNickname(action.player_id);
        string wlText         = GameCtrl.Inst.playerData.playerId == action.player_id ? "Winner!" : "Losser :(";

        wlText = string.Format("You {0}", wlText);

        winnerLosserText.text   = wlText;
        winnerNicknameText.text = winnerNickname;

        mainGameCanvas.SetActive(false);
        gameOverCanvas.SetActive(true);
    }
    private void ChangeSceneRequest(Protocol.BaseProtocol proto)
    {
        Protocol.SceneRequest sceneRequest = proto.AsType <Protocol.SceneRequest>();

        SceneManager.LoadScene(sceneRequest.scene_name, LoadSceneMode.Single);
    }