proto_rts/Assets/Scripts/Unit.cs

45 lines
1.1 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
public abstract class Unit : MonoBehaviour
{
// Meters is 1 unity unit, also a tile width/height.
[Tooltip("How many meters per second")]
public float speed;
protected Transform _transform;
private Vector3 _startPosition;
private Vector3 _moveToPosition;
private float _startTime;
private float _moveDuration;
protected Action _doThingAfterMove;
protected virtual void Start()
{
_transform = transform;
}
protected void MovementRoutine()
{
_startTime += Time.deltaTime;
_transform.position = Vector3.Lerp(_startPosition, _moveToPosition, _startTime / _moveDuration);
if (_startTime / _moveDuration > 1.0f)
{
_doThingAfterMove?.Invoke();
}
}
protected abstract void CancelCurrentTask();
// TODO: replace this with pathfinding.
public virtual void MoveTo(Vector3 position)
{
CancelCurrentTask();
_startPosition = _transform.position;
_moveToPosition = position;
_moveDuration = Vector3.Distance(_startPosition, _moveToPosition) / speed;
_startTime = 0;
}
}