CapersProject/Assets/02.Scripts/Character/Player/Combat/CombatPlayer.cs
2024-10-29 15:45:18 +09:00

184 lines
6.1 KiB
C#

using System.Collections;
using BlueWater.Uis;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BlueWater.Players.Combat
{
public class CombatPlayer : MonoBehaviour
{
// Variables
#region Variables
// Components
[field: SerializeField]
public Rigidbody Rigidbody { get; private set; }
[field: SerializeField]
public CapsuleCollider CharacterCollider { get; private set; }
[field: SerializeField]
public PlayerInput PlayerInput { get; private set; }
[field: SerializeField]
public Transform VisualLook { get; private set; }
[field: SerializeField]
public BoxCollider HitBoxCollider { get; private set; }
[field: SerializeField]
public SpriteRenderer SpriteRenderer { get; private set; }
[field: SerializeField]
public Animator Animator { get; private set; }
[field: SerializeField]
public PlayerHealthPoint PlayerHealthPoint { get; private set; }
[field: SerializeField]
public AnimationController AnimationController { get; private set; }
[field: SerializeField]
public CombatInput CombatInput { get; private set; }
[field: SerializeField]
public CombatMovement CombatMovement { get; private set; }
[field: SerializeField]
public CombatAttacker CombatAttacker { get; private set; }
[field: SerializeField]
public CombatSkillController CombatSkillController { get; private set; }
[field: SerializeField]
public CombatStatus CombatStatus { get; private set; }
#endregion
// Unity events
#region Unity events
private void Awake()
{
InitializeComponents();
}
private void Start()
{
if (!GameManager.Instance.CurrentCombatPlayer)
{
GameManager.Instance.SetCurrentCombatPlayer(this);
}
SubscribeEvents();
}
private void OnDestroy()
{
UnSubscribeEvents();
}
#endregion
// Initialize methods
#region Initialize methods
[Button("컴포넌트 초기화")]
private void InitializeComponents()
{
Rigidbody = GetComponent<Rigidbody>();
CharacterCollider = GetComponent<CapsuleCollider>();
PlayerInput = GetComponent<PlayerInput>();
VisualLook = transform.Find("VisualLook");
SpriteRenderer = VisualLook.GetComponent<SpriteRenderer>();
Animator = VisualLook.GetComponent<Animator>();
HitBoxCollider = transform.Find("HitBox").GetComponent<BoxCollider>();
PlayerHealthPoint = GetComponent<PlayerHealthPoint>();
AnimationController = GetComponent<AnimationController>();
CombatInput = GetComponent<CombatInput>();
CombatMovement = GetComponent<CombatMovement>();
CombatAttacker = GetComponent<CombatAttacker>();
CombatSkillController = GetComponent<CombatSkillController>();
CombatStatus = GetComponent<CombatStatus>();
}
#endregion
// Methods
#region Methods
private void SubscribeEvents()
{
// Input
CombatInput.OnMoveInputReceived += CombatMovement.InputMovement;
CombatInput.OnDashInputReceived += CombatMovement.Dash;
CombatInput.OnAttackInputReceived += CombatAttacker.Attack;
CombatInput.OnActivateMainSkillInputReceived += CombatSkillController.TryActivateSkill;
// PlayerHealthPoint
EventManager.OnDead += Die;
}
private void UnSubscribeEvents()
{
// Input
CombatInput.OnMoveInputReceived -= CombatMovement.InputMovement;
CombatInput.OnDashInputReceived -= CombatMovement.Dash;
CombatInput.OnAttackInputReceived -= CombatAttacker.Attack;
CombatInput.OnActivateMainSkillInputReceived -= CombatSkillController.TryActivateSkill;
// PlayerHealthPoint
EventManager.OnDead -= Die;
}
private void Die()
{
StartCoroutine(nameof(DieCoroutine));
}
private IEnumerator DieCoroutine()
{
CombatSkillController.StopAllCoroutine();
PostProcessingManager.Instance.ToggleRendererFeature(RendererFeatureName.GrayscaleRenderPassFeature, true);
PlayerInput.enabled = false;
Rigidbody.linearVelocity = Vector3.zero;
Rigidbody.isKinematic = true;
CharacterCollider.enabled = false;
HitBoxCollider.enabled = false;
AnimationController.SetAnimationTrigger("isDead");
var animationStarted = false;
yield return StartCoroutine(AnimationController.WaitForAnimationToRun("Die",
success => animationStarted = success));
if (!animationStarted)
{
yield break;
}
AnimationController.ResetAnimationSpeed();
while (AnimationController.IsComparingCurrentAnimation("Die") &&
AnimationController.GetCurrentAnimationNormalizedTime() < 1f)
{
yield return null;
}
yield return new WaitForSeconds(1f);
Destroy(gameObject);
CombatUiManager.Instance.GameOverPopupUi.Open();
}
public void ActivateInvincibility() => PlayerHealthPoint.ActivateInvincibility();
public void DeactivateInvincibility() => PlayerHealthPoint.DeactivateInvincibility();
public void SetCurrentHealthPoint(int value) => PlayerHealthPoint.SetCurrentHealthPoint(value);
public void SetCurrentHealthPointMax() => PlayerHealthPoint.SetCurrentHealthPoint(PlayerHealthPoint.MaxHealthPoint);
#endregion
}
}