Skip to content

Commit

Permalink
Implement object drafting
Browse files Browse the repository at this point in the history
Relates to #23.
  • Loading branch information
Indy2222 committed Jun 27, 2022
1 parent 38acb06 commit 2d1f632
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
57 changes: 57 additions & 0 deletions crates/objects/src/draft.rs
Original file line number Diff line number Diff line change
@@ -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<Draft>, Without<Ready>)>;

fn new_draft(mut commands: Commands, drafts: NonReadyDrafts, cache: Res<ObjectCache>) {
for (entity, object_type) in drafts.iter() {
commands
.entity(entity)
.insert(Ready)
.with_children(|parent| {
parent.spawn_scene(cache.get(*object_type).scene());
});
}
}
5 changes: 4 additions & 1 deletion crates/objects/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 2d1f632

Please sign in to comment.