public LevelTraining() { Height = 50; Width = 50; Level = new GameField.GameElements[Height, Width]; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { // edges are filled with borders if (i == 0 || j == 0 || i == Height - 1 || j == Width - 1) { Level[i, j] = GameField.GameElements.Border; } else { Level[i, j] = GameField.GameElements.Empty; } } } // set finish in the right upper edge Level[Height - 2, Width - 2] = GameField.GameElements.Finish; }
public LevelBeginner() { const int height = 20; const int width = 20; Height = height; Width = width; Level = new GameField.GameElements[Height, Width]; // for better visibility char[][] arr = new char[height][] { ("********************").ToCharArray(), ("* O** F*").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *** *").ToCharArray(), ("* O** *** *").ToCharArray(), ("* O** *** *").ToCharArray(), ("* O** *** *").ToCharArray(), ("* O** *** *").ToCharArray(), ("* O** *** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* O** *").ToCharArray(), ("* *").ToCharArray(), ("********************").ToCharArray() }; // convert this array to the right type for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { GameField.GameElements elem = new GameField.GameElements(); if (arr[i][j] == ' ') { elem = GameField.GameElements.Empty; } else if (arr[i][j] == '*') { elem = GameField.GameElements.Border; } else if (arr[i][j] == 'O') { elem = GameField.GameElements.Hole; } else if (arr[i][j] == 'F') { elem = GameField.GameElements.Finish; } Level[j, i] = elem; } } }
/// <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); }