/// <summary> /// Method that RESETS the whole Game Logic /// </summary> public void ResetGame() { // Reset FLAGs IsCalibrated = false; GameEnabled = false; // Reset Ball Position Field.BallPosition_X_W = 1; Field.BallPosition_Y_H = 1; Old_Value_X = 0; Old_Value_Y = 0; ball_idx_x = 0; ball_idx_y = 0; move_x = 0.0; move_y = 0.0; // Trigger Event so ViewModel can actually reset ball Position BallPositionHasChanged?.Invoke(this, EventArgs.Empty); }
/// <summary> /// Method that calculates and sets the new ball position depending on input motion values /// </summary> private void SetBallPosition() { bool CollisionDetectedX = false; bool CollisionDetectedY = false; // Calculate movement of ball int Diff_X = Old_Value_X + Data.Axis_X; int Diff_Y = Old_Value_Y + Data.Axis_Y; // Save new values Old_Value_X = Data.Axis_X; Old_Value_Y = Data.Axis_Y; move_x = Diff_X / 5000.0; move_x = Math.Round(move_x, 1); move_x = -move_x; move_y = Diff_Y / 5000.0; move_y = Math.Round(move_y, 1); move_y = -move_y; // keep the movement in ranges +/-1 if (move_x > 1) { move_x = 1; } else if (move_x < -1) { move_x = -1; } if (move_y > 1) { move_y = 1; } else if (move_y < -1) { move_y = -1; } // Prevent ball from hitting obstacle (border) var new_pos_x = Field.BallPosition_X_W + move_x; var new_pos_y = Field.BallPosition_Y_H + move_y; ball_idx_x = (int)Math.Round(Field.BallPosition_X_W, 0); ball_idx_y = (int)Math.Round(Field.BallPosition_Y_H, 0); // calculate if there was a collsion in x direction // get movement direction int dir = 1; if (move_x < 0) { dir = -1; } else if (move_x > 0) { dir = 1; } // Get obstacle next to ball GameField.GameElements obstacle = Field.PlayField[ball_idx_x + dir, ball_idx_y]; // do collision detection if it is a border if (obstacle == GameField.GameElements.Border) { // Calc Distance var dist = Math.Abs((new_pos_x) - (ball_idx_x + dir)) + .1; var radius_obstacle = Dimensions[(int)obstacle].Width / 2; var radius_ball = Dimensions[Dimensions.Length - 1].Width / 2; var minimum = radius_ball + radius_obstacle; // if the distance is smaller than the minimun distance -> a collsision was detected if (dist < minimum) { // set the balls position right next to the obstacle Field.BallPosition_X_W = ball_idx_x + dir * (0.5 - radius_ball) + .1; CollisionDetectedX = true; } } // calculate if there was a collsion in y direction // get movement direction dir = 1; if (move_y < 0) { dir = -1; } else if (move_y > 0) { dir = 1; } // Get obstacle next to ball obstacle = Field.PlayField[ball_idx_x, ball_idx_y + dir]; // do collision detection if it is a border if (obstacle == GameField.GameElements.Border) { // Calc Distance var dist = Math.Abs((new_pos_y) - (ball_idx_y + dir)) + .1; var radius_obstacle = Dimensions[(int)obstacle].Height / 2; var radius_ball = Dimensions[Dimensions.Length - 1].Height / 2; var minimum = radius_ball + radius_obstacle; // if the distance is smaller than the minimun distance -> a collsision was detected if (dist < minimum) { // set the balls position right next to the obstacle Field.BallPosition_Y_H = ball_idx_y + dir * (0.5 - radius_ball) + .1; CollisionDetectedY = true; } } // Move Ball if (!CollisionDetectedX) { Field.BallPosition_X_W += move_x; } if (!CollisionDetectedY) { Field.BallPosition_Y_H += move_y; } // If Ball is EITHER in HOLE or in FINISH --> no need to move it anymore --> Game is OVER if (IsBallInHole() || IsBallInFinish()) { return; } // Trigger Event --> inform ViewModel that Ball Position has changed BallPositionHasChanged?.Invoke(this, EventArgs.Empty); }