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 20, 2022
1 parent a61b80d commit 9ce0208
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/objects/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
106 changes: 106 additions & 0 deletions crates/objects/src/draft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! 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 std::f32::consts::TAU;

use bevy::prelude::*;
use de_core::{objects::ObjectType, projection::ToMsl, state::GameState};
use glam::Vec2;
use iyes_loopless::prelude::*;

use crate::ObjectCache;

pub(crate) struct DraftPlugin;

impl Plugin for DraftPlugin {
fn build(&self, app: &mut App) {
app.add_event::<NewDraftEvent>()
.add_event::<MoveDraftEvent>()
.add_event::<RotateDraftEvent>()
.add_event::<ClearDraftsEvent>()
.add_system(new_draft.run_in_state(GameState::Playing));
}
}

/// Places a new draft on the map and removes all other drafts.
pub struct NewDraftEvent {
object_type: ObjectType,
position: Vec2,
}

impl NewDraftEvent {
pub fn new(object_type: ObjectType, position: Vec2) -> Self {
Self {
object_type,
position,
}
}

fn object_type(&self) -> ObjectType {
self.object_type
}

fn position(&self) -> Vec2 {
self.position
}
}

pub struct MoveDraftEvent {
position: Vec2,
}

impl MoveDraftEvent {
pub fn new(position: Vec2) -> Self {
Self { position }
}
}

pub struct RotateDraftEvent {
angle: f32,
}

impl RotateDraftEvent {
/// # Panics
///
/// Might panic if `angle` is not between zero (inclusive) and 2π
/// (exclusive).
pub fn new(angle: f32) -> Self {
debug_assert!(angle >= 0.);
debug_assert!(angle < TAU);
Self { angle }
}
}

pub struct ClearDraftsEvent;

#[derive(Component)]
struct Draft;

fn new_draft(
mut commands: Commands,
drafts: Query<Entity, With<Draft>>,
cache: Res<ObjectCache>,
mut events: EventReader<NewDraftEvent>,
) {
let event = match events.iter().last() {
Some(event) => event,
None => return,
};

for draft in drafts.iter() {
commands.entity(draft).despawn();
}

let transform = Transform {
translation: event.position().to_msl(),
..Default::default()
};
let global_transform = GlobalTransform::from(transform);
let mut entity_commands = commands.spawn_bundle((global_transform, transform));
entity_commands.with_children(|parent| {
parent.spawn_scene(cache.get(event.object_type()).scene());
});
}

// TODO handle other events
4 changes: 3 additions & 1 deletion crates/objects/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use bevy::{app::PluginGroupBuilder, prelude::PluginGroup};
use cache::CachePlugin;
pub use cache::ObjectCache;
pub use collider::{ColliderCache, ObjectCollider};
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;
Expand All @@ -19,6 +21,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 9ce0208

Please sign in to comment.