AyCraft.js - A library for building and simulating interactive contraptions within a physics-based environment.
TODO: rename addPart to add()
Build contrapations with parts.
Think of the Minecraft Redstone system using real-world parts. AyCraft.js is a simulation-focused library great for building games or educational tools. AyCraft handles all aspects of your simulation while emitting events other outside systems can interact with.
Design Prinicpals
Rapid prototyping and design of simulated contraptions
- Quick and easy design and running of simulated contraptions
- Opt-in Realism, You decide what level of realism your simulation uses
- Granualar customizable conventions with reasonable defaults configurations
- Headless simulations
A low complexity Light Switch Contraption
import { AyCraft, Button, LEDLight, Wire } from 'aycraft';
let lightSwitch = new AyCraft();
let button = new Button();
let wire = new Wire();
let ledLight = new LEDLight();
// Connect button to wire, and wire to LED light
button.connect(wire);
wire.connect(ledLight);
// Add parts to AyCraft system
lightSwitch.addPart(button);
lightSwitch.addPart(wire);
lightSwitch.addPart(ledLight);
// Simulate pressing the button
button.press();
Super Easy Rendering
TODO blah SCREENSHOTS
AyCraft is available as a Plugin for the Mantra Game Developement Framework and can be rendered in 2d with CSS, Phaser 3 or 3D with Babylon.js
Part list
AyCraft Part | Minecraft Equivalent |
---|---|
Wire | Redstone Dust |
Transistor | Redstone Torch |
Power Supply | Redstone Block |
Relay | Redstone Relay |
Amplifier | Redstone Comparator |
Button | Button |
Pressure Sensor | Pressure Plate |
Impact Sensor | Target Block |
Motion Detector | Observer |
LED Light | Redstone Lamp |
More Parts are coming. If you'd like to see an additional Part added, please feel free to open a pull request.
Realism vs dynamic gameplay simulation
Four ways for turning on a Light Bulb, using the same parts, with increasing realism
Blah blah
Striking a balance between realism and dynamic gameplay often leads to situations where an exact simulation would not be appropiate. AyCraft simulations are well suited for varying degrees of realism depending on your requirements.
Consider a Light Switch
contraption. In the following four examples, we demonstrate ascending levels of simulated realism.
Most Basic
In this basic example, we create a light switch without any wires or power source, and all parts will default to position (0, 0, 0)
.
import { AyCraft, Button, LEDLight } from 'aycraft';
let lightSwitch = new AyCraft();
let button = new Button();
let ledLight = new LEDLight();
// Connect button directly to LED light
button.connect(ledLight);
// Add parts to AyCraft system
lightSwitch.addPart(button);
lightSwitch.addPart(ledLight);
// Simulate pressing the button
button.press();
In this most basic example the entire contraption sits at the (0, 0, 0)
position with no power source. Parts are connected directly to each other without the use of Wires. Triggering the button press event will toggle the light since the two parts are directly connected.
- no wires used
- no power source required
- no spatial awareness ( coordinates ) of parts
With Positioning
In this example we create the same contraption, however we apply spatial positioning so each part is laid out on the grid instead of being stacked on top of each other at starting position. Using this configuration, physical contact is not required between parts in order for Signal
to transmit.
import { AyCraft, Button, LEDLight } from 'aycraft';
let lightSwitch = new AyCraft();
let button = new Button(0, 0, 0);
let ledLight = new LEDLight(100, 50, 0);
// Connect button directly to LED light
button.connect(ledLight);
// Add parts to AyCraft system
lightSwitch.addPart(button);
lightSwitch.addPart(ledLight);
// Simulate pressing the button
button.press();
[] - Has spatial position [] - no wires used [] - no power source required
With Wire
Instead of directly connecting the parts together, we can uses Wires
to connect parts. A wire has inputs
, outputs
, carries Signal
, has a dynamic length based on it's connections, and may optionally have eletrical resistance or binary data encoding applied to the Signal
as it pases through.
import { AyCraft, Button, LEDLight, Wire } from 'aycraft';
let lightSwitch = new AyCraft();
let button = new Button(0, 0, 0);
let wire = new Wire();
let ledLight = new LEDLight(100, 50, 0);
// Connect button to wire, and wire to LED light
button.connect(wire);
wire.connect(ledLight);
// Add parts to AyCraft system
lightSwitch.addPart(button);
lightSwitch.addPart(wire);
lightSwitch.addPart(ledLight);
// Simulate pressing the button
button.press();
[]- Has spatial position [] - wires used [] - no power source required
Light with Power Supply
All our previous examples assumed infinite free power was available. By changing the powerRequired
setting we can enable power requirements for the contraption. This means we now need to power our contraption with a power source or it will not work.
import { AyCraft, Button, LEDLight, Wire, PowerSupply } from 'aycraft';
let lightSwitch = new AyCraft({ powerRequired: true });
let button = new Button(0, 0, 0);
let wire = new Wire(); // TODO: wire settings
let ledLight = new LEDLight(100, 50, 0); // TOOD: power watter
let powerSupply = new PowerSupply(200, 0, 0); // Adding a power supply // TODO: power settings
// Connect power supply to wire, and wire to other components
powerSupply.connect(wire);
button.connect(wire);
wire.connect(ledLight);
// Add parts to AyCraft system
lightSwitch.addPart(powerSupply);
lightSwitch.addPart(button);
lightSwitch.addPart(wire);
lightSwitch.addPart(ledLight);
// Simulate pressing the button
button.press();
[] - Has spatial position [] - wires used [] - power source used
Choose your own realisgm style simulations
blah blah , remove? better summary? As you can see, this opt-in realism design allows for ideal development of contraptions for gaming.
Immediate Mode and Tick Mode
Immediate Mode Behavior: In immediate mode, actions and their effects are processed instantly. For example, pressing a button would immediately trigger any connected LED lights without the need to call .tick().
Tick-based Mode Behavior: In tick-based mode, the system requires the .tick() method to be called to advance the state, allowing for time-dependent behaviors.
Termonologies
Parts
A Part
is a piece of a contrapation such as 'Button', 'LED Light', 'Wire', or 'Power Supply'. Parts are independant units which may be connected to other parts. Parts receive and send Signals
. Parts will emit events relevant to their specifc functionality which you can optionally listen for.
Contraptions
A Contraption
is a collection of parts which have been connected together in order to process Signal
. For example, a "Light Switch" contraption could consist of a Button
, Wire
, and LED Light
.
Signals
Signals are blah blah blah
Electrical Signal
Electrical signals can also be encoded to data formats like USB through setting values like dPlusVoltage and dMinusVoltage
voltage = 5, // Volts
current = 1, // Amps
resistance = 0, // Ohms
capacitance = 0, // Farads
inductance = 0, // Henrys
frequency = 0, // Hertz (important for AC)
phaseAngle = 0, // Degrees (important for AC)
powerFactor = 1 // Unitless (important for AC)
Light Signal
Light signals are used for optics like LED Light
or Laser
.
intensity = 1, // Light intensity (arbitrary units)
wavelength = 550, // Wavelength in nanometers (visible light spectrum)
direction = 0 // Direction of light in degrees
Test
npm run test
License
Yantra Works 2023 AGPL
Setting Contraption Root Part
This will pair Part.onFn
and Part.offFn
to Contraption.start()
and Contrapation.stop()
.
This allows you to specify any part as an optional "entry point" to the contraption.
The first part added to the contaption will always be the root part / entry part. It's that simple.