165 lines
4.6 KiB
C#
165 lines
4.6 KiB
C#
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;
|
|
public bool isChasing;
|
|
private Animator myAnim;
|
|
|
|
private float moveX, moveXWizard;
|
|
private float moveY, moveYWizard;
|
|
|
|
public Vector3 point;
|
|
|
|
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()
|
|
{
|
|
|
|
}
|
|
|
|
public void FindPath()
|
|
{
|
|
var player = FindObjectOfType<Player>().gameObject;
|
|
point = player.transform.position;
|
|
|
|
FindPoint();
|
|
}
|
|
|
|
public void FindPoint()
|
|
{
|
|
Pathfinder.FindPath(transform.position, point, this);
|
|
}
|
|
|
|
|
|
public void ControlAnim()
|
|
{
|
|
moveX = point.x - transform.position.x;
|
|
moveY = point.y - transform.position.y;
|
|
myAnim.SetFloat("Yinfo", moveY);
|
|
myAnim.SetFloat("Xinfo", moveX);
|
|
}
|
|
|
|
public IEnumerator FollowPath()
|
|
{
|
|
myAnim = GetComponent<Animator>();
|
|
myAnim.SetBool("isChasing", true);
|
|
myAnim.SetBool("left", false);
|
|
int targetIndex = 0;
|
|
if (path == null || path.Count == 0)
|
|
{
|
|
isChasing = false;
|
|
myAnim.SetBool("isChasing", false);
|
|
yield break;
|
|
}
|
|
|
|
Vector3 currentWorldPosition;
|
|
Node currentWaypoint = path[targetIndex];
|
|
while (true)
|
|
{
|
|
currentWorldPosition = transform.position;
|
|
//if (transform.position == currentWaypoint.worldPosition)
|
|
if (Vector3.Distance(transform.position, currentWaypoint.worldPosition)<0.001f)
|
|
{
|
|
targetIndex++;
|
|
bool reachedPosition = targetIndex >= path.Count;
|
|
|
|
if (reachedPosition)
|
|
{
|
|
//myAnim.SetBool("isRunning", false);
|
|
myAnim.SetBool("isChasing", false);
|
|
yield break;
|
|
}
|
|
currentWaypoint = path[targetIndex];
|
|
|
|
|
|
}
|
|
|
|
Vector3 current = transform.position;
|
|
Vector3 target = currentWaypoint.worldPosition;
|
|
|
|
if (point.x < current.x)
|
|
{
|
|
myAnim.SetBool("Left", true);
|
|
}
|
|
if (point.x >= current.x)
|
|
{
|
|
myAnim.SetBool("Left", false);
|
|
}
|
|
|
|
moveXWizard = path[targetIndex].worldPosition.x - current.x;
|
|
moveYWizard = path[targetIndex].worldPosition.y - current.y;
|
|
myAnim.SetFloat("moveYWizard", moveYWizard);
|
|
myAnim.SetFloat("moveXWizard", moveXWizard);
|
|
moveX = point.x - current.x;
|
|
moveY = point.y - current.y;
|
|
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);
|
|
}
|
|
}*/
|
|
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);
|
|
}
|
|
}
|
|
} |