Electricity and Wires #19

Open
opened 2020-01-02 21:55:51 +00:00 by kirsle · 0 comments

This is an idea for a future Doodle feature involving electricity and wires.

"Wire" Palette Type

Colors in the level palette could be flagged as being a "wire" in a similar way as "fire" and "water" colors.

Wire pixels would be capable of carrying a power signal from a Power Source doodad to one or more Powered Device doodads that are interested in the signal.

Wire pixels would have two states: "on" and "off". The default state would be "off" and would render in a darker shade than the pixel's color value. For example if wires were colored red (#FF0000) they would render darker when "off" (i.e. #AA0000) but return to their intended color when powered.

Wire Trace Algorithm

When the power state of a wire should change, an algorithm will "crawl" the wire pixels to visit each one and potentially modify their state.

From a given starting pixel, spread to the neighboring wire pixel in any of the 8 directions (including diagonals). This way wires of any shape will work: freehand, lines, rectangles, and thick lines too.

Wires would only be traced when needed, and the state of the pixels updated as needed, when a power state change has happened:

  • If a Power Source emits power, and the wire it's touching is already powered, do nothing.
  • If a Power Source emits power, and the wire is NOT already powered, trace the wire and set every pixel to "powered" as you go. (We know the wire is powered because the power source is giving power).
  • If a Power Source turns off, and the wire is already off, do nothing.
  • If a Power Source turns off, and the wire is on, trace the wire to make sure it's not touching other active Power Sources, keeping track of each pixel visited. If no other power source is on, turn off all the wire pixels.

When a wire is turned on or off, any doodad that is touching the wire would be given a "power" event with the wire's state. Interested doodads could then update their internal state accordingly.

Power Sources

Existing doodads that emit power events could also be updated to act as power sources for wires: Switches and Sticky Buttons would toggle the power source on/off, and normal Buttons would emit power while pressed and stop powering when released.

New doodads that could be added would include:

  • Constant power sources (like the Redstone Block from Minecraft)
  • Clocks (timers) could flash power each time they emit their tick signal.

Logic Gates

Special doodads placed along the path of a wire could act as logic gates that control the behavior of the wire during the Wire Trace Algorithm.

While a wire is being traced, if a given pixel falls beneath a logic gate, the gate's handler function would be called which would inform the next step for the wire trace algorithm.

Additional wire pixels beneath the gate will be ignored; so even if a wire line was un-interrupted going beneath the gate doodad, no signal will traverse the wire. When the gate's handler function returns a valid direction for the signal to pass, the pixel beneath that edge of the doodad would become lit up and the trace would continue from there.

Ideally, logic gates should be able to rotate (face any 90-degree direction) without needing a ton of redundant doodads for each direction. The engine will need to support rotating doodads for this to work.

  • One-way Gate
    • Enforces that electricity can only pass through the gate going a single direction, but not back the other way.
    • i.e. if a "Left to Right" gate was in use, and the Wire Trace Algorithm was moving rightward when it encountered the gate, the gate would allow it to pass through to wires going out the right side. But if a signal came in from the right side, it would be stopped and no additional wires would be traced coming out of the logic gate.
  • Or Gate
    • The doodad sprite would mark the input edges and the output edge visually.
    • If electricity comes from either input edge, it emits through the output edge.
  • And Gate
    • Needs electricity in both inputs to emit a signal to the output.
  • Xor Gate
This is an idea for a future Doodle feature involving electricity and wires. ## "Wire" Palette Type Colors in the level palette could be flagged as being a "wire" in a similar way as "fire" and "water" colors. Wire pixels would be capable of carrying a power signal from a Power Source doodad to one or more Powered Device doodads that are interested in the signal. Wire pixels would have two states: "on" and "off". The default state would be "off" and would render in a **darker** shade than the pixel's color value. For example if wires were colored red (#FF0000) they would render darker when "off" (i.e. #AA0000) but return to their intended color when powered. ## Wire Trace Algorithm When the power state of a wire should change, an algorithm will "crawl" the wire pixels to visit each one and potentially modify their state. From a given starting pixel, spread to the neighboring wire pixel in any of the 8 directions (including diagonals). This way wires of any shape will work: freehand, lines, rectangles, and thick lines too. Wires would only be traced when needed, and the state of the pixels updated as needed, when a power state change has happened: * If a Power Source emits power, and the wire it's touching is already powered, do nothing. * If a Power Source emits power, and the wire is NOT already powered, trace the wire and set every pixel to "powered" as you go. (We know the wire is powered because the power source is giving power). * If a Power Source turns off, and the wire is already off, do nothing. * If a Power Source turns off, and the wire is on, trace the wire to make sure it's not touching other active Power Sources, keeping track of each pixel visited. If no other power source is on, turn off all the wire pixels. When a wire is turned on or off, any doodad that is touching the wire would be given a "power" event with the wire's state. Interested doodads could then update their internal state accordingly. ## Power Sources Existing doodads that emit power events could also be updated to act as power sources for wires: Switches and Sticky Buttons would toggle the power source on/off, and normal Buttons would emit power while pressed and stop powering when released. New doodads that could be added would include: * Constant power sources (like the Redstone Block from Minecraft) * Clocks (timers) could flash power each time they emit their tick signal. ## Logic Gates Special doodads placed along the path of a wire could act as logic gates that control the behavior of the wire during the Wire Trace Algorithm. While a wire is being traced, if a given pixel falls beneath a logic gate, the gate's handler function would be called which would inform the next step for the wire trace algorithm. Additional wire pixels beneath the gate will be ignored; so even if a wire line was un-interrupted going beneath the gate doodad, no signal will traverse the wire. When the gate's handler function returns a valid direction for the signal to pass, the pixel beneath that edge of the doodad would become lit up and the trace would continue from there. Ideally, logic gates should be able to rotate (face any 90-degree direction) without needing a ton of redundant doodads for each direction. The engine will need to support rotating doodads for this to work. * **One-way Gate** * Enforces that electricity can only pass through the gate going a single direction, but not back the other way. * i.e. if a "Left to Right" gate was in use, and the Wire Trace Algorithm was moving rightward when it encountered the gate, the gate would allow it to pass through to wires going out the right side. But if a signal came in from the right side, it would be stopped and no additional wires would be traced coming out of the logic gate. * **Or Gate** * The doodad sprite would mark the input edges and the output edge visually. * If electricity comes from either input edge, it emits through the output edge. * **And Gate** * Needs electricity in both inputs to emit a signal to the output. * **Xor Gate**
kirsle added the
enhancement
label 2020-01-02 21:55:51 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: SketchyMaze/doodle#19
There is no content yet.