2023-08-09 07:47:55 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
|
|
|
|
|
|
|
public class Boat : MonoBehaviour
|
|
|
|
{
|
|
|
|
private NavMeshAgent agent;
|
2023-08-10 06:39:59 +00:00
|
|
|
private LineRenderer lineRenderer;
|
|
|
|
|
|
|
|
private GameObject spot;
|
|
|
|
|
|
|
|
private Coroutine draw;
|
2023-08-11 16:21:26 +00:00
|
|
|
|
2023-08-11 17:50:36 +00:00
|
|
|
public Vector3 target { get; set; }
|
|
|
|
|
2023-08-11 16:21:26 +00:00
|
|
|
public delegate void LandedEventHandler();
|
|
|
|
public event LandedEventHandler OnLanded;
|
2023-08-09 07:47:55 +00:00
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
agent = GetComponent<NavMeshAgent>();
|
2023-08-10 06:39:59 +00:00
|
|
|
lineRenderer = GetComponent<LineRenderer>();
|
|
|
|
lineRenderer.startWidth = 0.1f;
|
|
|
|
lineRenderer.endWidth = 0.1f;
|
|
|
|
lineRenderer.material.color = Color.yellow;
|
|
|
|
lineRenderer.enabled = false;
|
|
|
|
|
|
|
|
spot = Instantiate(DataManager.Inst.mouseSpot);
|
2023-08-09 07:47:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 17:50:36 +00:00
|
|
|
private void Start()
|
2023-08-09 07:47:55 +00:00
|
|
|
{
|
2023-08-11 17:50:36 +00:00
|
|
|
agent.SetDestination(target);
|
|
|
|
|
|
|
|
spot.gameObject.SetActive(true);
|
|
|
|
spot.transform.position = target;
|
2023-08-09 07:47:55 +00:00
|
|
|
|
2023-08-11 17:50:36 +00:00
|
|
|
if (draw != null) StopCoroutine(draw);
|
|
|
|
draw = StartCoroutine(DrawPath());
|
|
|
|
}
|
2023-08-10 06:39:59 +00:00
|
|
|
|
2023-08-11 17:50:36 +00:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
if (!(agent.remainingDistance < 0.1f && !agent.pathPending)) return;
|
|
|
|
spot.gameObject.SetActive(false);
|
2023-08-10 06:39:59 +00:00
|
|
|
|
2023-08-11 17:50:36 +00:00
|
|
|
lineRenderer.enabled = false;
|
|
|
|
if (draw != null) StopCoroutine(draw);
|
2023-08-11 16:21:26 +00:00
|
|
|
|
2023-08-11 17:50:36 +00:00
|
|
|
OnLanded?.Invoke();
|
2023-08-10 06:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator DrawPath()
|
|
|
|
{
|
|
|
|
lineRenderer.enabled = true;
|
|
|
|
yield return null;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
var count = agent.path.corners.Length;
|
|
|
|
lineRenderer.positionCount = count;
|
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
lineRenderer.SetPosition(i, agent.path.corners[i] + new Vector3(0,1,0));
|
2023-08-09 07:47:55 +00:00
|
|
|
}
|
2023-08-10 06:39:59 +00:00
|
|
|
yield return null;
|
2023-08-09 07:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|