diff --git a/Cargo.lock b/Cargo.lock index 73791c6..c6c45a6 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -5335,6 +5335,7 @@ name = "ui_range_indicator" version = "0.1.0" dependencies = [ "bevy", + "building", "combat", "input", ] diff --git a/crates/building/src/lib.rs b/crates/building/src/lib.rs index 7433f54..70e47a0 100755 --- a/crates/building/src/lib.rs +++ b/crates/building/src/lib.rs @@ -8,7 +8,7 @@ use bevy::{ platform::collections::HashMap, prelude::*, }; -use combat::{Health, Range, Team}; +use combat::{Defense, Health, Range, Team}; use input::Selectable; use resources::{Cost, Resources}; use state::GameSystemSet; @@ -61,6 +61,9 @@ 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) { @@ -141,7 +144,17 @@ fn place_building( mut reader: EventReader, mut commands: Commands, mut resources: ResMut, - info_query: Query<(&BuildingInfo, &Cost, &Name), With>, + info_query: Query< + ( + &BuildingInfo, + &Cost, + &Name, + &Health, + &Defense, + Option<&Range>, + ), + With, + >, building_query: Query<&Building>, core_query: Query<(&GlobalTransform, &Range), With>, spatial_query: SpatialQuery, @@ -151,7 +164,8 @@ fn place_building( location, } in reader.read() { - let Ok((info, cost, name)) = info_query.get(*info_entity) else { + let Ok((info, cost, name, health, defense, opt_range)) = info_query.get(*info_entity) + else { error!("tried to place building without info or required components!"); continue; }; @@ -175,27 +189,31 @@ fn place_building( continue; } - let instance = commands - .spawn(( - // adjust model up slightly so base of all towers are aligned - Transform::from_translation(position), - Building { - level: 0, - info: *info_entity, - }, - collider, - Mesh3d::default(), - name.clone(), - RigidBody::Static, - Team::Core.collision_layers(), - Upgradable::new(*info_entity), - BarSettings { - offset: size_v3.y / 2. + 0.5, - width: (size_v3.x.max(size_v3.z) * 8.).ceil() as u32, - ..default() - }, - )) - .id(); + let mut ec = commands.spawn(( + // adjust model up slightly so base of all towers are aligned + Transform::from_translation(position), + Building { + level: 0, + info: *info_entity, + }, + collider, + Mesh3d::default(), + name.clone(), + RigidBody::Static, + Team::Core.collision_layers(), + Upgradable::new(*info_entity), + *health, + *defense, + BarSettings { + offset: size_v3.y / 2. + 0.5, + width: (size_v3.x.max(size_v3.z) * 8.).ceil() as u32, + ..default() + }, + )); + if let Some(range) = opt_range { + ec.insert(*range); + } + let instance = ec.id(); commands.trigger_targets(PopulatePlacedBuilding { info: *info_entity }, instance); } diff --git a/crates/building_barrager/src/lib.rs b/crates/building_barrager/src/lib.rs index 33bd1e2..fc81dcb 100644 --- a/crates/building_barrager/src/lib.rs +++ b/crates/building_barrager/src/lib.rs @@ -147,6 +147,9 @@ fn register( size: SIZE, unlocked: true, }, + Range(10.0), + Health::new(20.0), + Defense::new(7.0), UpgradeTree { upgrades }, Cost(Quantity { souls: 10, @@ -178,10 +181,7 @@ fn populate( Barrager { projectile_count: 5, }, - Health::new(20.0), - Defense::new(7.0), ContactDamage::new(2.0), - Range(10.0), ProjectileLauncher::new( Duration::from_secs(1), 5.0, diff --git a/crates/building_core/src/lib.rs b/crates/building_core/src/lib.rs index 7861693..34528fa 100755 --- a/crates/building_core/src/lib.rs +++ b/crates/building_core/src/lib.rs @@ -59,6 +59,9 @@ fn register( unlocked: false, }, Name::new("Core"), + Health::new(100.0), + Defense::new(5.0), + Range(25.0), )); commands.insert_resource(CoreResources { mesh: mesh, @@ -78,9 +81,6 @@ fn populate( } commands.entity(trigger.target()).insert(( Core, - Health::new(100.0), - Defense::new(5.0), - Range(25.0), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), CustomRangeIndicator::new(materials.add(StandardMaterial { diff --git a/crates/building_impaler/src/lib.rs b/crates/building_impaler/src/lib.rs index c724130..60f24ee 100755 --- a/crates/building_impaler/src/lib.rs +++ b/crates/building_impaler/src/lib.rs @@ -63,6 +63,8 @@ fn register( unlocked: true, }, Cost(Quantity { souls: 5, bones: 0 }), + Health::new(20.0), + Defense::new(5.0), Name::new("Impaler"), )); commands.insert_resource(ImpalerResources { @@ -82,8 +84,6 @@ fn populate( } commands.entity(trigger.target()).insert(( Impaler, - Health::new(20.0), - Defense::new(5.0), ContactDamage::new(5.0), Mesh3d(res.mesh.clone()), MeshMaterial3d(res.material.clone()), diff --git a/crates/building_placement/src/lib.rs b/crates/building_placement/src/lib.rs index 04ed9b0..fc2ab49 100644 --- a/crates/building_placement/src/lib.rs +++ b/crates/building_placement/src/lib.rs @@ -4,7 +4,10 @@ use avian3d::prelude::SpatialQuery; use bevy::prelude::*; -use building::{Building, BuildingInfo, BuildingPreview, Core, PlaceBuilding, valid_placement}; +use building::{ + ActiveBuildingPreview, Building, BuildingInfo, BuildingPreview, Core, PlaceBuilding, + valid_placement, +}; use combat::Range; use input::{Cancellable, Inputs, Selected, actions::InputAction}; use physics::Ground; @@ -19,9 +22,6 @@ impl Plugin for BuildingPlacementPlugin { } } -#[derive(Component, Default)] -pub struct ActiveBuildingPreview(pub IVec3); - #[derive(Event)] pub struct SelectBuildingPreview(pub Option); diff --git a/crates/combat/src/lib.rs b/crates/combat/src/lib.rs index 985e63d..5b585a3 100755 --- a/crates/combat/src/lib.rs +++ b/crates/combat/src/lib.rs @@ -69,8 +69,8 @@ pub struct Died { pub slayer: Option, } -#[derive(Component, Clone, Debug, Reflect)] -#[require(InvulnerabilityTime)] +#[derive(Component, Clone, Copy, Debug, Reflect)] +#[require(InvulnerabilityTime, Defense)] pub struct Health { pub max: f32, pub current: f32, @@ -127,8 +127,7 @@ impl From<&InvulnerabilityTime> for Invulnerable { } } -#[derive(Component, Clone, Debug, Default)] -#[require(Health)] +#[derive(Component, Clone, Copy, Debug, Default)] pub struct Defense { pub amount: f32, } @@ -254,7 +253,7 @@ fn tick_invulnerability( } } -#[derive(Component, Deref, DerefMut)] +#[derive(Component, Deref, DerefMut, Clone, Copy)] pub struct Range(pub f32); impl Range { diff --git a/crates/ui_range_indicator/Cargo.toml b/crates/ui_range_indicator/Cargo.toml index f858eaf..55372f0 100644 --- a/crates/ui_range_indicator/Cargo.toml +++ b/crates/ui_range_indicator/Cargo.toml @@ -8,6 +8,7 @@ bevy = { workspace = true } combat = { path = "../combat" } input = { path = "../input" } +building = { path = "../building" } [lints] workspace = true diff --git a/crates/ui_range_indicator/src/lib.rs b/crates/ui_range_indicator/src/lib.rs index fa5d043..f57e6db 100644 --- a/crates/ui_range_indicator/src/lib.rs +++ b/crates/ui_range_indicator/src/lib.rs @@ -9,6 +9,7 @@ use bevy::{ pbr::{NotShadowCaster, NotShadowReceiver}, prelude::*, }; +use building::{ActiveBuildingPreview, BuildingPreview}; use combat::Range; use input::Selected; @@ -40,6 +41,7 @@ pub struct DisplayCustomRangeIndicator; #[derive(Resource)] struct RangeResources { base_mesh: Handle, + base_material: Handle, } impl CustomRangeIndicator { @@ -114,6 +116,7 @@ fn init( let mesh = meshes.add(Extrusion::new(Annulus::new(1000. - 0.1, 1000.), 1.0)); commands.insert_resource(RangeResources { base_mesh: mesh.clone(), + base_material: material.clone(), }); let selected_indicator = create_indicator(&mut commands, material, mesh); commands @@ -127,8 +130,24 @@ fn display( (Entity, &Range, &GlobalTransform), (With, Without), >, + preview_query: Query< + ( + Entity, + &BuildingPreview, + &GlobalTransform, + Option<&CustomRangeIndicator>, + ), + With, + >, + range_query: Query<&Range>, selected_indicator_query: Single< - (&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d), + ( + &mut Transform, + &mut Visibility, + &mut Indicator, + &mut Mesh3d, + &mut MeshMaterial3d, + ), With, >, custom_entity_query: Query<( @@ -143,7 +162,7 @@ fn display( (&mut Transform, &mut Visibility, &mut Indicator, &mut Mesh3d), Without, >, - + resources: Res, mut meshes: ResMut>, ) { for ( @@ -178,18 +197,37 @@ fn display( tf.translation = gtf.translation().with_y(1.0); } - //update selected entity + //update selected entity (or preview indicator) { - let (mut tf, mut vis, mut indicator, mut mesh) = selected_indicator_query.into_inner(); - let Ok((entity, range, gtf)) = selected_query.single() else { - *vis = Visibility::Hidden; - return; - }; + let (mut tf, mut vis, mut indicator, mut mesh, mut mesh_material) = + selected_indicator_query.into_inner(); + + let (entity, range, gtf, material) = + if let Ok((entity, preview, gtf, opt_custom_indicator)) = preview_query.single() { + let Ok(range) = range_query.get(preview.info) else { + *vis = Visibility::Hidden; + return; + }; + ( + entity, + range, + gtf, + opt_custom_indicator + .map(|CustomRangeIndicator { material, .. }| material.clone()) + .unwrap_or(resources.base_material.clone()), + ) + } else if let Ok((entity, range, gtf)) = selected_query.single() { + (entity, range, gtf, resources.base_material.clone()) + } else { + *vis = Visibility::Hidden; + return; + }; if entity != indicator.0 || indicator.1 != range.0 { indicator.0 = entity; indicator.1 = range.0; // range or entity changed, generate new mesh mesh.0 = meshes.add(Extrusion::new(Annulus::new(range.0 - 0.1, range.0), 1.0)); + mesh_material.0 = material; } *vis = Visibility::Inherited; tf.translation = gtf.translation().with_y(1.0);