85 lines
2.1 KiB
C#
85 lines
2.1 KiB
C#
using SHADE;
|
|
using SHADE_Scripting.Audio;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Xml.Linq;
|
|
|
|
public class Breakable : Script
|
|
{
|
|
public float threshHold = 1.0f;
|
|
public bool ignoreRaccoon = false;
|
|
private RigidBody rb;
|
|
private Transform trans;
|
|
public bool isBreak { get; set; }
|
|
private List<GameObject> itemPieces = new List<GameObject>();
|
|
|
|
[SerializeField]
|
|
private string breakClipPath;
|
|
|
|
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);
|
|
}
|
|
|
|
isBreak = false;
|
|
|
|
AudioHandler.audioClipHandlers["break"] = Audio.CreateAudioClip(breakClipPath);
|
|
Audio.AttachAudioClipToObject(AudioHandler.audioClipHandlers["break"], GameObject.EntityId);
|
|
}
|
|
|
|
protected override void update()
|
|
{
|
|
if (isBreak)
|
|
Break();
|
|
}
|
|
|
|
protected override void onCollisionEnter(CollisionInfo info)
|
|
{
|
|
if (ignoreRaccoon && info.GameObject.GetScript<PlayerController>())
|
|
return;
|
|
|
|
if (rb.LinearVelocity.GetSqrMagnitude() > threshHold)
|
|
{
|
|
isBreak = true;
|
|
if (GameObject.GetScript<Item>())
|
|
{
|
|
GameManager.Instance.totalItemCount -= 1;
|
|
GameManager.Instance.itemShatter = true;
|
|
}
|
|
}
|
|
}
|
|
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;
|
|
if (item.GetScript<Item>())
|
|
GameManager.Instance.totalItemCount += 1;
|
|
GameObject gO = item;
|
|
gO.Parent = GameObject.Null;
|
|
}
|
|
|
|
GameManager.Instance.itemShatter = false;
|
|
isBreak = false;
|
|
AudioHandler.audioClipHandlers["break"].Play();
|
|
GameObject.SetActive(false);
|
|
}
|
|
} |