103 lines
2.9 KiB
C#
103 lines
2.9 KiB
C#
using SepCore.Components;
|
|
using SepCore.Definition;
|
|
using SepCore.Event;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.Entity
|
|
{
|
|
public abstract class EnemyBase : TargetableObject
|
|
{
|
|
public ChildrenMap ChildrenMap;
|
|
|
|
protected Transform _target;
|
|
|
|
public EnemyData EnemyData { get; private set; }
|
|
|
|
protected EnemyMovementComponent _movementComponent { get; private set; }
|
|
|
|
protected override TargetableObjectData _targetableObjectData => EnemyData;
|
|
|
|
public virtual float AttackRange => EnemyData.AttackRange;
|
|
|
|
public float AttackCooldown => EnemyData.AttackCooldown;
|
|
|
|
public int AttackDamage => EnemyData.AttackDamage;
|
|
|
|
public override ImpactData GetImpactData()
|
|
{
|
|
return new ImpactData(EnemyData.Camp, EnemyData.AttackDamage);
|
|
}
|
|
|
|
public virtual void SetTarget(Transform target) => _target = target;
|
|
|
|
#region Lifecycle
|
|
|
|
protected override void OnInit(object userData)
|
|
{
|
|
base.OnInit(userData);
|
|
|
|
ChildrenMap = GetComponent<ChildrenMap>();
|
|
_movementComponent = ChildrenMap.Get("Movement").GetComponent<EnemyMovementComponent>();
|
|
_healthComponent = ChildrenMap.Get("Health").GetComponent<HealthComponent>();
|
|
}
|
|
|
|
protected override void OnShow(object userData)
|
|
{
|
|
base.OnShow(userData);
|
|
|
|
if (userData is EnemyData enemyData)
|
|
{
|
|
EnemyData = enemyData;
|
|
_healthComponent.OnInit(enemyData.MaxHealthBase);
|
|
_movementComponent.OnInit(enemyData.SpeedBase, true);
|
|
_movementComponent.SetMove(true);
|
|
|
|
StartAttackCooldown(enemyData.AttackCooldown);
|
|
CachedTransform.position = enemyData.Position;
|
|
}
|
|
else
|
|
{
|
|
Log.Error("Invalid enemy data type: {0}", userData?.GetType());
|
|
}
|
|
}
|
|
|
|
protected override void OnHide(bool isShutdown, object userData)
|
|
{
|
|
_movementComponent.OnReset();
|
|
_healthComponent.OnReset();
|
|
CancelAttackCooldown();
|
|
_target = null;
|
|
|
|
base.OnHide(isShutdown, userData);
|
|
}
|
|
|
|
protected override void OnDead(EntityBase attacker)
|
|
{
|
|
if (Random.value < EnemyData.DropPercent)
|
|
{
|
|
GameEntry.Event.Fire(this, DropSpawnRequestedEventArgs.Create(CachedTransform.position));
|
|
}
|
|
|
|
base.OnDead(attacker);
|
|
}
|
|
|
|
#endregion
|
|
|
|
protected Vector3 GetTargetDirection()
|
|
{
|
|
if (_target == null)
|
|
{
|
|
return Vector3.zero;
|
|
}
|
|
|
|
return new Vector3(
|
|
_target.position.x - CachedTransform.position.x,
|
|
0f,
|
|
_target.position.z - CachedTransform.position.z
|
|
).normalized;
|
|
}
|
|
}
|
|
}
|
|
|