Ejemplo n.º 1
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
 }