Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Slicer2DAI : MonoBehaviour {
- Slicer2DInputController controller;
- void Start () {
- // Getting slicer input controller
- controller = GetComponent<Slicer2DController>().input;
- // Enabling input events programming
- controller.SetRawInput(false);
- }
- void Update () {
- // Generate new input event if there are no controller actions left
- if (controller.Playing()) {
- return;
- }
- // Slicing the biggest object in the scene only
- Slicer2D biggestObject = GetBiggestObject();
- if (biggestObject != null) {
- controller.ClearActions();
- Polygon2D poly = biggestObject.GetPolygon().ToWorldSpace(biggestObject.transform);
- // Get center of the object
- Vector2D center = new Vector2D(poly.GetBounds().center);
- // Predict rigidbody movement
- Rigidbody2D rb = biggestObject.GetComponent<Rigidbody2D>();
- float vely = rb.velocity.y;
- center.y += vely / 3;
- // Random slice rotation
- double sliceRotation = Random.Range(0f, Mathf.PI * 2);
- // Get bounds of an object to know the slice size
- Rect bounds = poly.GetBounds();
- float sliceSize = Mathf.Sqrt(bounds.width * bounds.width + bounds.height * bounds.height) * 0.55f;
- Vector2D firstPoint = center.Copy();
- firstPoint.Push(sliceRotation, sliceSize);
- Vector2D secondPoint = center.Copy();
- secondPoint.Push(sliceRotation, -sliceSize);
- // How fast to perform actions?
- float actionTime = 0.125f;
- controller.SetMouse(firstPoint.ToVector2(), actionTime);
- controller.PressMouse(actionTime);
- controller.MoveMouse(secondPoint.ToVector2(), actionTime);
- controller.ReleaseMouse(actionTime);
- controller.Play();
- }
- }
- // Getting the biggest object in the scene
- Slicer2D GetBiggestObject() {
- Slicer2D obj = null;
- double area = -1e+10f;
- foreach(Slicer2D slicer in Slicer2D.GetList()) {
- Polygon2D poly = slicer.GetPolygon();
- if (poly.GetArea() > area) {
- obj = slicer;
- area = poly.GetArea();
- }
- }
- return(obj);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement