60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using Code.Infrastructure.Configs;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Example.Platformer.Entity.Enemies
|
|
{
|
|
public class Boar : Entity
|
|
{
|
|
[SerializeField] private Vector2 groundLeft;
|
|
[SerializeField] private Vector2 groundRight;
|
|
private bool _directionRight;
|
|
|
|
private float _acceleration = 3f;
|
|
|
|
protected override void Move()
|
|
{
|
|
if (_target)
|
|
{
|
|
_directionRight = ((_target.position - transform.position)).x > float.Epsilon;
|
|
UnityEngine.Debug.DrawLine(transform.position, (transform.position + (_target.position - transform.position)), Color.salmon);
|
|
|
|
CheckupDirection();
|
|
|
|
rigidbody2D.linearVelocityX = _directionRight ? 0.5f : -0.5f;
|
|
rigidbody2D.linearVelocityX *= _acceleration;
|
|
}
|
|
else
|
|
{
|
|
CheckupDirection();
|
|
|
|
rigidbody2D.linearVelocityX = _directionRight ? 0.5f : -0.5f;
|
|
}
|
|
}
|
|
|
|
protected override void HandleFlipX() => spriteRenderer.flipX = rigidbody2D.linearVelocity.x > -0.01f;
|
|
|
|
private void CheckupDirection()
|
|
{
|
|
if (_directionRight)
|
|
{
|
|
if (!Physics2D.OverlapPoint((Vector2)transform.position + groundRight,
|
|
LayerMasks.Masks[LayerMaskType.PlatformerGround]))
|
|
_directionRight = false;
|
|
}
|
|
else if (!Physics2D.OverlapPoint((Vector2)transform.position + groundLeft,
|
|
LayerMasks.Masks[LayerMaskType.PlatformerGround]))
|
|
_directionRight = true;
|
|
}
|
|
|
|
private Transform _target;
|
|
|
|
public void GotTarget(Transform target)
|
|
{
|
|
_target = target;
|
|
}
|
|
public void LostTarget()
|
|
{
|
|
_target = null;
|
|
}
|
|
}
|
|
} |