public void ResetBallPosition() { if (!this.GetComponent <Rigidbody2D>()) { return; } if (customizationManager.HasTrail()) { gameObject.GetComponent <TrailRenderer>().time = 0f; } this.transform.position = startPos; this.GetComponent <Rigidbody2D>().velocity = new Vector2(0, 0); Destroy(this.GetComponent <Rigidbody2D>()); GameObject.Find("BottomHole").GetComponent <Hole>().Kobe = true; }
// Use this for initialization void Start() { customizationManager = GameObject.Find("CustomizationManager").GetComponent <CustomizationManager>(); startPos = this.transform.position; botY = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, 0, 0)).y - 3; topY = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)).y + 3; rightX = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, 0)).x + 3; leftX = Camera.main.ScreenToWorldPoint(Vector3.zero).x - 3; // Setting the ball's custom attributes gameObject.AddComponent <SpriteRenderer>(customizationManager.GetCurrentBall()); gameObject.GetComponent <SpriteRenderer>().sortingOrder = 10; noGrav = GameObject.FindGameObjectsWithTag("NoGrav"); if (customizationManager.HasTrail()) { gameObject.AddComponent <TrailRenderer>(customizationManager.GetCurrentTrail()); } }
// Update is called once per frame void Update() { toggle = tool.toggle; // Delete the last user placed item if there is one // Don't delete if in shoot mode if (Input.GetKeyDown(KeyCode.Z) && uiController.placedObjects.Count > 0 && toggle != ToggleState.Shoot) { //Destroy(lines[lines.Count - 1]); //lines.RemoveAt(lines.Count - 1); uiController.UndoLastPlacement(); } // quick reference to the current line we are drawing if (toggle == ToggleState.Line) { LineRenderer l = null; if (lineRef != null) { l = lineRef.GetComponent <LineRenderer>(); } Vector3 mousPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f)); // while holding down the mouse or finger //Check to see if there's still line juice GameObject[] noDraw = GameObject.FindGameObjectsWithTag("NoDraw"); bool drawable = true; foreach (GameObject g in noDraw) { if (g.GetComponent <BoxCollider2D>().bounds.Contains(new Vector3(mousPos.x, mousPos.y, 1))) { drawable = false; break; } } if (Input.GetMouseButton(0) && lineJuice > 0 && drawable) { // get the point // if this is the first frame of drawing if (!drawing) { if (CheckMouseInput()) { return; } // make a new line and save the reference lineRef = Instantiate(linePre, Vector3.zero, Quaternion.identity); drawing = true; l = lineRef.GetComponent <LineRenderer>(); // set the frst point of the line and add it to the list l.SetPosition(0, mousPos); lastPoint = mousPos; lines.Add(lineRef); return; } wasDrawing = true; // not first frame of drawing // have we moved enough to add another segment? if ((lastPoint - mousPos).magnitude < minDrawDistance) { return; } // add a new point at the current location l.positionCount++; l.SetPosition(l.positionCount - 1, mousPos); lastPoint = mousPos; //Decrement Line Juice lineJuice--; UpdateJuiceBar(); } // first frame after not drawing anymore else if (wasDrawing) { drawing = false; wasDrawing = false; if (l.positionCount == 1) { l.positionCount++; l.SetPosition(1, mousPos + new Vector3(0.1f, 0f, 0f)); } // set up the edge collider using the line points EdgeCollider2D e = lineRef.GetComponent <EdgeCollider2D>(); Vector2[] points = new Vector2[l.positionCount]; for (int i = 0; i < l.positionCount; i++) { points[i] = new Vector2(l.GetPosition(i).x, l.GetPosition(i).y); } e.points = points; // add a rigidbody for gravity if (lineHasPhysics) { lineRef.AddComponent <Rigidbody2D>(); } // Add it to the stack of user-placed items uiController.placedObjects.Add(lineRef); } // make the ball roll if (Input.GetKeyDown(KeyCode.Return)) { ball.AddComponent <Rigidbody2D>(); ball.GetComponent <Rigidbody2D>().mass = 0.2f; } } else if (toggle == ToggleState.Shoot) { if (ball.GetComponent <Rigidbody2D>()) { return; } if (Input.GetMouseButtonDown(0)) { if (CheckMouseInput()) { return; } mDown = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f)); powerLine = GameObject.Instantiate(powerLinePref, ball.transform.position, Quaternion.identity); wasShooting = true; } else if (Input.GetMouseButton(0)) { if (CheckMouseInput()) { return; } mCurrent = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f)); powerLine.transform.localScale = new Vector3((mDown.x - mCurrent.x) / 2, .25f, 0); } else if (Input.GetMouseButtonUp(0) || wasShooting) { if (CheckMouseInput()) { return; } Destroy(powerLine); GetComponent <AudioSource>().Play(); mUp = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f)); ball.AddComponent <Rigidbody2D>(); ball.GetComponent <Rigidbody2D>().mass = 0.2f; ball.GetComponent <Rigidbody2D>().AddForce(new Vector2(((mDown.x - mUp.x) * dragForce), 0)); if (customizationManager.HasTrail()) { ball.GetComponent <TrailRenderer>().time = customizationManager.trailtime; } mDown = Vector3.zero; mCurrent = Vector3.zero; mUp = Vector3.zero; wasShooting = false; } } }