Files
Infratest/Assets/Code/Example/Platformer/Entity/Enemies/Bee.cs
T
ThomasCharester 60c28f54f2 Миксер
Почини интерфейс
2026-03-23 23:59:32 +03:00

100 lines
3.2 KiB
C#

using System.Collections.Generic;
using Code.Infrastructure.Audio;
using Cysharp.Threading.Tasks;
using PrimeTween;
using Reflex.Attributes;
using UnityEngine;
namespace Code.Example.Platformer.Entity.Enemies
{
public class Bee : Entity
{
[SerializeField] private List<Transform> partolPoints;
[SerializeField] private bool onPatrol = true;
private int _currentPatrolPoint;
private Vector3 _velocity = new Vector3(1,1,1);
[SerializeField] private Transform _currentTarget;
private Tween _patrolAction;
private float _attackThreshold = 0.5f;
[Inject] private IAudioService _audioService;
[SerializeField] private EntityHealth health;
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()
{
if (onPatrol && !_patrolAction.isAlive)
{
_patrolAction = Tween
.PositionAtSpeed(transform,partolPoints[_currentPatrolPoint].position, 0.5f)
.OnComplete(target: this, target => target.OnControlPointReached());
}
else if(_currentTarget)
{
TryAttack();
var direction = _currentTarget.position - transform.position;
direction.Normalize();
var targetPosition = _currentTarget.position + direction * -2f;
UnityEngine.Debug.DrawLine(transform.position, targetPosition, Color.red);
transform.position = Vector3.SmoothDamp(transform.position,_currentTarget.position,ref _velocity, 1f);
// Tween
// .PositionAtSpeed(transform, _currentTarget.position, 1f).OnUpdate(transform, );
// // .OnComplete(OnControlPointReached);
}
}
private void TryAttack()
{
if ((transform.position - _currentTarget.position).magnitude < _attackThreshold) _currentTarget.GetComponent<EntityHealth>().GiveDamage(1);
}
private void OnControlPointReached()
{
if (++_currentPatrolPoint >= partolPoints.Count) _currentPatrolPoint = 0;
}
public void GotTarget(Transform other)
{
onPatrol = false;
_currentTarget = other;
_patrolAction.Stop();
}
public void LostTarget()
{
onPatrol = true;
_currentTarget = null;
}
}
}