Drawing correct, cant do intersections

This commit is contained in:
Tobias Wennberg 2025-02-25 19:20:26 +01:00
commit 1a196ee3f2
4 changed files with 6440 additions and 0 deletions

1
.gitignore vendored Executable file
View File

@ -0,0 +1 @@
/target

6300
Cargo.lock generated Executable file

File diff suppressed because it is too large Load Diff

37
Cargo.toml Executable file
View File

@ -0,0 +1,37 @@
[package]
name = "mine_sweeper"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = "0.15.3"
bevy_pancam = "0.17.0"
bevy_touch_camera = "0.1.2"
# Enable a small amount of optimization in the dev profile.
[profile.dev]
opt-level = 1
# Enable a large amount of optimization in the dev profile for dependencies.
[profile.dev.package."*"]
opt-level = 3
# Enable more optimization in the release profile at the cost of compile time.
[profile.release]
# Compile the entire crate as one unit.
# Slows compile times, marginal improvements.
codegen-units = 1
# Do a second optimization pass over the entire program, including dependencies.
# Slows compile times, marginal improvements.
lto = "thin"
# Optimize for size in the wasm-release profile to reduce load times and bandwidth usage on web.
[profile.wasm-release]
# Default to release profile values.
inherits = "release"
# Optimize with size in mind (also try "z", sometimes it is better).
# Slightly slows compile times, great improvements to file size and runtime performance.
opt-level = "s"
# Strip all debugging information from the binary to slightly reduce file size.
strip = "debuginfo"

102
src/main.rs Executable file
View File

@ -0,0 +1,102 @@
use bevy::{ecs::system::SystemParam, input::mouse::MouseButtonInput, prelude::*, state::commands};
use bevy_pancam::*;
const BOARD_WIDTH :usize = 50;
const BOARD_HEIGHT :usize = 50;
const NUMER_OF_MINES :usize = 50;
#[derive(Default, Reflect)]
enum BoxType {
Open,
#[default]
Closed,
Mine
}
#[derive(Default, Reflect, Component)]
struct Box(BoxType);
#[derive(Resource)]
struct MouseWorldCoordinate(Option<Vec2>);
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PanCamPlugin)
.insert_resource(MouseWorldCoordinate(None))
.add_systems(Startup, (draw_mines, init_camera))
.add_systems(PreUpdate, mouse_world_pos)
.add_systems(Update, handle_box)
.run();
}
fn draw_mines(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
let rec = meshes.add(Rectangle::new(5., 5.));
let offset_x = 8.;
let offset_y = 8.;
for i in 0..BOARD_WIDTH {
for j in 0..BOARD_HEIGHT {
commands.spawn((
Mesh2d(rec.clone()),
MeshMaterial2d(materials.add(Color::hsl(10.,10.,10.))),
Transform::from_xyz(offset_x*(i as f32)+0., offset_y*(j as f32)+0., 0.),
Box::default(),
))
;
}
}
}
fn init_camera(mut commands: Commands) {
commands.spawn((Camera2d, PanCam::default()));
}
fn handle_box(
mouse_button_input: Res<ButtonInput<MouseButton>>,
mouse_world: Res<MouseWorldCoordinate>,
mut boxes: Query<(&mut Box, &Transform, &MeshMaterial2d<ColorMaterial>)>
) {
if !mouse_button_input.just_pressed(MouseButton::Left){
return
}
let mouse = match mouse_world.0 {
Some(x) => {x},
None => return,
};
for (mut r#box, transform, material) in boxes.iter() {
let lcl_x = transform.local_x().x;
let lcl_y = transform.local_y().y;
if lcl_x <= mouse.x &&
lcl_x+BOARD_WIDTH as f32 >= mouse.x &&
lcl_y <= mouse.y &&
lcl_y+BOARD_HEIGHT as f32 >= mouse.y
{
println!("TRUE");
}
}
println!("Open box");
//if pointers.button != PointerButton::Primary {
// return;
//}
// gizmos.circle_2d(Isometry2d::IDENTITY, 5., Color::WHITE);
}
fn mouse_world_pos(
camera: Single<(&Camera, &GlobalTransform)>,
windows: Single<&Window>,
mut mouse_world: ResMut<MouseWorldCoordinate>,
) {
let (camera, camera_transform) = *camera;
*mouse_world = MouseWorldCoordinate(
windows.cursor_position()
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor).ok())
.map(|ray| ray.origin.truncate())
);
}