resource production via graveyard, workers, and selectable buildlings
This commit is contained in:
parent
87fcc35a49
commit
921947c131
20 changed files with 364 additions and 24 deletions
|
|
@ -5,6 +5,7 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
avian3d = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
ron = { workspace = true }
|
||||
state = { path = "../state" }
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ pub mod conditions;
|
|||
|
||||
use std::{fs, marker::PhantomData};
|
||||
|
||||
use avian3d::prelude::*;
|
||||
use bevy::{
|
||||
input::mouse::{AccumulatedMouseMotion, AccumulatedMouseScroll},
|
||||
platform::collections::HashMap,
|
||||
|
|
@ -22,21 +23,93 @@ pub struct InputPlugin;
|
|||
|
||||
impl Plugin for InputPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<Inputs<FixedUpdate>>()
|
||||
app.add_event::<SelectEntity>()
|
||||
.init_resource::<Inputs<FixedUpdate>>()
|
||||
.init_resource::<Inputs<Update>>()
|
||||
.configure_sets(Update, GameSystemSet::UpdateInputs)
|
||||
.add_systems(Startup, setup_input_map)
|
||||
.add_systems(
|
||||
Update,
|
||||
(update_inputs_frame, save_input_map).in_set(GameSystemSet::UpdateInputs),
|
||||
(
|
||||
update_inputs_frame,
|
||||
save_input_map,
|
||||
(cancel_selection, update_selection).chain(),
|
||||
)
|
||||
.in_set(GameSystemSet::UpdateInputs),
|
||||
)
|
||||
.add_systems(FixedPostUpdate, clear_changed_fixed);
|
||||
.add_systems(FixedPostUpdate, clear_changed_fixed)
|
||||
.add_observer(select);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Cancellable;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct Selectable;
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Selected;
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct SelectEntity(pub Option<Entity>);
|
||||
|
||||
fn cancel_selection(
|
||||
mut writer: EventWriter<SelectEntity>,
|
||||
query: Query<(), (With<Selected>, With<Cancellable>)>,
|
||||
inputs: Res<Inputs<Update>>,
|
||||
) {
|
||||
if !query.is_empty() && inputs.just_pressed(InputAction::Cancel) {
|
||||
writer.write(SelectEntity(None));
|
||||
}
|
||||
}
|
||||
|
||||
fn select(
|
||||
trigger: Trigger<Pointer<Click>>,
|
||||
physics_query: Query<(), With<PhysicsPickable>>,
|
||||
mut writer: EventWriter<SelectEntity>,
|
||||
) {
|
||||
// avoid deselecting when clicking on ui
|
||||
if physics_query.contains(trigger.event().target) {
|
||||
writer.write(SelectEntity(Some(trigger.event().target)));
|
||||
}
|
||||
}
|
||||
|
||||
fn update_selection(
|
||||
mut reader: EventReader<SelectEntity>,
|
||||
mut old_query: Query<Entity, With<Selected>>,
|
||||
new_query: Query<(), With<Selectable>>,
|
||||
name_query: Query<&Name>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
let Some(SelectEntity(opt_entity)) = reader.read().last() else {
|
||||
return;
|
||||
};
|
||||
let Some(new_entity) = opt_entity else {
|
||||
for entity in old_query.iter_mut() {
|
||||
if *opt_entity != Some(entity) {
|
||||
commands.entity(entity).remove::<(Selected, Cancellable)>();
|
||||
}
|
||||
}
|
||||
return;
|
||||
};
|
||||
if !new_query.contains(*new_entity) {
|
||||
info!(
|
||||
"thing is not selectable {:?} {:?}",
|
||||
new_entity,
|
||||
name_query.get(*new_entity)
|
||||
);
|
||||
return;
|
||||
}
|
||||
for entity in old_query.iter_mut() {
|
||||
if *opt_entity != Some(entity) {
|
||||
commands.entity(entity).remove::<(Selected, Cancellable)>();
|
||||
}
|
||||
}
|
||||
|
||||
commands.entity(*new_entity).insert((Selected, Cancellable));
|
||||
}
|
||||
|
||||
#[derive(Hash, Clone, Copy, Debug, Reflect, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum InputMethod {
|
||||
Unbound,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue