Skip to content

Commit

Permalink
Update ProjectileHandler.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
woojin211 committed Jun 3, 2024
1 parent a70d78e commit 59fd2bb
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Assets/2Scripts/Projectile/ProjectileHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using UnityEngine;
using System.Threading.Tasks;

public class ProjectileHandler : MonoBehaviour
{
private Rigidbody2D rb;
private float angle; // 이동에 따른 회전각
public bool isDoubleShot = false; // 더블샷 여부

public float massPower; // 질량에 대한 파워

private void Awake()
{
rb = GetComponent<Rigidbody2D>();

rb.mass = InGameManager.IT.projectileMass; // 1.5f: 화살, 3.5f: 미사일
massPower = rb.mass;
}

public void Set(float speed, Vector3 position, int direction, bool isDoubleShot)
{
this.isDoubleShot = isDoubleShot; // 더블샷 여부 설정

// 방향 체크
// position.x *= direction;

// Debug.Log("set proj x: " + position.x + ", dir: " + direction);

rb.AddForce(position * speed * massPower, ForceMode2D.Impulse); // 발사

DeleteTimer(5);
}

private void FixedUpdate()
{
angle = Mathf.Atan2(rb.velocity.y, rb.velocity.x) * Mathf.Rad2Deg; // 이동에 따른 회전각 계산
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); // 회전

var windPower = InGameManager.IT.windPower;
var windPowerCoefficient = InGameManager.IT.windPowerCoefficient; // 바람 세기 계수
rb.AddForce(new Vector3(windPower * windPowerCoefficient,0,0), ForceMode2D.Force); // 바람 재적용 // AI 명중률 감소
}

private void OnDestroy()
{
// 더블샷이 아니면 타이머 시작
if (isDoubleShot)
return;

InGameManager.IT.StartTimer(); // 타이머 시작
InGameManager.IT.CleanLine(); // 예상각도 UI 제거
}

private async void DeleteTimer(float sec)
{
int t = (int)(sec * 1000);
await Task.Delay(t);

if (this)
Destroy(gameObject);
}
}

0 comments on commit 59fd2bb

Please sign in to comment.