From abf4031e8ec2fd4f8556fb7c2330e05a5b522c0b Mon Sep 17 00:00:00 2001 From: Mikail Khan Date: Thu, 22 Apr 2021 17:50:06 -0400 Subject: [PATCH] more scripting --- src/scripting.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/scripting.rs b/src/scripting.rs index 4bc68e0..1ac9bae 100644 --- a/src/scripting.rs +++ b/src/scripting.rs @@ -1,3 +1,4 @@ +use crate::nodes::NodeTy; use crate::{components::ConnectionTy, Pos}; use macroquad::prelude::Vec2; use rhai::Map; @@ -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 @@ -61,6 +62,73 @@ pub fn create_circuit(world: &mut World) { ) }) .collect::>(); + + struct RhaiNode { + pub ty: NodeTy, + pub input_wires: Vec>, + pub output_wires: Vec>, + pub pos: Vec2, + } + + let nodes: Array = { + let engine = world.fetch::(); + let mut scope = world.fetch_mut::(); + engine.0.eval_with_scope(&mut scope.0, "NODES").unwrap() + }; + + let nodes = nodes.iter().map(|node: &Dynamic| { + let node = node.clone_cast::(); + + let ty = node.get("type").unwrap(); + let ty = ty.clone_cast::(); + + 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::(); + arr.iter() + .map(|connection| { + let connection = connection.clone_cast::(); + connection + .iter() + .map(|input| input.clone_cast::()) + .collect::>() + }) + .collect::>>() + }; + + let input_wires = process_array("inputs"); + let output_wires = process_array("inputs"); + + let pos = node.get("pos").unwrap().clone_cast::(); + let x = pos.get("x").unwrap().clone_cast::(); + let y = pos.get("y").unwrap().clone_cast::(); + let pos = Vec2::new(x, y); + + RhaiNode { + ty, + input_wires, + output_wires, + pos, + } + }); } // pub struct CreateScriptedCircuitSys