Skip to content

Commit

Permalink
more scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhan45 committed Apr 22, 2021
1 parent a8a73de commit abf4031
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions src/scripting.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::nodes::NodeTy;
use crate::{components::ConnectionTy, Pos};
use macroquad::prelude::Vec2;
use rhai::Map;
Expand All @@ -18,8 +19,8 @@ use crate::{
// for humans to write but it's fine for auto serialization and deserialization

pub fn create_circuit(world: &mut World) {
// 1. Create wires
// 2. Get RhaiNodes which contain a type, [[input wires]], [[output wires]]
// 1. Create wires - done
// 2. Get RhaiNodes which contain a type, [[input wires]], [[output wires]] - done
// 3. Create Nodes and Connections
// 4. Profit

Expand Down Expand Up @@ -61,6 +62,73 @@ pub fn create_circuit(world: &mut World) {
)
})
.collect::<BTreeMap<String, Entity>>();

struct RhaiNode {
pub ty: NodeTy,
pub input_wires: Vec<Vec<String>>,
pub output_wires: Vec<Vec<String>>,
pub pos: Vec2,
}

let nodes: Array = {
let engine = world.fetch::<RhaiEngine>();
let mut scope = world.fetch_mut::<RhaiScope>();
engine.0.eval_with_scope(&mut scope.0, "NODES").unwrap()
};

let nodes = nodes.iter().map(|node: &Dynamic| {
let node = node.clone_cast::<Map>();

let ty = node.get("type").unwrap();
let ty = ty.clone_cast::<String>();

let ty = {
use crate::components::nodes::NodeTy::*;

match ty.as_str() {
"On" => OnNode,
"Off" => OffNode,
"Not" => NotNode,
"And" => AndNode,
"Or" => OrNode,
"Nand" => NandNode,
"Nor" => NorNode,
"Xor" => XorNode,
"Xnor" => XnorNode,
"Switch" => SwitchNode,
_ => panic!("Invalid Node"),
}
};

let process_array = |name: &str| {
let arr = node.get(name).unwrap();
let arr = arr.clone_cast::<Array>();
arr.iter()
.map(|connection| {
let connection = connection.clone_cast::<Array>();
connection
.iter()
.map(|input| input.clone_cast::<String>())
.collect::<Vec<String>>()
})
.collect::<Vec<Vec<String>>>()
};

let input_wires = process_array("inputs");
let output_wires = process_array("inputs");

let pos = node.get("pos").unwrap().clone_cast::<Map>();
let x = pos.get("x").unwrap().clone_cast::<f32>();
let y = pos.get("y").unwrap().clone_cast::<f32>();
let pos = Vec2::new(x, y);

RhaiNode {
ty,
input_wires,
output_wires,
pos,
}
});
}

// pub struct CreateScriptedCircuitSys<N, const I: usize, const O: usize>
Expand Down

0 comments on commit abf4031

Please sign in to comment.