Implemented setting blocks with the wiring system

This commit is contained in:
Knightress Paladin 2021-07-14 22:17:05 -07:00
parent eb08b6a153
commit 9864344cfe
4 changed files with 54 additions and 10 deletions

View File

@ -1944,6 +1944,7 @@ fn handle_spawn_wiring(
inputs: HashMap::new(), inputs: HashMap::new(),
outputs: outputs1, outputs: outputs1,
}) })
.with(comp::Density(100_f32))
.with(comp::Sticky); .with(comp::Sticky);
let ent1 = builder1.build(); let ent1 = builder1.build();
@ -1965,6 +1966,16 @@ fn handle_spawn_wiring(
}, },
}], }],
}, },
WiringAction {
formula: wiring::OutputFormula::Input {
name: String::from("deaths_accumulated"),
},
threshold: 1.0,
effects: vec![WiringActionEffect::SetBlock {
coords: vek::Vec3::new(0, 0, pos.0.z as i32),
block: Block::new(BlockKind::Water, vek::Rgb::new(0, 0, 0)),
}],
},
WiringAction { WiringAction {
formula: wiring::OutputFormula::Constant { value: 1.0 }, formula: wiring::OutputFormula::Constant { value: 1.0 },
threshold: 1.0, threshold: 1.0,
@ -1983,7 +1994,8 @@ fn handle_spawn_wiring(
], ],
inputs: HashMap::new(), inputs: HashMap::new(),
outputs: HashMap::new(), outputs: HashMap::new(),
}); })
.with(comp::Density(100_f32));
let ent2 = builder2.build(); let ent2 = builder2.build();
pos.0.x += 3.0; pos.0.x += 3.0;
@ -1994,6 +2006,7 @@ fn handle_spawn_wiring(
inputs: HashMap::new(), inputs: HashMap::new(),
outputs: HashMap::new(), outputs: HashMap::new(),
}) })
.with(comp::Density(comp::object::Body::TrainingDummy.density().0))
.with(Circuit { .with(Circuit {
wires: vec![ wires: vec![
Wire { Wire {

View File

@ -5,9 +5,10 @@ use common::{
resources::EntitiesDiedLastTick, resources::EntitiesDiedLastTick,
}; };
use common_ecs::{Job, Origin, Phase, System}; use common_ecs::{Job, Origin, Phase, System};
use common_state::BlockChange;
use hashbrown::HashMap; use hashbrown::HashMap;
use specs::{ use specs::{
join::Join, shred::ResourceId, Entities, Entity, Read, ReadStorage, SystemData, World, join::Join, shred::ResourceId, Entities, Entity, Read, ReadStorage, SystemData, World, Write,
WriteStorage, WriteStorage,
}; };
mod compute_outputs; mod compute_outputs;
@ -28,6 +29,7 @@ pub struct WiringData<'a> {
pub event_bus: Read<'a, EventBus<ServerEvent>>, pub event_bus: Read<'a, EventBus<ServerEvent>>,
pub entities_died_last_tick: Read<'a, EntitiesDiedLastTick>, pub entities_died_last_tick: Read<'a, EntitiesDiedLastTick>,
pub block_change: Write<'a, BlockChange>,
} }
/// This system is responsible for handling wiring (signals and wiring systems) /// This system is responsible for handling wiring (signals and wiring systems)

View File

@ -6,11 +6,13 @@ use common::{
comp::{object, Body, LightEmitter, PhysicsState, Pos, ProjectileConstructor}, comp::{object, Body, LightEmitter, PhysicsState, Pos, ProjectileConstructor},
event::{Emitter, ServerEvent}, event::{Emitter, ServerEvent},
resources::EntitiesDiedLastTick, resources::EntitiesDiedLastTick,
terrain::{block::Block, TerrainChunkSize},
util::Dir, util::Dir,
vol::RectVolSize,
}; };
use common_state::BlockChange;
use hashbrown::HashMap; use hashbrown::HashMap;
use specs::{join::Join, Entity, Read}; use specs::{join::Join, Entity, Read, Write};
use tracing::warn;
use vek::Rgb; use vek::Rgb;
pub fn dispatch_actions(system_data: &mut WiringData) { pub fn dispatch_actions(system_data: &mut WiringData) {
@ -21,6 +23,7 @@ pub fn dispatch_actions(system_data: &mut WiringData) {
physics_states, physics_states,
light_emitters, light_emitters,
entities_died_last_tick, entities_died_last_tick,
block_change,
pos, pos,
.. ..
} = system_data; } = system_data;
@ -57,6 +60,7 @@ pub fn dispatch_actions(system_data: &mut WiringData) {
&mut light_emitter, &mut light_emitter,
physics_state, physics_state,
entities_died_last_tick, entities_died_last_tick,
block_change,
pos, pos,
); );
}) })
@ -75,13 +79,14 @@ fn dispatch_action(
light_emitter: &mut Option<impl DerefMut<Target = LightEmitter>>, light_emitter: &mut Option<impl DerefMut<Target = LightEmitter>>,
physics_state: Option<&PhysicsState>, physics_state: Option<&PhysicsState>,
entities_died_last_tick: &Read<EntitiesDiedLastTick>, entities_died_last_tick: &Read<EntitiesDiedLastTick>,
block_change: &mut Write<BlockChange>,
pos: Option<&Pos>, pos: Option<&Pos>,
) { ) {
action_effects action_effects
.iter() .iter()
.for_each(|action_effect| match action_effect { .for_each(|action_effect| match action_effect {
WiringActionEffect::SetBlockCollidability { .. } => { WiringActionEffect::SetBlock { coords, block } => {
warn!("Not implemented WiringActionEffect::SetBlockCollidability") dispatch_action_set_block(*coords, *block, block_change, pos);
}, },
WiringActionEffect::SpawnProjectile { constr } => { WiringActionEffect::SpawnProjectile { constr } => {
dispatch_action_spawn_projectile(entity, constr, server_emitter) dispatch_action_spawn_projectile(entity, constr, server_emitter)
@ -140,3 +145,28 @@ fn dispatch_action_set_light(
light_emitter.col = Rgb::new(computed_r, computed_g, computed_b); light_emitter.col = Rgb::new(computed_r, computed_g, computed_b);
} }
} }
#[allow(clippy::too_many_arguments)]
fn dispatch_action_set_block(
coord: vek::Vec3<i32>,
block: Block,
block_change: &mut Write<BlockChange>,
pos: Option<&Pos>,
) {
let chunk_origin = match pos {
Some(opos) => vek::Vec3::new(
(opos.0.x as i32 / TerrainChunkSize::RECT_SIZE.x as i32)
* TerrainChunkSize::RECT_SIZE.x as i32,
(opos.0.y as i32 / TerrainChunkSize::RECT_SIZE.y as i32)
* TerrainChunkSize::RECT_SIZE.y as i32,
0,
),
None => vek::Vec3::new(0, 0, 0),
};
let offset_pos = chunk_origin
.iter()
.zip(coord.iter())
.map(|(p, c)| p + c)
.collect();
block_change.set(offset_pos, block);
}

View File

@ -1,5 +1,4 @@
use common::comp::ProjectileConstructor; use common::{comp::ProjectileConstructor, terrain::Block};
use core::f32;
use hashbrown::HashMap; use hashbrown::HashMap;
use specs::{Component, Entity}; use specs::{Component, Entity};
use specs_idvs::IdvStorage; use specs_idvs::IdvStorage;
@ -50,9 +49,9 @@ pub enum WiringActionEffect {
SpawnProjectile { SpawnProjectile {
constr: ProjectileConstructor, constr: ProjectileConstructor,
}, },
SetBlockCollidability { SetBlock {
coords: Vec3<i32>, coords: Vec3<i32>,
collidable: bool, block: Block,
}, },
SetLight { SetLight {
r: OutputFormula, r: OutputFormula,