Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Assets/2D_Destruction.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/2D_Destruction/Demo.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Assets/2D_Destruction/Demo/Demo Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Assets/2D_Destruction/Demo/Demo Scripts/ExplodeOnClick.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Explodable))]
public class ExplodeOnClick : MonoBehaviour {

private Explodable _explodable;

void Start()
{
_explodable = GetComponent<Explodable>();
}
void OnMouseDown()
{
_explodable.explode();
ExplosionForce ef = GameObject.FindObjectOfType<ExplosionForce>();
ef.doExplosion(transform.position);
}
}
12 changes: 12 additions & 0 deletions Assets/2D_Destruction/Demo/Demo Scripts/ExplodeOnClick.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions Assets/2D_Destruction/Demo/Demo Scripts/ExplosionForce.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class ExplosionForce : MonoBehaviour {
public float force = 50;
public float radius = 5;
public float upliftModifer = 5;

/// <summary>
/// create an explosion force
/// </summary>
/// <param name="position">location of the explosion</param>
public void doExplosion(Vector3 position){
transform.localPosition = position;
StartCoroutine(waitAndExplode());
}

/// <summary>
/// exerts an explosion force on all rigidbodies within the given radius
/// </summary>
/// <returns></returns>
private IEnumerator waitAndExplode(){
yield return new WaitForFixedUpdate();

Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position,radius);

foreach(Collider2D coll in colliders){
if(coll.GetComponent<Rigidbody2D>()&&coll.name!="hero"){
AddExplosionForce(coll.GetComponent<Rigidbody2D>(), force, transform.position, radius, upliftModifer);
}
}
}

/// <summary>
/// adds explosion force to given rigidbody
/// </summary>
/// <param name="body">rigidbody to add force to</param>
/// <param name="explosionForce">base force of explosion</param>
/// <param name="explosionPosition">location of the explosion source</param>
/// <param name="explosionRadius">radius of explosion effect</param>
/// <param name="upliftModifier">factor of additional upward force</param>
private void AddExplosionForce(Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius, float upliftModifier = 0)
{
var dir = (body.transform.position - explosionPosition);
float wearoff = 1 - (dir.magnitude / explosionRadius);
Vector3 baseForce = dir.normalized * explosionForce * wearoff;
baseForce.z = 0;
body.AddForce(baseForce);

if (upliftModifer != 0)
{
float upliftWearoff = 1 - upliftModifier / explosionRadius;
Vector3 upliftForce = Vector2.up * explosionForce * upliftWearoff;
upliftForce.z = 0;
body.AddForce(upliftForce);
}

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Assets/2D_Destruction/Demo/Demo Scripts/Reset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEngine;
using System.Collections;

public class Reset : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.R)){
Application.LoadLevel(Application.loadedLevel);
}
}
}
8 changes: 8 additions & 0 deletions Assets/2D_Destruction/Demo/Demo Scripts/Reset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/2D_Destruction/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions Assets/2D_Destruction/Scripts/ClipperHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ClipperLib;
using Polygon = System.Collections.Generic.List<ClipperLib.IntPoint>;
using Polygons = System.Collections.Generic.List<System.Collections.Generic.List<ClipperLib.IntPoint>>;
using Delaunay;

public static class ClipperHelper {
private static float multiplier = 1000;

public static List<List<Vector2>> clip(List<Vector2> boundary, Triangle piece)
{
//create Boundary Polygon
Polygons boundaryPoly = createPolygons(boundary);

//create Polygon from the triangular piece
Polygons subjPoly = createPolygons(piece);

//clip triangular polygon against the boundary polygon
Polygons result = new Polygons();
Clipper c = new Clipper();
c.AddPaths(subjPoly, PolyType.ptClip, true);
c.AddPaths(boundaryPoly, PolyType.ptSubject, true);
c.Execute(ClipType.ctIntersection, result, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);

List<List<Vector2>> clippedPolygons = new List<List<Vector2>>();

foreach (Polygon poly in result)
{
List<Vector2> clippedPoly = new List<Vector2>();
foreach (IntPoint p in poly)
{
clippedPoly.Add(new Vector2(p.X, p.Y) / multiplier);
}
clippedPolygons.Add(clippedPoly);

}
return clippedPolygons;

}
public static List<List<Vector2>> clip(List<Vector2> boundary, List<Vector2> region)
{
Polygons boundaryPoly = createPolygons(boundary);
Polygons regionPoly = createPolygons(region);

//clip triangular polygon against the boundary polygon
Polygons result = new Polygons();
Clipper c = new Clipper();
c.AddPaths(regionPoly, PolyType.ptClip, true);
c.AddPaths(boundaryPoly, PolyType.ptSubject, true);
c.Execute(ClipType.ctIntersection, result, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);

List<List<Vector2>> clippedPolygons = new List<List<Vector2>>();

foreach (Polygon poly in result)
{
List<Vector2> clippedPoly = new List<Vector2>();
foreach (IntPoint p in poly)
{
clippedPoly.Add(new Vector2(p.X, p.Y) / multiplier);
}
clippedPolygons.Add(clippedPoly);

}
return clippedPolygons;
}

private static Polygons createPolygons(List<Vector2> source)
{
Polygons poly = new Polygons(1);
poly.Add(new Polygon(source.Count));
foreach (Vector2 p in source)
{
poly[0].Add(new IntPoint(p.x * multiplier, p.y * multiplier));
}

return poly;
}
private static Polygons createPolygons(Triangle tri)
{
Polygons poly = new Polygons(1);
poly.Add(new Polygon(3));
for (int i = 0; i < 3; i++)
{
poly[0].Add(new IntPoint(tri.sites[i].x * multiplier, tri.sites[i].y * multiplier));
}

return poly;
}

}
12 changes: 12 additions & 0 deletions Assets/2D_Destruction/Scripts/ClipperHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/2D_Destruction/Scripts/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions Assets/2D_Destruction/Scripts/Editor/ExplodableEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

[CustomEditor(typeof(Explodable))]
public class ExplodableEditor : Editor {

public override void OnInspectorGUI()
{
Explodable myTarget = (Explodable)target;
myTarget.allowRuntimeFragmentation = EditorGUILayout.Toggle("Allow Runtime Fragmentation", myTarget.allowRuntimeFragmentation);
myTarget.shatterType = (Explodable.ShatterType)EditorGUILayout.EnumPopup("Shatter Type", myTarget.shatterType);
myTarget.extraPoints = EditorGUILayout.IntField("Extra Points", myTarget.extraPoints);
myTarget.subshatterSteps = EditorGUILayout.IntField("Subshatter Steps",myTarget.subshatterSteps);
if (myTarget.subshatterSteps > 1)
{
EditorGUILayout.HelpBox("Use subshatter steps with caution! Too many will break performance!!! Don't recommend more than 1", MessageType.Warning);
}

myTarget.fragmentLayer = EditorGUILayout.TextField("Fragment Layer", myTarget.fragmentLayer);
myTarget.sortingLayerName = EditorGUILayout.TextField("Sorting Layer", myTarget.sortingLayerName);
myTarget.orderInLayer = EditorGUILayout.IntField("Order In Layer", myTarget.orderInLayer);

if (myTarget.GetComponent<PolygonCollider2D>() == null && myTarget.GetComponent<BoxCollider2D>() == null)
{
EditorGUILayout.HelpBox("You must add a BoxCollider2D or PolygonCollider2D to explode this sprite", MessageType.Warning);
}
else
{
if (GUILayout.Button("Generate Fragments"))
{
myTarget.fragmentInEditor();
EditorUtility.SetDirty(myTarget);
}
if (GUILayout.Button("Destroy Fragments"))
{
myTarget.deleteFragments();
EditorUtility.SetDirty(myTarget);
}
}

}
}
12 changes: 12 additions & 0 deletions Assets/2D_Destruction/Scripts/Editor/ExplodableEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading