-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relates to #23.
- Loading branch information
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters