Shegotwet/Assets/Scripts/Game/LuckyGameObj.cs

140 lines
3.9 KiB
C#
Raw Normal View History

2025-09-12 19:39:24 +00:00
using GUPS.AntiCheat.Protected;
using UnityEngine;
2025-09-12 19:51:36 +00:00
using UnityEngine.UI;
2025-09-12 19:39:24 +00:00
public class LuckyGameObj : MonoBehaviour
{
2025-09-12 19:51:36 +00:00
public Image m_image;
public GameObject go_effect;
2025-09-12 20:15:49 +00:00
public Rigidbody2D m_Rigidbody2D;
public Collider2D m_Collider2D;
2025-09-12 19:51:36 +00:00
bool isInCup, isCollision, isRandomBlock;
public float velocityThreshold = 0.1f; // 이 값 이하이면 "거의 없음"으로 판단
float stoppedTime = 0f;
public float requiredStopDuration = 0.2f; // 0.2초 이상 멈춰야 인정
Transform tf_fallobjs;
eMoney m_Money;
void Update()
{
if (!isInCup && !isCollision && IsAlmostStopped())
{
2025-09-25 05:57:17 +00:00
//Debug.Log("움직임이 거의 없음");
Set_Collision(0);
}
}
bool IsAlmostStopped()
{
bool almostStoppedNow =
m_Rigidbody2D.linearVelocity.sqrMagnitude < velocityThreshold * velocityThreshold &&
Mathf.Abs(m_Rigidbody2D.angularVelocity) < velocityThreshold;
if (almostStoppedNow)
stoppedTime += Time.deltaTime;
else
stoppedTime = 0f;
return stoppedTime >= requiredStopDuration;
}
2025-09-12 19:39:24 +00:00
public void Init(eMoney money, Vector3 pos, Transform tfcupin, Transform tffallobjs)
2025-09-12 19:39:24 +00:00
{
transform.parent = tfcupin;
2025-10-02 19:08:57 +00:00
Init_Common(money, pos, tffallobjs);
2025-09-12 20:15:49 +00:00
isRandomBlock = isCollision = false;
m_Collider2D.enabled = isInCup = m_image.enabled = true;
}
2025-10-02 19:08:57 +00:00
public void Init_Add(eMoney money, Vector3 pos, Transform tffallobjs)
{
transform.parent = tffallobjs;
Init_Common(money, pos, tffallobjs);
isInCup = isCollision = false;
isRandomBlock = m_Collider2D.enabled = m_image.enabled = true;
}
void Init_Common(eMoney money, Vector3 pos, Transform tffallobjs)
{
gameObject.SetActive(true);
2025-10-02 19:08:57 +00:00
m_Money = money;
transform.localPosition = pos;
tf_fallobjs = tffallobjs;
2025-10-02 19:08:57 +00:00
m_image.sprite = UIAtlasMgr.Ins.Get_Sprite(m_Money);
2025-09-12 19:51:36 +00:00
go_effect.SetActive(false);
2025-09-12 20:15:49 +00:00
m_Rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
}
public void StopObj()
{
m_Rigidbody2D.bodyType = RigidbodyType2D.Kinematic;
}
public void Set_NoInCup()
2025-09-12 20:15:49 +00:00
{
isInCup = false;
2025-09-12 19:39:24 +00:00
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (isInCup || isCollision || collision == null) return;
2025-09-12 19:39:24 +00:00
//Debug.Log(collision.collider.tag + " : " + collision.collider.name);
2025-09-12 20:15:49 +00:00
switch (collision.collider.tag)
{
case "Player":
Set_Collision(1);
2025-09-12 20:15:49 +00:00
break;
case "Finish":
Set_Collision(0);
break;
2025-09-12 19:39:24 +00:00
}
}
2025-09-12 20:15:49 +00:00
2025-09-12 20:45:21 +00:00
private void OnTriggerEnter2D(Collider2D collision)
{
switch (collision.tag)
{
case "RandomBlock":
if (!isRandomBlock)
{
isRandomBlock = true;
var lrb = collision.GetComponent<LuckyRandomBlock>();
var addAmount = lrb.Get_Value(1);
for (int i = 0; i < addAmount - 1; i++)
{ // 공 추가하기
2025-10-02 19:08:57 +00:00
LobbyUI.Ins.m_Game_Lucky.Add_Ball(m_Money, transform.localPosition);
}
}
2025-09-12 20:45:21 +00:00
break;
case "LuckyGameCupTrigger":
isInCup = true;
break;
}
}
void OnTriggerExit2D(Collider2D other)
{
switch (other.tag)
{
case "LuckyGameCupTrigger":
isInCup = false;
transform.parent = tf_fallobjs;
break;
2025-09-12 20:45:21 +00:00
}
}
2025-09-12 20:15:49 +00:00
void Set_Collision(int amount)
{
2025-09-19 20:24:20 +00:00
SoundInfo.Ins.Play_OneShot((eSound)Random.Range(16, 19));
2025-10-02 19:08:57 +00:00
LobbyUI.Ins.m_Game_Lucky.Add_Amount(m_Money, amount);
2025-09-12 20:15:49 +00:00
m_image.enabled = false;
m_Collider2D.enabled = false;
2025-09-12 20:15:49 +00:00
go_effect.SetActive(true);
isCollision = true;
}
2025-09-12 19:39:24 +00:00
}