2022-02-21 15:53:23 +03:00
|
|
|
using Movement;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace AI
|
|
|
|
{
|
|
|
|
public class BasicPatrollingEnemyAI : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField] protected bool isMovingRight = false;
|
|
|
|
|
|
|
|
protected IMovement movement = null;
|
|
|
|
|
2022-02-21 16:27:23 +03:00
|
|
|
protected CollissionChecker leftWallChecker = null;
|
|
|
|
protected CollissionChecker rightWallChecker = null;
|
|
|
|
protected CollissionChecker leftGroundChecker = null;
|
|
|
|
protected CollissionChecker rightGroundChecker = null;
|
|
|
|
|
2022-02-21 15:53:23 +03:00
|
|
|
|
2022-02-21 16:27:23 +03:00
|
|
|
// If moving Right, and either there's no more ground to move into or there is a wall at the Right side of the enemy OR
|
|
|
|
// If moving Left, and either there's no more ground to move into or there is a wall at the Left side of the enemy
|
|
|
|
// We should change directions
|
|
|
|
private bool ShouldChangeDirection
|
|
|
|
=> (isMovingRight && (rightWallChecker.IsCollided || !rightGroundChecker.IsCollided)) ||
|
|
|
|
(!isMovingRight && (leftWallChecker.IsCollided || !leftGroundChecker.IsCollided));
|
2022-02-21 15:53:23 +03:00
|
|
|
|
|
|
|
protected virtual void Awake()
|
|
|
|
{
|
|
|
|
movement = gameObject.AddComponent<EnemyMovement>();
|
2022-02-21 16:27:23 +03:00
|
|
|
leftWallChecker = GetCollissionCheckerOnChild("Collission Checkers/Left Wall");
|
|
|
|
rightWallChecker = GetCollissionCheckerOnChild("Collission Checkers/Right Wall");
|
|
|
|
leftGroundChecker = GetCollissionCheckerOnChild("Collission Checkers/Left Ground");
|
|
|
|
rightGroundChecker = GetCollissionCheckerOnChild("Collission Checkers/Right Ground");
|
2022-02-21 15:53:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void FixedUpdate()
|
|
|
|
{
|
2022-02-21 16:27:23 +03:00
|
|
|
if (ShouldChangeDirection)
|
2022-02-21 15:53:23 +03:00
|
|
|
isMovingRight = !isMovingRight;
|
|
|
|
|
|
|
|
movement.Move(isMovingRight ? 1f : -1f);
|
|
|
|
}
|
|
|
|
|
2022-02-21 16:27:23 +03:00
|
|
|
protected CollissionChecker GetCollissionCheckerOnChild(string childName)
|
|
|
|
=> transform.Find(childName).GetComponent<CollissionChecker>();
|
2022-02-21 15:53:23 +03:00
|
|
|
}
|
|
|
|
}
|