114 lines
3.2 KiB
C#
114 lines
3.2 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System;
|
|
|
|
public class UnitSelection : MonoBehaviour
|
|
{
|
|
public LayerMask unitLayerMask;
|
|
private List<Unit> selectedUnits = new List<Unit>();
|
|
public RectTransform selectionBox;
|
|
private Vector2 startPos;
|
|
|
|
//components
|
|
private Camera cam;
|
|
private Player player;
|
|
|
|
void Awake()
|
|
{
|
|
//get the components
|
|
cam = Camera.main;
|
|
player = GetComponent<Player>();
|
|
}
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
//mouse up
|
|
if(Input.GetMouseButtonUp(0))
|
|
{
|
|
ReleaseSelectionBox();
|
|
}
|
|
//mouse held down
|
|
if(Input.GetMouseButton(0))
|
|
{
|
|
UpdateSelectionBox(Input.mousePosition);
|
|
}
|
|
//mouse down
|
|
if(Input.GetMouseButtonDown(0))
|
|
{
|
|
startPos = Input.mousePosition;
|
|
ToggleSelectionVisual(false);
|
|
selectedUnits = new List<Unit>();
|
|
TrySelect(Input.mousePosition);
|
|
}
|
|
}
|
|
|
|
//called when we release the selection box
|
|
void ReleaseSelectionBox()
|
|
{
|
|
selectionBox.gameObject.SetActive(false);
|
|
Vector2 min = selectionBox.anchoredPosition - (selectionBox.sizeDelta / 2);
|
|
Vector2 max = selectionBox.anchoredPosition + (selectionBox.sizeDelta / 2);
|
|
foreach(Unit unit in player.units)
|
|
{
|
|
Vector3 screenPos = cam.WorldToScreenPoint(unit.transform.position);
|
|
if(screenPos.x > min.x && screenPos.x < max.x && screenPos.y > min.y && screenPos.y < max.y)
|
|
{
|
|
selectedUnits.Add(unit);
|
|
unit.ToggleSelectionVisual(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
//called when we are creating a selection box
|
|
void UpdateSelectionBox(Vector2 curMousePos)
|
|
{
|
|
if(!selectionBox.gameObject.activeInHierarchy)
|
|
selectionBox.gameObject.SetActive(true);
|
|
float width = curMousePos.x - startPos.x;
|
|
float height = curMousePos.y - startPos.y;
|
|
selectionBox.sizeDelta = new Vector2(MathF.Abs(width), Mathf.Abs(height));
|
|
selectionBox.anchoredPosition = startPos + new Vector2(width / 2, height / 2);
|
|
}
|
|
|
|
//called when we click on a unit
|
|
private void TrySelect(Vector2 screenPos)
|
|
{
|
|
Ray ray = cam.ScreenPointToRay(screenPos);
|
|
RaycastHit hit;
|
|
if(Physics.Raycast(ray, out hit, 100, unitLayerMask))
|
|
{
|
|
Unit unit = hit.collider.GetComponent<Unit>();
|
|
if(player.IsMyUnit(unit))
|
|
{
|
|
selectedUnits.Add(unit);
|
|
unit.ToggleSelectionVisual(true);
|
|
}
|
|
|
|
}
|
|
}
|
|
//returns whether or not we're selecting a unit or units
|
|
public bool HasUnitSelected()
|
|
{
|
|
return selectedUnits.Count > 0 ? true : false;
|
|
}
|
|
//return the selected units
|
|
public Unit[] GetSelectedUnits()
|
|
{
|
|
return selectedUnits.ToArray();
|
|
}
|
|
|
|
void ToggleSelectionVisual(bool selected)
|
|
{
|
|
foreach(Unit unit in selectedUnits)
|
|
{
|
|
unit.ToggleSelectionVisual(selected);
|
|
}
|
|
}
|
|
}
|