#![allow(clippy::type_complexity)] use crate::{levels::stored::*, AppState}; use bevy::{ input::{keyboard::KeyCode, Input}, prelude::{ shape::{Circle, Quad}, *, }, sprite::Mesh2dHandle, }; use bevy_mod_picking::*; pub struct EditorPlugin; impl Plugin for EditorPlugin { fn build(&self, app: &mut App) { app.add_event::() .add_plugins(DefaultPickingPlugins) .add_system_set(SystemSet::on_enter(AppState::Editor).with_system(setup)) .add_system_set( SystemSet::on_update(AppState::Editor) .with_system(move_system) .with_system(follow_ends_system), ); } } // Events struct DragEndEvent(Entity); // Resources // Components #[derive(Component)] struct Platform(Entity, Entity); #[derive(Component)] struct Draggable; #[derive(Component)] struct End(Entity); // Bundles #[derive(Bundle)] struct PlatformBundle { #[bundle] mesh: ColorMesh2dBundle, } #[derive(Bundle)] struct PlatformEndBundle { #[bundle] mesh: ColorMesh2dBundle, #[bundle] pickable: PickableBundle, draggable: Draggable, end: End, } #[derive(Bundle)] struct CharacterBundle { #[bundle] mesh: ColorMesh2dBundle, #[bundle] pickable: PickableBundle, draggable: Draggable, } // Functions fn spawn_platform( commands: &mut Commands, meshes: &mut ResMut>, materials: &mut ResMut>, transform: Transform, size: Vec2, ) { let platform = commands .spawn_bundle(PlatformBundle { mesh: ColorMesh2dBundle { mesh: meshes.add(Mesh::from(Quad { size, flip: false })).into(), material: materials.add(ColorMaterial::from(Color::GRAY)), transform, ..default() }, }) .id(); let ends = Platform( commands .spawn_bundle(PlatformEndBundle { mesh: ColorMesh2dBundle { mesh: meshes .add(Mesh::from(Circle { radius: 8., vertices: 12, })) .into(), material: materials.add(ColorMaterial::from(Color::rgba(1., 1., 0., 0.7))), transform: Transform::from_xyz( transform.translation.x - size.x / 2., transform.translation.y - size.y / 2., 0.5, ), ..default() }, pickable: PickableBundle::default(), draggable: Draggable, end: End(platform), }) .id(), commands .spawn_bundle(PlatformEndBundle { mesh: ColorMesh2dBundle { mesh: meshes .add(Mesh::from(Circle { radius: 8., vertices: 12, })) .into(), material: materials.add(ColorMaterial::from(Color::rgba(1., 1., 0., 0.7))), transform: Transform::from_xyz( transform.translation.x + size.x / 2., transform.translation.y + size.y / 2., 0.5, ), ..default() }, pickable: PickableBundle::default(), draggable: Draggable, end: End(platform), }) .id(), ); commands.entity(platform).insert(ends); } fn spawn_character( commands: &mut Commands, meshes: &mut ResMut>, materials: &mut ResMut>, asset_server: &Res, transform: Transform, color: Color, index: usize, ) { let font = asset_server.get_handle("UacariLegacy-Thin.ttf"); commands .spawn_bundle(CharacterBundle { mesh: ColorMesh2dBundle { mesh: meshes .add(Mesh::from(Quad { size: Vec2 { x: 64., y: 64. }, flip: false, })) .into(), material: materials.add(ColorMaterial::from(color)), transform, ..default() }, pickable: PickableBundle::default(), draggable: Draggable, }) .with_children(|c| { c.spawn_bundle(Text2dBundle { text: Text::from_section( &index.to_string(), TextStyle { font: font.clone(), font_size: 32., color: Color::WHITE, }, ) .with_alignment(TextAlignment::CENTER), transform: Transform::from_xyz(0., 0., 1.), ..Default::default() }); }); } pub fn spawn_stored_level( commands: &mut Commands, meshes: &mut ResMut>, materials: &mut ResMut>, asset_server: &Res, stored_level: &StoredLevel, ) { for platform in stored_level.platforms.iter() { spawn_platform( commands, meshes, materials, Transform::from_xyz(platform.pos.x, platform.pos.y, 0.), platform.size, ); } for (i, character) in stored_level.characters.iter().enumerate() { spawn_character( commands, meshes, materials, asset_server, Transform::from_xyz(character.pos.x, character.pos.y, 0.), character.color.into(), i, ); } } // Systems fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, camera_query: Query>, level_id: Res, stored_levels_assets: Res>, stored_levels_handle: Res>, asset_server: Res, ) { commands .entity(camera_query.single()) .insert_bundle(PickingCameraBundle::default()); if let Some(stored_level) = stored_levels_assets .get(&stored_levels_handle) .unwrap() .levels .get(level_id.0 .0 as usize) { spawn_stored_level( &mut commands, &mut meshes, &mut materials, &asset_server, stored_level, ); } } fn move_system( keyboard_input: Res>, mut camera_query: Query<&mut Transform, (With, Without)>, mut drag_query: Query<(&mut Transform, &Selection, Option<&End>), With>, mut drag_end_event: EventWriter, ) { if keyboard_input.pressed(KeyCode::LControl) || keyboard_input.pressed(KeyCode::RControl) { let mut transform = camera_query.single_mut(); let drag = Vec3 { x: (keyboard_input.pressed(KeyCode::Right) as i8 - keyboard_input.pressed(KeyCode::Left) as i8) as _, y: (keyboard_input.pressed(KeyCode::Up) as i8 - keyboard_input.pressed(KeyCode::Down) as i8) as _, z: 0., } * 8.; transform.translation += drag; return; } let drag = if keyboard_input.pressed(KeyCode::LShift) || keyboard_input.pressed(KeyCode::RShift) { Vec3 { x: (keyboard_input.pressed(KeyCode::Right) as i8 - keyboard_input.pressed(KeyCode::Left) as i8) as _, y: (keyboard_input.pressed(KeyCode::Up) as i8 - keyboard_input.pressed(KeyCode::Down) as i8) as _, z: 0., } } else { Vec3 { x: (keyboard_input.just_pressed(KeyCode::Right) as i8 - keyboard_input.just_pressed(KeyCode::Left) as i8) as _, y: (keyboard_input.just_pressed(KeyCode::Up) as i8 - keyboard_input.just_pressed(KeyCode::Down) as i8) as _, z: 0., } } * 8.; if drag != Vec3::ZERO { for (mut transform, selection, end) in drag_query.iter_mut() { if selection.selected() { transform.translation += drag; if let Some(End(entity)) = end { drag_end_event.send(DragEndEvent(*entity)); } } } } } fn follow_ends_system( mut meshes: ResMut>, mut platform_query: Query<(&mut Transform, &mut Mesh2dHandle, &Platform)>, end_query: Query<&Transform, Without>, mut drag_end_event: EventReader, ) { for DragEndEvent(entity) in drag_end_event.iter() { if let Ok((mut transform, mut mesh, Platform(end1, end2))) = platform_query.get_mut(*entity) { if let (Ok(end1), Ok(end2)) = (end_query.get(*end1), end_query.get(*end2)) { transform.translation.x = (end1.translation.x + end2.translation.x) / 2.; transform.translation.y = (end1.translation.y + end2.translation.y) / 2.; *mesh = meshes .add(Mesh::from(Quad { size: Vec2 { x: (end2.translation.x - end1.translation.x).abs(), y: (end2.translation.y - end1.translation.y).abs(), }, flip: false, })) .into(); } } } }