Example #1
0
    public void SendTopScore()
    {
        string[]      splitScores = scores.Split(' ');
        StringBuilder sendBuffer  = new StringBuilder(((int)PhaNetworkingMessager.MessageType.ScoreUpdate).ToString() + " " + splitScores[0] + " " + splitScores[1] + " " + splitScores[2]);

        PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, PhaNetworkingAPI.targetIP);
    }
Example #2
0
 /// This function is called when the MonoBehaviour will be destroyed.
 void OnDestroy()
 {
     if (NetworkInitialized && singleton == this)
     {
         Debug.Log("Network is being shutdown");
         PhaNetworkingAPI.ShutDownNetwork(PhaNetworkingAPI.mainSocket);
         NetworkInitialized = false;
     }
 }
Example #3
0
    public int SendDoorUpdate(int id, Vector3 position, Quaternion rotation)
    {
        StringBuilder sendBuffer = new StringBuilder(((int)MessageType.DoorUpdate).ToString() + " " +
                                                     id.ToString() + " " +
                                                     position.x.ToString() + " " + position.y.ToString() + " " + position.z.ToString() + " " +
                                                     rotation.x.ToString() + " " + rotation.y.ToString() + " " + rotation.z.ToString() + " " + rotation.w.ToString());

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, PhaNetworkingAPI.targetIP));
    }
Example #4
0
 public int ReceiveInGameMessage()
 {
     receiveBuffer = new StringBuilder(recvBufferSize);
     PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, receiveBuffer, recvBufferSize);
     if (receiveBuffer.Length > 0)
     {
         return((int)char.GetNumericValue(receiveBuffer[0]));
     }
     return(-1);
 }
Example #5
0
    ///returns the value of damage taken.
    public int ReceiveHealthUpdate()
    {
        PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, receiveBuffer, recvBufferSize);

        if (receiveBuffer.ToString().StartsWith(((int)MessageType.HealthUpdate).ToString()))
        {
            return(int.Parse(receiveBuffer.ToString().Split()[1]));
        }
        return(0);
    }
Example #6
0
    public int SendEnemyUpdate(Vector3 position, Quaternion orientation, int index, StringBuilder givenAddress)
    {
        StringBuilder sendBuffer = new StringBuilder(
            ((int)MessageType.EnemyUpdate).ToString() + " " +
            index.ToString() + " " +
            position.x + " " + position.y + " " + position.z + " " +
            orientation.w + " " + orientation.x + " " + orientation.y + " " + orientation.z,
            recvBufferSize);

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, givenAddress));
    }
Example #7
0
    public int ReceiveLoadLevelMessage()
    {
        StringBuilder LevelReceiveBuffer = new StringBuilder(recvBufferSize);

        PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, LevelReceiveBuffer, recvBufferSize);
        if (LevelReceiveBuffer.ToString().StartsWith(((int)MessageType.LoadLevel).ToString()))
        {
            return(1);
        }
        return(0);
    }
Example #8
0
    public int SendPlayerUpdate(Vector3 position, Vector3 velocity, Quaternion orientation, StringBuilder givenAddress)
    {
        StringBuilder sendBuffer = new StringBuilder(
            ((int)MessageType.PlayerUpdate).ToString() + " " +
            position.x + " " + position.y + " " + position.z + " " +
            velocity.x + " " + velocity.y + " " + velocity.z + " " +
            orientation.w + " " + orientation.x + " " + orientation.y + " " + orientation.z,
            recvBufferSize);

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, givenAddress));
    }
Example #9
0
    //Receive information that the other player is online.
    public int ReceiveConnectionMessage()
    {
        StringBuilder recBuffer     = new StringBuilder(recvBufferSize);
        int           bytesReceived = PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, recBuffer, recvBufferSize);

        if (recBuffer.ToString().StartsWith(((int)MessageType.Connection).ToString()))
        {
            return(1);
        }
        //Debug.Log("buffer received: " + recBuffer);
        return(0);
    }
Example #10
0
    void SpawnPlayer(Scene _scene1, Scene _scene2)
    {
        if (_scene2.name != "Menu")
        {
            if (characterSelection == 0)
            {
                Ishost                 = true;
                AgentSpawned           = GameObject.Instantiate(AgentPrefab);       //Local player is agent.
                AgentHealth            = AgentSpawned.GetComponent <Health>();
                AgentRigidBody         = AgentSpawned.GetComponent <Rigidbody>();
                previousPlayerVelocity = new Vector3(AgentRigidBody.velocity.x, AgentRigidBody.velocity.y, AgentRigidBody.velocity.z);
                previousPlayerRotation = new Quaternion(AgentSpawned.transform.rotation.x, AgentSpawned.transform.rotation.y, AgentSpawned.transform.rotation.z, AgentSpawned.transform.rotation.w);

                SpawnedHacker = GameObject.Instantiate(RemoteHackerPrefab);
            }
            else if (characterSelection == 1)
            {
                Ishost       = false;
                AgentSpawned = GameObject.Instantiate(RemoteAgentPrefab);
                AgentHealth  = AgentSpawned.GetComponent <Health>();
                Vector3 startPosition = FindObjectOfType <PlayerStartLocation>().transform.position;
                AgentSpawned.transform.position = new Vector3(startPosition.x, startPosition.y, startPosition.z);
                AgentSpawned.GetComponent <NetworkedMovement>().receivedPosition = new Vector3(startPosition.x, startPosition.y, startPosition.z);
                AgentRigidBody  = AgentSpawned.GetComponent <Rigidbody>();
                AgentPrediction = AgentSpawned.GetComponent <NetworkedBehaviour>();
                SpawnedHacker   = GameObject.Instantiate(HackerPrefab);               //Local Player is Hacker. The order of instantiation here is important!
            }

            phantomManager = FindObjectOfType(typeof(PhantomManager)) as PhantomManager;
            if (phantomManager == null)
            {
                Debug.LogError("phantomManager not found. F**k.");
            }

            //skipeManager.startSkipe();
        }
        else
        {
            int bytesreceived = 0;
            do
            {
                bytesreceived = PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, receiveBuffer, recvBufferSize);
                Debug.Log("Continuing to flush the f*****g buffer, bytes: " + bytesreceived);
            } while (bytesreceived != 10035);
            //skipeManager.closeSkipe();
        }
    }
Example #11
0
    /// This function is called when the object becomes enabled and active.
    void OnEnable()
    {
        if (!NetworkInitialized && singleton == null)
        {
            singleton = this;
            PhaNetworkingAPI.mainSocket = PhaNetworkingAPI.InitializeNetworking();
            PhaNetworkManager.singleton.SendConnectionMessage(new StringBuilder("0.0.0.1"));
            Debug.Log("Networking initialized");

            SceneManager.activeSceneChanged += SpawnPlayer;
            NetworkInitialized = true;
            DontDestroyOnLoad(this);
        }
        else
        {
            Destroy(gameObject);
        }
    }
Example #12
0
    //Receive a message that tells you what character the other player has chosen.
    public int ReceiveCharacterLockMessage()
    {
        StringBuilder characterLockMessage = new StringBuilder(recvBufferSize);

        PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, characterLockMessage, recvBufferSize);
        if (characterLockMessage.ToString().StartsWith(((int)MessageType.CharacterLock).ToString()))
        {
            string[] message = characterLockMessage.ToString().Split(' ');
            Debug.Log(message);
            if (message.Length > 0)
            {
                int result;
                result = int.Parse(message[1]);
                return(result);
            }
        }
        return(-1);
    }
Example #13
0
    // Use this for initialization
    void Start()
    {
        PhaNetworkingAPI.mainSocket = PhaNetworkingAPI.InitializeNetworking();
        SendConnectionMessage(new StringBuilder("0.0.0.1"));

        //Get local IP address. Hope it doesn't change. It could. I should change this to whenever it specically tries to create a game.
        IPAddress[] ipv4Addresses = Dns.GetHostAddresses(Dns.GetHostName());
        for (int i = 0; i < ipv4Addresses.Length; i++)
        {
            if (ipv4Addresses.GetValue(i).ToString() != "127.0.0.1" && (ipv4Addresses.GetValue(i) as IPAddress).AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                PhaNetworkingAPI.hostAddress = ipv4Addresses.GetValue(i) as IPAddress;
                break;
            }
        }

        ipInput            = GetComponentInChildren <InputField>();
        selectAgentButton  = selectAgentUI.GetComponentInChildren <Button>();
        selectHackerButton = selectHackerUI.GetComponentInChildren <Button>();

        SceneManager.activeSceneChanged += SpawnPlayers;
    }
Example #14
0
    public void ReceivePlayerUpdate(Transform playerTransform)
    {
        StringBuilder PlayerReceiveBuffer = new StringBuilder(recvBufferSize);

        PhaNetworkingAPI.ReceiveFrom(PhaNetworkingAPI.mainSocket, PlayerReceiveBuffer, recvBufferSize);
        if (PlayerReceiveBuffer.ToString().StartsWith(((int)MessageType.PlayerUpdate).ToString()))
        {
            string[]   message = PlayerReceiveBuffer.ToString().Split(' ');
            Vector3    position;
            Quaternion orientation;

            position.x = float.Parse(message[1]);
            position.y = float.Parse(message[2]);
            position.z = float.Parse(message[3]);

            orientation.w = float.Parse(message[4]);
            orientation.x = float.Parse(message[5]);
            orientation.y = float.Parse(message[6]);
            orientation.z = float.Parse(message[7]);

            playerTransform.position = position;
            playerTransform.rotation = orientation;
        }
    }
Example #15
0
 /// <summary>
 /// This function is called when the MonoBehaviour will be destroyed.
 /// </summary>
 void OnDestroy()
 {
     PhaNetworkingAPI.ShutDownNetwork(PhaNetworkingAPI.mainSocket);
 }
Example #16
0
    void FixedUpdate()
    {
        Debug.Log("mainMenu state: " + mainMenuState);
        switch (mainMenuState)
        {
        case MainMenuState.Menu:

            break;

        case MainMenuState.HostWaiting:
            //Do some logic to change into CharacterSelect. Recv, check the message, check for confirmation and if yes, go to CharacterSelect.
            //Debug.Log(ReceiveConnectionMessage());
            int Result = 0;
            Result = ReceiveConnectionMessage();
            Debug.Log("Result of the receive: " + Result);
            if (Result == 1)
            {
                PhaNetworkingAPI.targetIP = new StringBuilder(recvBufferSize);
                PhaNetworkingAPI.GetRemoteAddress(PhaNetworkingAPI.mainSocket, PhaNetworkingAPI.targetIP, recvBufferSize);
                Debug.Log("rmeote address: " + PhaNetworkingAPI.targetIP);
                SendStartMessageTo();
                SetMenuState(MainMenuState.CharacterSelect);
            }
            break;

        case MainMenuState.ClientWaiting:
            //Hear back from the other player
            if (ReceiveConnectionMessage() == 1)
            {
                SetMenuState(MainMenuState.CharacterSelect);
            }
            break;

        case MainMenuState.CharacterSelect:
            if (SkipConnectionForDebug)
            {
                if (!selectAgentButton.interactable)
                {
                    playerPrefab = agentObj;
                    SetMenuState(MainMenuState.InGame);
                }
                else if (!selectHackerButton.interactable)
                {
                    playerPrefab = HackerObj;
                    SetMenuState(MainMenuState.InGame);
                }
            }
            else
            {
                int ReceiveResult = ReceiveCharacterLockMessage();
                Debug.Log("CharacterLock : " + ReceiveResult);
                if (ReceiveResult == 1)
                {
                    selectAgentButton.interactable = false;
                }
                else if (ReceiveResult == 2)
                {
                    selectHackerButton.interactable = false;
                }
                if (!selectAgentButton.interactable && !selectHackerButton.interactable)
                {
                    if (localSelection == 1)
                    {
                        playerPrefab = agentObj;
                        SetMenuState(MainMenuState.InGame);
                    }
                    else
                    {
                        playerPrefab = HackerObj;
                        SetMenuState(MainMenuState.InGame);
                    }
                    SendLoadLevelMessage(new StringBuilder(ipInput.text));
                }
            }
            break;

        case MainMenuState.InGame:
            DelegateInGameUpdates();
            break;

        default:
            break;
        }
    }
Example #17
0
    /// Update is called every frame, if the MonoBehaviour is interactable.
    void Update()
    {
        switch (mainMenuState)
        {
        case MainMenuState.Menu:
            break;

        case MainMenuState.HostWaiting:
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                SetMenuState(MainMenuState.Menu);
            }
            if (PhaNetworkManager.Singleton.ReceiveConnectionMessage() > 0)
            {
                SetMenuState(MainMenuState.CharacterSelect);
                PhaNetworkingAPI.targetIP = new StringBuilder(PhaNetworkManager.recvBufferSize);
                PhaNetworkingAPI.GetRemoteAddress(PhaNetworkingAPI.mainSocket, PhaNetworkingAPI.targetIP, PhaNetworkManager.recvBufferSize);
                Debug.Log(PhaNetworkingAPI.targetIP);
                Debug.Log("Bytes sent for connection message: " + PhaNetworkManager.Singleton.SendConnectionMessage(PhaNetworkingAPI.targetIP));
                Debug.Log("Bytes sent for connection message: " + PhaNetworkManager.Singleton.SendConnectionMessage(PhaNetworkingAPI.targetIP));
                Debug.Log("Bytes sent for connection message: " + PhaNetworkManager.Singleton.SendConnectionMessage(PhaNetworkingAPI.targetIP));
            }
            break;

        case MainMenuState.ClientWaiting:
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                SetMenuState(MainMenuState.Menu);
            }
            if (PhaNetworkManager.Singleton.ReceiveConnectionMessage() > 0)
            {
                Debug.Log("Received connection message");
                SetMenuState(MainMenuState.CharacterSelect);
                PhaNetworkManager.Singleton.SendConnectionMessage(PhaNetworkingAPI.targetIP);
                PhaNetworkManager.Singleton.SendConnectionMessage(PhaNetworkingAPI.targetIP);
                PhaNetworkManager.Singleton.SendConnectionMessage(PhaNetworkingAPI.targetIP);
            }
            break;

        case MainMenuState.CharacterSelect:
            int i = PhaNetworkManager.Singleton.ReceiveCharacterLockMessage();
            if (i > -1)
            {
                enemyPlayerSelection = i;
                if (enemyPlayerSelection == 0)
                {                //Select players properly, disable the other
                    selectAgentButton.interactable        = false;
                    selectAgentButton.targetGraphic.color = Color.black;

                    PhaNetworkManager.characterSelection = 1;
                    selectHackerButton.interactable      = false;
                    PhaNetworkManager.Singleton.SendCharacterLockMessage(PhaNetworkManager.characterSelection, PhaNetworkingAPI.targetIP);
                }
                if (enemyPlayerSelection == 1)
                {
                    selectHackerButton.interactable        = false;
                    selectHackerButton.targetGraphic.color = Color.black;

                    selectAgentButton.interactable       = false;
                    PhaNetworkManager.characterSelection = 0;
                    PhaNetworkManager.Singleton.SendCharacterLockMessage(PhaNetworkManager.characterSelection, PhaNetworkingAPI.targetIP);
                }
            }

            if (Input.GetKeyDown(KeyCode.Escape))
            {            //Return to main menu
                selectAgentButton.targetGraphic.color  = Color.white;
                selectHackerButton.targetGraphic.color = Color.white;
                SetMenuState(MainMenuState.Menu);
            }

            if (PhaNetworkManager.characterSelection != -1 && enemyPlayerSelection != -1)
            {            //Go to in game if both players have selected
                SetMenuState(MainMenuState.InGame);
            }
            break;

        case MainMenuState.InGame:
            break;

        default:
            break;
        }
    }
Example #18
0
    void SendVoiceCodeUpdate(ref CodeVoice speaker, int index)
    {
        StringBuilder doorCode = new StringBuilder(((int)MessageType.DoorUpdate).ToString() + " " + -2 + " " + index + " " + speaker.getCode());

        PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, doorCode, doorCode.Length, PhaNetworkingAPI.targetIP);
    }
Example #19
0
    //Send a message to inform the other player to begin loading the level.
    public int SendLoadLevelMessage(StringBuilder givenAddress)
    {
        StringBuilder sendBuffer = new StringBuilder(((int)MessageType.LoadLevel).ToString());

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, givenAddress));
    }
Example #20
0
    //Send a message informing the other player what character you have chosen.
    public int SendCharacterLockMessage(int choice, StringBuilder givenAddress)
    {
        StringBuilder sendBuffer = new StringBuilder(((int)MessageType.CharacterLock).ToString() + " " + choice.ToString());

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, givenAddress));
    }
Example #21
0
    public int SendHealthUpdate(int damageTaken, StringBuilder givenAddress)
    {
        StringBuilder sendBuffer = new StringBuilder(((int)MessageType.HealthUpdate).ToString() + " " + damageTaken.ToString());

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, givenAddress));
    }
Example #22
0
    void SendDoorCodeUpdate(ref GoodDoor givenDoor)
    {
        StringBuilder doorCode = new StringBuilder(((int)MessageType.DoorUpdate).ToString() + " " + -1 + " " + givenDoor.transform.parent.name + " " + givenDoor.GetCode());

        PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, doorCode, doorCode.Length, PhaNetworkingAPI.targetIP);
    }
Example #23
0
 /// <summary>
 /// This function is called when the behaviour becomes disabled or inactive.
 /// </summary>
 void OnDisable()
 {
     PhaNetworkingAPI.ShutDownNetwork(PhaNetworkingAPI.mainSocket);
 }
Example #24
0
 /// <summary>
 /// Callback sent to all game objects before the application is quit.
 /// </summary>
 void OnApplicationQuit()
 {
     PhaNetworkingAPI.ShutDownNetwork(PhaNetworkingAPI.mainSocket);
     PhaNetworkingAPI.mainSocket = (System.IntPtr) 0;
     Debug.Log(PhaNetworkingAPI.mainSocket + " from close");
 }
Example #25
0
    //Tell the other player that you are online
    public int SendConnectionMessage(StringBuilder givenAddress)
    {
        StringBuilder sendBuffer = new StringBuilder(((int)MessageType.Connection).ToString(), recvBufferSize);

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, givenAddress));
    }
Example #26
0
    public int SendConsoleMessage(string message, StringBuilder givenAddress)
    {
        StringBuilder sendBuffer = new StringBuilder(((int)PhaNetworkingMessager.MessageType.ConsoleMessage).ToString() + " " + message);

        return(PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, sendBuffer.Length, givenAddress));
    }
Example #27
0
 void SendAudioBuffer()
 {
     sendBuffer = new StringBuilder(((int)PhaNetworkingMessager.MessageType.AudioUpdate).ToString() + " " + audioBuffer);
     PhaNetworkingAPI.SendTo(PhaNetworkingAPI.mainSocket, sendBuffer, currentBufferSize + 2, PhaNetworkingAPI.targetIP);
 }