From 8ee22d7304ec2464e769452336aed8492ef43bcb Mon Sep 17 00:00:00 2001 From: Mikail Khan Date: Thu, 15 Apr 2021 13:18:38 -0400 Subject: [PATCH] some minor scripting work --- src/components.rs | 5 + src/scripting.rs | 250 +++++++++++++++++++++++++++------------------- 2 files changed, 152 insertions(+), 103 deletions(-) diff --git a/src/components.rs b/src/components.rs index 2d78e5a..2fc688c 100644 --- a/src/components.rs +++ b/src/components.rs @@ -81,3 +81,8 @@ pub struct Connection { pub ty: ConnectionTy, pub index: usize, } + +#[derive(Component)] +pub struct CompoundNode { + pub world: World, +} diff --git a/src/scripting.rs b/src/scripting.rs index fd4ea22..6896b47 100644 --- a/src/scripting.rs +++ b/src/scripting.rs @@ -1,4 +1,5 @@ use crate::{components::ConnectionTy, Pos}; +use macroquad::prelude::Vec2; use rhai::Map; use std::{collections::BTreeMap, convert::TryInto, marker::PhantomData}; @@ -10,111 +11,154 @@ use crate::{ resources::{RhaiEngine, RhaiScope}, }; -pub struct CreateScriptedCircuitSys -where - N: Node + 'static, -{ - node: PhantomData, - node_name: String, -} +// pub struct CreateScriptedCircuitSys +// where +// N: Node + 'static, +// { +// node: PhantomData, +// node_name: String, +// } + +// impl<'a, N, const I: usize, const O: usize> System<'a> for CreateScriptedCircuitSys +// where +// N: Node + 'static, +// { +// type SystemData = ( +// Read<'a, RhaiEngine>, +// Write<'a, RhaiScope<'static>>, +// WriteStorage<'a, Connected>, +// WriteStorage<'a, Pos>, +// WriteStorage<'a, Wire>, +// WriteStorage<'a, Connection>, +// Entities<'a>, +// ); + +// fn run( +// &mut self, +// (engine, mut scope, mut nodes, mut positions, mut wires, mut connections, entities): Self::SystemData, +// ) { +// let circuit: Array = engine +// .0 +// .eval_with_scope(&mut scope.0, "CIRCUIT") +// .unwrap_or(Array::new()) +// .to_owned(); + +// circuit.iter().cloned().for_each(|node: Dynamic| { +// let map = node.cast::(); +// let ty = map.get("type").unwrap(); +// let type_str = ty.clone().take_immutable_string().unwrap(); +// if type_str == self.node_name { +// let pos = map.get("pos").unwrap(); +// let pos_arr = pos.clone_cast::(); +// let x = pos_arr[0].clone_cast::(); +// let y = pos_arr[1].clone_cast::(); + +// let pos = Pos::from_vec((x as f32, y as f32).into()); +// let input_offsets = N::input_offsets(); +// let output_offsets = N::output_offsets(); + +// let inputs = (0..I) +// .map(|index| { +// entities +// .build_entity() +// .with( +// Connection { +// wires: Vec::new(), +// ty: ConnectionTy::Input, +// index, +// }, +// &mut connections, +// ) +// .with( +// Pos::from_vec_unrounded(pos.pos + input_offsets[index]), +// &mut positions, +// ) +// .build() +// }) +// .collect::>() +// .try_into() +// .unwrap(); + +// let outputs = (0..O) +// .map(|index| { +// entities +// .build_entity() +// .with( +// Connection { +// wires: Vec::new(), +// ty: ConnectionTy::Output, +// index, +// }, +// &mut connections, +// ) +// .with( +// Pos::from_vec_unrounded(pos.pos + output_offsets[index]), +// &mut positions, +// ) +// .build() +// }) +// .collect::>() +// .try_into() +// .unwrap(); -impl<'a, N, const I: usize, const O: usize> System<'a> for CreateScriptedCircuitSys -where - N: Node + 'static, -{ - type SystemData = ( - Read<'a, RhaiEngine>, - Write<'a, RhaiScope<'static>>, - WriteStorage<'a, Connected>, - WriteStorage<'a, Pos>, - WriteStorage<'a, Wire>, - WriteStorage<'a, Connection>, - Entities<'a>, - ); - - fn run( - &mut self, - (engine, mut scope, mut nodes, mut positions, mut wires, mut connections, entities): Self::SystemData, - ) { - let circuit: Array = engine - .0 - .eval_with_scope(&mut scope.0, "CIRCUIT") - .unwrap_or(Array::new()) - .to_owned(); - - circuit.iter().cloned().for_each(|node: Dynamic| { - let map = node.cast::(); - let ty = map.get("type").unwrap(); - let type_str = ty.clone().take_immutable_string().unwrap(); - if type_str == self.node_name { - let pos = map.get("pos").unwrap(); - let pos_arr = pos.clone_cast::(); - let x = pos_arr[0].clone_cast::(); - let y = pos_arr[1].clone_cast::(); - - let pos = Pos::from_vec((x as f32, y as f32).into()); - let input_offsets = N::input_offsets(); - let output_offsets = N::output_offsets(); - - let inputs = (0..I) - .map(|index| { - entities - .build_entity() - .with( - Connection { - wires: Vec::new(), - ty: ConnectionTy::Input, - index, - }, - &mut connections, - ) - .with( - Pos::from_vec_unrounded(pos.pos + input_offsets[index]), - &mut positions, - ) - .build() - }) - .collect::>() - .try_into() - .unwrap(); - - let outputs = (0..O) - .map(|index| { - entities - .build_entity() - .with( - Connection { - wires: Vec::new(), - ty: ConnectionTy::Output, - index, - }, - &mut connections, - ) - .with( - Pos::from_vec_unrounded(pos.pos + output_offsets[index]), - &mut positions, - ) - .build() - }) - .collect::>() - .try_into() - .unwrap(); - - entities - .build_entity() - .with( - Connected { - node: N::default(), - inputs, - outputs, - }, - &mut nodes, - ) - .with(pos, &mut positions) - .build(); - } - }); +// entities +// .build_entity() +// .with( +// Connected { +// node: N::default(), +// inputs, +// outputs, +// }, +// &mut nodes, +// ) +// .with(pos, &mut positions) +// .build(); +// } +// }); +// } +// } + +pub fn create_circuit(circuit: Array, world: &World) { + fn create_node(node: &Dynamic, world: &World) -> Entity { + let map = node.cast::(); + + let inputs = { + let inputs = map.get("inputs").unwrap(); + let input_arr = inputs.clone_cast::(); + input_arr + .iter() + .map(|node| create_node(node, world)) + .collect::>(); + }; + let outputs = { + let outputs = map.get("outputs").unwrap(); + let output_arr = outputs.clone_cast::(); + output_arr + .iter() + .map(|node| create_node(node, world)) + .collect::>(); + }; + let pos = { + let pos = map.get("pos").unwrap(); + let pos_arr = pos.clone_cast::(); + Pos::from_vec(Vec2::new( + pos_arr[0].clone_cast::(), + pos_arr[1].clone_cast::(), + )) + }; + + // We have a list of input nodes and output nodes + // create a list of input connections and output connections + // add wires for outputs + // + // actually maybe this should only go one way so the map just contains outputs and it adds + // inputs to the other nodes + todo!(); } + + circuit.iter().cloned().for_each(|node: Dynamic| { + create_node(&node, world); + }); } pub fn run_circuit_create_sys(script: String, world: &World) {