2022-11-07 22:18:28 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class AStarPathfindingAgent : MonoBehaviour
|
|
|
|
{
|
|
|
|
public float speed = 0.5f;
|
|
|
|
private NodeMap Map;
|
|
|
|
private Pathfinding Pathfinder;
|
|
|
|
public Node currentPosition;
|
2022-12-12 00:58:56 +01:00
|
|
|
public bool isChasing;
|
|
|
|
private Animator myAnim;
|
2023-01-02 19:31:45 +01:00
|
|
|
|
2023-01-05 01:30:01 +01:00
|
|
|
private float moveX;
|
|
|
|
private float moveY;
|
|
|
|
|
2023-01-02 19:31:45 +01:00
|
|
|
public Vector3 point;
|
2022-12-12 00:58:56 +01:00
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
public List<Node> path;
|
|
|
|
// Start is called before the first frame update
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
Map = FindObjectOfType<NodeMap>();
|
|
|
|
Pathfinder = FindObjectOfType<Pathfinding>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
currentPosition = Map.NodeFromWorldPoint(new Vector2(transform.position.x, transform.position.y));
|
|
|
|
//Debug.Log("current world position:" + currentPosition.worldPosition + " current transform position:" + transform.position);
|
|
|
|
Map.TeleportTo(this.gameObject, currentPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2023-01-05 01:30:01 +01:00
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void FindPath()
|
|
|
|
{
|
2023-01-02 19:31:45 +01:00
|
|
|
var player = FindObjectOfType<Player>().gameObject;
|
|
|
|
|
|
|
|
point = player.transform.position;
|
|
|
|
|
|
|
|
FindPoint();
|
|
|
|
}
|
|
|
|
|
2023-01-05 01:30:01 +01:00
|
|
|
public void FindPoint()
|
|
|
|
{
|
|
|
|
Pathfinder.FindPath(transform.position, point, this);
|
2022-11-07 22:18:28 +01:00
|
|
|
}
|
2023-01-05 14:19:56 +01:00
|
|
|
|
|
|
|
public void ControlAnim()
|
|
|
|
{
|
|
|
|
moveX = point.x - transform.position.x;
|
|
|
|
moveY = point.y - transform.position.y;
|
|
|
|
myAnim.SetFloat("Yinfo", moveY);
|
|
|
|
myAnim.SetFloat("Xinfo", moveX);
|
|
|
|
}
|
2022-11-07 22:18:28 +01:00
|
|
|
|
|
|
|
public IEnumerator FollowPath()
|
|
|
|
{
|
2022-12-12 00:58:56 +01:00
|
|
|
myAnim = GetComponent<Animator>();
|
|
|
|
myAnim.SetBool("isChasing", true);
|
|
|
|
myAnim.SetBool("left", false);
|
2022-11-07 22:18:28 +01:00
|
|
|
int targetIndex = 0;
|
2022-12-12 00:58:56 +01:00
|
|
|
if (path == null || path.Count == 0)
|
2022-11-07 22:18:28 +01:00
|
|
|
{
|
2022-12-12 00:58:56 +01:00
|
|
|
isChasing = false;
|
|
|
|
myAnim.SetBool("isChasing", false);
|
2022-11-07 22:18:28 +01:00
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 currentWorldPosition;
|
|
|
|
Node currentWaypoint = path[targetIndex];
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
currentWorldPosition = transform.position;
|
2022-12-16 14:03:23 +01:00
|
|
|
//if (transform.position == currentWaypoint.worldPosition)
|
2023-01-02 19:31:45 +01:00
|
|
|
if (Vector3.Distance(transform.position, currentWaypoint.worldPosition)<0.001f)
|
2022-11-07 22:18:28 +01:00
|
|
|
{
|
|
|
|
targetIndex++;
|
|
|
|
bool reachedPosition = targetIndex >= path.Count;
|
2023-01-06 03:44:07 +01:00
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
if (reachedPosition)
|
|
|
|
{
|
2023-01-05 01:30:01 +01:00
|
|
|
//myAnim.SetBool("isRunning", false);
|
2022-12-12 00:58:56 +01:00
|
|
|
myAnim.SetBool("isChasing", false);
|
2022-11-07 22:18:28 +01:00
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
currentWaypoint = path[targetIndex];
|
2023-01-05 01:30:01 +01:00
|
|
|
|
|
|
|
|
2022-11-07 22:18:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 current = transform.position;
|
|
|
|
Vector3 target = currentWaypoint.worldPosition;
|
2023-01-05 01:30:01 +01:00
|
|
|
|
2023-01-02 19:31:45 +01:00
|
|
|
if (point.x < current.x)
|
2022-12-12 00:58:56 +01:00
|
|
|
{
|
|
|
|
myAnim.SetBool("Left", true);
|
|
|
|
}
|
2023-01-02 19:31:45 +01:00
|
|
|
if (point.x >= current.x)
|
2022-12-12 00:58:56 +01:00
|
|
|
{
|
|
|
|
myAnim.SetBool("Left", false);
|
|
|
|
}
|
2023-01-05 01:30:01 +01:00
|
|
|
|
2023-01-05 14:19:56 +01:00
|
|
|
//moveX = path[targetIndex].worldPosition.x - current.x;
|
|
|
|
//moveY = path[targetIndex].worldPosition.y - current.y;
|
|
|
|
moveX = point.x - current.x;
|
|
|
|
moveY = point.y - current.y;
|
2023-01-05 01:30:01 +01:00
|
|
|
myAnim.SetFloat("Yinfo", moveY);
|
|
|
|
myAnim.SetFloat("Xinfo", moveX);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*if (point.y < current.y)
|
|
|
|
{
|
|
|
|
myAnim.SetFloat("Yinfo", -1);
|
|
|
|
if (point.x >= current.x)
|
|
|
|
{
|
|
|
|
myAnim.SetFloat("Xinfo", -1);
|
|
|
|
}
|
|
|
|
if (point.x < current.x)
|
|
|
|
{
|
|
|
|
myAnim.SetFloat("Xinfo", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (point.y >= current.y)
|
|
|
|
{
|
|
|
|
myAnim.SetFloat("Yinfo", 1);
|
|
|
|
if (point.x >= current.x)
|
|
|
|
{
|
|
|
|
myAnim.SetFloat("Xinfo", -1);
|
|
|
|
}
|
|
|
|
if (point.x < current.x)
|
|
|
|
{
|
|
|
|
myAnim.SetFloat("Xinfo", 1);
|
|
|
|
}
|
|
|
|
}*/
|
2022-11-07 22:18:28 +01:00
|
|
|
transform.position = Vector3.MoveTowards(current, target, speed * Time.deltaTime);
|
|
|
|
yield return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnDrawGizmos()
|
|
|
|
{
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
Gizmos.DrawCube(currentPosition.worldPosition, Vector3.one);
|
|
|
|
|
|
|
|
Gizmos.color = Color.black;
|
|
|
|
if (path == null) return;
|
|
|
|
foreach (var n in path)
|
|
|
|
{
|
|
|
|
Gizmos.DrawCube(n.worldPosition, Vector3.one);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|