Skip to content

Commit

Permalink
Hide gameplay features behind a flag
Browse files Browse the repository at this point in the history
Gameplay features, such as an inventory and the need to collect
resources, can unnecessarily restrict creative freedom, so while
gameplay features are being developed, it would be good to keep them
behind a flag to avoid interrupting people who just want to build.
  • Loading branch information
patowen committed Jun 24, 2024
1 parent 1da6c71 commit 2e110c9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
8 changes: 7 additions & 1 deletion client/src/graphics/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ impl GuiState {
align(Alignment::TOP_LEFT, || {
pad(Pad::all(8.0), || {
colored_box_container(Color::BLACK.with_alpha(0.7), || {
let material_count_string = if sim.cfg.gameplay_enabled {
sim.count_inventory_entities_matching_material(sim.selected_material())
.to_string()
} else {
"∞".to_string()
};
label(format!(
"Selected material: {:?} (×{})",
sim.selected_material(),
sim.count_inventory_entities_matching_material(sim.selected_material())
material_count_string
));
});
});
Expand Down
2 changes: 1 addition & 1 deletion client/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl Sim {
Material::Void
};

let consumed_entity = if placing {
let consumed_entity = if placing && self.cfg.gameplay_enabled {
Some(self.get_any_inventory_entity_matching_material(material)?)
} else {
None
Expand Down
4 changes: 4 additions & 0 deletions common/src/sim_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct SimConfigRaw {
/// Maximum distance at which anything can be seen in meters
pub view_distance: Option<f32>,
pub input_queue_size_ms: Option<u16>,
/// Whether gameplay-like restrictions exist, such as limited inventory
pub gameplay_enabled: Option<bool>,
/// Number of voxels along the edge of a chunk
pub chunk_size: Option<u8>,
/// Approximate length of the edge of a voxel in meters
Expand All @@ -36,6 +38,7 @@ pub struct SimConfig {
pub step_interval: Duration,
pub view_distance: f32,
pub input_queue_size: Duration,
pub gameplay_enabled: bool,
pub chunk_size: u8,
pub character: CharacterConfig,
/// Scaling factor converting meters to absolute units
Expand All @@ -51,6 +54,7 @@ impl SimConfig {
step_interval: Duration::from_secs(1) / x.rate.unwrap_or(30) as u32,
view_distance: x.view_distance.unwrap_or(90.0) * meters_to_absolute,
input_queue_size: Duration::from_millis(x.input_queue_size_ms.unwrap_or(50).into()),
gameplay_enabled: x.gameplay_enabled.unwrap_or(false),
chunk_size,
character: CharacterConfig::from_raw(&x.character, meters_to_absolute),
meters_to_absolute,
Expand Down
50 changes: 26 additions & 24 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,32 +451,34 @@ impl Sim {
tracing::warn!("Block update received from ungenerated chunk");
return;
};
if block_update.new_material != Material::Void {
let Some(consumed_entity_id) = block_update.consumed_entity else {
tracing::warn!("Tried to place block without consuming any entities");
return;
};
let Some(&consumed_entity) = self.entity_ids.get(&consumed_entity_id) else {
tracing::warn!("Tried to consume an unknown entity ID");
return;
};
if !self
.world
.get::<&Material>(consumed_entity)
.is_ok_and(|m| *m == block_update.new_material)
{
tracing::warn!("Tried to consume wrong material");
return;
if self.cfg.gameplay_enabled {
if block_update.new_material != Material::Void {
let Some(consumed_entity_id) = block_update.consumed_entity else {
tracing::warn!("Tried to place block without consuming any entities");
return;
};
let Some(&consumed_entity) = self.entity_ids.get(&consumed_entity_id) else {
tracing::warn!("Tried to consume an unknown entity ID");
return;
};
if !self
.world
.get::<&Material>(consumed_entity)
.is_ok_and(|m| *m == block_update.new_material)
{
tracing::warn!("Tried to consume wrong material");
return;
}
if !self.remove_from_inventory(subject, consumed_entity_id) {
tracing::warn!("Tried to consume entity not in player inventory");
return;
}
self.destroy(consumed_entity);
}
if !self.remove_from_inventory(subject, consumed_entity_id) {
tracing::warn!("Tried to consume entity not in player inventory");
return;
if old_material != Material::Void {
let (produced_entity, _) = self.spawn((old_material,));
self.add_to_inventory(subject, produced_entity);
}
self.destroy(consumed_entity);
}
if old_material != Material::Void {
let (produced_entity, _) = self.spawn((old_material,));
self.add_to_inventory(subject, produced_entity);
}
assert!(self.graph.update_block(&block_update));
self.modified_chunks.insert(block_update.chunk_id);
Expand Down

0 comments on commit 2e110c9

Please sign in to comment.