void SpawnLocalObject(BinaryReader reader)
    {
        GameObject       obj  = Instantiate(_networkingPrefabs[reader.ReadInt32()], new Vector3(0, 0.5f, 0), Quaternion.identity);
        NetworkingObject nObj = obj.GetComponent <NetworkingObject>();

        nObj.SetObjectID(reader.ReadInt32());
        nObj.SetPlayerNetworkingID(reader.ReadInt32());
        _objects.Add(obj);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Spawns the ball for the game
    /// </summary>
    private void SpawnBall()
    {
        GameObject       obj = Instantiate(_prefabs[0], new Vector3(0f, 0f, 0f), Quaternion.identity);
        NetworkingObject networkingObject = obj.GetComponent <NetworkingObject>();

        networkingObject.SetNetworkID(_networkID++);
        networkingObject.SetPlayerNetworkID(-1);

        _networkingObjects.Add(networkingObject);
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Creates a player object
    /// </summary>
    /// <param name="side"> The side of the player object determined by -1 or 1</param>
    /// <param name="clientID"> The client ID that this object belongs to</param>
    private void SpawnPlayerObject(int side, int clientID)
    {
        //create a new object
        GameObject       obj = Instantiate(_prefabs[1], new Vector3(10.5f * side, 0f, 0f), Quaternion.identity);
        NetworkingObject networkingObject = obj.GetComponent <NetworkingObject>();

        networkingObject.SetNetworkID(_networkID++);
        networkingObject.SetPlayerNetworkID(clientID);

        //add the object to the list of objects
        _networkingObjects.Add(networkingObject);
    }
Ejemplo n.º 4
0
    public void SpawnObject(GameObject prefab, int prefabIndex, int newNetworkingID, List <GameObject> objectList, BinaryReader reader, List <BinaryWriter> writers)
    {
        GameObject       obj  = Object.Instantiate(prefab, new Vector3(0, 0.5f, 0), Quaternion.identity);
        NetworkingObject nObj = obj.GetComponent <NetworkingObject>();

        nObj.SetObjectID(newNetworkingID);
        nObj.SetPlayerNetworkingID(reader.ReadInt32());
        objectList.Add(obj);
        foreach (BinaryWriter writer in writers)
        {
            writer.Write((int)Commands.Create);
            writer.Write(prefabIndex);
            writer.Write(nObj.GetObjectID());
            writer.Write(nObj.GetPlayerNetworkingID());
        }
    }
 // Update is called once per frame
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit hit;
         Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
         {
             if (hit.collider != null)
             {
                 Debug.Log(hit.collider.gameObject.name);
                 _selectedGameObject = hit.collider.gameObject.CompareTag("Selectable") ? hit.collider.gameObject : null;
             }
             else
             {
                 _selectedGameObject = null;
             }
         }
         Debug.Log(_selectedGameObject);
     }
     if (Input.GetMouseButtonDown(1) && _selectedGameObject != null && _selectedGameObject.GetComponent <NetworkingObject>().GetPlayerNetworkingID() == _networkID)
     {
         RaycastHit hit;
         Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
         {
             if (hit.collider != null)
             {
                 int          objectID   = _selectedGameObject.GetComponent <NetworkingObject>().GetObjectID();
                 List <float> dataFloats = new List <float>();
                 //if hit unit check if unit is enemy or ally
                 if (hit.collider.gameObject.CompareTag("Selectable"))
                 {
                     NetworkingObject netObj = hit.collider.gameObject.GetComponent <NetworkingObject>();
                     if (netObj.GetPlayerNetworkingID() == _networkID)
                     {
                         dataFloats.Add(netObj.GetObjectID());
                         _sender.SendCommand(Commands.Update, UnitCommands.Follow, objectID, dataFloats, false);
                     }
                     else
                     {
                         dataFloats.Add(netObj.GetObjectID());
                         _sender.SendCommand(Commands.Update, UnitCommands.Attack, objectID, dataFloats, false);
                     }
                 }
                 //if hit the ground tell unit to go there
                 else if (hit.collider.gameObject.CompareTag("Ground"))
                 {
                     Debug.Log("I did hit the ground");
                     Vector3 pointVector = hit.point;
                     dataFloats.Add(pointVector.x);
                     dataFloats.Add(pointVector.z);
                     _sender.SendCommand(Commands.Update, UnitCommands.Walk, objectID, dataFloats, false);
                     Debug.Log(pointVector.x + " " + pointVector.z + " ");
                 }
             }
         }
     }
     if (Input.GetKeyDown(KeyCode.E))
     {
         if (_objects.Count > 0)
         {
             foreach (GameObject obj in _objects)
             {
                 NetworkingObject netObj = obj.GetComponent <NetworkingObject>();
                 if (netObj.GetPlayerNetworkingID() == _networkID)
                 {
                     _sender.SendCommand(Commands.Destroy, UnitCommands.None, netObj.GetObjectID(), null, false);
                     break;
                 }
             }
         }
     }
     if (Input.GetKeyDown(KeyCode.A))
     {
         _sender.SendCommand(Commands.Create, UnitCommands.None, 0, null, true);
     }
     if (Input.GetKeyDown(KeyCode.D))
     {
         _sender.SendCommand(Commands.Create, UnitCommands.None, 1, null, true);
     }
     //do update stuff
     //TODO: tell the client to send input as commands to the server
 }