1
0

Make the game work

This commit is contained in:
Timothy Warren 2023-05-03 14:32:05 -04:00
parent 308ac5746c
commit 6698c73f37
3 changed files with 43 additions and 4 deletions

View File

@ -4,7 +4,10 @@ export component MainWindow inherits Window {
width: 326px; width: 326px;
height: 326px; height: 326px;
in property <[TileData]> memory_tiles: [{ callback check_if_pair_solved();
in property <bool> disable_tiles;
in-out property <[TileData]> memory_tiles: [{
image: @image-url("../../icons/at.png") image: @image-url("../../icons/at.png")
}, { }, {
image: @image-url("../../icons/balance-scale.png") image: @image-url("../../icons/balance-scale.png")
@ -32,7 +35,10 @@ export component MainWindow inherits Window {
// propagate the solved status from the model to the tile // propagate the solved status from the model to the tile
solved: tile.solved; solved: tile.solved;
clicked => { clicked => {
if (!root.disable_tiles) {
tile.image_visible = !tile.image_visible; tile.image_visible = !tile.image_visible;
root.check_if_pair_solved();
}
} }
} }
} }

View File

@ -13,6 +13,7 @@ export component MemoryTile inherits Rectangle {
width: 64px; width: 64px;
height: 64px; height: 64px;
background: solved ? #34CE57 : #3960D5; background: solved ? #34CE57 : #3960D5;
border-radius: 12px;
Image { Image {
source: icon; source: icon;

View File

@ -1,7 +1,8 @@
use ::rand::seq::SliceRandom; use ::rand::seq::SliceRandom;
use ::slint::include_modules; use ::slint::include_modules;
use ::slint::{Model, VecModel}; use ::slint::{Model, Timer, VecModel};
use std::rc::Rc; use std::rc::Rc;
use std::time::Duration;
include_modules!(); include_modules!();
@ -18,7 +19,38 @@ fn main() {
// Update the model // Update the model
let tiles_model = Rc::new(VecModel::from(tiles)); let tiles_model = Rc::new(VecModel::from(tiles));
main_window.set_memory_tiles(tiles_model.into()); main_window.set_memory_tiles(tiles_model.clone().into());
let main_window_weak = main_window.as_weak();
main_window.on_check_if_pair_solved(move || {
let mut flipped_tiles = tiles_model
.iter()
.enumerate()
.filter(|(_, tile)| tile.image_visible && !tile.solved);
if let (Some((t1_idx, mut t1)), Some((t2_idx, mut t2))) =
(flipped_tiles.next(), flipped_tiles.next())
{
let is_pair_solved = t1 == t2;
if is_pair_solved {
t1.solved = true;
tiles_model.set_row_data(t1_idx, t1);
t2.solved = true;
tiles_model.set_row_data(t2_idx, t2);
} else {
let main_window = main_window_weak.unwrap();
main_window.set_disable_tiles(true);
let tiles_model = tiles_model.clone();
Timer::single_shot(Duration::from_secs(1), move || {
main_window.set_disable_tiles(false);
t1.image_visible = false;
tiles_model.set_row_data(t1_idx, t1);
t2.image_visible = false;
tiles_model.set_row_data(t2_idx, t2);
});
}
}
});
main_window.run().unwrap(); main_window.run().unwrap();
} }