diff --git a/Cargo.lock b/Cargo.lock index 863e99b..7521d3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,15 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -887,6 +896,26 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" +[[package]] +name = "serde" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "shred" version = "0.10.2" @@ -910,11 +939,13 @@ checksum = "b5752e017e03af9d735b4b069f53b7a7fd90fefafa04d8bd0c25581b0bff437f" name = "simple_electronics" version = "0.1.0" dependencies = [ + "bincode", "egui", "egui-macroquad", "macroquad", "resvg", "rhai", + "serde", "specs", "tiny-skia", "usvg", diff --git a/Cargo.toml b/Cargo.toml index 720f189..982a5c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,9 @@ egui = "0.10.0" rhai = { version = "0.19.15" } +serde = { version = "1.0.125", features = ["derive"] } +bincode = "1.3.3" + [profile.release] opt-level = 's' diff --git a/src/components.rs b/src/components.rs index 3fcb5aa..e9b6ca2 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; use std::collections::HashSet; use macroquad::prelude::Vec2; diff --git a/src/components/nodes.rs b/src/components/nodes.rs index c8fdb7c..bee9906 100644 --- a/src/components/nodes.rs +++ b/src/components/nodes.rs @@ -4,7 +4,7 @@ use crate::systems::simulation_systems::WireSys; use macroquad::prelude::Vec2; use specs::{prelude::*, Component}; -#[derive(Component, Default)] +#[derive(Component, Default, Clone)] pub struct Wire { pub input_state: bool, pub output_state: bool, diff --git a/src/main.rs b/src/main.rs index d85722e..babb73b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use specs::prelude::*; mod components; mod resources; -// mod scripting; +mod scripting; mod svg; mod systems; mod ui; diff --git a/src/scripting.rs b/src/scripting.rs index 20e6376..4bc68e0 100644 --- a/src/scripting.rs +++ b/src/scripting.rs @@ -11,6 +11,58 @@ use crate::{ resources::{RhaiEngine, RhaiScope}, }; +// Ok so the logical graph structure is potentially cyclic and pretty wonky actually so it's +// impossible to describe it just using straightforward JSON or similar +// +// This basically just seeks to make the Rhai code mirror the Rust circuit creation; it's not great +// 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]] + // 3. Create Nodes and Connections + // 4. Profit + + let wires: Map = { + let engine = world.fetch::(); + let mut scope = world.fetch_mut::(); + engine.0.eval_with_scope(&mut scope.0, "WIRES").unwrap() + }; + + let wires = wires + .iter() + .map(|(name, wire)| { + let wire = wire.clone_cast::(); + let bends = wire + .get("bends") + .unwrap() + .clone_cast::() + .iter() + .map(|point| { + let point = point.clone_cast::(); + let x = point.get("x").unwrap().clone(); + let y = point.get("y").unwrap().clone(); + Vec2::new(x.cast::(), y.cast::()) + }) + .collect(); + + ( + name.to_string(), + Wire { + points: bends, + ..Wire::default() + }, + ) + }) + .map(|(name, wire)| { + ( + name.clone(), + world.create_entity().with(wire.clone()).build(), + ) + }) + .collect::>(); +} + // pub struct CreateScriptedCircuitSys // where // N: Node + 'static, @@ -118,48 +170,35 @@ use crate::{ // } // } -pub fn create_circuit(circuit: Array, world: &World) { - fn create_node(node: &Dynamic, world: &World) -> Entity { - let map = node.cast::(); +// pub fn create_circuit(circuit: Array, world: &World) { +// fn create_node(node: &Dynamic, world: &World) -> Entity { +// let map = node.cast::(); + +// 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::(), +// )) +// }; - 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); - }); -} +// // We have a list of output node entities +// todo!(); +// } + +// circuit.iter().cloned().for_each(|node: Dynamic| { +// create_node(&node, world); +// }); +// } // pub fn run_circuit_create_sys(script: String, world: &World) { // use crate::all_nodes; diff --git a/test_scripts/basic_circuit.rhai b/test_scripts/basic_circuit.rhai index ddb647f..5d753d7 100644 --- a/test_scripts/basic_circuit.rhai +++ b/test_scripts/basic_circuit.rhai @@ -1,7 +1,6 @@ let CIRCUIT = [ #{ type: "SwitchNode", - inputs: [], outputs: [], pos: [0.0, 0.0], },