Works pretty well now

This commit is contained in:
Tobias Wennberg 2025-03-12 10:11:19 +01:00
parent efaad5c268
commit 9517b89479
2 changed files with 35 additions and 32 deletions

View File

@ -141,6 +141,16 @@ impl RelatedBoxes {
}
impl BoxType {
pub fn is_opened(&self) -> bool {
match self {
BoxType::Open => true,
BoxType::Safe => false,
BoxType::MarkedSafe => false,
BoxType::Mine => false,
BoxType::MarkedMine => false,
BoxType::OpenMine => true,
}
}
pub fn box_type_bin(&self) -> BoxTypeBin {
match self {
BoxType::Open => BoxTypeBin::Other,

View File

@ -1,8 +1,4 @@
mod helpers;
use helpers::*;
use std::borrow::Borrow;
use rand::{rng, Rng};
use bevy::prelude::*;
use bevy_pancam::*;
@ -13,7 +9,7 @@ const COLOR_MARKED_BOX: Color = Color::srgb(0.769, 0.435, 0.043);
const COLOR_OPEN_MINE: Color = Color::srgb(1., 0., 0.);
const BOARD_WIDTH :usize = 16;
const BOARD_HEIGHT :usize = 300;
const BOARD_HEIGHT :usize = 30;
const NUMER_OF_MINES :usize = 99;
#[derive(Component)]
@ -88,8 +84,8 @@ fn main() {
.add_systems(Update, (
(handle_box).run_if(in_state(GameState::Playing)).run_if(any_with_component::<Hovered>),
open_box,
draw_marked_box,
draw_marked_flip_box,
mark_box,
markflip_box,
switch_to_win,
))
.add_systems(OnEnter(GameState::Fail), draw_fail)
@ -118,7 +114,7 @@ fn draw_fail(
commands.spawn(Text2d::new("FAIL!!"));
}
fn draw_marked_flip_box(
fn markflip_box(
mut commands: Commands,
box_entity_to_open: Query<Entity, With<BoxToMarkFlip>>,
mut boxes: Query<&mut Box>,
@ -126,6 +122,7 @@ fn draw_marked_flip_box(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
for entity in box_entity_to_open.iter() {
println!("FLIP");
commands.entity(entity).remove::<BoxToMarkFlip>();
let mut r#box = boxes.get_mut(entity).unwrap();
*r#box = r#box.mark_flip();
@ -140,7 +137,7 @@ fn draw_marked_flip_box(
};
}
}
fn draw_marked_box(
fn mark_box(
mut commands: Commands,
box_entity_to_open: Query<Entity, With<BoxToMark>>,
mut boxes: Query<&mut Box>,
@ -173,20 +170,27 @@ fn open_box(
mut next_state: ResMut<NextState<GameState>>,
) {
let mut boxes_to_open: Vec<Entity> = box_entity_to_open.iter().collect();
if boxes_to_open.len() == 0 {
return
}
let mut counter: usize = 0;
loop {
let entity = boxes_to_open[0];
commands.entity(*entity).remove::<BoxToOpen>();
let mut r#box = boxes.get_mut(*entity).unwrap();
if counter >= boxes_to_open.len() {
break
}
let entity = boxes_to_open[counter];
counter += 1;
commands.entity(entity).remove::<BoxToOpen>();
let mut r#box = boxes.get_mut(entity).unwrap();
if !r#box.0.can_open() {
continue
}
*r#box = r#box.open();
commands.entity(*entity).remove::<SafeUnopenedBox>();
commands.entity(entity).remove::<SafeUnopenedBox>();
if r#box.0 == BoxType::OpenMine {
next_state.set(GameState::Fail);
}
let mut r#material = color_material.get_mut(*entity).unwrap();
let mut r#material = color_material.get_mut(entity).unwrap();
*r#material = match r#box.0 {
BoxType::Open => MeshMaterial2d(materials.add(COLOR_OPEN_BOX)),
BoxType::Safe => MeshMaterial2d(materials.add(COLOR_HIDDEN_BOX)),
@ -195,16 +199,16 @@ fn open_box(
BoxType::MarkedMine => MeshMaterial2d(materials.add(COLOR_MARKED_BOX)),
BoxType::OpenMine => MeshMaterial2d(materials.add(COLOR_OPEN_MINE)),
};
let rel_mine = related_boxes.get(*entity).unwrap();
let rel_mine = related_boxes.get(entity).unwrap();
let rel_mine_count = rel_mine.nearby_mines_count_box_mut(&boxes);
let transform = transforms.get(*entity).unwrap();
let transform = transforms.get(entity).unwrap();
commands.spawn((
Text2d::new( if rel_mine_count != 0 { rel_mine_count.to_string() } else {"".to_string()}),
TextLayout::new_with_justify(JustifyText::Center),
transform.clone().with_scale(Vec3::new(0.3,0.3,0.)),
));
if rel_mine_count != 0 {
return
continue
}
for related_box in rel_mine.iter() {
let box_entity = match related_box {
@ -215,13 +219,12 @@ fn open_box(
if box_val.0 == BoxType::Open {
continue
}
if boxes_to_open.contains(&related_box.unwrap()) {
continue
}
boxes_to_open.push(related_box.unwrap());
//commands.entity(related_box.unwrap()).insert(BoxToOpen);
}
if boxes_to_open.len() >= counter {
break
}
counter += 1;
}
}
@ -362,7 +365,7 @@ fn handle_box(
else if mouse_button_input.just_pressed(MouseButton::Right) {MouseButton::Right}
else {return};
let (entity, r#box) = hovered_boxes.get_single().unwrap();
if r#box.0.can_open() || r#box.0.is_mine(){
if !r#box.0.is_opened() {
match mouse_key {
MouseButton::Left => {
commands.entity(entity).insert(BoxToOpen);
@ -410,13 +413,3 @@ fn random_bool(probability: f64) -> bool {
let mut rnd = rng();
rnd.random::<f64>() < probability
}
/*
fn calc_related_mines(
boxes: Query<(&mut Box, &RelatedMine, &Transform, &mut MeshMaterial2d<ColorMaterial>)>,
rels: RelatedMine
) -> usize {
let mut count
}
*/