disable resource collection outside of core range, disable purchase buttons if too much money
This commit is contained in:
parent
6a4978041c
commit
e6b04bbdd1
8 changed files with 97 additions and 56 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -5311,6 +5311,7 @@ dependencies = [
|
||||||
"bevy_egui",
|
"bevy_egui",
|
||||||
"building",
|
"building",
|
||||||
"building_placement",
|
"building_placement",
|
||||||
|
"combat",
|
||||||
"input",
|
"input",
|
||||||
"resources",
|
"resources",
|
||||||
"state",
|
"state",
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
(
|
(
|
||||||
map: {
|
map: {
|
||||||
KeyCode(ShiftLeft): [
|
|
||||||
MultiPlaceTower,
|
|
||||||
],
|
|
||||||
MouseButton(Middle): [
|
MouseButton(Middle): [
|
||||||
CameraOrbitEnable,
|
CameraOrbitEnable,
|
||||||
],
|
],
|
||||||
|
KeyCode(ShiftLeft): [
|
||||||
|
MultiPlaceTower,
|
||||||
|
],
|
||||||
KeyCode(F1): [
|
KeyCode(F1): [
|
||||||
ToggleDebugUI,
|
ToggleDebugUI,
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -121,7 +121,7 @@ pub fn valid_placement(
|
||||||
core_position_range: Option<(Vec3, f32)>,
|
core_position_range: Option<(Vec3, f32)>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
if let Some((core_position, core_range)) = core_position_range
|
if let Some((core_position, core_range)) = core_position_range
|
||||||
&& position.distance_squared(core_position) > core_range * core_range
|
&& !Range(core_range).within(position, core_position)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use building::{BuildingInfo, BuildingPreview, Core, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
use building::{BuildingInfo, BuildingPreview, Core, PREVIEW_ALPHA, PopulatePlacedBuilding};
|
||||||
use combat::{Defense, Health, Range};
|
use combat::{Defense, Health, Range};
|
||||||
use ui_range_indicator::{CustomRangeIndicator, DisplayCustomRangeIndicator};
|
use ui_range_indicator::CustomRangeIndicator;
|
||||||
|
|
||||||
pub struct BuildingCorePlugin;
|
pub struct BuildingCorePlugin;
|
||||||
|
|
||||||
|
|
@ -80,7 +80,7 @@ fn populate(
|
||||||
Core,
|
Core,
|
||||||
Health::new(100.0),
|
Health::new(100.0),
|
||||||
Defense::new(5.0),
|
Defense::new(5.0),
|
||||||
Range(20.0),
|
Range(25.0),
|
||||||
Mesh3d(res.mesh.clone()),
|
Mesh3d(res.mesh.clone()),
|
||||||
MeshMaterial3d(res.material.clone()),
|
MeshMaterial3d(res.material.clone()),
|
||||||
CustomRangeIndicator::new(materials.add(StandardMaterial {
|
CustomRangeIndicator::new(materials.add(StandardMaterial {
|
||||||
|
|
|
||||||
|
|
@ -257,6 +257,12 @@ fn tick_invulnerability(
|
||||||
#[derive(Component, Deref, DerefMut)]
|
#[derive(Component, Deref, DerefMut)]
|
||||||
pub struct Range(pub f32);
|
pub struct Range(pub f32);
|
||||||
|
|
||||||
|
impl Range {
|
||||||
|
pub fn within(&self, a: Vec3, b: Vec3) -> bool {
|
||||||
|
a.distance_squared(b) < self.0 * self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for Range {
|
impl Default for Range {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self(5.)
|
Self(5.)
|
||||||
|
|
|
||||||
|
|
@ -19,34 +19,23 @@ impl Plugin for ResourcesPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(mut commands: Commands) {
|
fn setup(mut commands: Commands) {
|
||||||
commands.insert_resource(Resources {
|
commands.insert_resource(Resources(Quantity {
|
||||||
souls: 10,
|
souls: 10,
|
||||||
bones: 10,
|
bones: 10,
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default, Deref, DerefMut)]
|
||||||
pub struct Resources {
|
pub struct Resources(pub Quantity);
|
||||||
pub souls: u32,
|
|
||||||
pub bones: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Resources {
|
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 {
|
pub fn buy(&mut self, cost: &Cost) -> bool {
|
||||||
if !self.can_buy(cost) {
|
if !self.can_buy(cost.0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self.souls = self.souls.saturating_sub(cost.souls);
|
self.0 = self.0 - cost.0;
|
||||||
self.bones = self.bones.saturating_sub(cost.bones);
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
pub fn add(&mut self, quantity: Quantity) {
|
|
||||||
self.souls = self.souls.saturating_add(quantity.souls);
|
|
||||||
self.bones = self.bones.saturating_add(quantity.bones);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Default, Deref, DerefMut)]
|
#[derive(Component, Default, Deref, DerefMut)]
|
||||||
|
|
@ -59,7 +48,7 @@ pub struct Quantity {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Quantity {
|
impl Quantity {
|
||||||
pub fn to_string(&self) -> String {
|
pub fn to_string(self) -> String {
|
||||||
let souls_text = if self.souls > 0 {
|
let souls_text = if self.souls > 0 {
|
||||||
format!("{} souls", self.souls)
|
format!("{} souls", self.souls)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -79,6 +68,10 @@ impl Quantity {
|
||||||
};
|
};
|
||||||
format!("{}{}{}", souls_text, divider_text, bones_text)
|
format!("{}{}{}", souls_text, divider_text, bones_text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn can_buy(self, product: Quantity) -> bool {
|
||||||
|
self.bones >= product.bones && self.souls >= product.souls
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Mul<u32> for Quantity {
|
impl std::ops::Mul<u32> for Quantity {
|
||||||
|
|
@ -92,6 +85,28 @@ impl std::ops::Mul<u32> for Quantity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::ops::Add<Quantity> for Quantity {
|
||||||
|
type Output = Quantity;
|
||||||
|
|
||||||
|
fn add(self, rhs: Quantity) -> Self::Output {
|
||||||
|
Quantity {
|
||||||
|
souls: self.souls.saturating_add(rhs.souls),
|
||||||
|
bones: self.bones.saturating_add(rhs.bones),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::Sub<Quantity> for Quantity {
|
||||||
|
type Output = Quantity;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Quantity) -> Self::Output {
|
||||||
|
Quantity {
|
||||||
|
souls: self.souls.saturating_sub(rhs.souls),
|
||||||
|
bones: self.bones.saturating_sub(rhs.bones),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Drops {
|
pub struct Drops {
|
||||||
pub souls: u32,
|
pub souls: u32,
|
||||||
|
|
@ -146,7 +161,7 @@ fn on_harvest(
|
||||||
error!("harvested something that was not a resource deposit or not worked OwO");
|
error!("harvested something that was not a resource deposit or not worked OwO");
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
resources.add(deposit.bounty * worked.workers);
|
resources.0 = resources.0 + deposit.bounty * worked.workers;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ building = { path = "../building" }
|
||||||
input = { path = "../input" }
|
input = { path = "../input" }
|
||||||
building_placement = { path = "../building_placement" }
|
building_placement = { path = "../building_placement" }
|
||||||
upgrade = { path = "../upgrade" }
|
upgrade = { path = "../upgrade" }
|
||||||
|
combat = { path = "../combat" }
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,9 @@ use bevy_egui::{
|
||||||
EguiContexts, EguiPrimaryContextPass,
|
EguiContexts, EguiPrimaryContextPass,
|
||||||
egui::{self, Align2, RichText},
|
egui::{self, Align2, RichText},
|
||||||
};
|
};
|
||||||
use building::BuildingInfo;
|
use building::{BuildingInfo, Core};
|
||||||
use building_placement::SelectBuildingPreview;
|
use building_placement::SelectBuildingPreview;
|
||||||
|
use combat::Range;
|
||||||
use input::{SelectEntity, Selected};
|
use input::{SelectEntity, Selected};
|
||||||
use resources::{AvailableWorkers, Cost, ResourceDeposit, Resources, WorkedResourceDeposit};
|
use resources::{AvailableWorkers, Cost, ResourceDeposit, Resources, WorkedResourceDeposit};
|
||||||
use state::{BasePhaseState, Phase};
|
use state::{BasePhaseState, Phase};
|
||||||
|
|
@ -184,7 +185,12 @@ fn side_panel(
|
||||||
selected: Query<(Entity, &Name), With<Selected>>,
|
selected: Query<(Entity, &Name), With<Selected>>,
|
||||||
upgradable: Query<&Upgradable>,
|
upgradable: Query<&Upgradable>,
|
||||||
upgrades: Query<(&Upgrade, &Name)>,
|
upgrades: Query<(&Upgrade, &Name)>,
|
||||||
mut selected_resource_deposit: Query<(&ResourceDeposit, Option<&mut WorkedResourceDeposit>)>,
|
core_query: Query<(&GlobalTransform, &Range), With<Core>>,
|
||||||
|
mut selected_resource_deposit: Query<(
|
||||||
|
&ResourceDeposit,
|
||||||
|
&GlobalTransform,
|
||||||
|
Option<&mut WorkedResourceDeposit>,
|
||||||
|
)>,
|
||||||
mut selected_preview_writer: EventWriter<SelectBuildingPreview>,
|
mut selected_preview_writer: EventWriter<SelectBuildingPreview>,
|
||||||
mut select_writer: EventWriter<SelectEntity>,
|
mut select_writer: EventWriter<SelectEntity>,
|
||||||
) {
|
) {
|
||||||
|
|
@ -203,7 +209,9 @@ fn side_panel(
|
||||||
|
|
||||||
ui.heading(selected_name.as_str());
|
ui.heading(selected_name.as_str());
|
||||||
// resource deposit management
|
// resource deposit management
|
||||||
if let Ok((deposit, mut opt_worked)) = selected_resource_deposit.get_mut(selected) {
|
if let Ok((deposit, deposit_gtf, mut opt_worked)) =
|
||||||
|
selected_resource_deposit.get_mut(selected)
|
||||||
|
{
|
||||||
ui.label(
|
ui.label(
|
||||||
RichText::new(format!("Level {} deposit", deposit.level)).size(FONT_SIZE),
|
RichText::new(format!("Level {} deposit", deposit.level)).size(FONT_SIZE),
|
||||||
);
|
);
|
||||||
|
|
@ -218,7 +226,10 @@ fn side_panel(
|
||||||
);
|
);
|
||||||
// worker management buttons
|
// worker management buttons
|
||||||
// add
|
// add
|
||||||
if available_workers.0 == 0 {
|
if let Ok((core_gtf, range)) = core_query.single() {
|
||||||
|
if !range.within(core_gtf.translation(), deposit_gtf.translation()) {
|
||||||
|
ui.add_enabled(false, egui::Button::new("Out of Core's range"));
|
||||||
|
} else if available_workers.0 == 0 {
|
||||||
ui.add_enabled(false, egui::Button::new("No workers available"));
|
ui.add_enabled(false, egui::Button::new("No workers available"));
|
||||||
} else {
|
} else {
|
||||||
if ui.button("Add worker").clicked() {
|
if ui.button("Add worker").clicked() {
|
||||||
|
|
@ -233,6 +244,10 @@ fn side_panel(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ui.add_enabled(false, egui::Button::new("Core required"));
|
||||||
|
}
|
||||||
|
|
||||||
// remove
|
// remove
|
||||||
if let Some(ref mut worked) = opt_worked {
|
if let Some(ref mut worked) = opt_worked {
|
||||||
if ui.button("Remove worker").clicked() {
|
if ui.button("Remove worker").clicked() {
|
||||||
|
|
@ -266,15 +281,18 @@ fn side_panel(
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|u| upgrades.get(*u).map(|(up, n)| (*u, up, n)).ok())
|
.filter_map(|u| upgrades.get(*u).map(|(up, n)| (*u, up, n)).ok())
|
||||||
{
|
{
|
||||||
if ui
|
let button_text =
|
||||||
.button(RichText::new(upgrade_name.as_str()).size(LARGE_FONT_SIZE))
|
RichText::new(upgrade_name.as_str()).size(LARGE_FONT_SIZE);
|
||||||
.clicked()
|
if resources.0.can_buy(upgrade.cost) {
|
||||||
{
|
if ui.button(button_text).clicked() {
|
||||||
commands.trigger_targets(
|
commands.trigger_targets(
|
||||||
UnlockUpgrade { building: selected },
|
UnlockUpgrade { building: selected },
|
||||||
upgrade_entity,
|
upgrade_entity,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ui.add_enabled(false, egui::Button::new(button_text));
|
||||||
|
}
|
||||||
ui.label(RichText::new(upgrade.description.as_str()).size(FONT_SIZE));
|
ui.label(RichText::new(upgrade.description.as_str()).size(FONT_SIZE));
|
||||||
ui.label(RichText::new(upgrade.cost.to_string()).size(FONT_SIZE));
|
ui.label(RichText::new(upgrade.cost.to_string()).size(FONT_SIZE));
|
||||||
}
|
}
|
||||||
|
|
@ -304,15 +322,15 @@ fn side_panel(
|
||||||
if !info.unlocked {
|
if !info.unlocked {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
let button_text =
|
||||||
if ui
|
RichText::new(format!("{} - {}", name, cost.0.to_string())).size(FONT_SIZE);
|
||||||
.button(
|
if resources.can_buy(cost.0) {
|
||||||
RichText::new(format!("{} - {}", name, cost.0.to_string())).size(FONT_SIZE),
|
if ui.button(button_text).clicked() {
|
||||||
)
|
|
||||||
.clicked()
|
|
||||||
{
|
|
||||||
selected_preview_writer.write(SelectBuildingPreview(Some(info.preview)));
|
selected_preview_writer.write(SelectBuildingPreview(Some(info.preview)));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ui.add_enabled(false, egui::Button::new(button_text));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue