Skip to content

Commit

Permalink
Merge pull request #397 from AnthonyTornetta/385-line-systems-dont-pr…
Browse files Browse the repository at this point in the history
…operly-update-when-mined

Fixed line systems not updating on block breaks properly
  • Loading branch information
AnthonyTornetta authored Jan 15, 2025
2 parents 390e422 + 1cf981e commit a04c4ee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cosmos_client/src/rendering/lod_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ pub(super) fn register(app: &mut App) {
)
.chain()
.in_set(MaterialsSystemSet::RequestMaterialChanges)
.run_if(in_state(GameState::Playing).or(in_state(GameState::LoadingWorld))),
.run_if(in_state(GameState::Playing)),
);

add_statebound_resource::<RenderingLods, GameState>(app, GameState::Playing);
Expand Down
17 changes: 17 additions & 0 deletions cosmos_core/src/structure/systems/mining_laser_system.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Represents all the mining lasers on a structure
use bevy::prelude::*;
use bevy::reflect::Reflect;
use serde::{Deserialize, Serialize};

use super::StructureSystemsSet;
use super::{
line_system::{LineProperty, LinePropertyCalculator, LineSystem},
sync::SyncableSystem,
Expand Down Expand Up @@ -48,3 +50,18 @@ impl LinePropertyCalculator<MiningLaserProperty> for MiningLaserPropertyCalculat
"cosmos:mining_laser_system"
}
}

fn name_mining_laser_system(mut commands: Commands, q_added: Query<Entity, Added<MiningLaserSystem>>) {
for e in q_added.iter() {
commands.entity(e).insert(Name::new("Plasma Drill System"));
}
}

pub(super) fn register(app: &mut App) {
app.register_type::<MiningLaserSystem>().add_systems(
Update,
name_mining_laser_system
.ambiguous_with_all() // doesn't matter if this is 1-frame delayed
.after(StructureSystemsSet::InitSystems),
);
}
1 change: 1 addition & 0 deletions cosmos_core/src/structure/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,5 +533,6 @@ pub(super) fn register(app: &mut App) {
thruster_system::register(app);
missile_launcher_system::register(app);
laser_cannon_system::register(app);
mining_laser_system::register(app);
dock_system::register(app);
}
58 changes: 34 additions & 24 deletions cosmos_server/src/structure/systems/line_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy::{
schedule::IntoSystemConfigs,
system::{Commands, Query, Res, ResMut},
},
log::info,
state::{condition::in_state, state::OnEnter},
};
use cosmos_core::{
Expand Down Expand Up @@ -35,33 +36,39 @@ fn block_update_system<T: LineProperty, S: LinePropertyCalculator<T>>(
systems_query: Query<&StructureSystems>,
) {
for ev in event.read() {
if let Ok(systems) = systems_query.get(ev.block.structure()) {
if let Ok(mut system) = systems.query_mut(&mut system_query) {
let old_block = blocks.from_numeric_id(ev.old_block);
let new_block = blocks.from_numeric_id(ev.new_block);
let Ok(systems) = systems_query.get(ev.block.structure()) else {
continue;
};

if laser_cannon_blocks.get(old_block).is_some() {
system.remove_block(ev.block.coords());
}
let Ok(mut system) = systems.query_mut(&mut system_query) else {
continue;
};

if let Some(property) = laser_cannon_blocks.get(new_block) {
system.add_block(ev.block.coords(), ev.new_block_rotation(), property);
}
let old_block = blocks.from_numeric_id(ev.old_block);
let new_block = blocks.from_numeric_id(ev.new_block);

let mut recalc = false;
if color_blocks.from_block(old_block).is_some() {
system.colors.retain(|x| x.0 != ev.block.coords());
recalc = true;
}
if laser_cannon_blocks.get(old_block).is_some() {
info!("Removing block {old_block:?}");
system.remove_block(ev.block.coords());
}

if let Some(color_property) = color_blocks.from_block(new_block) {
system.colors.push((ev.block.coords(), color_property.properties));
recalc = true;
}
if recalc {
recalculate_colors(&mut system, Some(ev.block.coords()));
}
}
if let Some(property) = laser_cannon_blocks.get(new_block) {
info!("Adding block {old_block:?}");
system.add_block(ev.block.coords(), ev.new_block_rotation(), property);
}

let mut recalc = false;
if color_blocks.from_block(old_block).is_some() {
system.colors.retain(|x| x.0 != ev.block.coords());
recalc = true;
}

if let Some(color_property) = color_blocks.from_block(new_block) {
system.colors.push((ev.block.coords(), color_property.properties));
recalc = true;
}
if recalc {
recalculate_colors(&mut system, Some(ev.block.coords()));
}
}
}
Expand Down Expand Up @@ -263,6 +270,8 @@ impl<T: LineProperty, S: LinePropertyCalculator<T>> BlockStructureSystem<T> for

if line.start == sb {
let (dx, dy, dz) = line.direction.to_i32_tuple();
line.properties.remove(0);
line.property = S::calculate_property(&line.properties);
line.start.x = (line.start.x as i32 + dx) as CoordinateType;
line.start.y = (line.start.y as i32 + dy) as CoordinateType;
line.start.z = (line.start.z as i32 + dz) as CoordinateType;
Expand All @@ -273,8 +282,9 @@ impl<T: LineProperty, S: LinePropertyCalculator<T>> BlockStructureSystem<T> for
}
return;
} else if line.end() == sb {
line.properties.pop();
line.property = S::calculate_property(&line.properties);
line.len -= 1;

if line.len == 0 {
self.lines.swap_remove(i);
}
Expand Down

0 comments on commit a04c4ee

Please sign in to comment.