2022-08-26 08:05:32 +00:00
|
|
|
#![allow(clippy::type_complexity)]
|
2022-08-25 22:33:44 +00:00
|
|
|
use crate::{levels::stored::*, AppState};
|
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
input::{keyboard::KeyCode, Input},
|
|
|
|
prelude::{
|
|
|
|
shape::{Circle, Quad},
|
|
|
|
*,
|
|
|
|
},
|
2022-08-26 08:05:32 +00:00
|
|
|
sprite::Mesh2dHandle,
|
2022-08-25 22:33:44 +00:00
|
|
|
};
|
|
|
|
use bevy_mod_picking::*;
|
|
|
|
|
|
|
|
pub struct EditorPlugin;
|
|
|
|
|
|
|
|
impl Plugin for EditorPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
2022-08-26 08:05:32 +00:00
|
|
|
app.add_event::<DragEndEvent>()
|
|
|
|
.add_plugins(DefaultPickingPlugins)
|
2022-08-25 22:33:44 +00:00
|
|
|
.add_system_set(SystemSet::on_enter(AppState::Editor).with_system(setup))
|
|
|
|
.add_system_set(
|
2022-08-26 08:05:32 +00:00
|
|
|
SystemSet::on_update(AppState::Editor)
|
2022-08-26 10:00:36 +00:00
|
|
|
.with_system(move_system)
|
2022-08-26 12:57:53 +00:00
|
|
|
.with_system(input_control_system)
|
2022-08-26 08:05:32 +00:00
|
|
|
.with_system(follow_ends_system),
|
2022-08-25 22:33:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 08:05:32 +00:00
|
|
|
// Events
|
|
|
|
|
|
|
|
struct DragEndEvent(Entity);
|
|
|
|
|
2022-08-26 10:00:36 +00:00
|
|
|
// Resources
|
|
|
|
|
2022-08-26 12:57:53 +00:00
|
|
|
struct CharacterList(Vec<Entity>);
|
|
|
|
|
2022-08-26 08:05:32 +00:00
|
|
|
// Components
|
|
|
|
|
2022-08-25 22:33:44 +00:00
|
|
|
#[derive(Component)]
|
2022-08-26 12:57:53 +00:00
|
|
|
struct Platform;
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Ends(Entity, Entity);
|
2022-08-25 22:33:44 +00:00
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Draggable;
|
|
|
|
|
|
|
|
#[derive(Component)]
|
2022-08-26 08:05:32 +00:00
|
|
|
struct End(Entity);
|
|
|
|
|
2022-08-26 12:57:53 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
struct CharacterColor(Color);
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Size(Vec2);
|
|
|
|
|
2022-08-26 08:05:32 +00:00
|
|
|
// Bundles
|
2022-08-25 22:33:44 +00:00
|
|
|
|
|
|
|
#[derive(Bundle)]
|
|
|
|
struct PlatformBundle {
|
|
|
|
#[bundle]
|
|
|
|
mesh: ColorMesh2dBundle,
|
2022-08-26 12:57:53 +00:00
|
|
|
size: Size,
|
|
|
|
platform: Platform,
|
2022-08-25 22:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Bundle)]
|
|
|
|
struct PlatformEndBundle {
|
|
|
|
#[bundle]
|
|
|
|
mesh: ColorMesh2dBundle,
|
|
|
|
#[bundle]
|
|
|
|
pickable: PickableBundle,
|
|
|
|
draggable: Draggable,
|
|
|
|
end: End,
|
|
|
|
}
|
|
|
|
|
2022-08-26 10:00:36 +00:00
|
|
|
#[derive(Bundle)]
|
|
|
|
struct CharacterBundle {
|
|
|
|
#[bundle]
|
|
|
|
mesh: ColorMesh2dBundle,
|
2022-08-26 12:57:53 +00:00
|
|
|
color: CharacterColor,
|
2022-08-26 10:00:36 +00:00
|
|
|
#[bundle]
|
|
|
|
pickable: PickableBundle,
|
|
|
|
draggable: Draggable,
|
|
|
|
}
|
|
|
|
|
2022-08-26 08:05:32 +00:00
|
|
|
// Functions
|
|
|
|
|
2022-08-25 22:33:44 +00:00
|
|
|
fn spawn_platform(
|
|
|
|
commands: &mut Commands,
|
|
|
|
meshes: &mut ResMut<Assets<Mesh>>,
|
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
|
|
|
|
|
|
|
transform: Transform,
|
|
|
|
size: Vec2,
|
|
|
|
) {
|
2022-08-26 08:05:32 +00:00
|
|
|
let platform = commands
|
2022-08-25 22:33:44 +00:00
|
|
|
.spawn_bundle(PlatformBundle {
|
|
|
|
mesh: ColorMesh2dBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(Quad { size, flip: false })).into(),
|
|
|
|
material: materials.add(ColorMaterial::from(Color::GRAY)),
|
|
|
|
transform,
|
|
|
|
..default()
|
|
|
|
},
|
2022-08-26 12:57:53 +00:00
|
|
|
size: Size(size),
|
|
|
|
platform: Platform,
|
2022-08-25 22:33:44 +00:00
|
|
|
})
|
2022-08-26 08:05:32 +00:00
|
|
|
.id();
|
2022-08-26 12:57:53 +00:00
|
|
|
let ends = Ends(
|
2022-08-26 08:05:32 +00:00
|
|
|
commands
|
|
|
|
.spawn_bundle(PlatformEndBundle {
|
2022-08-25 22:33:44 +00:00
|
|
|
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))),
|
2022-08-26 08:05:32 +00:00
|
|
|
transform: Transform::from_xyz(
|
|
|
|
transform.translation.x - size.x / 2.,
|
|
|
|
transform.translation.y - size.y / 2.,
|
|
|
|
0.5,
|
|
|
|
),
|
2022-08-25 22:33:44 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
pickable: PickableBundle::default(),
|
|
|
|
draggable: Draggable,
|
2022-08-26 08:05:32 +00:00
|
|
|
end: End(platform),
|
|
|
|
})
|
|
|
|
.id(),
|
|
|
|
commands
|
|
|
|
.spawn_bundle(PlatformEndBundle {
|
2022-08-25 22:33:44 +00:00
|
|
|
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))),
|
2022-08-26 08:05:32 +00:00
|
|
|
transform: Transform::from_xyz(
|
|
|
|
transform.translation.x + size.x / 2.,
|
|
|
|
transform.translation.y + size.y / 2.,
|
|
|
|
0.5,
|
|
|
|
),
|
2022-08-25 22:33:44 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
pickable: PickableBundle::default(),
|
|
|
|
draggable: Draggable,
|
2022-08-26 08:05:32 +00:00
|
|
|
end: End(platform),
|
|
|
|
})
|
|
|
|
.id(),
|
|
|
|
);
|
|
|
|
commands.entity(platform).insert(ends);
|
2022-08-25 22:33:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 10:00:36 +00:00
|
|
|
fn spawn_character(
|
|
|
|
commands: &mut Commands,
|
|
|
|
meshes: &mut ResMut<Assets<Mesh>>,
|
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
|
|
|
asset_server: &Res<AssetServer>,
|
|
|
|
|
|
|
|
transform: Transform,
|
|
|
|
color: Color,
|
|
|
|
index: usize,
|
2022-08-26 12:57:53 +00:00
|
|
|
) -> Entity {
|
2022-08-26 10:00:36 +00:00
|
|
|
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()
|
|
|
|
},
|
2022-08-26 12:57:53 +00:00
|
|
|
color: CharacterColor(color),
|
2022-08-26 10:00:36 +00:00
|
|
|
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()
|
|
|
|
});
|
2022-08-26 12:57:53 +00:00
|
|
|
})
|
|
|
|
.id()
|
2022-08-26 10:00:36 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 12:57:53 +00:00
|
|
|
fn spawn_stored_level(
|
2022-08-25 22:33:44 +00:00
|
|
|
commands: &mut Commands,
|
|
|
|
meshes: &mut ResMut<Assets<Mesh>>,
|
|
|
|
materials: &mut ResMut<Assets<ColorMaterial>>,
|
|
|
|
asset_server: &Res<AssetServer>,
|
|
|
|
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
2022-08-26 10:00:36 +00:00
|
|
|
|
2022-08-26 12:57:53 +00:00
|
|
|
let mut character_list = Vec::new();
|
2022-08-26 10:00:36 +00:00
|
|
|
for (i, character) in stored_level.characters.iter().enumerate() {
|
2022-08-26 12:57:53 +00:00
|
|
|
character_list.push(spawn_character(
|
2022-08-26 10:00:36 +00:00
|
|
|
commands,
|
|
|
|
meshes,
|
|
|
|
materials,
|
|
|
|
asset_server,
|
|
|
|
Transform::from_xyz(character.pos.x, character.pos.y, 0.),
|
|
|
|
character.color.into(),
|
|
|
|
i,
|
2022-08-26 12:57:53 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
commands.insert_resource(CharacterList(character_list));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save_level(
|
|
|
|
level_id: &Res<crate::game::FirstLevel>,
|
|
|
|
stored_levels_assets: &mut ResMut<Assets<StoredLevels>>,
|
|
|
|
stored_levels_handle: &Res<Handle<StoredLevels>>,
|
|
|
|
character_list: &Res<CharacterList>,
|
|
|
|
character_query: &Query<(&Transform, &CharacterColor), Without<Platform>>,
|
|
|
|
platform_query: &Query<(&Transform, &Size), With<Platform>>,
|
|
|
|
) {
|
|
|
|
let stored_levels = stored_levels_assets.get_mut(stored_levels_handle).unwrap();
|
|
|
|
if let Some(stored_level) = stored_levels.levels.get_mut(level_id.0 .0 as usize) {
|
|
|
|
stored_level.platforms.clear();
|
|
|
|
for (transform, size) in platform_query.iter() {
|
|
|
|
stored_level.platforms.push(StoredPlatform {
|
|
|
|
pos: Vec2 {
|
|
|
|
x: transform.translation.x,
|
|
|
|
y: transform.translation.y,
|
|
|
|
},
|
|
|
|
size: size.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
stored_level.characters.clear();
|
|
|
|
for entity in character_list.0.iter() {
|
|
|
|
if let Ok((transform, color)) = character_query.get(*entity) {
|
|
|
|
stored_level.characters.push(StoredCharacter {
|
|
|
|
pos: Vec2 {
|
|
|
|
x: transform.translation.x,
|
|
|
|
y: transform.translation.y,
|
|
|
|
},
|
|
|
|
color: color.0.as_rgba_f32().into(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open("assets/game.levels.json")
|
|
|
|
{
|
|
|
|
Ok(mut file) => {
|
|
|
|
serde_json::to_writer_pretty(&mut file, stored_levels).unwrap();
|
|
|
|
println!("Saved!");
|
|
|
|
}
|
|
|
|
Err(e) => eprintln!("Error writing levels file: {:?}", e),
|
2022-08-26 10:00:36 +00:00
|
|
|
}
|
2022-08-25 22:33:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 08:05:32 +00:00
|
|
|
// Systems
|
|
|
|
|
2022-08-25 22:33:44 +00:00
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
camera_query: Query<Entity, With<Camera>>,
|
|
|
|
level_id: Res<crate::game::FirstLevel>,
|
|
|
|
stored_levels_assets: Res<Assets<StoredLevels>>,
|
|
|
|
stored_levels_handle: Res<Handle<StoredLevels>>,
|
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
) {
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 12:57:53 +00:00
|
|
|
fn input_control_system(
|
|
|
|
keyboard_input: Res<Input<KeyCode>>,
|
|
|
|
level_id: Res<crate::game::FirstLevel>,
|
|
|
|
mut stored_levels_assets: ResMut<Assets<StoredLevels>>,
|
|
|
|
stored_levels_handle: Res<Handle<StoredLevels>>,
|
|
|
|
character_list: Res<CharacterList>,
|
|
|
|
character_query: Query<(&Transform, &CharacterColor), Without<Platform>>,
|
|
|
|
platform_query: Query<(&Transform, &Size), With<Platform>>,
|
|
|
|
) {
|
|
|
|
if keyboard_input.just_released(KeyCode::S)
|
|
|
|
&& (keyboard_input.pressed(KeyCode::LControl) || keyboard_input.pressed(KeyCode::RControl))
|
|
|
|
{
|
|
|
|
save_level(
|
|
|
|
&level_id,
|
|
|
|
&mut stored_levels_assets,
|
|
|
|
&stored_levels_handle,
|
|
|
|
&character_list,
|
|
|
|
&character_query,
|
|
|
|
&platform_query,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 10:00:36 +00:00
|
|
|
fn move_system(
|
2022-08-25 22:33:44 +00:00
|
|
|
keyboard_input: Res<Input<KeyCode>>,
|
2022-08-26 08:16:40 +00:00
|
|
|
mut camera_query: Query<&mut Transform, (With<Camera>, Without<Draggable>)>,
|
2022-08-26 08:05:32 +00:00
|
|
|
mut drag_query: Query<(&mut Transform, &Selection, Option<&End>), With<Draggable>>,
|
|
|
|
mut drag_end_event: EventWriter<DragEndEvent>,
|
2022-08-25 22:33:44 +00:00
|
|
|
) {
|
2022-08-26 08:16:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-26 08:05:32 +00:00
|
|
|
let drag = if keyboard_input.pressed(KeyCode::LShift) || keyboard_input.pressed(KeyCode::RShift)
|
|
|
|
{
|
|
|
|
Vec3 {
|
2022-08-25 22:33:44 +00:00
|
|
|
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.,
|
2022-08-26 08:05:32 +00:00
|
|
|
}
|
|
|
|
} 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.;
|
2022-08-25 22:33:44 +00:00
|
|
|
if drag != Vec3::ZERO {
|
2022-08-26 08:05:32 +00:00
|
|
|
for (mut transform, selection, end) in drag_query.iter_mut() {
|
2022-08-25 22:33:44 +00:00
|
|
|
if selection.selected() {
|
|
|
|
transform.translation += drag;
|
2022-08-26 08:05:32 +00:00
|
|
|
if let Some(End(entity)) = end {
|
|
|
|
drag_end_event.send(DragEndEvent(*entity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn follow_ends_system(
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
2022-08-26 12:57:53 +00:00
|
|
|
mut follower_query: Query<(&mut Transform, &mut Mesh2dHandle, &mut Size, &Ends)>,
|
|
|
|
end_query: Query<&Transform, Without<Ends>>,
|
2022-08-26 08:05:32 +00:00
|
|
|
mut drag_end_event: EventReader<DragEndEvent>,
|
|
|
|
) {
|
|
|
|
for DragEndEvent(entity) in drag_end_event.iter() {
|
2022-08-26 12:57:53 +00:00
|
|
|
if let Ok((mut transform, mut mesh, mut size, Ends(end1, end2))) =
|
|
|
|
follower_query.get_mut(*entity)
|
2022-08-26 08:05:32 +00:00
|
|
|
{
|
|
|
|
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.;
|
2022-08-26 12:57:53 +00:00
|
|
|
size.0 = Vec2 {
|
|
|
|
x: (end2.translation.x - end1.translation.x).abs(),
|
|
|
|
y: (end2.translation.y - end1.translation.y).abs(),
|
|
|
|
};
|
2022-08-26 08:05:32 +00:00
|
|
|
*mesh = meshes
|
|
|
|
.add(Mesh::from(Quad {
|
2022-08-26 12:57:53 +00:00
|
|
|
size: size.0,
|
2022-08-26 08:05:32 +00:00
|
|
|
flip: false,
|
|
|
|
}))
|
|
|
|
.into();
|
2022-08-25 22:33:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|