2023-08-09 07:47:55 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-08-30 15:36:22 +00:00
|
|
|
using BlueWaterProject;
|
2023-08-09 07:47:55 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.AI;
|
2023-08-17 03:42:12 +00:00
|
|
|
using UnityEngine.Animations;
|
2023-08-17 01:04:08 +00:00
|
|
|
using UnityEngine.UI;
|
2023-08-09 07:47:55 +00:00
|
|
|
|
|
|
|
public class Boat : MonoBehaviour
|
|
|
|
{
|
|
|
|
private NavMeshAgent agent;
|
2023-08-10 06:39:59 +00:00
|
|
|
private LineRenderer lineRenderer;
|
2023-08-17 01:04:08 +00:00
|
|
|
private GameObject spot; //도착지점 표시 이미지
|
|
|
|
private Coroutine draw; //경로 그리기 코루틴
|
2023-08-17 03:42:12 +00:00
|
|
|
|
|
|
|
private Transform typeCard;
|
|
|
|
public SpriteRenderer TypeCardSprite { get; private set; }
|
|
|
|
private LookAtConstraint typeCardLookAtConstraint;
|
2023-08-11 16:21:26 +00:00
|
|
|
|
2023-08-30 15:36:22 +00:00
|
|
|
public Vector3 Target { get; set; }
|
|
|
|
public string CardIndex { get; set; }
|
|
|
|
|
2023-08-31 07:38:08 +00:00
|
|
|
public delegate void LandedEventHandler(string cardIndex, Vector3 assignPos);
|
2023-08-11 16:21:26 +00:00
|
|
|
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-17 03:42:12 +00:00
|
|
|
typeCard = transform.Find("TypeCard");
|
|
|
|
TypeCardSprite = typeCard.GetComponent<SpriteRenderer>();
|
|
|
|
typeCardLookAtConstraint = typeCard.GetComponent<LookAtConstraint>();
|
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-30 15:36:22 +00:00
|
|
|
agent.SetDestination(Target);
|
2023-08-11 17:50:36 +00:00
|
|
|
|
|
|
|
spot.gameObject.SetActive(true);
|
2023-08-30 15:36:22 +00:00
|
|
|
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-17 03:42:12 +00:00
|
|
|
|
|
|
|
typeCardLookAtConstraint.SetSources(new List<ConstraintSource>
|
|
|
|
{
|
|
|
|
new ConstraintSource
|
|
|
|
{
|
|
|
|
sourceTransform = Camera.main.transform,
|
|
|
|
weight = 1
|
|
|
|
}
|
|
|
|
});
|
2023-08-11 17:50:36 +00:00
|
|
|
}
|
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-31 07:38:08 +00:00
|
|
|
OnLanded?.Invoke(CardIndex, Target);
|
2023-08-11 19:57:41 +00:00
|
|
|
Destroy(gameObject);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|