public void OnRenderObject() { if (Camera.current != Camera.main) { return; } Max2D.SetLineWidth(0.25f); Max2D.SetColor(Color.black); Max2D.SetBorder(false); Max2D.SetLineMode(Max2D.LineMode.Smooth); inspectorPosition = Vector3.zero; Vector2D pos = new Vector2D(Camera.main.ScreenToWorldPoint(Input.mousePosition)); foreach (Slicer2D slicer in Slicer2D.GetList()) { Polygon2D poly = slicer.GetPolygon().ToWorldSpace(slicer.transform); if (poly.PointInPoly(pos)) { Rect rect = poly.GetBounds(); Max2D.DrawLineRectf(rect.x, rect.y, rect.width, rect.height); Max2D.DrawLinef(rect.center.x, rect.center.y, rect.center.x, rect.center.y + rect.height / 2 + 1); inspectorPosition = new Vector2(rect.center.x, rect.center.y + rect.height / 2); originalSize = slicer.GetComponent <Slicer2DInspectorTracker>().originalSize; currentSize = poly.GetArea(); sliced = slicer.sliceCounter; } } }
public bool ItersectsWithMap() { Polygon2D edges = GetEdges().ToWorldSpace(gameObject.transform); bool intersect = false; foreach (Slicer2D slicer in Slicer2D.GetList()) { Polygon2D polyB = slicer.GetPolygon().ToWorldSpace(slicer.gameObject.transform); if (Math2D.SliceIntersectPoly(edges.pointsList, polyB)) { intersect = true; } foreach (Vector2D p in edges.pointsList) { if (Math2D.PointInPoly(p, polyB)) { return(true); } } } return(intersect); }
void Update() { Polygon2D cameraPolygon = Polygon2D.CreateFromCamera(Camera.main); cameraPolygon = cameraPolygon.ToRotation(Camera.main.transform.rotation.eulerAngles.z * Mathf.Deg2Rad); cameraPolygon = cameraPolygon.ToOffset(new Vector2D(Camera.main.transform.position)); foreach (Slicer2D slicer in Slicer2D.GetList()) { if (Math2D.PolyCollidePoly(slicer.GetPolygon().ToWorldSpace(slicer.transform), cameraPolygon) == false) { if (slicer.enabled == true) { lives--; if (lives >= 0) { SpriteRenderer sr = livesObjects[lives].GetComponent <SpriteRenderer>(); sr.color = Color.white; } else { Debug.Log("lose"); } } Destroy(slicer.gameObject); } } scoreText.text = score.ToString(); }
public void Update(Vector2 position, float minVertexDistance = 1f) { foreach (Slicer2D slicer in Slicer2D.GetList()) { LinearSlicerTrackerObject tracker = GetSlicerTracker(slicer); if (tracker == null) { tracker = new LinearSlicerTrackerObject(); tracker.slicer = slicer; trackerList.Add(tracker); } Vector2D trackedPos = new Vector2D(slicer.transform.transform.InverseTransformPoint(position)); if (tracker.lastPosition != null) { if (slicer.GetPolygon().PointInPoly(trackedPos)) { if (tracker.tracking == false) { //tracker.pointsList.Add(tracker.lastPosition); tracker.firstPosition = tracker.lastPosition; } tracker.tracking = true; //if (tracker.pointsList.Count < 1 || (Vector2D.Distance (trackedPos, tracker.pointsList.Last ()) > minVertexDistance / 4f)) { // tracker.pointsList.Add(trackedPos); //} } else if (tracker.tracking == true) { tracker.tracking = false; //tracker.pointsList.Add(trackedPos); if (tracker.firstPosition != null) { tracker.lastPosition = trackedPos; Pair2D slicePair = new Pair2D(new Vector2D(slicer.transform.TransformPoint(tracker.firstPosition.ToVector2())), new Vector2D(slicer.transform.TransformPoint(tracker.lastPosition.ToVector2()))); Slice2D slice = slicer.LinearSlice(slicePair); if (slice.gameObjects.Count > 0) { CopyTracker(slice, slicer); } ; } trackerList.Remove(tracker); } } if (tracker != null) { tracker.lastPosition = trackedPos; } } }
public void UpdateCurrentArea() { currentArea = 0f; foreach (Slicer2D slicer in Slicer2D.GetList()) { currentArea += slicer.GetPolygon().GetArea(); } }
void Start() { foreach (Slicer2D slicer in Slicer2D.GetList()) { startingArea += Polygon2DList.CreateFromGameObject(slicer.gameObject)[0].ToWorldSpace(slicer.transform).GetArea(); } instance = this; }
public List <Slice2D> Update(Vector2D position, float timer) { List <Slice2D> result = new List <Slice2D>(); foreach (Slicer2D slicer in Slicer2D.GetList()) { SlicerTrailObject trail = GetSlicerTrail(slicer); if (trail == null) { trail = new SlicerTrailObject(); trail.slicer = slicer; trailList.Add(trail); } if (trail.lastPosition != null) { if (Vector2D.Distance(trail.lastPosition, position) > 0.05f) { trail.pointsList.Add(new TrailPoint(position, timer)); } } else { trail.pointsList.Add(new TrailPoint(position, timer)); } if (trail.pointsList.Count > 1) { foreach (TrailPoint trailPoint in new List <TrailPoint>(trail.pointsList)) { if (trailPoint.Update() == false) { trail.pointsList.Remove(trailPoint); } } List <Vector2D> points = new List <Vector2D>(); foreach (TrailPoint trailPoint in trail.pointsList) { points.Add(trailPoint.position); } Slicer2D.complexSliceType = Slicer2D.SliceType.Regular; Slice2D slice = slicer.ComplexSlice(points); if (slice.gameObjects.Count > 0) { trailList.Remove(trail); result.Add(slice); } ; } trail.lastPosition = position; } return(result); }
public void ResetLevel() { startingArea = 0; foreach (Slicer2D slicer in Slicer2D.GetList()) { startingArea += slicer.shape.GetWorld().GetArea(); } }
public void Update(Vector2 position, float minVertsDistance = 1f) { foreach (Slicer2D slicer in Slicer2D.GetList()) { ComplexSlicerTrackerObject tracker = GetSlicerTracker(slicer); if (tracker == null) { tracker = new ComplexSlicerTrackerObject(); tracker.slicer = slicer; trackerList.Add(tracker); } Vector2D trackedPos = new Vector2D(slicer.transform.transform.InverseTransformPoint(position)); if (tracker.lastPosition != null) { if (slicer.GetPolygon().PointInPoly(trackedPos)) { if (tracker.tracking == false) { tracker.pointsList.Add(tracker.lastPosition); } tracker.tracking = true; if (tracker.pointsList.Count < 1 || (Vector2D.Distance(trackedPos, tracker.pointsList.Last()) > minVertsDistance / 4f)) { tracker.pointsList.Add(trackedPos); } } else if (tracker.tracking == true) { tracker.tracking = false; tracker.pointsList.Add(trackedPos); List <Vector2D> slicePoints = new List <Vector2D>(); foreach (Vector2D point in tracker.pointsList) { slicePoints.Add(new Vector2D(slicer.transform.TransformPoint(point.vector))); } Slice2D slice = slicer.ComplexSlice(slicePoints); if (slice.gameObjects.Count > 0) { CopyTracker(slice, slicer); } ; trackerList.Remove(tracker); } } if (tracker != null) { tracker.lastPosition = trackedPos; } } }
// Recalculate area that is left static public void UpdateLevelProgress() { instance.leftArea = 0; foreach (Slicer2D slicer in Slicer2D.GetList()) { instance.leftArea += slicer.shape.GetWorld().GetArea(); } instance.leftArea = ((instance.leftArea) / instance.startingArea) * 100f; }
void OnCollisionEnter2D(Collision2D collision) { if (collision.collider.name.Contains("Terrain")) { Vector2f pos = new Vector2f(transform.position); Polygon.defaultCircleVerticesCount = 15;; Polygon slicePolygon = Polygon.Create(Polygon.PolygonType.Circle, 2f); Polygon slicePolygonDestroy = null; Polygon sliceDestroy = null; slicePolygonDestroy = Polygon.Create(Polygon.PolygonType.Circle, 2.5f); sliceDestroy = new Polygon(); foreach (Vector2f id in slicePolygonDestroy.pointsList) { sliceDestroy.AddPoint(new Vector2f(id.Get() + pos.Get())); } Polygon slice = new Polygon(); foreach (Vector2f id in slicePolygon.pointsList) { slice.AddPoint(new Vector2f(id.Get() + pos.Get())); } foreach (Slicer2D id in Slicer2D.GetList()) { Slice2D result = id.PolygonSlice2(slice); // Why not use Slice All? if (result.polygons.Count > 0) { foreach (Polygon p in new List <Polygon>(result.polygons)) { if (sliceDestroy.PolyInPoly(p) == true) { result.polygons.Remove(p); } } if (result.polygons.Count > 0) { id.PerformResult(result.polygons); } else { Destroy(id.gameObject); } } } Destroy(gameObject); Polygon.defaultCircleVerticesCount = 25; } }
bool PointInObjects(Vector2D pos) { foreach (Slicer2D slicer in Slicer2D.GetList()) { if (slicer.GetPolygon().PointInPoly(pos.InverseTransformPoint(slicer.transform))) { return(true); } } return(false); }
void SliceEvent(Slice2D slice) { Vector2D midPoint = SliceMidPoint(slice.slices[0]); foreach (Slicer2D g in Slicer2D.GetList()) { Vector2D center = new Vector2D(Polygon2DList.CreateFromGameObject(g.gameObject)[0].ToWorldSpace(g.transform).GetBounds().center); Vector2D position = new Vector2D(g.transform.position); position.Push(Vector2D.Atan2(center, midPoint), 0.2f); // + Mathf.PI / 2 g.transform.position = position.ToVector2(); } }
// Recalculate area that is left static public void UpdateLevelBar() { instance.leftArea = 0; foreach (Slicer2D slicer in Slicer2D.GetList()) { Polygon2D poly = Polygon2DList.CreateFromGameObject(slicer.gameObject)[0]; instance.leftArea += poly.ToWorldSpace(slicer.gameObject.transform).GetArea(); } instance.leftArea = ((instance.leftArea) / instance.startingArea) * 100f; }
static public Slicer2D PointInSlicerComponent(Vector2D point) { foreach (Slicer2D slicer in Slicer2D.GetList()) { Polygon2D poly = slicer.shape.GetWorld(); if (poly.PointInPoly(point)) { return(slicer); } } return(null); }
private bool InSlicerComponents(Vector2D point) { foreach (Slicer2D slicer in Slicer2D.GetList()) { Polygon2D poly = slicer.GetPolygon().ToWorldSpace(slicer.transform); if (poly.PointInPoly(point)) { return(true); } } return(false); }
// Recalculate area that is left public void UpdateText() { leftArea = 0; foreach (Slicer2D slicer in Slicer2D.GetList()) { Polygon2D poly = Polygon2D.CreateFromCollider(slicer.gameObject); leftArea += poly.ToWorldSpace(slicer.gameObject.transform).GetArea(); } leftArea = ((leftArea) / startingArea) * 100f; text.text = "Left: " + (int)leftArea + "%"; }
void Update() { piecesCount = Slicer2D.GetListCount(); slicesText.text = "Slices: " + slicesCount + " / " + Levels[currentLevelID].allowedSlices; piecesText.text = "Pieces: " + piecesCount + " / " + Levels[currentLevelID].allowedPieces; Vector3 pos = Camera.main.transform.position; pos.x = pos.x * 0.95f + (float)(currentLevelID * 50f) * 0.05f; Camera.main.transform.position = pos; if (Slicer2D.GetList().Count < 1) { return; } if (slicesCount <= Levels[currentLevelID].allowedSlices && piecesCount == Levels[currentLevelID].allowedPieces) { foreach (Slicer2D slicer in Slicer2D.GetListCopy()) { slicer.gameObject.AddComponent <GreenFade>(); DestroyObject(slicer); garbage.Add(slicer.gameObject); } UIFade.instance.state = true; UIFade.instance.menuObjects[0].SetActive(false); UIFade.instance.menuObjects[1].SetActive(false); UIFade.instance.menuObjects[2].SetActive(true); //currentLevelID++; //Levels[currentLevelID].level.SetActive(true); //slicesCount = 0; } else if (slicesCount > Levels[currentLevelID].allowedSlices) { foreach (Slicer2D slicer in Slicer2D.GetListCopy()) { GreenFade fade = slicer.gameObject.AddComponent <GreenFade>(); fade.fadeTyp = GreenFade.FadeType.Red; DestroyObject(slicer); garbage.Add(slicer.gameObject); } UIFade.instance.menuObjects[0].SetActive(false); UIFade.instance.menuObjects[1].SetActive(true); UIFade.instance.menuObjects[2].SetActive(false); UIFade.instance.state = true; } }
public void BallToMapCollision() { Vector2D position = new Vector2D(transform.position); // Balls vs Map Collisions foreach (Slicer2D slicer in Slicer2D.GetList()) { foreach (Pair2D id in Pair2D.GetList(slicer.shape.GetWorld().pointsList)) { if (Math2D.Circle.IntersectLine(id, position, radius) == true) { transform.Translate(direction * -speed); SetDirection(Math2D.ReflectAngle(direction, (float)Vector2D.Atan2(id.A, id.B))); transform.Translate(direction * speed); } } } }
void OnCollisionEnter2D(Collision2D collision) { if (collision.collider.name.Contains("Terrain")) { Vector2D pos = new Vector2D(transform.position); Polygon2D.defaultCircleVerticesCount = 15; Polygon2D slicePolygon = Polygon2D.Create(Polygon2D.PolygonType.Circle, 2f); Polygon2D slicePolygonDestroy = Polygon2D.Create(Polygon2D.PolygonType.Circle, 2.5f); slicePolygon = slicePolygon.ToOffset(pos); slicePolygonDestroy = slicePolygonDestroy.ToOffset(pos); foreach (Slicer2D id in Slicer2D.GetList()) { Slice2D result = Slicer2D.API.PolygonSlice(id.GetPolygon().ToWorldSpace(id.transform), slicePolygon); if (result.polygons.Count > 0) { foreach (Polygon2D p in new List <Polygon2D>(result.polygons)) { if (slicePolygonDestroy.PolyInPoly(p) == true) { result.polygons.Remove(p); } } if (result.polygons.Count > 0) { id.PerformResult(result.polygons, new Slice2D()); } else { // Polygon is Destroyed!!! Destroy(id.gameObject); } } } Destroy(gameObject); Polygon2D.defaultCircleVerticesCount = 25; } }
Slicer2D GetSlicerInZone() { foreach (Slicer2D slicer in Slicer2D.GetList()) { if (slicer.limit.counter > 0) { continue; } if (Math2D.PolyCollidePoly(slicer.shape.GetWorld(), GetAIZone()) == false) { continue; } return(slicer); } return(null); }
void Update() { Polygon2D cameraPolygon = Polygon2D.CreateFromCamera(Camera.main); cameraPolygon = cameraPolygon.ToRotation(Camera.main.transform.rotation.eulerAngles.z * Mathf.Deg2Rad); cameraPolygon = cameraPolygon.ToOffset(new Vector2D(Camera.main.transform.position)); foreach (Slicer2D slicer in Slicer2D.GetList()) { if (Math2D.PolyCollidePoly(slicer.GetPolygon().ToWorldSpace(slicer.transform), cameraPolygon) == false) { Destroy(slicer.gameObject); } } UpdateCurrentArea(); int percent = (int)((currentArea / startingArea) * 100); percentText.text = "Left: " + percent + "%"; }
// This manages ball movement and collisions with level walls void UpdateMovement() { transform.Translate(direction * speed); float ballSize = 1; // Balls vs Map Collisions foreach (Slicer2D slicer in Slicer2D.GetList()) { foreach (Pair2D id in Pair2D.GetList(slicer.GetPolygon().ToWorldSpace(slicer.transform).pointsList)) { if (Math2D.LineIntersectCircle(id, new Vector2D(transform.position), ballSize) == true) { transform.Translate(direction * -speed); SetDirection(Math2D.ReflectAngle(direction, (float)Vector2D.Atan2(id.A, id.B))); transform.Translate(direction * speed); } } } // Balls vs Balls Collision foreach (ThinSliceBall ball in ThinSliceBall.GetList()) { if (ball == this) { continue; } if (Vector2.Distance(transform.position, ball.transform.position) < ballSize + ballSize) { ball.direction = Vector2D.RotToVec(Vector2D.Atan2(transform.position, ball.transform.position) - Mathf.PI).ToVector2(); direction = Vector2D.RotToVec(Vector2D.Atan2(transform.position, ball.transform.position)).ToVector2(); ball.transform.Translate(ball.direction * ball.speed); transform.Translate(direction * speed); } } }
public List <Slice2D> Update(Vector2D position, float timer) { List <Slice2D> result = new List <Slice2D>(); foreach (Slicer2D slicer in Slicer2D.GetList()) { SlicerTrailObject trail = GetSlicerTrail(slicer); if (trail == null) { trail = new SlicerTrailObject(); trail.slicer = slicer; trailList.Add(trail); } if (trail.lastPosition != null) { if (Vector2D.Distance(trail.lastPosition, position) > 0.05f) { trail.pointsList.Insert(0, new TrailPoint(position, timer)); } } else { trail.pointsList.Insert(0, new TrailPoint(position, timer)); } foreach (TrailPoint trailPoint in new List <TrailPoint>(trail.pointsList)) { if (trailPoint.Update() == false) { trail.pointsList.Remove(trailPoint); } } if (trail.pointsList.Count > 1) { Vector2D firstPoint = null; Vector2D lastPoint = null; bool insideState = false; foreach (TrailPoint trailPoint in trail.pointsList) { bool inside = false; if (slicer.GetPolygon().PointInPoly(trailPoint.position.InverseTransformPoint(slicer.transform))) { inside = true; } switch (insideState) { case true: // Slice! if (inside == false) { lastPoint = trailPoint.position; insideState = false; break; } break; case false: if (inside == false) { // Searching For Start of Slice firstPoint = trailPoint.position; insideState = true; } break; } if (lastPoint != null) { break; } } if (firstPoint != null && lastPoint != null) { Slicer2D.complexSliceType = Slicer2D.SliceType.Regular; Slice2D slice = slicer.LinearSlice(new Pair2D(firstPoint, lastPoint)); if (slice.gameObjects.Count > 0) { trailList.Remove(trail); result.Add(slice); } ; } } trail.lastPosition = position; } return(result); }
public void Update() { input.Update(); if (visuals.drawSlicer == false) { return; } if (linearPair.A.ToVector2() == Vector2.zero && linearPair.B.ToVector2() == Vector2.zero) { return; } visuals.Clear(); visuals.GenerateLinearMesh(linearPair, transform); visuals.Draw(); if (target != null) { Polygon2D poly = target.shape.GetWorld(); int pointIDA = ((verticeID - 1) + poly.pointsList.Count) % poly.pointsList.Count; int pointIDB = verticeID; int pointIDC = (verticeID + 1) % poly.pointsList.Count; Vector2 pointA = poly.pointsList[pointIDA].ToVector2(); Vector2 pointB = poly.pointsList[pointIDB].ToVector2(); Vector2 pointC = poly.pointsList[pointIDC].ToVector2(); double angle = Math2D.FindAngle(pointA, pointB, pointC); Vector2D offset = new Vector2D(pointB); double angleZero = Vector2D.Atan2(new Vector2D(pointA), new Vector2D(pointB)); Debug.Log(angle * Mathf.Rad2Deg); offset.Push(-angle / 2 + angleZero, 0.5f); linearPair.A = offset; } if (Input.GetMouseButtonDown(1)) { Vector2D point = input.GetInputPosition(0); if (target != null) { Polygon2D poly = target.shape.GetWorld(); if (poly.PointInPoly(point) == false) { target = null; linearPair.A = Vector2D.Zero(); linearPair.B = Vector2D.Zero(); } } foreach (Slicer2D slicer in Slicer2D.GetList()) { Polygon2D poly = slicer.shape.GetWorld(); if (poly.PointInPoly(point)) { int id = 0; double distance = 1000000; foreach (Vector2D p in poly.pointsList) { double newDistance = Vector2D.Distance(p, point); if (newDistance < distance) { distance = newDistance; id = poly.pointsList.IndexOf(p); } } verticeID = id; target = slicer; break; } } } }