Ejemplo n.º 1
0
    public void SelectedUnit(SelectableUnit selectedUnit)
    {
        if (!this.IsValidCommand(this.nextCommand))
        {
            Debug.LogWarningFormat(this, "{0} wont move. cause {1} has invalid command {2}", selectedUnit.name, this.name, nextCommand);
            return;
        }
        SoldierController soldier = selectedUnit.GetComponent <SoldierController> ();

        if (soldier != null)
        {
            if (soldier.Team != this.playerId)
            {
                return;
            }

            if (this.selectedUnit != null)
            {
                this.selectedUnit.Deselect();
            }

            this.selectedUnit = selectedUnit;
            this.selectedUnit.Select();


            this.gameManager.IssueCommandTo(this.playerId, soldier, this.nextCommand);
            if (this.isNetworkedScene && messanger != null)
            {
                messanger.SendMessage(this.playerId.ToString() + "-" + soldier.id + "-" + this.nextCommand.ToString());
            }
            this.gameManager.EndTurn(this.playerId);
        }
    }
Ejemplo n.º 2
0
    public void SetIndex()
    {
        if (leader)
        {
            return;
        }
        SelectableUnit[] objs = new SelectableUnit[followers.Count];
        followers.CopyTo(objs);
        float root = Mathf.Sqrt(followers.Count + 1);
        int   rt   = (int)Mathf.Clamp(root + (root % (int)root > Mathf.Epsilon ? 1 : 0), 1f, float.MaxValue);

        switch (MouseController.Instance.formation)
        {
        case FormationState.Line:
            for (int i = 0; i < followers.Count; i++)
            {
                objs[i].SetIndex(i + 1);
            }
            return;

        case FormationState.Square:
            for (int i = 0; i < followers.Count; i++)
            {
                objs[i].SetIndex((i + 1) / rt, (i + 1) % rt);//SetDest(selectedObjs[0].dest + -selectedObjs[0].transform.forward * (i / rt) * selectedObjs[i].sepRadius + selectedObjs[0].transform.right.normalized * (i % rt) * selectedObjs[i].sepRadius);
            }
            return;

        case FormationState.Circle:
            for (int i = 0; i < followers.Count; i++)
            {
                objs[i].SetIndex(i, (int)360f / followers.Count);
            }
            return;
        }
    }
Ejemplo n.º 3
0
    void SelectWithin(Vector3 cornerA, Vector3 cornerB, bool union = false)
    {
        if (!union)
        {
            ClearSelection();
        }

        Vector3 p1 = cam.ScreenToWorldPoint(new Vector3(cornerA.x, cornerA.y, cam.nearClipPlane));
        Vector3 p2 = cam.ScreenToWorldPoint(new Vector3(cornerB.x, cornerB.y, cam.nearClipPlane));

        Vector3 centre = (p1 + p2) / 2f;
        Vector3 half   = new Vector3(Mathf.Abs(p1.x - p2.x) / 2f, Mathf.Abs(p1.y - p2.y) / 2f, 0.5f);

        RaycastHit[] hits = Physics.BoxCastAll(centre, half, cam.transform.forward, cam.transform.rotation, Mathf.Infinity, selectMask);

        for (int i = 0; i < hits.Length; i++)
        {
            SelectableUnit unit = hits[i].collider.GetComponent <SelectableUnit> ();
            if (unit)
            {
                unit.isSelected = true;
            }

            selecteds.Add(hits[i].collider.gameObject);
        }
    }
Ejemplo n.º 4
0
 public void OnBeginDrag(PointerEventData eventData)
 {
     if (!Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightControl))
     {
         SelectableUnit.DeselectAll(new BaseEventData(EventSystem.current));
     }
     startPosition = eventData.position;
 }
Ejemplo n.º 5
0
    public bool IsValidTarget(SelectableUnit other)
    {
        if (!ownerData.IsHostile(other.ownerData))
        {
            return(false);
        }
        return(true);

        //return ownerData.IsHostile(other.ownerData);
    }
Ejemplo n.º 6
0
 public void CheckFirstUnit()
 {
     if (firstUnit == null || firstUnit.GetComponent <RtsObject>().unitType != SelectionManager.main.FirstUnit().GetComponent <RtsObject>().unitType || isBuildingPanelOpen == true)
     {
         firstUnit = SelectionManager.main.FirstUnit();
         ClearUnitPanel();
         UnitCommands();
         isBuildingPanelOpen = false;
     }
 }
    public void RemoveUnitFromSelection(SelectableUnit unit)
    {
        unit.selected = false;
        var movement = unit.GetComponent <UnitMovement>();

        if (movement != null)
        {
            moveableSelectedUnits.Remove(movement);
        }
        selectedUnits.Remove(unit);
    }
    public void AddUnitToSelection(SelectableUnit unit)
    {
        unit.selected = true;
        var movement = unit.GetComponent <UnitMovement>();

        if (movement != null)
        {
            moveableSelectedUnits.Add(movement);
        }
        selectedUnits.Add(unit);
    }
Ejemplo n.º 9
0
 void ClearSelection()
 {
     for (int i = 0; i < selecteds.Count; i++)
     {
         SelectableUnit unit = selecteds[i].GetComponent <SelectableUnit> ();
         if (unit)
         {
             unit.isSelected = false;
         }
     }
     selecteds.Clear();
 }
Ejemplo n.º 10
0
    void Die()
    {
        Debug.Log(name + " dead!");

        SelectableUnit unit = GetComponent <SelectableUnit> ();

        if (unit)
        {
            unit.Unselect();
        }

        Instantiate(splatPrefab, transform.position, Quaternion.identity);
        Destroy(gameObject, 0.2f);
    }
Ejemplo n.º 11
0
    public void SelectUnit(SelectableUnit unit)
    {
        CurrentSelection = unit;
        CurrentSelection.OnSelect();
        CurrentMode = SelectionMode.GivingOrder;

        if (SelectionCursor == null)
        {
            SelectionCursor = GameObject.Instantiate(SelectionCursorPrefab);
        }

        SelectionCursor.transform.position = CurrentSelection.transform.position;
        SelectionCursor.SetActive(true);
    }
Ejemplo n.º 12
0
    public void BeLeader()
    {
        if (leader)
        {
            leader.DeleteFollower(this);
            leader = null;
        }
        try
        {
            followers.Clear();
        }
        catch {}

        SetDirection();
    }
Ejemplo n.º 13
0
    public void OnPointerClick(PointerEventData eventData)
    {
        // click selection only with left-clicking
        if (eventData.button != PointerEventData.InputButton.Left)
        {
            return;
        }

        Ray ray = Camera.main.ScreenPointToRay(eventData.position);

        if (Physics.Raycast(ray, out RaycastHit hit))
        {
            var unit = hit.collider.GetComponent <SelectableUnit>();
            if (unit == null)
            {
                // deselect all, did not click a selectable
                if (SelectableUnit.currentlySelected.Any())
                {
                    SelectableUnit.DeselectAll(eventData);
                }

                return;
            }

            if (SelectableUnit.currentlySelected.Any())
            {
                if (unit.selectionCircle == null && Input.GetKey(KeyCode.LeftControl))
                {
                    unit.OnSelect(eventData);
                }
                else if (unit.selectionCircle != null && !Input.GetKey(KeyCode.LeftControl))
                {
                    unit.OnDeselect(eventData);
                }
                else
                {
                    SelectableUnit.DeselectAll(eventData);
                    unit.OnSelect(eventData);
                }
            }
            else
            {
                unit.OnSelect(eventData);
            }
        }
    }
Ejemplo n.º 14
0
    public void ChooseLeader(SelectableUnit l)
    {
        if (l == this)
        {
            return;
        }

        leader = l;
        l.AddFollower(this);
        foreach (SelectableUnit t in followers)
        {
            if (t == l)
            {
                continue;
            }
            t.leader = l;
            l.AddFollower(t);
        }
        l.SetIndex();
    }
Ejemplo n.º 15
0
    public void ClearSelection()
    {
        if (CurrentSelection != null)
        {
            CurrentSelection.OnDeselect();
            CurrentSelection = null;
        }
        if (SelectionCursor != null)
        {
            SelectionCursor.SetActive(false);
        }

        CurrentBuilder = null;
        if (HoverCell != null)
        {
            HoverCell.OnDeselect();
            HoverCell = null;
        }

        CurrentMode = SelectionMode.Normal;
    }
Ejemplo n.º 16
0
    void SelectOnly(Vector3 atPosition, bool union = false)
    {
        Ray        ray = cam.ScreenPointToRay(atPosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            if (!union)
            {
                ClearSelection();
            }

            SelectableUnit unit = hit.collider.GetComponent <SelectableUnit> ();
            if (unit)
            {
                unit.isSelected = true;
            }

            selecteds.Add(hit.collider.gameObject);
        }
    }
Ejemplo n.º 17
0
    private void _drawPotentialSelectableUnitsBox()
    {
        CommonMenuUtilities.drawMainMenuHeader(GUI.skin, "Select Unit");

        foreach (GameObject SelectableUnit in this._Player.PotentialSelectedUnits)
        {
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            //Just display the first squaddie info for now
            //In reality we should display the leader stuff
            Unit        UnitInfo      = SelectableUnit.GetComponent <Unit>();
            SquadMember FirstSquaddie = UnitInfo.SquadMembers[0] as SquadMember;

            GUILayout.Label(FirstSquaddie.SquadViewTexture);
            GUILayout.Space(10.0f);
            if (GUILayout.Button(FirstSquaddie.name))               //Player selected one of these guys

            {
                this.GuiAudioSource.PlayOneShot(this.Click1);
                Time.timeScale = 1;
                this._Player.PotentialSelectedUnits.RemoveRange(0, this._Player.PotentialSelectedUnits.Count);
                this._Player.SelectedUnit = SelectableUnit;

                //Unit.displaySelectSprite() should already handle deselecting previously-selected unit
                this._Player.displaySelectSprite();
                break;
            }

            GUILayout.FlexibleSpace();

            GUILayout.EndHorizontal();

            //For vertical space
            GUILayout.FlexibleSpace();
        }

        CommonMenuUtilities.endCenterBox();
    }
Ejemplo n.º 18
0
    public void OnLeftClick(RaycastHit hit)
    {
        if (hit.collider.gameObject.tag == "SelectableUnit")
        {
            if (selectedUnit != null)
            {
                // Deselect previous unit
                selectedUnit.Deselected();
            }

            // Select new unit
            selectedUnit = hit.collider.gameObject.GetComponent <SelectableUnit>();
            selectedUnit.Selected();
        }
        else
        {
            if (selectedUnit != null)
            {
                selectedUnit.Deselected();
                selectedUnit = null;
            }
        }
    }
Ejemplo n.º 19
0
    // Update is called once per frame
    void Update()
    {
        if (gameStep == GameState.SELECT_RECIPIENT)
        {
            //SELECTING UNITS
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;
                Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit);
                Vector3 point = hit.point;

                Transform hitTransform = hit.transform;
                if (hitTransform != null)
                {
                    if (hitTransform.name.Contains("Soldier"))
                    {
                        Debug.Log("hit");
                        SelectableUnit su = hitTransform.GetComponent <SelectableUnit>();
                        this.localPlayer.SelectedUnit(su);
                    }
                }
            }
        }
    }
Ejemplo n.º 20
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            MousePos1 = Camera.main.ScreenToViewportPoint(Input.mousePosition);

            RaycastHit2D rayHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, clickableLayer);

            if (rayHit.collider != null)
            {
                SelectableUnit unit = rayHit.collider.GetComponent <SelectableUnit>();
                if (Input.GetKey("left ctrl"))
                {
                    if (unit.isSelected == false)
                    {
                        selectedUnits.Add(unit);
                        unit.isSelected = true;
                        unit.UpdateSelect();
                    }
                    else
                    {
                        selectedUnits.Remove(unit);
                        unit.isSelected = false;
                        unit.UpdateSelect();
                    }
                }
                else
                {
                    if (selectedUnits.Count > 0)
                    {
                        ClearSelection();
                    }
                    selectedUnits.Add(unit);
                    unit.isSelected = true;
                    unit.UpdateSelect();
                }
            }
            else
            {
                ClearSelection();
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            MousePos2 = Camera.main.ScreenToViewportPoint(Input.mousePosition);

            if (MousePos1 != MousePos2)
            {
                SelectObjects();
            }
        }

        if (Input.GetMouseButtonDown(1))
        {
            foreach (Selectable pawn in selectedUnits)
            {
                SelectableUnit unit = pawn.GetComponent <SelectableUnit>();
                if (unit != null)
                {
                    unit.FindPath(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                }
            }
        }
    }
Ejemplo n.º 21
0
    void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            ClearSelection();
        }

        // Handle Selection with left click
        if (CurrentMode == SelectionMode.Normal || CurrentMode == SelectionMode.GivingOrder)
        {
            // First: Left mouse button to select a target unit
            if (Input.GetMouseButtonDown(0)) // Left mouse button
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                Debug.DrawRay(ray.origin, ray.direction * float.MaxValue, Color.cyan, 10f); // Debug our ray!

                RaycastHit hitInfo;
                if (Physics.Raycast(ray, out hitInfo))
                {
                    SelectableUnit leftClickedObj = hitInfo.collider.GetComponentInParent <SelectableUnit>();
                    if (leftClickedObj != null)
                    {
                        ClearSelection(); // Deselect previous object

                        if (leftClickedObj.CanBeSelected())
                        {
                            SelectUnit(leftClickedObj);
                        }
                        else
                        {
                            Debug.Log("Unit not selectable by you!");
                        }
                    }
                    else
                    {
                        Debug.Log("Clicked object not a selectable unit!");
                    }
                }
            }
        }

        if (CurrentMode == SelectionMode.GivingOrder)
        {
            // Right mouse button to give a target to our current selection
            if (Input.GetMouseButtonDown(1)) // Right mouse button
            {
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;
                if (Physics.Raycast(ray, out hitInfo))
                {
                    Selectable rightClickedObj = hitInfo.collider.GetComponentInParent <Selectable>();
                    if (rightClickedObj != null)
                    {
                        CurrentSelection.GiveTarget(rightClickedObj);
                        ClearSelection();
                    }
                }
            }
        }

        if (CurrentMode == SelectionMode.PlacingUnit)
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                SelectableCell newHoverCell = hitInfo.collider.GetComponentInParent <SelectableCell>();
                if (HoverCell != newHoverCell)
                {
                    if (HoverCell != null)
                    {
                        HoverCell.OnDeselect();
                    }
                    HoverCell = newHoverCell; // Can also be null!
                    if (HoverCell != null)
                    {
                        HoverCell.OnSelect();
                    }
                }
            }
            else // Moused over nothing
            {
                if (HoverCell != null)
                {
                    HoverCell.OnDeselect();
                    HoverCell = null;
                }
            }

            if (Input.GetMouseButton(0)) // Place with left mouse button
            {
                if (HoverCell != null)
                {
                    CurrentBuilder.SpawnUnit(HoverCell.GetCell());
                    ClearSelection();
                }
            }

            if (Input.GetMouseButton(1)) // cancel with right
            {
                ClearSelection();
            }
        }
    }
Ejemplo n.º 22
0
 public void AddFollower(SelectableUnit t)
 {
     followers.Add(t);
 }
 // Use this for initialization
 void Start()
 {
     selectable = GetComponent <SelectableUnit>();
 }
Ejemplo n.º 24
0
 public void DeleteFollower(SelectableUnit f)
 {
     followers.Remove(f);
     SetIndex();
 }