add tower selection UI, cancellable actions, and pause UI
This commit is contained in:
parent
138a524abd
commit
918b4884b2
18 changed files with 468 additions and 23 deletions
|
|
@ -62,6 +62,7 @@ fn on_building_removed(mut world: DeferredWorld, context: HookContext) {
|
|||
pub struct BuildingInfo {
|
||||
pub preview: Entity,
|
||||
pub size: UVec3,
|
||||
pub unlocked: bool,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
|
|
@ -69,9 +70,6 @@ pub struct BuildingPreview {
|
|||
pub info: Entity,
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct ActiveBuildingPreview(pub IVec3);
|
||||
|
||||
pub const PREVIEW_ALPHA: f32 = 0.5;
|
||||
|
||||
fn register_building_info(mut world: DeferredWorld, context: HookContext) {
|
||||
|
|
|
|||
|
|
@ -59,6 +59,7 @@ fn register(
|
|||
BuildingInfo {
|
||||
preview,
|
||||
size: SIZE,
|
||||
unlocked: false,
|
||||
},
|
||||
Name::new("Core"),
|
||||
));
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@
|
|||
|
||||
use avian3d::prelude::*;
|
||||
use bevy::prelude::*;
|
||||
use building::{
|
||||
ActiveBuildingPreview, BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding,
|
||||
};
|
||||
use building::{BuildingInfo, BuildingPreview, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
||||
use combat::{Defense, Health};
|
||||
use combat_damagers::ContactDamage;
|
||||
use resources::Cost;
|
||||
|
|
@ -54,7 +52,7 @@ fn register(
|
|||
let preview = commands
|
||||
.spawn((
|
||||
BuildingPreview { info },
|
||||
ActiveBuildingPreview::default(), // Visibility::Hidden,
|
||||
Visibility::Hidden,
|
||||
Mesh3d(mesh.clone()),
|
||||
MeshMaterial3d(preview_material),
|
||||
))
|
||||
|
|
@ -63,6 +61,7 @@ fn register(
|
|||
BuildingInfo {
|
||||
preview,
|
||||
size: SIZE,
|
||||
unlocked: true,
|
||||
},
|
||||
Cost { souls: 5, bones: 0 },
|
||||
Name::new("Impaler"),
|
||||
|
|
|
|||
|
|
@ -3,20 +3,25 @@
|
|||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use bevy::prelude::*;
|
||||
use building::{
|
||||
ActiveBuildingPreview, BuildingInfo, BuildingPreview, PlaceBuilding, PlacedBuildings,
|
||||
};
|
||||
use input::Inputs;
|
||||
use building::{BuildingInfo, BuildingPreview, PlaceBuilding, PlacedBuildings};
|
||||
use input::{Cancellable, Inputs, actions::InputAction};
|
||||
use physics::Ground;
|
||||
|
||||
pub struct BuildingPlacementPlugin;
|
||||
|
||||
impl Plugin for BuildingPlacementPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, init);
|
||||
app.add_systems(Update, (init, cancel_preview, select_preview).chain())
|
||||
.add_event::<SelectBuildingPreview>();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default)]
|
||||
pub struct ActiveBuildingPreview(pub IVec3);
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct SelectBuildingPreview(pub Option<Entity>);
|
||||
|
||||
fn init(mut commands: Commands, query: Single<Entity, With<Ground>>, mut setup: Local<bool>) {
|
||||
if *setup {
|
||||
return;
|
||||
|
|
@ -29,6 +34,16 @@ fn init(mut commands: Commands, query: Single<Entity, With<Ground>>, mut setup:
|
|||
info!("added building placement observers!");
|
||||
}
|
||||
|
||||
fn cancel_preview(
|
||||
mut writer: EventWriter<SelectBuildingPreview>,
|
||||
query: Query<(), (With<ActiveBuildingPreview>, With<Cancellable>)>,
|
||||
inputs: Res<Inputs<Update>>,
|
||||
) {
|
||||
if !query.is_empty() && inputs.just_pressed(InputAction::Cancel) {
|
||||
writer.write(SelectBuildingPreview(None));
|
||||
}
|
||||
}
|
||||
|
||||
fn move_preview(
|
||||
trigger: Trigger<Pointer<Move>>,
|
||||
preview: Single<(&mut Transform, &mut ActiveBuildingPreview, &BuildingPreview)>,
|
||||
|
|
@ -60,8 +75,9 @@ fn place_preview(
|
|||
info_query: Query<&BuildingInfo>,
|
||||
placed_buildings: Res<PlacedBuildings>,
|
||||
mut place_writer: EventWriter<PlaceBuilding>,
|
||||
mut select_writer: EventWriter<SelectBuildingPreview>,
|
||||
) {
|
||||
if !inputs.pressed(input::actions::InputAction::PlaceTower) {
|
||||
if !inputs.pressed(InputAction::PlaceTower) {
|
||||
return;
|
||||
}
|
||||
let (preview, active) = query.into_inner();
|
||||
|
|
@ -73,8 +89,42 @@ fn place_preview(
|
|||
info!("invalid placement!!!!!");
|
||||
return;
|
||||
}
|
||||
if !inputs.pressed(InputAction::MultiPlaceTower) {
|
||||
select_writer.write(SelectBuildingPreview(None));
|
||||
}
|
||||
place_writer.write(PlaceBuilding {
|
||||
info: preview.info,
|
||||
location: active.0,
|
||||
});
|
||||
}
|
||||
|
||||
fn select_preview(
|
||||
mut reader: EventReader<SelectBuildingPreview>,
|
||||
mut old_query: Query<(Entity, &mut Visibility), With<ActiveBuildingPreview>>,
|
||||
new_query: Query<(), With<BuildingPreview>>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
let Some(SelectBuildingPreview(opt_entity)) = reader.read().last() else {
|
||||
return;
|
||||
};
|
||||
for (entity, mut vis) in old_query.iter_mut() {
|
||||
if *opt_entity != Some(entity) {
|
||||
commands
|
||||
.entity(entity)
|
||||
.remove::<(ActiveBuildingPreview, Cancellable)>();
|
||||
*vis = Visibility::Hidden;
|
||||
}
|
||||
}
|
||||
let Some(new_entity) = opt_entity else {
|
||||
return;
|
||||
};
|
||||
if !new_query.contains(*new_entity) {
|
||||
error!("tried to select a building without a preview!");
|
||||
return;
|
||||
}
|
||||
commands.entity(*new_entity).insert((
|
||||
Visibility::Visible,
|
||||
ActiveBuildingPreview::default(),
|
||||
Cancellable,
|
||||
));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ pub enum InputAction {
|
|||
CameraPanEnable,
|
||||
CameraOrbitEnable,
|
||||
PlaceTower,
|
||||
MultiPlaceTower,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
impl Default for InputMap {
|
||||
|
|
@ -43,6 +45,11 @@ impl Default for InputMap {
|
|||
InputMethod::MouseButton(MouseButton::Right),
|
||||
InputAction::PlaceTower,
|
||||
),
|
||||
(
|
||||
InputMethod::KeyCode(KeyCode::ShiftLeft),
|
||||
InputAction::MultiPlaceTower,
|
||||
),
|
||||
(InputMethod::KeyCode(KeyCode::Escape), InputAction::Cancel),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,9 @@ impl Plugin for InputPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct Cancellable;
|
||||
|
||||
#[derive(Hash, Clone, Copy, Debug, Reflect, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum InputMethod {
|
||||
Unbound,
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ edition = "2024"
|
|||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
avian3d = { workspace = true }
|
||||
bevy_egui = { workspace = true }
|
||||
|
||||
state = { path = "../state" }
|
||||
camera = { path = "../camera" }
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use bevy::{
|
|||
prelude::*,
|
||||
render::camera::Exposure,
|
||||
};
|
||||
use bevy_egui::PrimaryEguiContext;
|
||||
use building::{BuildingTypes, PlaceBuilding};
|
||||
use camera::ControlledCamera;
|
||||
use inhabitant_coranth::SpawnCoranth;
|
||||
|
|
@ -86,6 +87,7 @@ fn setup(
|
|||
Exposure::SUNLIGHT,
|
||||
Bloom::NATURAL,
|
||||
PhysicsPickable,
|
||||
PrimaryEguiContext,
|
||||
Transform::from_xyz(-2.5, 9.5, 9.0).looking_at(Vec3::ZERO, Dir3::Y),
|
||||
));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
bevy-inspector-egui = { workspace = true }
|
||||
bevy_egui = { workspace = true }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_inspector_egui::bevy_egui::EguiPlugin;
|
||||
use bevy_egui::EguiPlugin;
|
||||
|
||||
pub struct UiPlugin;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,12 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
bevy_egui = { workspace = true }
|
||||
|
||||
ui_state = { path = "../ui_state" }
|
||||
resources = { path = "../resources" }
|
||||
building = { path = "../building" }
|
||||
building_placement = { path = "../building_placement" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
|||
|
|
@ -3,9 +3,59 @@
|
|||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use bevy::prelude::*;
|
||||
use bevy_egui::{
|
||||
EguiContexts, EguiPrimaryContextPass,
|
||||
egui::{self, Align2, RichText},
|
||||
};
|
||||
use building::BuildingInfo;
|
||||
use building_placement::SelectBuildingPreview;
|
||||
use resources::{Cost, Resources};
|
||||
use ui_state::GameUIState;
|
||||
|
||||
pub struct UiIngamePlugin;
|
||||
|
||||
impl Plugin for UiIngamePlugin {
|
||||
fn build(&self, app: &mut App) {}
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(
|
||||
EguiPrimaryContextPass,
|
||||
resource_ui.run_if(in_state(GameUIState::Default)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn resource_ui(
|
||||
mut contexts: EguiContexts,
|
||||
resources: Res<Resources>,
|
||||
buildings: Query<(&BuildingInfo, &Cost, &Name)>,
|
||||
mut select_writer: EventWriter<SelectBuildingPreview>,
|
||||
) {
|
||||
let ctx = contexts.ctx_mut().unwrap();
|
||||
let large_font_size = 24.;
|
||||
let font_size = 16.;
|
||||
egui::Window::new("Resources")
|
||||
.resizable(false)
|
||||
.collapsible(false)
|
||||
.title_bar(false)
|
||||
.anchor(Align2::RIGHT_TOP, [0., 0.])
|
||||
.show(ctx, |ui| {
|
||||
ui.label(RichText::new(format!("Souls: {}", resources.souls)).size(large_font_size));
|
||||
ui.heading(RichText::new("Buildings").underline());
|
||||
for (info, cost, name) in buildings.iter() {
|
||||
if !info.unlocked {
|
||||
continue;
|
||||
}
|
||||
if ui
|
||||
.button(
|
||||
RichText::new(format!(
|
||||
"{} - {} souls, {} bones",
|
||||
name, cost.souls, cost.bones
|
||||
))
|
||||
.size(font_size),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
select_writer.write(SelectBuildingPreview(Some(info.preview)));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
bevy = { workspace = true }
|
||||
bevy_egui = { workspace = true }
|
||||
|
||||
state = { path = "../state" }
|
||||
input = { path = "../input" }
|
||||
|
|
|
|||
|
|
@ -3,7 +3,11 @@
|
|||
#![allow(clippy::too_many_arguments)]
|
||||
|
||||
use bevy::prelude::*;
|
||||
use input::{actions::InputAction, conditions::action_just_pressed};
|
||||
use bevy_egui::{
|
||||
EguiContexts, EguiPrimaryContextPass,
|
||||
egui::{self, Align2, RichText},
|
||||
};
|
||||
use input::{Cancellable, actions::InputAction, conditions::action_just_pressed};
|
||||
use state::GameState;
|
||||
|
||||
pub struct UiPausePlugin;
|
||||
|
|
@ -13,11 +17,35 @@ impl Plugin for UiPausePlugin {
|
|||
app.add_systems(
|
||||
Update,
|
||||
toggle_pause.run_if(action_just_pressed::<Update>(InputAction::TogglePause)),
|
||||
)
|
||||
.add_systems(
|
||||
EguiPrimaryContextPass,
|
||||
pause_screen.run_if(in_state(GameState::Paused)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_pause(state: Res<State<GameState>>, mut next_state: ResMut<NextState<GameState>>) {
|
||||
fn pause_screen(mut contexts: EguiContexts) {
|
||||
let ctx = contexts.ctx_mut().unwrap();
|
||||
egui::Window::new("Pause")
|
||||
.resizable(false)
|
||||
.collapsible(false)
|
||||
.title_bar(false)
|
||||
.anchor(Align2::CENTER_CENTER, [0., 0.])
|
||||
.show(ctx, |ui| {
|
||||
ui.label(RichText::new(format!("Paused")).size(64.));
|
||||
});
|
||||
}
|
||||
|
||||
fn toggle_pause(
|
||||
state: Res<State<GameState>>,
|
||||
mut next_state: ResMut<NextState<GameState>>,
|
||||
query: Query<(), With<Cancellable>>,
|
||||
) {
|
||||
if !query.is_empty() {
|
||||
// cancelling takes priority over pausing
|
||||
return;
|
||||
}
|
||||
let next = match state.get() {
|
||||
GameState::Loading => GameState::Loading,
|
||||
GameState::Running => GameState::Paused,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue