move to retained mode complete

This commit is contained in:
cool-mist
2026-03-15 17:24:41 +05:30
parent 194136dfc7
commit 82d13cfd66
12 changed files with 544 additions and 540 deletions
+174 -69
View File
@@ -1,7 +1,9 @@
use std::collections::HashMap;
use super::{Game, GameMode, Settings, constants};
use crate::{
game::{self, Command, w_board, w_button, w_counter},
resources::*,
game::{self, Command},
resources::{self, *},
widgets::*,
};
use macroquad::prelude::*;
@@ -11,80 +13,175 @@ use sol_lib::{
};
impl Game {
pub async fn new() -> Self {
let resources = resources::init().await;
let game_mode = GameMode::Medium;
let settings = Settings {
volume: constants::VOLUME,
max_age: 3,
max_pieces: 2,
..Default::default()
};
let puzzle = generate_new_puzzle(game_mode, &settings);
let game_mode = GameMode::Medium;
let show_rules = false;
let mut widgets = HashMap::new();
let w_label_heading = LabelWidget::new(constants::HEADING_TEXT);
let label_heading = add_widget(&mut widgets, Widget::Label(w_label_heading));
let w_label_pieces = LabelWidget::new(constants::PIECES_LABEL_TEXT);
let label_pieces = add_widget(&mut widgets, Widget::Label(w_label_pieces));
let w_label_max_age = LabelWidget::new(constants::AGE_LABEL_TEXT);
let label_max_age = add_widget(&mut widgets, Widget::Label(w_label_max_age));
let mut w_counter_pieces =
CounterWidget::new(2, 7, settings.max_pieces, false, UiColor::Yellow);
w_counter_pieces.on_increment = |c| Command::UpdateMaxPieceCount(c.current);
w_counter_pieces.on_decrement = |c| Command::UpdateMaxPieceCount(c.current);
let counter_pieces = add_widget(&mut widgets, Widget::Counter(w_counter_pieces));
let mut w_counter_moves =
CounterWidget::new(1, 7, settings.max_age, false, UiColor::Yellow);
w_counter_moves.on_increment = |c| Command::UpdateMaxAge(c.current);
w_counter_moves.on_decrement = |c| Command::UpdateMaxAge(c.current);
let counter_moves = add_widget(&mut widgets, Widget::Counter(w_counter_moves));
let mut w_btn_reset = ButtonWidget::new(
true,
UiColor::Yellow,
constants::RESET_BUTTON_TEXT,
SoundKind::Button,
);
w_btn_reset.on_click = |_| Command::ResetBoard;
let btn_reset = add_widget(&mut widgets, Widget::Button(w_btn_reset));
let mut w_btn_next = ButtonWidget::new(
false,
UiColor::Green,
constants::NEXT_BUTTON_TEXT,
SoundKind::Button,
);
w_btn_next.on_click = |_| Command::NextPuzzle;
let btn_next = add_widget(&mut widgets, Widget::Button(w_btn_next));
let mut w_btn_rules = ButtonWidget::new(
true,
UiColor::Brown,
constants::RULES_BUTTON_TEXT,
SoundKind::Button,
);
w_btn_rules.on_click = |_| Command::ToggleRules;
let btn_rules = add_widget(&mut widgets, Widget::Button(w_btn_rules));
let mut w_btn_generate = ButtonWidget::new(
false,
UiColor::Pink,
constants::GENERATE_BUTTON_TEXT,
SoundKind::Button,
);
w_btn_generate.on_click = |_| Command::NewCustomPuzzle;
let btn_generate = add_widget(&mut widgets, Widget::Button(w_btn_generate));
let mut game_mode_btns = HashMap::new();
let mut w_game_mode_easy_btn = ButtonWidget::new(
true,
UiColor::Yellow,
constants::EASY_BUTTON_TEXT,
SoundKind::Mode,
);
w_game_mode_easy_btn.on_click = |_| Command::SwitchGameMode(GameMode::Easy);
game_mode_btns.insert(
GameMode::Easy,
add_widget(&mut widgets, Widget::Button(w_game_mode_easy_btn)),
);
let mut w_game_mode_medium_btn = ButtonWidget::new(
true,
UiColor::Yellow,
constants::MEDIUM_BUTTON_TEXT,
SoundKind::Mode,
);
w_game_mode_medium_btn.on_click = |_| Command::SwitchGameMode(GameMode::Medium);
game_mode_btns.insert(
GameMode::Medium,
add_widget(&mut widgets, Widget::Button(w_game_mode_medium_btn)),
);
let mut w_game_mode_hard_btn = ButtonWidget::new(
true,
UiColor::Yellow,
constants::HARD_BUTTON_TEXT,
SoundKind::Mode,
);
w_game_mode_hard_btn.on_click = |_| Command::SwitchGameMode(GameMode::Hard);
game_mode_btns.insert(
GameMode::Hard,
add_widget(&mut widgets, Widget::Button(w_game_mode_hard_btn)),
);
let mut w_game_mode_custom_btn = ButtonWidget::new(
true,
UiColor::Blue,
constants::CUSTOM_BUTTON_TEXT,
SoundKind::Mode,
);
w_game_mode_custom_btn.on_click = |_| Command::SwitchGameMode(GameMode::Custom);
game_mode_btns.insert(
GameMode::Custom,
add_widget(&mut widgets, Widget::Button(w_game_mode_custom_btn)),
);
let active_game_mode_btn = game_mode_btns.get(&game_mode).unwrap();
game::button(&mut widgets, *active_game_mode_btn).is_active = false;
let mut w_board = BoardWidget::new(puzzle.board.size, puzzle.board.clone());
w_board.on_board_state_changed = |s| Command::UpdateBoard(s.new_board_state);
let board = add_widget(&mut widgets, Widget::Board(w_board));
let mut w_controls = ControlsWidget::new();
w_controls.add(KeyCode::Q, Command::Quit);
w_controls.add(KeyCode::Escape, Command::HideRules);
w_controls.add(KeyCode::D, Command::ToggleDebug);
add_widget(&mut widgets, Widget::Controls(w_controls));
Game {
puzzle,
board,
resources,
settings,
game_mode,
game_mode_btns,
btn_reset,
btn_next,
btn_rules,
show_rules,
widgets,
counter_pieces,
counter_moves,
btn_generate,
label_pieces,
label_max_age,
label_heading,
..Default::default()
}
}
pub fn update(&mut self, interaction: Interaction) {
let command = self.get_command(interaction);
let command = self.map_to_command(interaction);
self.update_state(command);
}
fn get_command(&mut self, interaction: Interaction) -> Command {
fn map_to_command(&mut self, interaction: Interaction) -> Command {
for (_, widget) in &mut self.widgets {
let cmd = widget.update(&interaction, &self.resources, &self.settings);
let ret_cmd = match cmd {
Command::NoInteraction => false,
_ => true,
};
if ret_cmd {
return cmd;
if let Some(command) = widget.update(&interaction, &self.resources, &self.settings) {
return command;
}
}
let board_interaction =
w_board!(self, board).handle_input(&interaction, &self.resources, &self.settings);
if let Some(new_board_state) = board_interaction.new_board_state {
return Command::UpdateBoard(new_board_state);
}
if w_button!(self, btn_reset)
.handle_input(&interaction, &self.resources, &self.settings)
.clicked
{
return Command::ResetBoard;
}
if w_button!(self, btn_next)
.handle_input(&interaction, &self.resources, &self.settings)
.clicked
{
return Command::NextPuzzle;
}
for (mode, btn) in &mut self.game_mode_btns {
if game::button(&mut self.widgets, *btn)
.handle_input(&interaction, &self.resources, &self.settings)
.clicked
{
return Command::SwitchGameMode(*mode);
}
}
if w_button!(self, btn_rules)
.handle_input(&interaction, &self.resources, &self.settings)
.clicked
{
return Command::ToggleRules;
}
if w_button!(self, btn_generate)
.handle_input(&interaction, &self.resources, &self.settings)
.clicked
{
return Command::NewCustomPuzzle;
}
if interaction.keys_released.contains(&KeyCode::Escape) {
return Command::HideRules;
}
if interaction.keys_released.contains(&KeyCode::D) {
return Command::ToggleDebug;
}
if interaction.keys_released.contains(&KeyCode::Q) {
return Command::Quit;
}
return Command::NoInteraction;
}
@@ -177,7 +274,15 @@ impl Game {
}
}
pub fn generate_new_puzzle(mode: GameMode, settings: &Settings) -> Puzzle {
fn add_widget(widgets: &mut HashMap<WidgetId, Widget>, w: Widget) -> WidgetId {
let id = rand::rand() as u64;
let widget_id = WidgetId(id);
widgets.insert(widget_id, w);
widget_id
}
fn generate_new_puzzle(mode: GameMode, settings: &Settings) -> Puzzle {
let piece_count = match mode {
GameMode::Easy => 3,
GameMode::Medium => 5,