Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            Camera.main.transform.position += DIRECTION_UP * CAMERA_MOVEMENT_SPEED;
        }

        if (Input.GetKey(KeyCode.A))
        {
            Camera.main.transform.position += DIRECTION_LEFT * CAMERA_MOVEMENT_SPEED;
        }

        if (Input.GetKey(KeyCode.S))
        {
            Camera.main.transform.position += DIRECTION_DOWN * CAMERA_MOVEMENT_SPEED;
        }

        if (Input.GetKey(KeyCode.D))
        {
            Camera.main.transform.position += DIRECTION_RIGHT * CAMERA_MOVEMENT_SPEED;
        }

        bool leftMouseButtonDown = Input.GetMouseButtonDown(0);

        if (leftMouseButtonDown)
        {
            this.isDragging = true;
            this.dragStartScreenPositionX = Input.mousePosition.x;
            this.dragStartScreenPositionY = Input.mousePosition.y;
        }

        if (this.isDragging)
        {
            float selectionBoxWidth  = Input.mousePosition.x - this.dragStartScreenPositionX;
            float selectionBoxHeight = Input.mousePosition.y - this.dragStartScreenPositionY;

            if (selectionBoxWidth != 0 && selectionBoxHeight != 0 && !this.dragSelectionBox.gameObject.activeSelf)
            {
                this.dragSelectionBox.gameObject.SetActive(true);
            }

            this.dragSelectionBox.sizeDelta        = new Vector2(Mathf.Abs(selectionBoxWidth), Mathf.Abs(selectionBoxHeight));
            this.dragSelectionBox.anchoredPosition = new Vector2(this.dragStartScreenPositionX + selectionBoxWidth / 2 - Screen.width / 2, this.dragStartScreenPositionY + selectionBoxHeight / 2 - Screen.height / 2);
        }

        bool leftMouseButtonUp = Input.GetMouseButtonUp(0);

        if (leftMouseButtonUp)
        {
            this.isDragging = false;
            this.dragSelectionBox.gameObject.SetActive(false);

            if (Input.mousePosition.x != this.dragStartScreenPositionX && Input.mousePosition.y != this.dragStartScreenPositionY)
            {
                this.dragSelectionMeshFilter.mesh.vertices = new Vector3[5]
                {
                    Camera.main.transform.position,
                    this.ComputeScreenPointAtFarPlane(this.dragStartScreenPositionX, this.dragStartScreenPositionY),
                    this.ComputeScreenPointAtFarPlane(Input.mousePosition.x, this.dragStartScreenPositionY),
                    this.ComputeScreenPointAtFarPlane(this.dragStartScreenPositionX, Input.mousePosition.y),
                    this.ComputeScreenPointAtFarPlane(Input.mousePosition.x, Input.mousePosition.y)
                };
                this.dragSelectionMeshFilter.mesh.RecalculateBounds();

                MeshCollider existingCollider = this.dragSelection.GetComponent <MeshCollider>();
                if (existingCollider)
                {
                    Destroy(existingCollider);
                }

                MeshCollider dragSelectionCollider = this.dragSelection.AddComponent <MeshCollider>();
                dragSelectionCollider.convex    = true;
                dragSelectionCollider.isTrigger = true;

                this.ClearSelectedUnits();
            }
        }

        bool rightMouseButtonUp = Input.GetMouseButtonUp(1);

        if (leftMouseButtonUp || rightMouseButtonUp)
        {
            RaycastHit hit;

            // Pass QueryTriggerInteraction.Ignore to avoid hitting trigger colliders
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                GameObject hitObject = hit.collider.gameObject;

                if (leftMouseButtonUp)
                {
                    if (hitObject.CompareTag("Friendly"))
                    {
                        // clear current selection first
                        this.ClearSelectedUnits();

                        UnitBehavior unitBehavior = hitObject.GetComponent <UnitBehavior>();
                        unitBehavior.SetSelectedState(true);
                        this.selectedUnits.Add(hitObject);
                    }
                    else if (hitObject.CompareTag("Resource"))
                    {
                        Debug.Log("res");
                    }
                }
                else if (rightMouseButtonUp)
                {
                    if (hitObject.isStatic)
                    {
                        this.MoveUnitsToDestination(hit.point);
                    }
                    else
                    {
                        foreach (GameObject selectedUnit in this.selectedUnits)
                        {
                            UnitBehavior unitBehavior = selectedUnit.GetComponent <UnitBehavior>();

                            if (unitBehavior != null)
                            {
                                unitBehavior.SetTarget(hitObject);
                            }
                        }
                    }
                    //else if (hitObject.CompareTag("Enemy"))
                    //{
                    //    foreach (GameObject selectedUnit in this.selectedUnits)
                    //    {
                    //        selectedUnit.GetComponent<UnitBehavior>().SetAttackTarget(hitObject);
                    //    }
                    //}
                    //else if (hitObject.CompareTag("Resource"))
                    //{
                    //    foreach (GameObject selectedUnit in this.selectedUnits)
                    //    {
                    //        selectedUnit.GetComponent<UnitBehavior>().SetGatherTarget(hitObject);
                    //    }
                    //}
                }
            }
        }
    }