diff --git a/Cargo.lock b/Cargo.lock index 360e2c51a..8e5bda30e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1718,6 +1718,7 @@ dependencies = [ "de_core", "de_map", "enum-map", + "glam", "iyes_loopless", "iyes_progress", "parry2d", diff --git a/crates/objects/Cargo.toml b/crates/objects/Cargo.toml index 465152f37..468e62b50 100644 --- a/crates/objects/Cargo.toml +++ b/crates/objects/Cargo.toml @@ -14,6 +14,7 @@ de_map = { path = "../map", version = "0.1.0-dev" } bevy = "0.7.0" iyes_loopless = "0.5.1" iyes_progress = { version = "0.3.0", features = [ "iyes_loopless" ] } +glam = "^0.20.2" parry2d = { git = "https://github.com/Indy2222/parry", branch = "feature/dilation" } parry3d = "0.9.0" enum-map = "2.3.0" diff --git a/crates/objects/src/draft.rs b/crates/objects/src/draft.rs new file mode 100644 index 000000000..a549ec3a6 --- /dev/null +++ b/crates/objects/src/draft.rs @@ -0,0 +1,43 @@ +//! This module implements a Bevy plugin for drafting new objects on the map. +//! This serves mostly as a visual guide for the user to correctly place new +//! structures on the map. + +use bevy::prelude::*; +use de_core::{objects::ObjectType, state::GameState}; +use iyes_loopless::prelude::*; + +use crate::ObjectCache; + +pub(crate) struct DraftPlugin; + +impl Plugin for DraftPlugin { + fn build(&self, app: &mut App) { + app.add_system(new_draft.run_in_state(GameState::Playing)); + } +} + +/// This is a marker component. +/// +/// An entity with this component is considered to be a (construction) draft. +/// Its model will be automatically spawned by a system in this module. +/// +/// An entity marked as a draft must have components +/// [`bevy::transform::components::Transform`] and +/// [`de_core::objects::ObjectType`]. +#[derive(Component)] +pub struct Draft; + +fn new_draft( + mut commands: Commands, + drafts: Query<(Entity, &ObjectType), With>, + cache: Res, +) { + for (entity, object_type) in drafts.iter() { + commands + .entity(entity) + .remove::() + .with_children(|parent| { + parent.spawn_scene(cache.get(*object_type).scene()); + }); + } +} diff --git a/crates/objects/src/lib.rs b/crates/objects/src/lib.rs index 5a86eb512..194f0ad1b 100644 --- a/crates/objects/src/lib.rs +++ b/crates/objects/src/lib.rs @@ -5,12 +5,15 @@ use bevy::{app::PluginGroupBuilder, prelude::PluginGroup}; use cache::CachePlugin; pub use cache::ObjectCache; pub use collider::{ColliderCache, ObjectCollider}; +pub use draft::Draft; +use draft::DraftPlugin; pub use ichnography::{Ichnography, IchnographyCache}; pub use spawner::SpawnEvent; use spawner::SpawnerPlugin; mod cache; mod collider; +mod draft; mod ichnography; mod loader; mod spawner; @@ -19,6 +22,6 @@ pub struct ObjectsPluginGroup; impl PluginGroup for ObjectsPluginGroup { fn build(&mut self, group: &mut PluginGroupBuilder) { - group.add(CachePlugin).add(SpawnerPlugin); + group.add(CachePlugin).add(SpawnerPlugin).add(DraftPlugin); } }