/// <summary> /// Triggers a OnNewBall event /// </summary> public void BroadcastBallSpawnEvent(PingPongBall ball) { if (OnNewBall != null) { OnNewBall(ball); } }
/// <summary> /// Triggers a OnRemoveBall event /// </summary> public void BroadcastBallRemoveEvent(PingPongBall ball) { if (OnRemoveBall != null) { OnRemoveBall(ball); } }
private void Playground_ContentRendered(object sender, EventArgs e) { Background = new ImageBrush { ImageSource = new BitmapImage(new Uri(System.IO.Path.GetFullPath("Assets/Images/earthBackgr.jpg"), UriKind.Absolute)) }; GamePaddle = Paddle.getInstance(GameArea); GamePaddle.Draw(GameArea); PingPongBall.Draw(GameArea); bdrWelcomePanel.Visibility = Visibility.Visible; }
/// <summary> /// OnTriggerEnter is called when the Collider other enters the trigger. /// </summary> /// <param name="other">The other Collider involved in this collision.</param> void OnTriggerEnter(Collider other) { // not a ball, don't care if (other.gameObject.tag != "Ball") { return; } // trigger score event int scoringSlot = OwningSlot == 1 ? 0 : 1; EventMngr.BroadcastGoalEvent(scoringSlot); // destory ball and trigger event PingPongBall ball = other.gameObject.GetComponent <PingPongBall>(); EventMngr.BroadcastBallRemoveEvent(ball); GameObject.Destroy(other.gameObject); }
/// <summary> /// Finds the closest ball /// </summary> /// <returns></returns> private PingPongBall FindClosestBall() { PingPongBall closest = null; float minDist = 0f; foreach (PingPongBall b in Balls) { Vector3 diff = b.gameObject.transform.position - gameObject.transform.position; float distSquared = diff.sqrMagnitude; if (distSquared > minDist) { minDist = distSquared; closest = b; } } return(closest); }
/// <summary> /// Update is called every frame, if the MonoBehaviour is enabled. /// </summary> void Update() { // find closest ball (highest danger) PingPongBall ball = FindClosestBall(); // no ball to track if (ball == null) { return; } Target.z = ball.gameObject.transform.position.z; float tDist = _paddle.Speed / Vector3.Distance(_paddle.MaxPoint, _paddle.MinPoint); tDist *= Time.deltaTime; // don't move if already closely aligned if (Mathf.Abs(gameObject.transform.position.z - Target.z) < 1f) { return; } // move up if (gameObject.transform.position.z < Target.z) { _paddle.TValue += tDist; _paddle.TValue = _paddle.TValue > 1f ? 1f : _paddle.TValue; } // move down else if (gameObject.transform.position.z > Target.z) { _paddle.TValue -= tDist; // min _t at 0 _paddle.TValue = _paddle.TValue < 0f ? 0f : _paddle.TValue; } }
private void GameTicker_Tick(object sender, EventArgs e) { PingPongBall.Move(GamePaddle); PingPongBall.Draw(GameArea); DetectCollision(); }
/// <summary> /// Removes a ball from the list /// </summary> /// <param name="ball"></param> private void RemoveBall(PingPongBall ball) { Balls.Remove(ball); }
/// <summary> /// Adds a ball to the list /// </summary> /// <param name="ball"></param> private void AddBall(PingPongBall ball) { Balls.Add(ball); }