64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
using Code.Infrastructure.Audio;
|
|
using Code.Infrastructure.Configs;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Example.Platformer.Entity.Enemies
|
|
{
|
|
public class Snail : Entity
|
|
{
|
|
[SerializeField] private Vector2 groundLeft;
|
|
[SerializeField] private Vector2 groundRight;
|
|
[SerializeField] private bool _directionRight;
|
|
|
|
[SerializeField] private EntityHealth health;
|
|
[Inject] private IAudioService _audioService;
|
|
|
|
private void Start()
|
|
{
|
|
health.OnEntityDamage = OnEntityDamage;
|
|
health.OnEntityDeath = OnEntityDeath;
|
|
}
|
|
|
|
private void OnEntityDeath()
|
|
{
|
|
animator.SetBool(Died, true);
|
|
animator.SetTrigger(OnDeath);
|
|
Die().Forget();
|
|
}
|
|
|
|
private async UniTaskVoid Die()
|
|
{
|
|
await UniTask.WaitForSeconds(1);
|
|
_audioService.PlayOneShot(AudioNames.Platformer, 3 , AudioGroupType.SFX);
|
|
Destroy(gameObject);
|
|
}
|
|
private void OnEntityDamage(int obj)
|
|
{
|
|
_audioService.PlayOneShot(AudioNames.Platformer, 2, AudioGroupType.SFX);
|
|
UnityEngine.Debug.Log(gameObject.name + " damaged");
|
|
animator.SetTrigger(OnHit);
|
|
}
|
|
|
|
protected override void Move()
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
} |