2026-03-19 22:32:52 +00:00
|
|
|
using System.Collections;
|
|
|
|
|
using Unity.VisualScripting;
|
2026-03-19 11:57:23 +00:00
|
|
|
using UnityEngine;
|
2026-03-19 17:31:51 +00:00
|
|
|
using UnityEngine.AI;
|
2026-03-19 22:32:52 +00:00
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
|
|
public enum UnitState
|
|
|
|
|
{
|
|
|
|
|
Idle,
|
|
|
|
|
Move,
|
|
|
|
|
MoveToResource,
|
|
|
|
|
Gather
|
|
|
|
|
}
|
2026-03-19 11:57:23 +00:00
|
|
|
|
|
|
|
|
public class Unit : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[Header("Components")]
|
|
|
|
|
public GameObject selectionVisual;
|
2026-03-19 17:31:51 +00:00
|
|
|
private NavMeshAgent navAgent;
|
|
|
|
|
private SelectionMarker selectionMarker;
|
2026-03-19 22:32:52 +00:00
|
|
|
[Header("Stats")]
|
|
|
|
|
public UnitState state;
|
|
|
|
|
public int gatherAmount;
|
|
|
|
|
public float gatherRate;
|
|
|
|
|
private float lastGatherTime;
|
|
|
|
|
private ResourceSource resource;
|
|
|
|
|
//events
|
|
|
|
|
public class StateChangeEvent : UnitEvent<UnitState> { }
|
|
|
|
|
public StateChangeEvent onStateChange;
|
|
|
|
|
public Player player;
|
2026-03-19 17:31:51 +00:00
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
//get the components
|
|
|
|
|
navAgent = GetComponent<NavMeshAgent>();
|
|
|
|
|
}
|
2026-03-19 11:57:23 +00:00
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2026-03-19 17:31:51 +00:00
|
|
|
|
2026-03-19 11:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2026-03-19 17:31:51 +00:00
|
|
|
if (selectionMarker != null && !navAgent.pathPending && navAgent.remainingDistance <= navAgent.stoppingDistance)
|
|
|
|
|
{
|
|
|
|
|
selectionMarker.DestroySelectionMarker();
|
|
|
|
|
selectionMarker = null;
|
|
|
|
|
}
|
2026-03-19 22:32:52 +00:00
|
|
|
switch(state)
|
|
|
|
|
{
|
|
|
|
|
case UnitState.Move
|
|
|
|
|
{
|
|
|
|
|
MoveUpdate();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case UnitState.MoveToResource
|
|
|
|
|
{
|
|
|
|
|
MoveToResourceUpdate();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case UnitState.Gather
|
|
|
|
|
{
|
|
|
|
|
GatherUpdate();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-19 11:57:23 +00:00
|
|
|
}
|
|
|
|
|
public void ToggleSelectionVisual(bool selected)
|
|
|
|
|
{
|
|
|
|
|
selectionVisual.SetActive(selected);
|
|
|
|
|
}
|
2026-03-19 17:31:51 +00:00
|
|
|
public void MoveToPosition(Vector3 pos, SelectionMarker marker = null)
|
|
|
|
|
{
|
|
|
|
|
if (selectionMarker != null)
|
|
|
|
|
{
|
|
|
|
|
selectionMarker.DestroySelectionMarker();
|
|
|
|
|
selectionMarker = null;
|
|
|
|
|
}
|
|
|
|
|
navAgent.isStopped = false;
|
|
|
|
|
navAgent.SetDestination(pos);
|
|
|
|
|
selectionMarker = marker;
|
|
|
|
|
}
|
2026-03-19 22:32:52 +00:00
|
|
|
//move to a resource and begin to gather it
|
|
|
|
|
public void GatherResource(ResourceSource resource, Vector3 pos)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
void SetState(UnitState toState)
|
|
|
|
|
{
|
|
|
|
|
state = toState;
|
|
|
|
|
//calling the event
|
|
|
|
|
if(onStateChange != null)
|
|
|
|
|
onStateChange.Invoke(state);
|
|
|
|
|
if(toState == UnitState.Idle)
|
|
|
|
|
{
|
|
|
|
|
navAgent.isStopped = true;
|
|
|
|
|
navAgent.ResetPath();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void MoveUpdate()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
void MoveToResourceUpdate()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
void GatherUpdate()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2026-03-19 11:57:23 +00:00
|
|
|
}
|