OneShotOneKill/Assets/Script/InGame/Actor/AimArrowController.cs

136 lines
3.9 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class AimArrowController : MonoBehaviour
{
public Image arrow;
public Transform projectileParent;
float m_attackTime, m_attackLifeTime;
void Start()
{
arrow.enabled = false;
}
private void OnEnable()
{
var unit = IngameMgr.Ins.Get_CurUnitTData();
m_attackLifeTime = m_attackTime = unit.f_AttackSpeed;
}
public void Set_AttackLifeTime()
{
var unit = IngameMgr.Ins.Get_CurUnitTData();
m_attackLifeTime = unit.f_AttackSpeed - IngameMgr.Ins.Get_SkillValue(eSkillType.AttackSpeedUp);
}
private void Update()
{
m_attackLifeTime -= Time.deltaTime;
if (m_attackLifeTime <= 0)
{
m_attackLifeTime = m_attackTime;
StartCoroutine(Co_Shoot());
}
}
IEnumerator Co_Shoot()
{
int count = 1 + (int)IngameMgr.Ins.Get_SkillValue(eSkillType.DoubleShot);
for (int i = 0; i < count; i++)
{
Shoot();
yield return new WaitForSeconds(0.15f);
}
}
public void OnAimStart(Vector2 screenPos)
{
UpdateArrow(screenPos);
}
public void OnAimDrag(Vector2 screenPos)
{
UpdateArrow(screenPos);
}
public void OnAimEnd()
{
arrow.enabled = false;
}
void Shoot()
{
var unit = IngameMgr.Ins.Get_CurUnitTData();
ProjectileData basePd = new ProjectileData
{
IsPC = true,
m_Data = table_projectile.Ins.Get_Data(unit.n_ProjectileID),
tf_Start = arrow.transform,
Dmg = unit.n_DefaultAttack + (int)IngameMgr.Ins.Get_SkillValue(eSkillType.AttackUp),
Ignore_WallHP = true,
PreviousArrow = (int)IngameMgr.Ins.Get_SkillValue(eSkillType.PreviousArrow),
DiagonalArrow = (int)IngameMgr.Ins.Get_SkillValue(eSkillType.DiagonalArrow),
ArrowLeftRight = (int)IngameMgr.Ins.Get_SkillValue(eSkillType.ArrowLeftRight),
ArrowUpDown = (int)IngameMgr.Ins.Get_SkillValue(eSkillType.ArrowUpDown),
Bounce = (int)IngameMgr.Ins.Get_SkillValue(eSkillType.Bounce),
Pierce = (int)IngameMgr.Ins.Get_SkillValue(eSkillType.ProjectilePierce)
};
int lineCnt = 1 + basePd.PreviousArrow;
int diagCnt = 1 + basePd.DiagonalArrow;
for (int l = 0; l < lineCnt; l++)
{
for (int d = 0; d < diagCnt; d++)
{
ProjectileMgr.Ins.Shoot_Projectile(new ProjectileData
{
LineIndex = l,
DiagIndex = d,
PreviousArrow = basePd.PreviousArrow,
DiagonalArrow = basePd.DiagonalArrow,
Ignore_WallHP = basePd.Ignore_WallHP,
ArrowLeftRight = basePd.ArrowLeftRight,
ArrowUpDown = basePd.ArrowUpDown,
Bounce = basePd.Bounce,
Pierce = basePd.Pierce,
IsPC = basePd.IsPC,
m_Data = basePd.m_Data,
tf_Start = basePd.tf_Start,
Dmg = basePd.Dmg
});
}
}
}
void UpdateArrow(Vector2 screenPos)
{
arrow.enabled = true;
Vector2 touchWorldPos = Camera.main.ScreenToWorldPoint(screenPos);
Vector2 dir = touchWorldPos - (Vector2)arrow.transform.position;
if (dir.sqrMagnitude < 0.001f)
return;
// 1. 방향 → 각도 (위 기준)
float angle = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
// ↑ x,y 순서 중요 (up 기준)
// 2. 각도 제한
angle = Mathf.Clamp(angle, -80f, 80f);
// 3. 각도 → 방향
Vector2 clampedDir = Quaternion.Euler(0, 0, -angle) * Vector2.up;
arrow.transform.up = clampedDir;
}
}