2026-04-22 15:58:44 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using Platformer.Gameplay;
|
|
|
|
|
|
using static Platformer.Core.Simulation;
|
|
|
|
|
|
using Platformer.Model;
|
|
|
|
|
|
using Platformer.Core;
|
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Platformer.Mechanics
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This is the main class used to implement control of the player.
|
|
|
|
|
|
/// It is a superset of the AnimationController class, but is inlined to allow for any kind of customisation.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class PlayerController : KinematicObject
|
|
|
|
|
|
{
|
|
|
|
|
|
public AudioClip jumpAudio;
|
|
|
|
|
|
public AudioClip respawnAudio;
|
|
|
|
|
|
public AudioClip ouchAudio;
|
2026-04-23 14:47:51 +00:00
|
|
|
|
/// <summary>
|
2026-04-24 07:22:13 +00:00
|
|
|
|
/// Attack sound effect. 자동 발동 시 PlayerAttack.Execute가 PlayOneShot으로 재생.
|
|
|
|
|
|
/// BT7-Plan 2026-04-24 — VS 순수형 자동 발동 전환 후에도 audio hook 유지.
|
2026-04-23 14:47:51 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public AudioClip attackAudio;
|
2026-04-22 15:58:44 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Max horizontal speed of the player.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public float maxSpeed = 7;
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initial jump velocity at the start of a jump.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public float jumpTakeOffSpeed = 7;
|
|
|
|
|
|
|
2026-04-23 14:47:51 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Attack hitbox component (자동 GetComponent, 없으면 null — PlayerAttack.Execute에서 null 체크).
|
2026-04-24 07:22:13 +00:00
|
|
|
|
/// 공격은 PlayerAttackTicker가 주기적으로 Schedule<PlayerAttack>을 발화하여 실행.
|
2026-04-23 14:47:51 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public AttackHitbox attackHitbox;
|
|
|
|
|
|
|
2026-04-22 15:58:44 +00:00
|
|
|
|
public JumpState jumpState = JumpState.Grounded;
|
|
|
|
|
|
private bool stopJump;
|
|
|
|
|
|
/*internal new*/ public Collider2D collider2d;
|
|
|
|
|
|
/*internal new*/ public AudioSource audioSource;
|
|
|
|
|
|
public Health health;
|
|
|
|
|
|
public bool controlEnabled = true;
|
|
|
|
|
|
|
|
|
|
|
|
bool jump;
|
|
|
|
|
|
Vector2 move;
|
|
|
|
|
|
SpriteRenderer spriteRenderer;
|
|
|
|
|
|
internal Animator animator;
|
|
|
|
|
|
readonly PlatformerModel model = Simulation.GetModel<PlatformerModel>();
|
|
|
|
|
|
|
|
|
|
|
|
private InputAction m_MoveAction;
|
|
|
|
|
|
private InputAction m_JumpAction;
|
2026-04-23 14:47:51 +00:00
|
|
|
|
|
2026-04-24 07:22:13 +00:00
|
|
|
|
// 현재 facing 방향 (마지막 이동 입력 기반, 정지 시 이전 값 유지).
|
|
|
|
|
|
// BT7-Plan 2026-04-24 — PlayerAttackTicker가 자동 발동 시 참조하므로 public 노출.
|
2026-04-23 14:47:51 +00:00
|
|
|
|
Vector2 facing = Vector2.right;
|
2026-04-22 15:58:44 +00:00
|
|
|
|
|
2026-04-24 07:22:13 +00:00
|
|
|
|
/// <summary>현재 플레이어 facing 방향. PlayerAttackTicker가 Schedule 시점에 참조.</summary>
|
|
|
|
|
|
public Vector2 Facing => facing;
|
|
|
|
|
|
|
2026-04-22 15:58:44 +00:00
|
|
|
|
public Bounds Bounds => collider2d.bounds;
|
|
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
health = GetComponent<Health>();
|
|
|
|
|
|
audioSource = GetComponent<AudioSource>();
|
|
|
|
|
|
collider2d = GetComponent<Collider2D>();
|
|
|
|
|
|
spriteRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
|
|
animator = GetComponent<Animator>();
|
2026-04-23 14:47:51 +00:00
|
|
|
|
if (attackHitbox == null) attackHitbox = GetComponent<AttackHitbox>();
|
2026-04-22 15:58:44 +00:00
|
|
|
|
|
|
|
|
|
|
m_MoveAction = InputSystem.actions.FindAction("Player/Move");
|
|
|
|
|
|
m_JumpAction = InputSystem.actions.FindAction("Player/Jump");
|
2026-04-23 14:47:51 +00:00
|
|
|
|
|
2026-04-22 15:58:44 +00:00
|
|
|
|
m_MoveAction.Enable();
|
|
|
|
|
|
m_JumpAction.Enable();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (controlEnabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
move.x = m_MoveAction.ReadValue<Vector2>().x;
|
|
|
|
|
|
if (jumpState == JumpState.Grounded && m_JumpAction.WasPressedThisFrame())
|
|
|
|
|
|
jumpState = JumpState.PrepareToJump;
|
|
|
|
|
|
else if (m_JumpAction.WasReleasedThisFrame())
|
|
|
|
|
|
{
|
|
|
|
|
|
stopJump = true;
|
|
|
|
|
|
Schedule<PlayerStopJump>().player = this;
|
|
|
|
|
|
}
|
2026-04-24 07:22:13 +00:00
|
|
|
|
// 공격은 PlayerAttackTicker가 자동 주기로 Schedule<PlayerAttack>을 발화 (BT7-Plan VS 순수형).
|
2026-04-22 15:58:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
move.x = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
UpdateJumpState();
|
|
|
|
|
|
base.Update();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UpdateJumpState()
|
|
|
|
|
|
{
|
|
|
|
|
|
jump = false;
|
|
|
|
|
|
switch (jumpState)
|
|
|
|
|
|
{
|
|
|
|
|
|
case JumpState.PrepareToJump:
|
|
|
|
|
|
jumpState = JumpState.Jumping;
|
|
|
|
|
|
jump = true;
|
|
|
|
|
|
stopJump = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case JumpState.Jumping:
|
|
|
|
|
|
if (!IsGrounded)
|
|
|
|
|
|
{
|
|
|
|
|
|
Schedule<PlayerJumped>().player = this;
|
|
|
|
|
|
jumpState = JumpState.InFlight;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case JumpState.InFlight:
|
|
|
|
|
|
if (IsGrounded)
|
|
|
|
|
|
{
|
|
|
|
|
|
Schedule<PlayerLanded>().player = this;
|
|
|
|
|
|
jumpState = JumpState.Landed;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case JumpState.Landed:
|
|
|
|
|
|
jumpState = JumpState.Grounded;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void ComputeVelocity()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (jump && IsGrounded)
|
|
|
|
|
|
{
|
|
|
|
|
|
velocity.y = jumpTakeOffSpeed * model.jumpModifier;
|
|
|
|
|
|
jump = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (stopJump)
|
|
|
|
|
|
{
|
|
|
|
|
|
stopJump = false;
|
|
|
|
|
|
if (velocity.y > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
velocity.y = velocity.y * model.jumpDeceleration;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (move.x > 0.01f)
|
2026-04-23 14:47:51 +00:00
|
|
|
|
{
|
2026-04-22 15:58:44 +00:00
|
|
|
|
spriteRenderer.flipX = false;
|
2026-04-23 14:47:51 +00:00
|
|
|
|
facing = Vector2.right;
|
|
|
|
|
|
}
|
2026-04-22 15:58:44 +00:00
|
|
|
|
else if (move.x < -0.01f)
|
2026-04-23 14:47:51 +00:00
|
|
|
|
{
|
2026-04-22 15:58:44 +00:00
|
|
|
|
spriteRenderer.flipX = true;
|
2026-04-23 14:47:51 +00:00
|
|
|
|
facing = Vector2.left;
|
|
|
|
|
|
}
|
2026-04-22 15:58:44 +00:00
|
|
|
|
|
|
|
|
|
|
animator.SetBool("grounded", IsGrounded);
|
|
|
|
|
|
animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
|
|
|
|
|
|
|
|
|
|
|
|
targetVelocity = move * maxSpeed;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public enum JumpState
|
|
|
|
|
|
{
|
|
|
|
|
|
Grounded,
|
|
|
|
|
|
PrepareToJump,
|
|
|
|
|
|
Jumping,
|
|
|
|
|
|
InFlight,
|
|
|
|
|
|
Landed
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|