editor: characters

This commit is contained in:
Pascal Engélibert 2022-08-26 12:00:36 +02:00
parent 5d2289d1c0
commit da5f9b0820
Signed by: tuxmain
GPG key ID: 3504BC6D362F7DCA

View file

@ -20,7 +20,7 @@ impl Plugin for EditorPlugin {
.add_system_set(SystemSet::on_enter(AppState::Editor).with_system(setup))
.add_system_set(
SystemSet::on_update(AppState::Editor)
.with_system(keyboard_input_system)
.with_system(move_system)
.with_system(follow_ends_system),
);
}
@ -30,6 +30,8 @@ impl Plugin for EditorPlugin {
struct DragEndEvent(Entity);
// Resources
// Components
#[derive(Component)]
@ -47,7 +49,6 @@ struct End(Entity);
struct PlatformBundle {
#[bundle]
mesh: ColorMesh2dBundle,
//platform: Platform,
}
#[derive(Bundle)]
@ -60,6 +61,15 @@ struct PlatformEndBundle {
end: End,
}
#[derive(Bundle)]
struct CharacterBundle {
#[bundle]
mesh: ColorMesh2dBundle,
#[bundle]
pickable: PickableBundle,
draggable: Draggable,
}
// Functions
fn spawn_platform(
@ -129,6 +139,50 @@ fn spawn_platform(
commands.entity(platform).insert(ends);
}
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,
) {
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<Assets<Mesh>>,
@ -137,8 +191,6 @@ pub fn spawn_stored_level(
stored_level: &StoredLevel,
) {
let _font = asset_server.get_handle::<Font, _>("UacariLegacy-Thin.ttf");
for platform in stored_level.platforms.iter() {
spawn_platform(
commands,
@ -148,6 +200,18 @@ pub fn spawn_stored_level(
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
@ -182,7 +246,7 @@ fn setup(
}
}
fn keyboard_input_system(
fn move_system(
keyboard_input: Res<Input<KeyCode>>,
mut camera_query: Query<&mut Transform, (With<Camera>, Without<Draggable>)>,
mut drag_query: Query<(&mut Transform, &Selection, Option<&End>), With<Draggable>>,