Shegotwet/Assets/Scripts/Game/LuckyGameObj.cs

136 lines
3.8 KiB
C#

using GUPS.AntiCheat.Protected;
using UnityEngine;
using UnityEngine.UI;
public class LuckyGameObj : MonoBehaviour
{
public Image m_image;
public GameObject go_effect;
public Rigidbody2D m_Rigidbody2D;
public Collider2D m_Collider2D;
bool isInCup, isCollision, isRandomBlock;
public float velocityThreshold = 0.1f; // 이 값 이하이면 "거의 없음"으로 판단
float stoppedTime = 0f;
public float requiredStopDuration = 0.2f; // 0.2초 이상 멈춰야 인정
Transform tf_fallobjs;
void Update()
{
if (!isInCup && !isCollision && IsAlmostStopped())
{
//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;
}
public void Init(Vector3 pos, Transform tfcupin, Transform tffallobjs)
{
gameObject.SetActive(true);
transform.parent = tfcupin;
transform.localPosition = pos;
tf_fallobjs = tffallobjs;
isRandomBlock = isCollision = false;
m_Collider2D.enabled = isInCup = m_image.enabled = true;
go_effect.SetActive(false);
m_Rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
}
public void Init_Add(Vector3 pos, Transform tffallobjs)
{
gameObject.SetActive(true);
transform.parent = tffallobjs;
transform.localPosition = pos;
tf_fallobjs = tffallobjs;
isInCup = isCollision = false;
isRandomBlock = m_Collider2D.enabled = m_image.enabled = true;
go_effect.SetActive(false);
m_Rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
}
public void StopObj()
{
m_Rigidbody2D.bodyType = RigidbodyType2D.Kinematic;
}
public void Set_NoInCup()
{
isInCup = false;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (isInCup || isCollision || collision == null) return;
//Debug.Log(collision.collider.tag + " : " + collision.collider.name);
switch (collision.collider.tag)
{
case "Player":
Set_Collision(1);
break;
case "Finish":
Set_Collision(0);
break;
}
}
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++)
{ // 공 추가하기
LobbyUI.Ins.m_Game_Lucky.Add_Ball(transform.localPosition);
}
}
break;
case "LuckyGameCupTrigger":
isInCup = true;
break;
}
}
void OnTriggerExit2D(Collider2D other)
{
switch (other.tag)
{
case "LuckyGameCupTrigger":
isInCup = false;
transform.parent = tf_fallobjs;
break;
}
}
void Set_Collision(int amount)
{
SoundInfo.Ins.Play_OneShot((eSound)Random.Range(16, 19));
LobbyUI.Ins.m_Game_Lucky.Add_Amount(amount);
m_image.enabled = false;
m_Collider2D.enabled = false;
go_effect.SetActive(true);
isCollision = true;
}
}