Browse Source

game::piece is no longer an Option

master
Inderjit Gill 1 year ago
parent
commit
7083c8188c
1 changed files with 93 additions and 110 deletions
  1. +93
    -110
      src/game.rs

+ 93
- 110
src/game.rs View File

@@ -117,10 +117,9 @@ pub struct Game {
board_offset: Block2D,

shapes: HashMap<ShapeKind, Shape>,
piece: Option<Piece>, // the current piece under player control
piece: Piece, // the current piece under player control
next_shape: ShapeKind,


is_game_over: bool,

score: i32,
@@ -151,14 +150,14 @@ impl Game {
board_offset: Block2D { x: 5, y: 5 },

shapes: define_shapes(),
piece: Some(Piece {
piece: Piece {
shape_kind: ShapeKind::J,
pos: BoardPos {
x: 5,
y: 19,
},
angle: PieceAngle::R0,
}),
},
next_shape: ShapeKind::L,

is_game_over: false,
@@ -223,43 +222,35 @@ impl Game {

self.terrible_rng = elapsed as i32;

if let Some(ref piece) = self.piece {
if let Some(shape) = self.shapes.get(&piece.shape_kind) {
remove_shape_from_board(&mut self.board, shape, &piece.pos, &piece.angle);
}
if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
remove_shape_from_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle);
}

apply_user_input(self, controller, delta);

if self.piece_is_dropping {
if let Some(ref mut piece) = self.piece {
if let Some(shape) = self.shapes.get(&piece.shape_kind) {
if !lower_piece(&self.board, shape, piece) {
// NOTE: don't lock in the shape when it stops dropping.
//
// the game 'feels' better if the user has time to modify the
// piece's position at the bottom of the drop
self.piece_is_dropping = false;
}
if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
if !lower_piece(&self.board, shape, &mut self.piece) {
// NOTE: don't lock in the shape when it stops dropping.
//
// the game 'feels' better if the user has time to modify the
// piece's position at the bottom of the drop
self.piece_is_dropping = false;
}
}
} else if is_time_to_lower_piece(self, delta) {

let mut piece_cant_go_lower = false;

if let Some(ref mut piece) = self.piece {
if let Some(shape) = self.shapes.get(&piece.shape_kind) {
piece_cant_go_lower = !lower_piece(&self.board, shape, piece);
}
if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
piece_cant_go_lower = !lower_piece(&self.board, shape, &mut self.piece);
}

if piece_cant_go_lower {
let mut num_lines = 0;
if let Some(ref piece) = self.piece {
if let Some(shape) = self.shapes.get(&piece.shape_kind) {
add_shape_to_board(&mut self.board, shape, &piece.pos, &piece.angle);
num_lines = remove_complete_lines(&mut self.board);
}
if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
add_shape_to_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle);
num_lines = remove_complete_lines(&mut self.board);
}

if num_lines > 0 {
@@ -273,10 +264,8 @@ impl Game {
}
}

if let Some(ref piece) = self.piece {
if let Some(shape) = self.shapes.get(&piece.shape_kind) {
add_shape_to_board(&mut self.board, shape, &piece.pos, &piece.angle);
}
if let Some(shape) = self.shapes.get(&self.piece.shape_kind) {
add_shape_to_board(&mut self.board, shape, &self.piece.pos, &self.piece.angle);
}

true
@@ -306,22 +295,18 @@ fn check_for_game_over(game: &mut Game) -> bool {
// new pieces are spawned at the highest valid point,
// if any of those blocks are already active then it's game over
//
if let Some(ref piece) = game.piece {
if let Some(shape) = game.shapes.get(&piece.shape_kind) {
return is_colliding(&game.board, shape, &piece.pos, &piece.angle);
}
if let Some(shape) = game.shapes.get(&game.piece.shape_kind) {
return is_colliding(&game.board, shape, &game.piece.pos, &game.piece.angle);
}
false
}

fn use_next_shape(game: &mut Game) {
if let Some(ref mut piece) = game.piece {
piece.shape_kind = game.next_shape.clone();
if let Some(shape) = game.shapes.get(&piece.shape_kind) {
piece.pos.x = ((game.board.width as i32) - shape.aabb_width) / 2;
piece.pos.y = (game.board.height as i32) - shape.real_height;
piece.angle = PieceAngle::R0;
}
game.piece.shape_kind = game.next_shape.clone();
if let Some(shape) = game.shapes.get(&game.piece.shape_kind) {
game.piece.pos.x = ((game.board.width as i32) - shape.aabb_width) / 2;
game.piece.pos.y = (game.board.height as i32) - shape.real_height;
game.piece.angle = PieceAngle::R0;
}
game.next_shape = get_random_shape(game);
}
@@ -363,88 +348,86 @@ fn lower_piece(board: &Board, shape: &Shape, piece: &mut Piece) -> bool {
}

fn apply_user_input(game: &mut Game, controller: &Controller, delta: f32) {
if let Some(ref mut piece) = game.piece {
if let Some(shape) = game.shapes.get(&piece.shape_kind) {
if let Some(shape) = game.shapes.get(&game.piece.shape_kind) {

if can_apply_input(&mut game.left_cooldown, controller.left, game.default_cooldown, delta) {
let new_position = BoardPos { x: piece.pos.x - 1, y: piece.pos.y };
if is_allowed(&game.board, shape, &new_position, &piece.angle) {
piece.pos = new_position;
}
if can_apply_input(&mut game.left_cooldown, controller.left, game.default_cooldown, delta) {
let new_position = BoardPos { x: game.piece.pos.x - 1, y: game.piece.pos.y };
if is_allowed(&game.board, shape, &new_position, &game.piece.angle) {
game.piece.pos = new_position;
}
}

if can_apply_input(&mut game.right_cooldown, controller.right, game.default_cooldown, delta) {
let new_position = BoardPos { x: piece.pos.x + 1, y: piece.pos.y };
if is_allowed(&game.board, shape, &new_position, &piece.angle) {
piece.pos = new_position;
}
if can_apply_input(&mut game.right_cooldown, controller.right, game.default_cooldown, delta) {
let new_position = BoardPos { x: game.piece.pos.x + 1, y: game.piece.pos.y };
if is_allowed(&game.board, shape, &new_position, &game.piece.angle) {
game.piece.pos = new_position;
}
}

if can_apply_input(&mut game.up_cooldown, controller.up, game.default_cooldown, delta) {
let new_angle;
if shape.fully_rotate {
new_angle = match piece.angle {
PieceAngle::R0 => PieceAngle::R270,
PieceAngle::R270 => PieceAngle::R180,
PieceAngle::R180 => PieceAngle::R90,
PieceAngle::R90 => PieceAngle::R0,
};
} else {
new_angle = match piece.angle {
PieceAngle::R0 => PieceAngle::R90,
_ => PieceAngle::R0,
};
}

if is_allowed(&game.board, shape, &piece.pos, &new_angle) {
piece.angle = new_angle;
}
if can_apply_input(&mut game.up_cooldown, controller.up, game.default_cooldown, delta) {
let new_angle;
if shape.fully_rotate {
new_angle = match game.piece.angle {
PieceAngle::R0 => PieceAngle::R270,
PieceAngle::R270 => PieceAngle::R180,
PieceAngle::R180 => PieceAngle::R90,
PieceAngle::R90 => PieceAngle::R0,
};
} else {
new_angle = match game.piece.angle {
PieceAngle::R0 => PieceAngle::R90,
_ => PieceAngle::R0,
};
}

if can_apply_input(&mut game.down_cooldown, controller.down, game.default_cooldown, delta) {
game.piece_is_dropping = true;
if is_allowed(&game.board, shape, &game.piece.pos, &new_angle) {
game.piece.angle = new_angle;
}
}

if can_apply_input(&mut game.a_cooldown, controller.a, game.default_cooldown, delta) {
let new_angle;
if shape.fully_rotate {
new_angle = match piece.angle {
PieceAngle::R0 => PieceAngle::R90,
PieceAngle::R90 => PieceAngle::R180,
PieceAngle::R180 => PieceAngle::R270,
PieceAngle::R270 => PieceAngle::R0,
};
} else {
new_angle = match piece.angle {
PieceAngle::R0 => PieceAngle::R90,
_ => PieceAngle::R0,
};
}
if can_apply_input(&mut game.down_cooldown, controller.down, game.default_cooldown, delta) {
game.piece_is_dropping = true;
}

if is_allowed(&game.board, shape, &piece.pos, &new_angle) {
piece.angle = new_angle;
}
if can_apply_input(&mut game.a_cooldown, controller.a, game.default_cooldown, delta) {
let new_angle;
if shape.fully_rotate {
new_angle = match game.piece.angle {
PieceAngle::R0 => PieceAngle::R90,
PieceAngle::R90 => PieceAngle::R180,
PieceAngle::R180 => PieceAngle::R270,
PieceAngle::R270 => PieceAngle::R0,
};
} else {
new_angle = match game.piece.angle {
PieceAngle::R0 => PieceAngle::R90,
_ => PieceAngle::R0,
};
}

if can_apply_input(&mut game.b_cooldown, controller.b, game.default_cooldown, delta) {
let new_angle;
if shape.fully_rotate {
new_angle = match piece.angle {
PieceAngle::R0 => PieceAngle::R270,
PieceAngle::R270 => PieceAngle::R180,
PieceAngle::R180 => PieceAngle::R90,
PieceAngle::R90 => PieceAngle::R0,
};
} else {
new_angle = match piece.angle {
PieceAngle::R0 => PieceAngle::R90,
_ => PieceAngle::R0,
};
}
if is_allowed(&game.board, shape, &game.piece.pos, &new_angle) {
game.piece.angle = new_angle;
}
}

if is_allowed(&game.board, shape, &piece.pos, &new_angle) {
piece.angle = new_angle;
}
if can_apply_input(&mut game.b_cooldown, controller.b, game.default_cooldown, delta) {
let new_angle;
if shape.fully_rotate {
new_angle = match game.piece.angle {
PieceAngle::R0 => PieceAngle::R270,
PieceAngle::R270 => PieceAngle::R180,
PieceAngle::R180 => PieceAngle::R90,
PieceAngle::R90 => PieceAngle::R0,
};
} else {
new_angle = match game.piece.angle {
PieceAngle::R0 => PieceAngle::R90,
_ => PieceAngle::R0,
};
}

if is_allowed(&game.board, shape, &game.piece.pos, &new_angle) {
game.piece.angle = new_angle;
}
}
}

Loading…
Cancel
Save