Initial Commit
This commit is contained in:
25
Assets/Scripts/Player.cs
Normal file
25
Assets/Scripts/Player.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
[Header("Units")]
|
||||
public List<Unit> units = new List<Unit>();
|
||||
// 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()
|
||||
{
|
||||
|
||||
}
|
||||
//is this my unit?
|
||||
public bool IsMyUnit(Unit unit)
|
||||
{
|
||||
return units.Contains(unit);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Player.cs.meta
Normal file
2
Assets/Scripts/Player.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ea3fcb1ceb3ba74da2ee9ceba7667f6
|
||||
17
Assets/Scripts/SelectionMarker.cs
Normal file
17
Assets/Scripts/SelectionMarker.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SelectionMarker : MonoBehaviour
|
||||
{
|
||||
|
||||
// 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/SelectionMarker.cs.meta
Normal file
2
Assets/Scripts/SelectionMarker.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a8f32ff27a2db046802396bb5ddeeb4
|
||||
22
Assets/Scripts/Unit.cs
Normal file
22
Assets/Scripts/Unit.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Unit : MonoBehaviour
|
||||
{
|
||||
[Header("Components")]
|
||||
public GameObject selectionVisual;
|
||||
// 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()
|
||||
{
|
||||
|
||||
}
|
||||
public void ToggleSelectionVisual(bool selected)
|
||||
{
|
||||
selectionVisual.SetActive(selected);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Unit.cs.meta
Normal file
2
Assets/Scripts/Unit.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f8b0e87bcbd4604bbd5586aaa650b48
|
||||
16
Assets/Scripts/UnitCommander.cs
Normal file
16
Assets/Scripts/UnitCommander.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class UnitCommander : MonoBehaviour
|
||||
{
|
||||
// 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UnitCommander.cs.meta
Normal file
2
Assets/Scripts/UnitCommander.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa6dc96105d46ae44b9f4384031dd38f
|
||||
16
Assets/Scripts/UnitMover.cs
Normal file
16
Assets/Scripts/UnitMover.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class UnitMover : MonoBehaviour
|
||||
{
|
||||
// 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
7
Assets/Scripts/UnitMover.cs.meta
Normal file
7
Assets/Scripts/UnitMover.cs.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae3471825595e3e4d9674cbde78fa3e8
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
103
Assets/Scripts/UnitSelection.cs
Normal file
103
Assets/Scripts/UnitSelection.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void ToggleSelectionVisual(bool selected)
|
||||
{
|
||||
foreach(Unit unit in selectedUnits)
|
||||
{
|
||||
unit.ToggleSelectionVisual(selected);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UnitSelection.cs.meta
Normal file
2
Assets/Scripts/UnitSelection.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 674a6053cb5f7904b8a919dd5c35807d
|
||||
Reference in New Issue
Block a user