Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1.  
  2. public class Slicer2DAI : MonoBehaviour {
  3.     Slicer2DInputController controller;
  4.  
  5.     void Start () {
  6.         // Getting slicer input controller
  7.         controller = GetComponent<Slicer2DController>().input;
  8.        
  9.         // Enabling input events programming
  10.         controller.SetRawInput(false);
  11.     }
  12.    
  13.     void Update () {
  14.         // Generate new input event if there are no controller actions left
  15.         if (controller.Playing()) {
  16.             return;
  17.         }
  18.  
  19.         // Slicing the biggest object in the scene only
  20.         Slicer2D biggestObject = GetBiggestObject();
  21.  
  22.         if (biggestObject != null) {
  23.             controller.ClearActions();
  24.  
  25.             Polygon2D poly = biggestObject.GetPolygon().ToWorldSpace(biggestObject.transform);
  26.  
  27.             // Get center of the object
  28.             Vector2D center = new Vector2D(poly.GetBounds().center);
  29.  
  30.             // Predict rigidbody movement
  31.             Rigidbody2D rb = biggestObject.GetComponent<Rigidbody2D>();
  32.             float vely = rb.velocity.y;
  33.             center.y += vely / 3;
  34.  
  35.             // Random slice rotation
  36.             double sliceRotation = Random.Range(0f, Mathf.PI * 2);
  37.  
  38.             // Get bounds of an object to know the slice size
  39.             Rect bounds = poly.GetBounds();
  40.             float sliceSize = Mathf.Sqrt(bounds.width * bounds.width + bounds.height * bounds.height) * 0.55f;
  41.  
  42.             Vector2D firstPoint = center.Copy();
  43.             firstPoint.Push(sliceRotation, sliceSize);
  44.  
  45.             Vector2D secondPoint = center.Copy();
  46.             secondPoint.Push(sliceRotation, -sliceSize);
  47.  
  48.             // How fast to perform actions?
  49.             float actionTime = 0.125f;
  50.  
  51.             controller.SetMouse(firstPoint.ToVector2(), actionTime);
  52.             controller.PressMouse(actionTime);
  53.             controller.MoveMouse(secondPoint.ToVector2(), actionTime);
  54.             controller.ReleaseMouse(actionTime);
  55.  
  56.             controller.Play();
  57.         }
  58.     }
  59.  
  60.     // Getting the biggest object in the scene
  61.     Slicer2D GetBiggestObject() {
  62.         Slicer2D obj = null;
  63.         double area = -1e+10f;
  64.  
  65.         foreach(Slicer2D slicer in Slicer2D.GetList()) {
  66.             Polygon2D poly = slicer.GetPolygon();
  67.             if (poly.GetArea() > area) {
  68.                 obj = slicer;
  69.                 area = poly.GetArea();
  70.             }
  71.         }
  72.  
  73.         return(obj);
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement