103 lines
3.1 KiB
C#
103 lines
3.1 KiB
C#
using SepCore.Debugger;
|
|
using SepCore.Entity;
|
|
using UnityEngine;
|
|
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
namespace SepCore.Components
|
|
{
|
|
public class EnemyMovementComponent : MonoBehaviour
|
|
{
|
|
[SerializeField] private float _enemyBodyRadius;
|
|
|
|
private bool _isMoving;
|
|
private Vector3 _direction;
|
|
private bool _avoidEnemyOverlap;
|
|
private int _separationIterations;
|
|
private float _speedBase;
|
|
|
|
private EnemyBase _enemy;
|
|
|
|
public float Speed => _speedBase;
|
|
public bool AvoidEnemyOverlap => _avoidEnemyOverlap;
|
|
public float EnemyBodyRadius => _enemyBodyRadius;
|
|
public int SeparationIterations => _separationIterations;
|
|
public bool IsMoving => _isMoving;
|
|
public Vector3 Direction => _direction;
|
|
|
|
public void OnInit(float speed, bool avoidEnemyOverlap = false,
|
|
float? enemyBodyRadius = null, int? separationIterations = null)
|
|
{
|
|
_speedBase = speed;
|
|
_direction = Vector3.forward;
|
|
_avoidEnemyOverlap = avoidEnemyOverlap;
|
|
_enemyBodyRadius = enemyBodyRadius ?? GameEntry.Luban.Global.EnemyBodyRadius;
|
|
_separationIterations = separationIterations ?? GameEntry.Luban.Global.EnemySeparationIterations;
|
|
_enemy = GetComponentInParent<EnemyBase>();
|
|
|
|
SyncToSimulationWorld();
|
|
}
|
|
|
|
public void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
|
{
|
|
SyncToSimulationWorld();
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
_enemyBodyRadius = GameEntry.Luban.Global.EnemyBodyRadius;
|
|
_separationIterations = GameEntry.Luban.Global.EnemySeparationIterations;
|
|
_speedBase = 0f;
|
|
_direction = Vector3.zero;
|
|
_isMoving = false;
|
|
_avoidEnemyOverlap = false;
|
|
_enemy = null;
|
|
}
|
|
|
|
public void SetMove(bool isMoving)
|
|
{
|
|
_isMoving = isMoving;
|
|
SyncToSimulationWorld();
|
|
}
|
|
|
|
public void SetDirection(Vector3 direction)
|
|
{
|
|
_direction = direction;
|
|
SyncToSimulationWorld();
|
|
}
|
|
|
|
private void SyncToSimulationWorld()
|
|
{
|
|
using (CustomProfilerMarker.Movement_Update.Auto())
|
|
{
|
|
Vector3 direction = _direction;
|
|
direction.y = 0f;
|
|
if (direction.sqrMagnitude > Mathf.Epsilon)
|
|
{
|
|
direction.Normalize();
|
|
}
|
|
|
|
GameEntry.SimulationWorld.SyncEnemyMovementInput(_enemy.Id, _isMoving, direction, Speed,
|
|
_avoidEnemyOverlap, _enemyBodyRadius, _separationIterations);
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
if (_enemyBodyRadius <= 0f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Color previousColor = Handles.color;
|
|
Handles.color = Color.cyan;
|
|
Handles.DrawWireDisc(transform.position, Vector3.up, _enemyBodyRadius);
|
|
Handles.color = previousColor;
|
|
}
|
|
#endif
|
|
}
|
|
}
|