(Wiring) Parametrize OutputFormulas with OutputFormulas

This commit is contained in:
Daniel Mizerski 2021-05-07 16:32:52 +02:00
parent a2999ce96f
commit 4adb963c8e
3 changed files with 66 additions and 55 deletions

View File

@ -4,7 +4,7 @@
use crate::{ use crate::{
settings::{BanRecord, EditableSetting}, settings::{BanRecord, EditableSetting},
wiring::{Logic, OutputFormula}, wiring::{BoxConst, Const, Input, Logic, OutputFormula},
Server, SpawnPoint, StateExt, Server, SpawnPoint, StateExt,
}; };
use assets::AssetExt; use assets::AssetExt;
@ -1723,57 +1723,40 @@ fn handle_spawn_wiring(
pos.0.x += 3.0; pos.0.x += 3.0;
let mut outputs1 = HashMap::new(); let mut outputs1 = HashMap::new();
outputs1.insert( outputs1.insert("deaths_last_tick".to_string(), OutputFormula::OnDeath {
"deaths_last_tick".to_string(), value: BoxConst(1.0),
wiring::OutputFormula::OnDeath { radius: BoxConst(30.0),
value: 1.0, });
radius: 30.0,
},
);
outputs1.insert( outputs1.insert(
"deaths_accumulated".to_string(), "deaths_accumulated".to_string(),
OutputFormula::Logic(Box::new(Logic { OutputFormula::Logic(Box::new(Logic {
kind: wiring::LogicKind::Sum, kind: wiring::LogicKind::Sum,
left: OutputFormula::Logic(Box::new(Logic { left: OutputFormula::Logic(Box::new(Logic {
kind: wiring::LogicKind::Sub, kind: wiring::LogicKind::Sub,
left: OutputFormula::Input { left: Input("deaths_accumulated"),
name: "deaths_accumulated".to_string(),
},
right: OutputFormula::Logic(Box::new(Logic { right: OutputFormula::Logic(Box::new(Logic {
kind: wiring::LogicKind::Min, kind: wiring::LogicKind::Min,
left: OutputFormula::Input { left: Input("pressed"),
name: "pressed".to_string(), right: Input("deaths_accumulated"),
},
right: OutputFormula::Input {
name: "deaths_accumulated".to_string(),
},
})), })),
})), })),
right: OutputFormula::Input { right: Input("deaths_last_tick"),
name: "deaths_last_tick".to_string(),
},
})), })),
); );
outputs1.insert("pressed".to_string(), OutputFormula::OnCollide { outputs1.insert("pressed".to_string(), OutputFormula::OnCollide {
value: f32::MAX, value: Box::new(OutputFormula::Constant { value: f32::MAX }),
}); });
let builder1 = server let builder1 = server
.state .state
.create_wiring(pos, comp::object::Body::Coins, WiringElement { .create_wiring(pos, comp::object::Body::Coins, WiringElement {
actions: vec![WiringAction { actions: vec![WiringAction {
formula: wiring::OutputFormula::Constant { value: 1.0 }, formula: Const(1.0),
threshold: 1.0, threshold: Const(1.0),
effects: vec![WiringActionEffect::SetLight { effects: vec![WiringActionEffect::SetLight {
r: wiring::OutputFormula::Input { r: Input("color"),
name: String::from("color"), g: Input("color"),
}, b: Input("color"),
g: wiring::OutputFormula::Input {
name: String::from("color"),
},
b: wiring::OutputFormula::Input {
name: String::from("color"),
},
}], }],
}], }],
inputs: HashMap::new(), inputs: HashMap::new(),
@ -1788,10 +1771,8 @@ fn handle_spawn_wiring(
.create_wiring(pos, comp::object::Body::Coins, WiringElement { .create_wiring(pos, comp::object::Body::Coins, WiringElement {
actions: vec![ actions: vec![
WiringAction { WiringAction {
formula: wiring::OutputFormula::Input { formula: Input("deaths_accumulated"),
name: String::from("deaths_accumulated"), threshold: Const(5.0),
},
threshold: 5.0,
effects: vec![WiringActionEffect::SpawnProjectile { effects: vec![WiringActionEffect::SpawnProjectile {
constr: comp::ProjectileConstructor::Arrow { constr: comp::ProjectileConstructor::Arrow {
damage: 1.0, damage: 1.0,
@ -1801,18 +1782,12 @@ fn handle_spawn_wiring(
}], }],
}, },
WiringAction { WiringAction {
formula: wiring::OutputFormula::Constant { value: 1.0 }, formula: Const(1.0),
threshold: 1.0, threshold: Const(1.0),
effects: vec![WiringActionEffect::SetLight { effects: vec![WiringActionEffect::SetLight {
r: wiring::OutputFormula::Input { r: Input("color"),
name: String::from("color"), g: Input("color"),
}, b: Input("color"),
g: wiring::OutputFormula::Input {
name: String::from("color"),
},
b: wiring::OutputFormula::Input {
name: String::from("color"),
},
}], }],
}, },
], ],

View File

@ -46,7 +46,13 @@ pub fn dispatch_actions(system_data: &mut WiringData) {
physics_state, physics_state,
entities_died_last_tick, entities_died_last_tick,
pos, pos,
) >= wiring_action.threshold ) >= compute_output(
&wiring_action.threshold,
&wiring_element.inputs,
physics_state,
entities_died_last_tick,
pos,
)
}) })
.for_each(|wiring_action| { .for_each(|wiring_action| {
dispatch_action( dispatch_action(

View File

@ -22,14 +22,28 @@ pub struct Circuit {
} }
pub enum OutputFormula { pub enum OutputFormula {
Constant { value: f32 }, Constant {
Input { name: String }, value: f32,
},
Input {
name: String,
},
Logic(Box<Logic>), Logic(Box<Logic>),
SineWave { amplitude: f32, frequency: f32 }, SineWave {
OnCollide { value: f32 }, amplitude: Box<OutputFormula>,
OnInteract { value: f32 }, frequency: Box<OutputFormula>,
OnDeath { value: f32, radius: f32 }, },
OnCollide {
value: Box<OutputFormula>,
},
OnInteract {
value: Box<OutputFormula>,
},
OnDeath {
value: Box<OutputFormula>,
radius: Box<OutputFormula>,
},
} }
pub enum LogicKind { pub enum LogicKind {
@ -42,7 +56,7 @@ pub enum LogicKind {
pub struct WiringAction { pub struct WiringAction {
pub formula: OutputFormula, pub formula: OutputFormula,
pub threshold: f32, pub threshold: OutputFormula,
pub effects: Vec<WiringActionEffect>, pub effects: Vec<WiringActionEffect>,
} }
@ -75,3 +89,19 @@ impl Component for WiringElement {
impl Component for Circuit { impl Component for Circuit {
type Storage = IdvStorage<Self>; type Storage = IdvStorage<Self>;
} }
pub fn BoxConst(constant_value: f32) -> Box<OutputFormula> { Box::new(Const(constant_value)) }
pub fn Const(constant_value: f32) -> OutputFormula {
OutputFormula::Constant {
value: constant_value,
}
}
pub fn BoxInput(name: &str) -> Box<OutputFormula> { Box::new(Input(name)) }
pub fn Input(name: &str) -> OutputFormula {
OutputFormula::Input {
name: name.to_string(),
}
}