Browse Source

in-game option: restart working

master
Inderjit Gill 1 year ago
parent
commit
0940e96bc8
3 changed files with 58 additions and 41 deletions
  1. +57
    -10
      src/game.rs
  2. +1
    -1
      src/lib.rs
  3. +0
    -30
      web/js/index.js

+ 57
- 10
src/game.rs View File

@@ -83,6 +83,14 @@ impl Board {
}
}

pub fn reset(&mut self) {
for line in 0..self.height {
for x in 0..self.width {
self.board[line][x].reset(&self.inactive_colour);
}
}
}

pub fn reconfigure(&mut self, config: &Config) {
// not updating board width/height in order to retain the current state of the board
self.inactive_colour = Col::new(
@@ -107,7 +115,7 @@ impl Board {
line.iter().all(|block| block.occupied)
}

pub fn remove_line(&mut self, line_number: usize, inactive_colour: &Col) -> error::Result<()> {
pub fn remove_line(&mut self, line_number: usize) -> error::Result<()> {
if line_number >= self.height {
return Err(error::TetrisError::LineRemoval);
}
@@ -123,7 +131,7 @@ impl Board {
}
let highest_line = self.height - 1;
for x in 0..self.width {
self.board[highest_line][x].reset(inactive_colour);
self.board[highest_line][x].reset(&self.inactive_colour);
}

Ok(())
@@ -299,6 +307,41 @@ impl Game {
game
}

pub fn restart(&mut self, random: f32) -> error::Result<()> {
self.is_game_over = false;
self.score = 0;
self.level = 0;
self.lines = 0;
self.tetris = false;
self.time_to_next_lowering = get_time_to_next_lowering(0);
self.left_cooldown = 0.0;
self.right_cooldown = 0.0;
self.up_cooldown = 0.0;
self.down_cooldown = 0.0;
self.a_cooldown = 0.0;
self.b_cooldown = 0.0;
self.i_count = 0;
self.j_count = 0;
self.l_count = 0;
self.o_count = 0;
self.s_count = 0;
self.t_count = 0;
self.z_count = 0;

self.board.reset();
self.next_piece_shape = get_random_shape(1.0 - random)?;
self.next_piece_colour = get_colour_from_hsl(
((1.0 - random) * 360.0) as f64,
self.colour_saturation,
self.colour_lightness,
1.0
)?;

use_next_shape(self, random)?;

Ok(())
}

pub fn get_curvature(&self) -> f32 {
let f = self.menu_curvature as f32 / self.menu_max_curvature as f32;
f * (1.0 / 8.0)
@@ -384,7 +427,6 @@ impl Game {
}

pub fn tick(&mut self, controller: &Controller, delta: f32, random: f32) -> bool {

if controller.just_released(ControllerButton::Start) {
if self.mode == GameMode::Playing {
play_sound(SoundEffect::PauseIn);
@@ -426,15 +468,15 @@ impl Game {
Ok(res) => res,
Err(_) => false,
},
GameMode::Paused => match self.tick_paused(controller) {
GameMode::Paused => match self.tick_paused(controller, random) {
Ok(res) => res,
Err(_) => false,
},
}
}

fn tick_paused(&mut self, controller: &Controller) -> error::Result<bool> {
let updated = update_pause_menu(self, controller);
fn tick_paused(&mut self, controller: &Controller, random: f32) -> error::Result<bool> {
let updated = update_pause_menu(self, controller, random);
Ok(updated)
}

@@ -825,11 +867,9 @@ fn is_colliding(board: &Board, shape: &Shape, pos: &BoardPos, angle: &PieceAngle
fn remove_complete_lines(board: &mut Board) -> error::Result<i32> {
let mut offset = 0;

let c = board.inactive_colour;

for y in 0..board.height {
if board.is_line_filled(y - offset) {
board.remove_line(y - offset, &c)?;
board.remove_line(y - offset)?;
offset += 1;
}
}
@@ -1027,7 +1067,7 @@ fn render_next_shape(geometry: &mut Geometry, shape: &Shape, offset: Block2D, co
}


fn update_pause_menu(game: &mut Game, controller: &Controller) -> bool {
fn update_pause_menu(game: &mut Game, controller: &Controller, random: f32) -> bool {

let mut update = false;

@@ -1097,6 +1137,13 @@ fn update_pause_menu(game: &mut Game, controller: &Controller) -> bool {
game.menu_curvature = clamp(game.menu_curvature, 0, game.menu_max_curvature);
}

if game.menu_active_item == 4 {
if controller.just_pressed(ControllerButton::Start) {
let _ = game.restart(random);
update = true;
}
}

if update {
play_sound(SoundEffect::MenuMove);
}

+ 1
- 1
src/lib.rs View File

@@ -126,7 +126,7 @@ impl Config {
tileset_texture_height: 0, // tileset texture height
sprite_width: 16,
sprite_height: 16,
colour_saturation: 99.0,
colour_saturation: 85.0,
colour_lightness: 50.0,
board_offset_x: 1,
board_offset_y: 1,

+ 0
- 30
web/js/index.js View File

@@ -119,36 +119,6 @@ function main() {

const canvasElement = document.getElementById(gState.canvasId);

gState.config.render_texture_width = 512;
gState.config.render_texture_height = 512;

gState.config.block_size = 32; // how many blocks fit along a dimension
// 512 / 32 = 16 (the size of each block in pixels)

gState.config.block_limit_x = 16;
gState.config.block_limit_y = 22;

// the size of each sprite in the tileset (in pixels)
gState.config.sprite_width = 16;
gState.config.sprite_height = 16;

gState.config.colour_saturation = 85.0;
gState.config.colour_lightness = 50.0;

gState.config.default_cooldown = 100.0;

gState.config.block_inactive_col_r = 0.4;
gState.config.block_inactive_col_g = 0.6,
gState.config.block_inactive_col_b = 0.0,
gState.config.block_inactive_col_a = 0.1,

gState.config.menu_volume = 5;
gState.config.menu_max_volume = 5;
gState.config.menu_crt_update_speed = 4;
gState.config.menu_max_crt_update_speed = 5;
gState.config.menu_curvature = 3;
gState.config.menu_max_curvature = 5;

resize_canvas_element(canvasElement, gState.config);

gState.audio = new Audio(config);

Loading…
Cancel
Save