From 2d1f6325a2a88c459084f6c7997d1849901604f5 Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Mon, 20 Jun 2022 22:27:06 +0200 Subject: [PATCH] Implement object drafting Relates to #23. --- crates/objects/src/draft.rs | 57 +++++++++++++++++++++++++++++++++++++ crates/objects/src/lib.rs | 5 +++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 crates/objects/src/draft.rs diff --git a/crates/objects/src/draft.rs b/crates/objects/src/draft.rs new file mode 100644 index 000000000..3fd0709cf --- /dev/null +++ b/crates/objects/src/draft.rs @@ -0,0 +1,57 @@ +//! This module implements a Bevy plugin for drafting new objects on the map. +//! An entity marked with a component [`Draft`] is automatically handled and +//! visualized by the plugin. + +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)); + } +} + +/// Bundle to spawn a construction draft. +#[derive(Bundle)] +pub struct DraftBundle { + object_type: ObjectType, + transform: Transform, + global_transform: GlobalTransform, + draft: Draft, +} + +impl DraftBundle { + pub fn new(object_type: ObjectType, transform: Transform) -> Self { + Self { + object_type, + transform, + global_transform: transform.into(), + draft: Draft, + } + } +} + +#[derive(Component)] +pub struct Draft; + +#[derive(Component)] +struct Ready; + +type NonReadyDrafts<'w, 's> = + Query<'w, 's, (Entity, &'static ObjectType), (With, Without)>; + +fn new_draft(mut commands: Commands, drafts: NonReadyDrafts, cache: Res) { + for (entity, object_type) in drafts.iter() { + commands + .entity(entity) + .insert(Ready) + .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 a367e67f9..f9b1c9c76 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}; +use draft::DraftPlugin; +pub use draft::{Draft, DraftBundle}; pub use ichnography::{Ichnography, IchnographyCache}; pub use spawner::SpawnBundle; 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); } }