87 lines
4.4 KiB
C#
87 lines
4.4 KiB
C#
using Lofelt.NiceVibrations;
|
||
using MoreMountains.Feedbacks;
|
||
using UnityEngine;
|
||
|
||
namespace TKS
|
||
{
|
||
/// <summary>
|
||
/// 충돌 타격감 — Feel(MoreMountains · PD 2026-11-26 「Feel 에셋을 살펴보고 적과 부딪힐 때 좀 더 임팩트 있게 충돌 연출을 보강」):
|
||
/// 유닛 그림(UnitView.Anim 의 「Art」)마다 MMF_Player 하나 = 찌그러짐(MMF_SquashAndStretch · 그림 크기 기준 곱) + 덜컹(MMF_PositionShake + MMPositionShaker · 맞은 방향) ·
|
||
/// 휴대폰 진동 = NiceVibrations 프리셋. 값은 표 Resources/Skills/hit_feel.csv(HitFeel 이 읽어 넘김 · C45). 도형 유닛(그림 없음) · 가상 실행 = 없음.
|
||
/// </summary>
|
||
public class ImpactFeel : MonoBehaviour
|
||
{
|
||
MMF_Player player;
|
||
MMF_SquashAndStretch squash;
|
||
MMF_PositionShake shake;
|
||
|
||
/// <summary>u 의 그림에 붙은 타격감(처음 부를 때 만듦 · 그림 없는 도형 유닛 = null).</summary>
|
||
public static ImpactFeel Of(Unit u)
|
||
{
|
||
var a = u != null && u.View != null ? u.View.Anim : null;
|
||
if (a == null) return null;
|
||
var f = a.GetComponent<ImpactFeel>();
|
||
if (f == null)
|
||
{
|
||
f = a.gameObject.AddComponent<ImpactFeel>();
|
||
f.Build();
|
||
}
|
||
return f;
|
||
}
|
||
|
||
void Build()
|
||
{
|
||
var shaker = gameObject.AddComponent<MMPositionShaker>(); // 그림의 로컬 자리만 흔듦(유닛 몸 · 콜라이더는 그대로)
|
||
player = gameObject.AddComponent<MMF_Player>();
|
||
squash = new MMF_SquashAndStretch
|
||
{
|
||
SquashAndStretchTarget = transform,
|
||
Mode = MMF_SquashAndStretch.Modes.Absolute, // 크기 = 지금 그림 크기 × (1 + 세기 × 곡선)
|
||
Axis = MMF_SquashAndStretch.PossibleAxis.YtoX, // 세로가 눌리면 가로가 벌어짐(부피 유지)
|
||
RemapCurveZero = 1f,
|
||
RemapCurveOne = 1f,
|
||
DetermineScaleOnPlay = true, // 재생 때 지금 크기를 기준으로(겹쳐 재생은 Punch 가 막음)
|
||
AllowAdditivePlays = false,
|
||
AnimateCurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.18f, -1f), new Keyframe(0.5f, 0.45f), new Keyframe(0.78f, -0.12f), new Keyframe(1f, 0f)), // 눌림 → 튀어오름 → 제자리
|
||
};
|
||
player.AddFeedback(squash);
|
||
shake = new MMF_PositionShake
|
||
{
|
||
TargetShaker = shaker,
|
||
ShakeSpeed = 40f,
|
||
RandomizeDirection = false,
|
||
ResetShakerValuesAfterShake = true,
|
||
ResetTargetValuesAfterShake = true,
|
||
};
|
||
player.AddFeedback(shake);
|
||
player.Initialization();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 한 번 치기: 찌그러짐 세기 squashAmt(0~1 · 0 = 없음) · squashSec 초 · 덜컹 knock(월드 · 0 = 없음) · knockSec 초 · 맞은 방향 dir(덜컹 방향).
|
||
/// 찌그러짐이 아직 도는 중이면 덜컹만(그림 크기 기준이 흐트러지지 않게).
|
||
/// </summary>
|
||
public void Punch(float squashAmt, float squashSec, float knock, float knockSec, Vector2 dir)
|
||
{
|
||
if (player == null || Sim.Active || !isActiveAndEnabled) return;
|
||
squash.Active = squashAmt > 0.001f && squashSec > 0f && !squash.IsPlaying;
|
||
squash.RemapCurveOne = 1f + squashAmt;
|
||
squash.AnimateScaleDuration = Mathf.Max(0.01f, squashSec);
|
||
shake.Active = knock > 0.0001f && knockSec > 0f;
|
||
shake.ShakeRange = knock;
|
||
shake.Duration = Mathf.Max(0.01f, knockSec);
|
||
shake.ShakeMainDirection = dir.sqrMagnitude > 1e-6f ? (Vector3)dir.normalized : Vector3.right;
|
||
if (squash.Active || shake.Active) player.PlayFeedbacks(transform.position, 1f);
|
||
}
|
||
|
||
/// <summary>휴대폰 진동(NiceVibrations 프리셋 이름 · 빈 값 · 모름 = 없음). 진동을 못 하는 기기 · 에디터 = 아무 일 없음.</summary>
|
||
public static void Haptic(string preset)
|
||
{
|
||
if (Sim.Active || string.IsNullOrEmpty(preset)) return;
|
||
HapticPatterns.PresetType p;
|
||
if (!System.Enum.TryParse(preset, true, out p) || p == HapticPatterns.PresetType.None) return;
|
||
HapticPatterns.PlayPreset(p);
|
||
}
|
||
}
|
||
}
|