2026-06-15 리듬게임 자동기능
This commit is contained in:
8
Assets/02_Scripts/Core.meta
Normal file
8
Assets/02_Scripts/Core.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 47b192809f9e2fc409f9ebeeff62cf64
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
43
Assets/02_Scripts/Core/Util.cs
Normal file
43
Assets/02_Scripts/Core/Util.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
public static class Util
|
||||
{
|
||||
/// 특정 시간(초) 후에 액션을 실행
|
||||
public static async Awaitable RunDelayed(float delay, Action action, CancellationToken token = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 유니티 전용 비동기 대기
|
||||
await Awaitable.WaitForSecondsAsync(delay, token);
|
||||
|
||||
// 함수 실행
|
||||
action?.Invoke();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// 취소되었을 때의 처리 (필요 시)
|
||||
}
|
||||
}
|
||||
|
||||
/// 다음 프레임에 액션을 실행
|
||||
public static async Awaitable RunNextFrame(Action action, CancellationToken token = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Awaitable.EndOfFrameAsync(token);
|
||||
action?.Invoke();
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
}
|
||||
|
||||
public static float NormalizeAngle(float angle)
|
||||
{
|
||||
while (angle > 180)
|
||||
angle -= 360;
|
||||
while (angle < -180)
|
||||
angle += 360;
|
||||
return angle;
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Core/Util.cs.meta
Normal file
2
Assets/02_Scripts/Core/Util.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 414cb910207b7644a8bbb9be341333df
|
||||
@@ -19,6 +19,10 @@ public class RhythmManager : MonoBehaviour
|
||||
[Header("효과음")]
|
||||
[SerializeField] private AudioClip _hitSfx; // 내려칠 때마다 재생 (판정과 무관, 헛침 포함)
|
||||
|
||||
[Header("오토플레이 (아이템)")]
|
||||
[SerializeField] private bool _autoPlay; // 켜지면 모든 노트를 판정선 도달 순간 자동 Perfect 처리
|
||||
[SerializeField] private RhythmStick[] _autoPlaySticks; // 인덱스 = Note.Lane. 오토플레이 시 자동으로 휘두를 스틱들
|
||||
|
||||
[SerializeField] private GameObject StartButtonObj;
|
||||
|
||||
// 모든 타이밍의 기준. 오디오 클럭(dspTime) 기반이라 리드인 동안 음수(-leadTime→0)로 흐른다
|
||||
@@ -40,6 +44,8 @@ public class RhythmManager : MonoBehaviour
|
||||
public event Action<RhythmScore> OnScoreChanged; // 점수/콤보가 바뀔 때 (실시간 HUD)
|
||||
public event Action<RhythmScore> OnSongFinished; // 곡이 끝났을 때 (결과창)
|
||||
|
||||
private RhythmCat[] _rhythmCats;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_songTimeProvider = () => SongTime; // 한 번만 만들어 모든 노트가 공유
|
||||
@@ -51,6 +57,8 @@ private void Start()
|
||||
|
||||
InputManager.Instance.OnKey_Left_Event += OnPlayerInput;
|
||||
InputManager.Instance.OnKey_Right_Event += OnPlayerInput;
|
||||
|
||||
_rhythmCats = FindObjectsByType<RhythmCat>(FindObjectsSortMode.None);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -65,6 +73,59 @@ private void Update()
|
||||
}
|
||||
|
||||
SpawnDueNotes();
|
||||
|
||||
if (_autoPlay)
|
||||
{
|
||||
DriveAutoPlaySticks(); // 다가오는 노트에 맞춰 스틱을 들었다 내림 (판정 전에 호출)
|
||||
AutoHitDueNotes(); // 판정선 도달한 노트 자동 Perfect
|
||||
}
|
||||
}
|
||||
|
||||
// 오토플레이: 레인별로 가장 임박한 노트에 맞춰 스틱을 휘두름
|
||||
private void DriveAutoPlaySticks()
|
||||
{
|
||||
if (_autoPlaySticks == null) return;
|
||||
|
||||
for (int lane = 0; lane < _autoPlaySticks.Length; lane++)
|
||||
{
|
||||
RhythmStick stick = _autoPlaySticks[lane];
|
||||
if (stick == null) continue;
|
||||
|
||||
// 이 레인에서 아직 안 친(미래의) 노트 중 가장 가까운 타격 시각
|
||||
float nextHit = float.PositiveInfinity;
|
||||
foreach (RhythmNoteInstance note in _activeNotes)
|
||||
{
|
||||
int noteLane = Mathf.Clamp(note.Lane, 0, _autoPlaySticks.Length - 1);
|
||||
if (noteLane != lane) continue;
|
||||
if (note.HitTime < SongTime) continue; // 이미 지난(곧 처리될) 노트 제외
|
||||
if (note.HitTime < nextHit) nextHit = note.HitTime;
|
||||
}
|
||||
|
||||
stick.Drive(nextHit - SongTime); // 다가오는 노트 없으면 +∞ → 대기 자세
|
||||
}
|
||||
}
|
||||
|
||||
// 오토플레이: 판정선에 도달한(HitTime을 지난) 노트를 자동으로 Perfect 처리
|
||||
private void AutoHitDueNotes()
|
||||
{
|
||||
// 뒤에서부터 순회해야 처리한 노트를 안전하게 리스트에서 제거할 수 있다
|
||||
for (int i = _activeNotes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
RhythmNoteInstance note = _activeNotes[i];
|
||||
if (SongTime < note.HitTime) continue; // 아직 판정선 도달 전이면 건너뜀
|
||||
|
||||
// 수동 입력과 동일한 연출: 내려치는 효과음 + 히트 이펙트
|
||||
if (_hitSfx != null && SoundManager.Instance != null)
|
||||
SoundManager.Instance.PlaySFX(_hitSfx);
|
||||
|
||||
if (note.TryGetComponent(out RhythmProjectile projectile))
|
||||
projectile.Detonate();
|
||||
|
||||
_activeNotes.RemoveAt(i);
|
||||
Destroy(note.gameObject);
|
||||
|
||||
ApplyJudge(Result.Perfect); // 시간차 ≈ 0 → 항상 Perfect
|
||||
}
|
||||
}
|
||||
|
||||
// SongTime 기준으로 소환할 때가 된 노트들을 순서대로 생성
|
||||
@@ -87,6 +148,9 @@ public void ChangeSong()
|
||||
_audioSource.clip = _currentChart.SongClip;
|
||||
}
|
||||
|
||||
// 오토플레이 아이템 사용: 곡 시작 전(또는 도중)에 호출하면 남은 노트가 전부 자동 Perfect
|
||||
public void EnableAutoPlay() => _autoPlay = true;
|
||||
|
||||
// 곡 재생 + 채보 로드
|
||||
public void StartSong()
|
||||
{
|
||||
@@ -110,6 +174,11 @@ public void StartSong()
|
||||
|
||||
//BGM·환경음을 낮춰 리듬게임 소리만 들리게
|
||||
if (SoundManager.Instance != null) SoundManager.Instance.EnterMinigameMode();
|
||||
|
||||
for(int i=0;i<_rhythmCats.Length;i++)
|
||||
{
|
||||
_rhythmCats[i].Dance(i*1f);
|
||||
}
|
||||
}
|
||||
|
||||
// 곡 정지 + 주변음 복구
|
||||
@@ -120,9 +189,20 @@ public void StopSong()
|
||||
_audioSource.Stop();
|
||||
_isPlaying = false;
|
||||
|
||||
if (_autoPlay && _autoPlaySticks != null) // 곡 끝나면 들려있던 스틱 대기 자세로 복귀
|
||||
foreach (RhythmStick stick in _autoPlaySticks)
|
||||
if (stick != null) stick.ResetPose();
|
||||
|
||||
_autoPlay = false; // 아이템 효과는 한 곡만 — 곡이 끝나면 자동 해제
|
||||
|
||||
if (SoundManager.Instance != null) SoundManager.Instance.ExitMinigameMode();
|
||||
|
||||
OnSongFinished?.Invoke(Score); // 결과창 표시
|
||||
|
||||
for(int i=0;i<_rhythmCats.Length;i++)
|
||||
{
|
||||
_rhythmCats[i].DanceStop();
|
||||
}
|
||||
}
|
||||
|
||||
// 노트가 판정선을 지나쳐 스스로 Miss 처리될 때 호출 (노트는 이미 자기 파괴됨)
|
||||
|
||||
8
Assets/02_Scripts/Npcs.meta
Normal file
8
Assets/02_Scripts/Npcs.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b32bc9c54d149494f80435205fe281ea
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Assets/02_Scripts/Npcs/RhythmCat.cs
Normal file
21
Assets/02_Scripts/Npcs/RhythmCat.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class RhythmCat : MonoBehaviour
|
||||
{
|
||||
Animator anim;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
anim = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
public void Dance(float delay)
|
||||
{
|
||||
_ = Util.RunDelayed(delay,()=>{anim.SetBool("Dance",true);},this.destroyCancellationToken);
|
||||
}
|
||||
|
||||
public void DanceStop()
|
||||
{
|
||||
anim.SetBool("Dance",false);
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Npcs/RhythmCat.cs.meta
Normal file
2
Assets/02_Scripts/Npcs/RhythmCat.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9cd0b46988c9bf4f927ad5d9cb2f5ed
|
||||
@@ -4,6 +4,7 @@
|
||||
public class RhythmNoteInstance : MonoBehaviour
|
||||
{
|
||||
[HideInInspector] public float HitTime; // 이 노트의 도달 시각
|
||||
[HideInInspector] public int Lane; // 이 노트의 레인 (오토플레이 스틱 매칭용)
|
||||
|
||||
[SerializeField] private float _missWindow = 0.15f; // 판정선을 이만큼 지나면 자동 Miss
|
||||
|
||||
@@ -14,13 +15,14 @@ public class RhythmNoteInstance : MonoBehaviour
|
||||
private Action<RhythmNoteInstance> _onMiss; // 지나쳐서 Miss 났을 때 통지
|
||||
|
||||
// 스폰 시 목적지·타이밍 주입 (B 방식)
|
||||
public void Setup(Vector3 start, Vector3 target, float spawnTime, float hitTime,
|
||||
public void Setup(Vector3 start, Vector3 target, float spawnTime, float hitTime, int lane,
|
||||
Func<float> songTime, Action<RhythmNoteInstance> onMiss = null)
|
||||
{
|
||||
_start = start;
|
||||
_target = target;
|
||||
_spawnTime = spawnTime;
|
||||
HitTime = hitTime;
|
||||
Lane = lane;
|
||||
_songTime = songTime;
|
||||
_onMiss = onMiss;
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ public class LaneVisual
|
||||
Vector3 target = _judgmentLine.position + worldOffset;
|
||||
|
||||
RhythmNoteInstance instance = Instantiate(prefab, start, origin.rotation, transform);
|
||||
instance.Setup(start, target, spawnTime, note.Time, songTime, onMiss);
|
||||
instance.Setup(start, target, spawnTime, note.Time, note.Lane, songTime, onMiss);
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
99
Assets/02_Scripts/Rhythm/RhythmStick.cs
Normal file
99
Assets/02_Scripts/Rhythm/RhythmStick.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using UnityEngine;
|
||||
|
||||
// 오토플레이용 스틱 포즈 드라이버.
|
||||
// 매니저가 매 프레임 Drive(dt)를 호출하면 dt(타격까지 남은 시간)에 따라 회전을 세팅한다.
|
||||
// 모션: 대기 → (윈드업) 위로 들기 → (스트라이크) 내려치며 대기 자세보다 _strikeOvershoot 만큼 더 깊이 →
|
||||
// (팔로스루) 대기 자세로 부드럽게 복귀.
|
||||
// 노트는 타격 순간 파괴돼 그 뒤 dt가 끊기므로, 팔로스루만 Time.deltaTime으로 스틱이 자체 처리한다.
|
||||
public class RhythmStick : MonoBehaviour
|
||||
{
|
||||
[Tooltip("들어올릴 때 회전할 로컬 축 (예: 손목 꺾이는 축)")]
|
||||
[SerializeField] private Vector3 _raiseAxis = Vector3.right;
|
||||
|
||||
[Tooltip("대기 자세에서 위로 들어올리는 각도(도)")]
|
||||
[SerializeField] private float _raiseAngle = 50f;
|
||||
|
||||
[Tooltip("타격 시 대기 자세보다 더 깊이 내려가는 각도(도)")]
|
||||
[SerializeField] private float _strikeOvershoot = 10f;
|
||||
|
||||
[Tooltip("타격 몇 초 전부터 들어올리기 시작하는지")]
|
||||
[SerializeField] private float _windupTime = 0.1f;
|
||||
|
||||
[Tooltip("내려치는 데 걸리는 시간(초). _windupTime 보다 작아야 한다")]
|
||||
[SerializeField] private float _strikeTime = 0.04f;
|
||||
|
||||
[Tooltip("타격 후 대기 자세로 되돌아오는 시간(초)")]
|
||||
[SerializeField] private float _recoverTime = 0.08f;
|
||||
|
||||
private Quaternion _restRot; // 대기 자세
|
||||
private Quaternion _raisedRot; // 들어올린 자세
|
||||
private Quaternion _overshootRot; // 타격 시 더 깊이 내려간 자세
|
||||
|
||||
private bool _armed; // 내려치는 중 → 다음 프레임 타격 예정
|
||||
private bool _recovering; // 타격 후 복귀 중
|
||||
private float _recoverElapsed;
|
||||
private Quaternion _recoverFrom; // 복귀 시작 회전(스냅 방지)
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_restRot = transform.localRotation;
|
||||
Vector3 axis = _raiseAxis.normalized;
|
||||
_raisedRot = _restRot * Quaternion.AngleAxis(_raiseAngle, axis);
|
||||
_overshootRot = _restRot * Quaternion.AngleAxis(-_strikeOvershoot, axis); // 반대 방향 = 더 내려감
|
||||
}
|
||||
|
||||
// dt = 다음 타격까지 남은 시간(초). 다가오는 노트가 없으면 +∞.
|
||||
public void Drive(float dt)
|
||||
{
|
||||
// 1) 임박한 스윙(윈드업~타격)이 최우선
|
||||
if (dt > 0f && dt <= _windupTime)
|
||||
{
|
||||
_recovering = false;
|
||||
_armed = true; // 곧 타격함
|
||||
|
||||
if (dt > _strikeTime)
|
||||
{
|
||||
// 들어올리는 구간: dt가 _windupTime→_strikeTime 으로 줄며 rest→raised
|
||||
float u = Mathf.InverseLerp(_windupTime, _strikeTime, dt);
|
||||
transform.localRotation = Quaternion.Slerp(_restRot, _raisedRot, Mathf.SmoothStep(0f, 1f, u));
|
||||
}
|
||||
else
|
||||
{
|
||||
// 내려치는 구간: dt가 _strikeTime→0 으로 줄며 raised→overshoot (dt=0에 가장 깊이)
|
||||
float s = Mathf.InverseLerp(_strikeTime, 0f, dt);
|
||||
transform.localRotation = Quaternion.Slerp(_raisedRot, _overshootRot, Mathf.SmoothStep(0f, 1f, s));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 2) 방금 타격이 끝났다면(노트 파괴로 dt가 끊김) 팔로스루 시작
|
||||
if (_armed)
|
||||
{
|
||||
_armed = false;
|
||||
_recovering = true;
|
||||
_recoverElapsed = 0f;
|
||||
_recoverFrom = transform.localRotation; // 현재 위치에서 복귀(스냅 방지)
|
||||
}
|
||||
|
||||
// 3) 팔로스루: 현재 → 대기 자세로 부드럽게
|
||||
if (_recovering)
|
||||
{
|
||||
_recoverElapsed += Time.deltaTime;
|
||||
float r = _recoverTime > 0f ? Mathf.Clamp01(_recoverElapsed / _recoverTime) : 1f;
|
||||
transform.localRotation = Quaternion.Slerp(_recoverFrom, _restRot, Mathf.SmoothStep(0f, 1f, r));
|
||||
if (r >= 1f) _recovering = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 4) 평상시 대기 자세
|
||||
transform.localRotation = _restRot;
|
||||
}
|
||||
|
||||
// 즉시 대기 자세로 되돌림 (오토플레이 종료 시 호출)
|
||||
public void ResetPose()
|
||||
{
|
||||
_armed = false;
|
||||
_recovering = false;
|
||||
transform.localRotation = _restRot;
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Rhythm/RhythmStick.cs.meta
Normal file
2
Assets/02_Scripts/Rhythm/RhythmStick.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ae74de6faba37f4ca52b1c6177a98bf
|
||||
Reference in New Issue
Block a user