2023-10-18 07:28:15 +00:00
|
|
|
using System;
|
|
|
|
using Sirenix.OdinInspector;
|
2023-09-27 02:55:13 +00:00
|
|
|
using UnityEngine;
|
2023-10-06 05:41:16 +00:00
|
|
|
using UnityEngine.InputSystem;
|
2023-09-27 02:55:13 +00:00
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
2023-10-06 05:41:16 +00:00
|
|
|
[RequireComponent(typeof(PlayerInput))]
|
2023-10-18 07:28:15 +00:00
|
|
|
public class Player : BaseCharacter, IDamageable, IView
|
2023-09-27 02:55:13 +00:00
|
|
|
{
|
2023-10-26 07:00:08 +00:00
|
|
|
protected PlayerInput playerInput;
|
2023-10-06 05:41:16 +00:00
|
|
|
protected Vector2 movementInput;
|
2023-10-26 07:00:08 +00:00
|
|
|
|
|
|
|
protected override void Awake()
|
|
|
|
{
|
|
|
|
base.Awake();
|
|
|
|
|
|
|
|
playerInput = GetComponent<PlayerInput>();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnEnable()
|
|
|
|
{
|
|
|
|
base.OnEnable();
|
|
|
|
|
|
|
|
playerInput.actions.Enable();
|
|
|
|
playerInput.actions.FindAction("Move").performed += OnMove;
|
|
|
|
playerInput.actions.FindAction("Move").canceled += OnMove;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnDisable()
|
|
|
|
{
|
|
|
|
base.OnDisable();
|
|
|
|
|
|
|
|
playerInput.actions.Disable();
|
|
|
|
playerInput.actions.FindAction("Move").performed -= OnMove;
|
|
|
|
playerInput.actions.FindAction("Move").canceled -= OnMove;
|
|
|
|
}
|
|
|
|
|
2023-10-11 20:26:45 +00:00
|
|
|
public virtual void TakeDamage(float attackerPower, float attackerShieldPenetrationRate = default, Vector3? attackPos = null)
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException();
|
|
|
|
}
|
2023-10-20 06:10:48 +00:00
|
|
|
|
|
|
|
public virtual void Die()
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2023-10-26 07:00:08 +00:00
|
|
|
public virtual void OnMove(InputAction.CallbackContext context) // WASD
|
2023-10-06 05:41:16 +00:00
|
|
|
{
|
2023-10-26 07:00:08 +00:00
|
|
|
movementInput = context.ReadValue<Vector2>();
|
2023-10-06 05:41:16 +00:00
|
|
|
}
|
2023-10-18 07:28:15 +00:00
|
|
|
|
|
|
|
#region Interfaces
|
|
|
|
|
|
|
|
// IView
|
|
|
|
[field: Title("IView")]
|
|
|
|
[field: SerializeField] public float ViewRadius { get; set; } = 15f;
|
|
|
|
[field: SerializeField] public float AtkRange { get; set; } = 1.5f;
|
|
|
|
[field: SerializeField] public Collider[] Targets { get; set; } = new Collider[MAX_COLLIDERS];
|
|
|
|
[field: SerializeField] public Collider Target { get; set; }
|
|
|
|
[field: SerializeField] public LayerMask TargetLayer { get; set; }
|
|
|
|
|
2023-10-26 07:00:08 +00:00
|
|
|
protected const int MAX_COLLIDERS = 30;
|
2023-10-18 07:28:15 +00:00
|
|
|
|
|
|
|
public virtual void FindNearestTargetInRange(Vector3 centerPos, bool targetIsTrigger = true)
|
|
|
|
{
|
|
|
|
Array.Clear(Targets, 0, MAX_COLLIDERS);
|
|
|
|
|
|
|
|
var numResults = Physics.OverlapSphereNonAlloc(centerPos, ViewRadius, Targets, TargetLayer,
|
|
|
|
targetIsTrigger ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore);
|
|
|
|
|
|
|
|
if (numResults <= 0)
|
|
|
|
{
|
|
|
|
SetTarget(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nearestDistance = ViewRadius * ViewRadius;
|
|
|
|
Collider nearestTargetCollider = null;
|
|
|
|
|
|
|
|
for (var i = 0; i < numResults; i++)
|
|
|
|
{
|
|
|
|
var distanceSqrToTarget = (centerPos - Targets[i].bounds.center).sqrMagnitude;
|
|
|
|
|
|
|
|
if (distanceSqrToTarget >= nearestDistance) continue;
|
|
|
|
|
|
|
|
nearestDistance = distanceSqrToTarget;
|
|
|
|
nearestTargetCollider = Targets[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
SetTarget(nearestTargetCollider);
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void SetTarget(Collider value)
|
|
|
|
{
|
|
|
|
Target = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool IsTargetWithinRange(Vector3 centerPos, float range)
|
|
|
|
{
|
|
|
|
var inRange = Vector3.Distance(centerPos, Target.bounds.center) <= range;
|
|
|
|
return inRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void MoveTarget(Vector3 targetPos, float speed, float stopDistance = Single.MaxValue)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2023-09-27 02:55:13 +00:00
|
|
|
}
|
|
|
|
}
|