OldBlueWater/BlueWater/Assets/02.Scripts/Character/Player/Player.cs
NTG_Lenovo c8d1acec32 #42 플레이어 수동 제어로 변경
+ Input System 로직 변경 및 Actions 추가
  ㄴ 컴포넌트 내의 Behavior :
     SendMessage -> Invoke Unity Events
  ㄴ OnEnable, OnDisable에서 이벤트 연결 방식
+ 마우스 왼쪽 키, K 기본 공격
+ 마우스 Space 키 구르기(무적)
+ 구르기 중 Trail 파티클로 임시 표현
+ 공격 시 Trail Particles로 임시 범위 표시
2023-10-26 16:00:08 +09:00

114 lines
3.6 KiB
C#

using System;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.InputSystem;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
[RequireComponent(typeof(PlayerInput))]
public class Player : BaseCharacter, IDamageable, IView
{
protected PlayerInput playerInput;
protected Vector2 movementInput;
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;
}
public virtual void TakeDamage(float attackerPower, float attackerShieldPenetrationRate = default, Vector3? attackPos = null)
{
throw new System.NotImplementedException();
}
public virtual void Die()
{
throw new NotImplementedException();
}
public virtual void OnMove(InputAction.CallbackContext context) // WASD
{
movementInput = context.ReadValue<Vector2>();
}
#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; }
protected const int MAX_COLLIDERS = 30;
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
}
}