using System.Collections;
using System.Reflection;
using NUnit.Framework;
using TKS.OutGame;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace TKS.Tests
{
///
/// 화면 가장자리 당김(PD 2026-11-26 「휴대폰 … 화면 가장자리에서 당기려 해도 더 이상 화면을 이동할 수 없어 당겨지지 못함 →
/// 해당 방향으로 밀고 있는 상태가 길어지면 최대 슬링샷 거리만큼은 당겨질 수 있도록」):
/// ① 손가락이 화면 아래 끝에서 바깥(아래)으로 밀고 있으면 edge_pull_sec 안에 최대 세기 거리까지 차오르고 ② 끝에서 떨어지면 손가락 거리로 돌아온다
/// ③ 실제 조준(끌기 조준 AimWorld → DragPoint)도 화면 끝에서 버티면 슬링 최대 거리까지 간다.
///
public class EdgePullTests
{
static readonly BindingFlags NP = BindingFlags.NonPublic | BindingFlags.Instance;
static float Dist(object v, Vector2 anchor)
{
return Vector2.Distance((Vector2)v, anchor);
}
[UnityTest]
public IEnumerator HoldAtScreenEdge_PullGrowsToMax()
{
LogAssert.ignoreFailingMessages = true;
int prevMode = TestOptions.AimModeOverride;
TestOptions.AimModeOverride = 1; // 끌기 조준(당긴 거리 = 세기)
try
{
WorldProgress.Reset();
SaveStore.UseMemory(true);
SceneManager.LoadScene("Battle");
yield return null;
yield return null;
var bm = BattleManager.Instance;
bm.StartStage(0);
BossIntro.Abort();
if (bm.Phase == BattlePhase.Setup) bm.ForcePhase(BattlePhase.PlayerTurn);
yield return null;
var input = Object.FindFirstObjectByType();
Assert.IsNotNull(input);
var u = bm.AliveUnits(Faction.Player)[0];
input.Select(u);
typeof(SlingshotInput).GetField("dragging", NP).SetValue(input, true); // 누르고 있는 중(Update 가 더한 세기를 지우지 않게)
typeof(SlingshotInput).GetField("aimArmed", NP).SetValue(input, true);
var edgePush = typeof(SlingshotInput).GetMethod("EdgePush", NP);
float sec = IbUi.Num("edge_pull_sec", 0.8f);
// ① · ② 순수 계산: 유닛이 화면 아래쪽 1/4 · 손가락 = 바로 아래 화면 끝 · 최대 세기 거리 = 유닛 높이의 3 배(손가락으로는 못 닿음)
Vector2 anchor = new Vector2(Screen.width * 0.5f, Screen.height * 0.25f), edge = new Vector2(anchor.x, 1f);
float full = anchor.y * 3f;
float first = Dist(edgePush.Invoke(input, new object[] { edge, anchor, full, false }), anchor), last = first; // 처음(아직 더하지 않음) = 손가락 거리
float t0 = Time.unscaledTime;
while (Time.unscaledTime - t0 < sec + 0.3f)
{
last = Dist(edgePush.Invoke(input, new object[] { edge, anchor, full, true }), anchor);
yield return null;
}
Assert.Less(first, full * 0.5f, "처음 = 손가락 거리에 가까움");
Assert.AreEqual(full, last, 1f, "화면 끝에서 밀고 있으면 최대 세기 거리까지 차오름");
Assert.AreEqual(full, Dist(edgePush.Invoke(input, new object[] { edge, anchor, full, false }), anchor), 1f, "놓는 순간(더하지 않음)도 그 세기 그대로");
Vector2 inner = new Vector2(anchor.x, anchor.y * 0.5f);
t0 = Time.unscaledTime;
while (Time.unscaledTime - t0 < sec)
{
last = Dist(edgePush.Invoke(input, new object[] { inner, anchor, full, true }), anchor);
yield return null;
}
Assert.AreEqual(anchor.y * 0.5f, last, 1f, "끝에서 떨어지면 손가락 거리로 돌아옴");
// ③ 실제 조준 경로: 유닛 바로 아래 화면 끝에서 버팀 → 화살표 = 슬링 최대 거리
var cam = Camera.main;
typeof(SlingshotInput).GetField("pullBaseH", NP).SetValue(input, cam.orthographicSize * 2f);
Vector2 us = cam.WorldToScreenPoint(new Vector3(u.Position.x, u.Position.y, 0f));
Vector2 finger = new Vector2(us.x, 1f);
var aimWorld = typeof(SlingshotInput).GetMethod("AimWorld", NP);
float max = input.AimMax(u);
Vector2? p = null;
t0 = Time.unscaledTime;
float firstAim = -1f;
while (Time.unscaledTime - t0 < sec + 0.3f)
{
p = input.DragPoint(u, (Vector2)aimWorld.Invoke(input, new object[] { finger, true }));
if (firstAim < 0f && p.HasValue) firstAim = Vector2.Distance(u.Position, p.Value);
yield return null;
}
Assert.IsTrue(p.HasValue, "조준점 없음");
Assert.AreEqual(max, Vector2.Distance(u.Position, p.Value), 0.05f, "화면 끝에서 버티면 슬링 최대 거리(처음 " + firstAim.ToString("0.00") + ")");
Debug.Log("[TKS 가장자리 당김] 유닛 화면 y " + us.y.ToString("0") + " px · 처음 세기 " + firstAim.ToString("0.00") + " → " + max.ToString("0.00"));
}
finally
{
TestOptions.AimModeOverride = prevMode;
}
}
}
}