add souls and bones + tower cost and enemy drops

This commit is contained in:
Jim 2025-08-30 23:39:18 -04:00
parent bc02db7b26
commit 138a524abd
Signed by: jim
GPG key ID: 3D7D94BA53088BF4
18 changed files with 151 additions and 14 deletions

View file

@ -8,6 +8,7 @@ bevy = { workspace = true }
avian3d = { workspace = true }
combat = { path = "../combat" }
state = { path = "../state" }
resources = { path = "../resources" }
[lints]
workspace = true

View file

@ -9,6 +9,7 @@ use bevy::{
prelude::*,
};
use combat::{Health, Team};
use resources::{Cost, Resources};
use state::GameSystemSet;
pub struct BuildingPlugin;
@ -54,7 +55,7 @@ fn on_building_removed(mut world: DeferredWorld, context: HookContext) {
}
#[derive(Component, Clone)]
#[require(Name)]
#[require(Name, Cost)]
#[component(on_add = register_building_info)]
#[component(on_remove = unregister_building_info)]
#[component(immutable)]
@ -197,14 +198,15 @@ fn place_building(
mut reader: EventReader<PlaceBuilding>,
mut building_map: ResMut<PlacedBuildings>,
mut commands: Commands,
info_query: Query<&BuildingInfo>,
mut resources: ResMut<Resources>,
info_query: Query<(&BuildingInfo, &Cost)>,
) {
for PlaceBuilding {
info: info_entity,
location,
} in reader.read()
{
let Ok(info) = info_query.get(*info_entity) else {
let Ok((info, cost)) = info_query.get(*info_entity) else {
error!("tried to place building without info!");
continue;
};
@ -212,6 +214,10 @@ fn place_building(
error!("invalid building placement at {:?}, rejecting", location);
continue;
}
if !resources.buy(&cost) {
info!("not enough money to buy tower!");
continue;
}
let size_v3 = info.size.as_vec3();
let instance = commands
.spawn((

View file

@ -10,6 +10,7 @@ avian3d = { workspace = true }
state = { path = "../state" }
combat = { path = "../combat" }
building = { path = "../building" }
resources = { path = "../resources" }
combat_damagers = { path = "../combat_damagers" }
[lints]

View file

@ -9,6 +9,7 @@ use building::{
};
use combat::{Defense, Health};
use combat_damagers::ContactDamage;
use resources::Cost;
pub struct BuildingImpalerPlugin;
@ -63,6 +64,7 @@ fn register(
preview,
size: SIZE,
},
Cost { souls: 5, bones: 0 },
Name::new("Impaler"),
));
commands.insert_resource(ImpalerResources {

View file

@ -9,6 +9,7 @@ avian3d = { workspace = true }
state = { path = "../state" }
physics = { path = "../physics"}
resources = { path = "../resources" }
[lints]
workspace = true

View file

@ -6,6 +6,7 @@ use std::time::Duration;
use avian3d::prelude::*;
use bevy::prelude::*;
use resources::{Drops, Resources};
use state::GameSystemSet;
pub struct CombatPlugin;
@ -190,7 +191,17 @@ fn on_attack(
}
}
fn on_die(trigger: Trigger<Died>, names: Query<&Name>, mut commands: Commands) {
fn on_die(
trigger: Trigger<Died>,
names: Query<&Name>,
drops: Query<&Drops>,
mut resources: ResMut<Resources>,
mut commands: Commands,
) {
if let Ok(drop) = drops.get(trigger.target()) {
resources.souls += drop.souls;
info!("you now have {} souls", resources.souls);
}
if let Ok(mut ec) = commands.get_entity(trigger.target()) {
info!(
"{:?} killed {:?}!",

View file

@ -6,6 +6,7 @@ edition = "2024"
[dependencies]
bevy = { workspace = true }
combat = { path = "../combat" }
resources = { path = "../resources" }
[lints]
workspace = true

View file

@ -4,6 +4,7 @@
use bevy::prelude::*;
use combat::{Health, Team};
use resources::Drops;
pub struct InhabitantPlugin;
@ -12,5 +13,5 @@ impl Plugin for InhabitantPlugin {
}
#[derive(Component, Default)]
#[require(Health, Team::Inhabitant)]
#[require(Health, Team::Inhabitant, Drops)]
pub struct Inhabitant;

View file

@ -64,7 +64,6 @@ fn explode(
trigger_query: Query<&GlobalTransform>,
mut other_query: Query<(&GlobalTransform, &mut LinearVelocity)>,
) {
info!("blasthing!");
let Ok(gtf) = trigger_query.get(trigger.target()) else {
return;
};
@ -78,7 +77,7 @@ fn explode(
) {
if let Ok((gtf, mut v)) = other_query.get_mut(e) {
let delta = gtf.translation() - origin;
v.0 += delta.normalize_or_zero() * 10.0;
v.0 += delta.normalize_or_zero() * 2.0;
}
}
}

View file

@ -0,0 +1,12 @@
[package]
name = "resources"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { workspace = true }
state = { path = "../state" }
[lints]
workspace = true

View file

@ -0,0 +1,58 @@
//bevy system signatures often violate these rules
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use bevy::prelude::*;
use state::AppState;
pub struct ResourcesPlugin;
impl Plugin for ResourcesPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(AppState::Game), setup);
}
}
fn setup(mut commands: Commands) {
commands.insert_resource(Resources {
souls: 10,
bones: 10,
});
}
#[derive(Resource, Default)]
pub struct Resources {
pub souls: u32,
pub bones: u32,
}
impl Resources {
pub fn can_buy(&self, cost: &Cost) -> bool {
self.souls >= cost.souls && self.bones >= cost.bones
}
pub fn buy(&mut self, cost: &Cost) -> bool {
if !self.can_buy(cost) {
return false;
}
self.souls = self.souls.saturating_sub(cost.souls);
self.bones = self.bones.saturating_sub(cost.bones);
true
}
}
#[derive(Component, Default)]
pub struct Cost {
pub souls: u32,
pub bones: u32,
}
#[derive(Component)]
pub struct Drops {
pub souls: u32,
}
impl Default for Drops {
fn default() -> Self {
Self { souls: 1 }
}
}

View file

@ -0,0 +1,10 @@
[package]
name = "ui_ingame"
version = "0.1.0"
edition = "2024"
[dependencies]
bevy = { workspace = true }
[lints]
workspace = true

View file

@ -0,0 +1,11 @@
//bevy system signatures often violate these rules
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]
use bevy::prelude::*;
pub struct UiIngamePlugin;
impl Plugin for UiIngamePlugin {
fn build(&self, app: &mut App) {}
}

View file

@ -4,7 +4,7 @@
use bevy::prelude::*;
use input::{actions::InputAction, conditions::action_just_pressed};
use state::{GameState, GameSystemSet};
use state::GameState;
pub struct UiPausePlugin;
@ -12,9 +12,7 @@ impl Plugin for UiPausePlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
toggle_pause
.in_set(GameSystemSet::PreUpdate)
.run_if(action_just_pressed::<Update>(InputAction::TogglePause)),
toggle_pause.run_if(action_just_pressed::<Update>(InputAction::TogglePause)),
);
}
}