2022-11-25 14:45:19 +08:00
|
|
|
|
using SHADE;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
public class Breakable : Script
|
|
|
|
|
{
|
|
|
|
|
public float threshHold = 1.0f;
|
2023-01-31 00:43:25 +08:00
|
|
|
|
public bool ignoreRaccoon = false;
|
2022-11-25 14:45:19 +08:00
|
|
|
|
private RigidBody rb;
|
|
|
|
|
private Transform trans;
|
2023-01-31 18:13:34 +08:00
|
|
|
|
public bool isBreak { get; set; }
|
2022-11-25 14:45:19 +08:00
|
|
|
|
private List<GameObject> itemPieces = new List<GameObject>();
|
|
|
|
|
private Random ran = new Random();
|
2023-01-31 18:13:34 +08:00
|
|
|
|
|
2022-11-25 14:45:19 +08:00
|
|
|
|
protected override void awake()
|
|
|
|
|
{
|
|
|
|
|
rb = GetComponent<RigidBody>();
|
|
|
|
|
if (!rb)
|
|
|
|
|
Debug.LogError("RIGIDBODY EMPTY");
|
|
|
|
|
|
|
|
|
|
trans = GetComponent<Transform>();
|
|
|
|
|
if(!trans)
|
|
|
|
|
Debug.LogError("TRANSFORM EMPTY");
|
|
|
|
|
|
|
|
|
|
foreach (GameObject pieces in GameObject)
|
|
|
|
|
{
|
|
|
|
|
itemPieces.Add(pieces);
|
|
|
|
|
pieces.SetActive(false);
|
|
|
|
|
}
|
2023-01-31 18:13:34 +08:00
|
|
|
|
|
|
|
|
|
isBreak = false;
|
2022-11-25 14:45:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void update()
|
|
|
|
|
{
|
|
|
|
|
if (isBreak)
|
|
|
|
|
Break();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void onCollisionEnter(CollisionInfo info)
|
|
|
|
|
{
|
2023-01-31 00:43:25 +08:00
|
|
|
|
if (ignoreRaccoon && info.GameObject.GetScript<PlayerController>())
|
|
|
|
|
return;
|
|
|
|
|
|
2022-11-25 14:45:19 +08:00
|
|
|
|
if (rb.LinearVelocity.GetSqrMagnitude() > threshHold)
|
|
|
|
|
{
|
|
|
|
|
isBreak = true;
|
2023-02-02 11:16:56 +08:00
|
|
|
|
if(GameObject.GetScript<Item>())
|
|
|
|
|
GameManager.Instance.totalItemCount -= 1;
|
2022-11-25 14:45:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
protected override void onTriggerEnter(CollisionInfo info)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Break()
|
|
|
|
|
{
|
|
|
|
|
foreach (GameObject item in itemPieces)
|
|
|
|
|
{
|
|
|
|
|
item.SetActive(true);
|
|
|
|
|
item.GetComponent<Transform>().GlobalPosition = trans.LocalPosition + item.GetComponent<Transform>().LocalPosition;
|
2023-02-02 11:16:56 +08:00
|
|
|
|
if (item.GetScript<Item>())
|
|
|
|
|
GameManager.Instance.totalItemCount += 1;
|
2022-11-25 14:45:19 +08:00
|
|
|
|
GameObject gO = item;
|
|
|
|
|
gO.Parent = GameObject.Null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isBreak = false;
|
2022-11-25 22:32:41 +08:00
|
|
|
|
Audio.PlaySFXOnce2D("event:/Props/impact_break");
|
2023-02-02 11:16:56 +08:00
|
|
|
|
GameObject.SetActive(false);
|
2022-11-25 14:45:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|