Files
TutorialRTS/Assets/Scripts/Unit.cs
2026-03-19 22:32:52 +00:00

115 lines
2.6 KiB
C#

using System.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Events;
public enum UnitState
{
Idle,
Move,
MoveToResource,
Gather
}
public class Unit : MonoBehaviour
{
[Header("Components")]
public GameObject selectionVisual;
private NavMeshAgent navAgent;
private SelectionMarker selectionMarker;
[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;
void Awake()
{
//get the components
navAgent = GetComponent<NavMeshAgent>();
}
// 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()
{
if (selectionMarker != null && !navAgent.pathPending && navAgent.remainingDistance <= navAgent.stoppingDistance)
{
selectionMarker.DestroySelectionMarker();
selectionMarker = null;
}
switch(state)
{
case UnitState.Move
{
MoveUpdate();
break;
}
case UnitState.MoveToResource
{
MoveToResourceUpdate();
break;
}
case UnitState.Gather
{
GatherUpdate();
break;
}
}
}
public void ToggleSelectionVisual(bool selected)
{
selectionVisual.SetActive(selected);
}
public void MoveToPosition(Vector3 pos, SelectionMarker marker = null)
{
if (selectionMarker != null)
{
selectionMarker.DestroySelectionMarker();
selectionMarker = null;
}
navAgent.isStopped = false;
navAgent.SetDestination(pos);
selectionMarker = marker;
}
//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()
{
}
}