use bevy::{ input::{keyboard::KeyboardInput, mouse::MouseButtonInput, ButtonState}, math::Vec3Swizzles, prelude::*, utils::HashMap, }; use bevy::window::PrimaryWindow; use bevy_match3::prelude::*; const GEM_SIDE_LENGTH: f32 = 50.0; fn main() { App::new() .add_plugins( DefaultPlugins .set(WindowPlugin { primary_window: Some(Window { resizable: false, title: "bevy_match3 basic example".to_string(), ..default() }), ..default() }), ) .insert_resource(Selection::default()) .add_plugins(Match3Plugin) .add_systems(Startup, setup_graphics) .add_systems(Update, ( move_to, consume_events, input, visualize_selection, control, animate_once, shuffle)) .run(); } #[derive(Component, Clone)] struct VisibleBoard(HashMap); #[derive(Component)] struct MainCamera; fn setup_graphics(mut commands: Commands, board: Res, asset_server: Res) { let board_side_length = GEM_SIDE_LENGTH * 10.0; let centered_offset_x = board_side_length / 2.0 - GEM_SIDE_LENGTH / 2.0; let centered_offset_y = board_side_length / 2.0 - GEM_SIDE_LENGTH / 2.0; let mut camera = Camera2dBundle::default(); camera.transform = Transform::from_xyz( centered_offset_x, 0.0 - centered_offset_y, camera.transform.translation.z, ); commands.spawn(camera).insert(MainCamera); let mut gems = HashMap::default(); let vis_board = commands.spawn(SpatialBundle::default()).id(); board.iter().for_each(|(position, typ)| { let transform = Transform::from_xyz( position.x as f32 * GEM_SIDE_LENGTH, position.y as f32 * -GEM_SIDE_LENGTH, 0.0, ); let child = commands .spawn(SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(GEM_SIDE_LENGTH, GEM_SIDE_LENGTH)), ..Sprite::default() }, transform, texture: asset_server.load(&map_type_to_path(*typ)), ..SpriteBundle::default() }) .insert(Name::new(format!("{};{}", position.x, position.y))) .id(); gems.insert(*position, child); commands.entity(vis_board).add_child(child); }); let board = VisibleBoard(gems); commands.entity(vis_board).insert(board); } fn map_type_to_path(typ: u32) -> String { format!("{typ}.png") } #[derive(Component)] struct MoveTo(Vec2); fn move_to( mut commands: Commands, time: Res