129 lines
5.4 KiB
Rust
129 lines
5.4 KiB
Rust
use common::space::{
|
|
level_tile::LevelTile, three_dimensional::hash_map_container::VoxelHashMapLayer,
|
|
two_dimensional::depth_tiles::{DepthTileContainer, DepthTile},
|
|
};
|
|
use common::strum::IntoEnumIterator;
|
|
use std::collections::VecDeque;
|
|
|
|
use bevy::{prelude::*, tasks::{TaskPool, AsyncComputeTaskPool}};
|
|
use common::space::two_dimensional::depth_tiles::Direction;
|
|
|
|
use super::{generic_command_queue::CommandQueue, ui::ui_spawner::{UiSpawnerCommand, build_message_box_command, build_command, EditorPopup}};
|
|
|
|
pub fn layer_spawner(
|
|
mut spawner_commands: ResMut<CommandQueue<LayerSpawnerCommand>>,
|
|
mut commands: Commands,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut tile_hashmap_layer_query: Query<&mut VoxelHashMapLayer<LevelTile>>,
|
|
ui_spawner_commands: ResMut<CommandQueue<UiSpawnerCommand>>,
|
|
) {
|
|
for command in spawner_commands.get_commands() {
|
|
match command {
|
|
LayerSpawnerCommand::FromVoxData { name, data } => {
|
|
match common::space::three_dimensional::hash_map_container::import_from_bytes::<
|
|
LevelTile,
|
|
>(&data)
|
|
{
|
|
Ok(voxels) => {
|
|
crate::voxels::layers::container_to_layer_component(
|
|
name,
|
|
voxels,
|
|
StandardMaterial::from(Color::rgb(5.0, 5.0, 5.0)),
|
|
&mut meshes,
|
|
&mut commands,
|
|
&mut materials,
|
|
);
|
|
}
|
|
Err(e) => {
|
|
println!("Couldn't load file {}: {:?}", name, e)
|
|
}
|
|
}
|
|
}
|
|
LayerSpawnerCommand::FromTileProjectedLayer {
|
|
name,
|
|
source_layer,
|
|
direction,
|
|
} => {
|
|
if let Ok(layer) = tile_hashmap_layer_query.get_mut(source_layer) {
|
|
let layer = layer.into_inner();
|
|
|
|
let tiles = common::space::three_dimensional::project_to_2d::create_tilemap(
|
|
layer, direction, 32, 32, 32,
|
|
);
|
|
let tile_map_layer = DepthTileContainer::<LevelTile> { tiles };
|
|
|
|
crate::voxels::layers::container_to_layer_component(
|
|
name,
|
|
tile_map_layer,
|
|
StandardMaterial::from(Color::rgb(0.0, 5.0, 0.0)),
|
|
&mut meshes,
|
|
&mut commands,
|
|
&mut materials,
|
|
);
|
|
}
|
|
}
|
|
LayerSpawnerCommand::ExportProjectedTilesToml { name, source_layer } => {
|
|
let sender = ui_spawner_commands.get_async_sender();
|
|
if let Ok(layer) = tile_hashmap_layer_query.get_mut(source_layer) {
|
|
let mut layer = layer.into_inner().clone();
|
|
let pool = AsyncComputeTaskPool::get();
|
|
pool.spawn(async move {
|
|
match common::space::level_serde::create_export_map(&mut layer) {
|
|
Ok(map) => {
|
|
match common::ron::to_string(&map) {
|
|
Ok(map_ron) => {
|
|
sender.send(build_command(EditorPopup {
|
|
title: "exported".to_string(),
|
|
data: map_ron,
|
|
open: true
|
|
})).await;
|
|
},
|
|
Err(e) => {
|
|
let (cmd, recv) = build_message_box_command("error".to_string(), format!("serialization failed: {:?}", e), vec![":(", "show Debug"], true);
|
|
sender.send(cmd).await;
|
|
if let Ok(button_pressed) = recv.recv().await {
|
|
if button_pressed == "show Debug" {
|
|
|
|
sender.send(build_command(EditorPopup {
|
|
title: "Debug of export".to_string(),
|
|
data: format!("{:?}", map),
|
|
open: true
|
|
})).await;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
},
|
|
Err(e) => {
|
|
let (cmd, recv) = build_message_box_command("error".to_string(), format!("export failed: {:?}", e), vec![":("], true);
|
|
sender.send(cmd).await;
|
|
},
|
|
}
|
|
|
|
}).detach();
|
|
}
|
|
},
|
|
}
|
|
}
|
|
}
|
|
|
|
pub enum LayerSpawnerCommand {
|
|
FromVoxData {
|
|
name: String,
|
|
data: Vec<u8>,
|
|
},
|
|
FromTileProjectedLayer {
|
|
name: String,
|
|
source_layer: Entity,
|
|
direction: common::space::two_dimensional::depth_tiles::Direction,
|
|
},
|
|
ExportProjectedTilesToml {
|
|
name: String,
|
|
source_layer: Entity,
|
|
}
|
|
}
|