Skip to content

Commit

Permalink
fixed wire add bug, added all_nodes! macro
Browse files Browse the repository at this point in the history
wasm
  • Loading branch information
mkhan45 committed Apr 11, 2021
1 parent bea5b01 commit 9736927
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 37 deletions.
Binary file modified docs/simple_electronics.wasm
Binary file not shown.
31 changes: 19 additions & 12 deletions src/components/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ impl Node<2, 1> for XnorNode {
}
}

#[macro_export]
macro_rules! all_nodes {
($macro:ident) => {
$macro!(
[OnNode, 0, 1],
[OffNode, 0, 1],
[Wire, 1, 1],
[NotNode, 1, 1],
[AndNode, 2, 1],
[OrNode, 2, 1],
[NandNode, 2, 1],
[NorNode, 2, 1],
[XorNode, 2, 1],
[XnorNode, 2, 1],
)
};
}

pub fn add_node_systems<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> DispatcherBuilder<'a, 'b> {
macro_rules! add_systems {
( $([$node:ident, $i:expr, $o:expr]),* $(,)? ) => {
Expand All @@ -113,16 +131,5 @@ pub fn add_node_systems<'a, 'b>(builder: DispatcherBuilder<'a, 'b>) -> Dispatche
};
}

add_systems!(
[OnNode, 0, 1],
[OffNode, 0, 1],
[Wire, 1, 1],
[NotNode, 1, 1],
[AndNode, 2, 1],
[OrNode, 2, 1],
[NandNode, 2, 1],
[NorNode, 2, 1],
[XorNode, 2, 1],
[XnorNode, 2, 1],
)
all_nodes!(add_systems)
}
1 change: 1 addition & 0 deletions src/systems.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cleanup_sys;
pub mod draw_systems;
pub mod place_node_sys;
pub mod place_wire_sys;
Expand Down
48 changes: 48 additions & 0 deletions src/systems/cleanup_sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::Connected;
use std::marker::PhantomData;

use crate::components::Node;
use specs::prelude::*;

#[derive(Default)]
pub struct CleanupWires<N, const I: usize, const O: usize>
where
N: Node<I, O> + 'static,
{
node: PhantomData<N>,
}
impl<'a, N, const I: usize, const O: usize> System<'a> for CleanupWires<N, I, O>
where
N: Node<I, O> + 'static,
{
type SystemData = (WriteStorage<'a, Connected<N, I, O>>, Entities<'a>);

fn run(&mut self, (mut nodes, entities): Self::SystemData) {
(&mut nodes).join().for_each(|node| {
node.inputs
.iter_mut()
.chain(node.outputs.iter_mut())
.filter(|wire_opt| wire_opt.is_some())
.for_each(|o: &mut Option<Entity>| {
let wire_e = o.unwrap();
dbg!(wire_e);
if !entities.is_alive(wire_e) {
*o = None;
}
});
});
}
}

pub fn run_cleanup_sys(world: &mut World) {
use crate::nodes::*;

macro_rules! cleanup_nodes {
( $([$node:ident, $i:expr, $o:expr]),*, $(,)? ) => {
$(CleanupWires::<$node, $i, $o>::default().run_now(world);)*
};
}

use crate::all_nodes;
all_nodes!(cleanup_nodes);
}
42 changes: 17 additions & 25 deletions src/ui/mouse_click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ use crate::nodes;
use crate::resources;

pub fn handle_mouse_click(world: &mut World) {
world.insert(resources::AddingWire(None));
// clear adding wire including removing the wire entity
{
let adding_wire_state = world.fetch::<AddingWire>().0;
if let Some((_, wire_entity, _, _)) = adding_wire_state {
dbg!(wire_entity);
world.delete_entity(wire_entity).unwrap();

crate::systems::cleanup_sys::run_cleanup_sys(world);

std::mem::drop(adding_wire_state);
world.insert(AddingWire(None));
}
}
let node = world.fetch::<resources::AddingNode>();

if let resources::AddingNode(Some(n)) = &*node {
Expand All @@ -24,18 +36,8 @@ pub fn handle_mouse_click(world: &mut World) {
};
}

place_node_systems!(
[OnNode, 0, 1],
[OffNode, 0, 1],
[Wire, 1, 1],
[NotNode, 1, 1],
[AndNode, 2, 1],
[OrNode, 2, 1],
[NandNode, 2, 1],
[NorNode, 2, 1],
[XorNode, 2, 1],
[XnorNode, 2, 1]
);
use crate::all_nodes;
all_nodes!(place_node_systems);

std::mem::drop(node);
world.insert(resources::AddingNode(None));
Expand Down Expand Up @@ -73,18 +75,8 @@ pub fn handle_mouse_right_click(world: &mut World) {
};
}

place_wire_systems!(
[OnNode, 0, 1],
[OffNode, 0, 1],
[Wire, 1, 1],
[NotNode, 1, 1],
[AndNode, 2, 1],
[OrNode, 2, 1],
[NandNode, 2, 1],
[NorNode, 2, 1],
[XorNode, 2, 1],
[XnorNode, 2, 1]
);
use crate::all_nodes;
all_nodes!(place_wire_systems);
}
};
}

0 comments on commit 9736927

Please sign in to comment.