Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Slicer2DAnchor : MonoBehaviour {
  6.     public enum AnchorType {AttachRigidbody, CancelSlice};
  7.     public Collider2D[] anchorColliders = new Collider2D[1];
  8.     public AnchorType anchorType = AnchorType.AttachRigidbody;
  9.  
  10.     private List<Polygon> polygons = new List<Polygon>();
  11.     private List<Collider2D> colliders = new List<Collider2D>();
  12.  
  13.     void Start () {
  14.         bool addEvents = false;
  15.  
  16.         foreach(Collider2D collider in anchorColliders) {
  17.             addEvents = true;
  18.         }
  19.  
  20.         if (addEvents == false) {
  21.             return;
  22.         }
  23.  
  24.         Slicer2D slicer = GetComponent<Slicer2D> ();
  25.         if (slicer != null) {
  26.             slicer.AddResultEvent (OnSliceResult);
  27.             slicer.AddEvent (OnSlice);
  28.         }
  29.  
  30.         foreach(Collider2D collider in anchorColliders) {
  31.             polygons.Add(Polygon.CreateFromCollider (collider.gameObject));
  32.             colliders.Add(collider);
  33.         }
  34.     }
  35.  
  36.     Polygon GetPolygonInWorldSpace(Polygon poly) {
  37.         return(poly.ToWorldSpace(colliders[polygons.IndexOf(poly)].transform));
  38.     }
  39.  
  40.     bool OnSlice(Slice2D sliceResult)
  41.     {
  42.         switch (anchorType) {
  43.             case AnchorType.CancelSlice:
  44.                 foreach (Polygon polyA in new List<Polygon>(sliceResult.polygons)) {
  45.                     bool perform = true;
  46.                     foreach(Polygon polyB in polygons) {
  47.                         if (MathHelper.PolyCollidePoly (polyA.pointsList, GetPolygonInWorldSpace(polyB).pointsList) ) {
  48.                             perform = false;
  49.                         }
  50.                     }
  51.                     if (perform) {
  52.                         return(false);
  53.                     }
  54.                 }
  55.                 break;
  56.  
  57.             default:
  58.                 break;
  59.         }
  60.         return(true);
  61.     }
  62.  
  63.     void OnSliceResult(Slice2D sliceResult)
  64.     {
  65.         if (polygons.Count < 1)
  66.             return;
  67.  
  68.         foreach (GameObject p in sliceResult.gameObjects) {
  69.             Polygon polyA = Polygon.CreateFromCollider (p).ToWorldSpace (p.transform);
  70.             bool perform = true;
  71.  
  72.             foreach(Polygon polyB in polygons) {
  73.                 if (MathHelper.PolyCollidePoly (polyA.pointsList, GetPolygonInWorldSpace(polyB).pointsList)) {
  74.                     perform = false;
  75.                 }
  76.             }
  77.  
  78.             if (perform) {
  79.                 switch (anchorType) {
  80.                     case AnchorType.AttachRigidbody:
  81.                         if (p.GetComponent<Rigidbody2D> () == null) {
  82.                             p.AddComponent<Rigidbody2D> ();
  83.                         }
  84.  
  85.                         p.GetComponent<Rigidbody2D> ().isKinematic = false;
  86.                         break;
  87.                     default:
  88.                         break;
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement