-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplodeAction.cs
More file actions
91 lines (77 loc) · 3.12 KB
/
Copy pathExplodeAction.cs
File metadata and controls
91 lines (77 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections.Generic;
using UnityEngine;
namespace Unity.LEGO.Behaviours.Actions
{
public class ExplodeAction : Action
{
[SerializeField, Range(1,50), Tooltip("The power of the explosion.")]
uint m_Power = 10;
bool m_Detonated;
List<LEGOBehaviour> m_Behaviours = new List<LEGOBehaviour>();
protected override void Reset()
{
base.Reset();
m_IconPath = "Assets/LEGO/Gizmos/LEGO Behaviour Icons/Explode Action.png";
}
protected override void Start()
{
base.Start();
if (IsPlacedOnBrick())
{
// Find all LEGOBehaviours in scope.
foreach (var brick in m_ScopedBricks)
{
m_Behaviours.AddRange(brick.GetComponentsInChildren<LEGOBehaviour>());
}
}
}
protected void Update()
{
if (m_Active)
{
if (!m_Detonated)
{
// Remove all game objects with LEGOBehaviourCollider components.
foreach (var brick in m_ScopedBricks)
{
foreach (var behaviourCollider in brick.GetComponentsInChildren<LEGOBehaviourCollider>())
{
Destroy(behaviourCollider.gameObject);
}
// Restore part's original colliders.
foreach(var part in brick.parts)
{
BrickColliderCombiner.RestoreOriginalColliders(part);
}
}
var lift = m_Power * 0.25f;
// Send all bricks in scope flying.
foreach (var brick in m_ScopedBricks)
{
brick.DisconnectAll();
var rigidBody = brick.gameObject.GetComponent<Rigidbody>();
if (!rigidBody)
{
rigidBody = brick.gameObject.AddComponent<Rigidbody>();
}
rigidBody.AddExplosionForce(m_Power, transform.position + transform.TransformVector(m_BrickPivotOffset), m_ScopedBounds.extents.magnitude, lift, ForceMode.VelocityChange);
}
PlayAudio(moveWithScope: false, destroyWithAction: false);
// Delay destruction of LEGOBehaviours one frame to allow multiple Explode Actions to detonate.
m_Detonated = true;
}
else
{
// Destroy all the LEGOBehaviours in scope (including this script).
foreach (var behaviour in m_Behaviours)
{
if (behaviour)
{
Destroy(behaviour);
}
}
}
}
}
}
}