From 24d9268b4add12b29b81a93053426d6592e7b036 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 12 Mar 2022 22:23:17 +0100 Subject: [PATCH] redstone change listner --- src/kernel/peripherals/Redstone.hx | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/kernel/peripherals/Redstone.hx b/src/kernel/peripherals/Redstone.hx index cfe3409..25b7ef6 100644 --- a/src/kernel/peripherals/Redstone.hx +++ b/src/kernel/peripherals/Redstone.hx @@ -2,22 +2,45 @@ package kernel.peripherals; import util.Color; +using tink.CoreApi; + typedef BundleMask = Color; class Redstone implements IPeripheral { + public final onChange:Signal; + + private final onChangeTrigger:SignalTrigger = Signal.trigger(); + private final addr:Side; + private var analogInputState:Int; + private var bundleInputState:BundleMask; + @:allow(kernel.peripherals) private function new(side: Side) { this.addr = side; + this.onChange = this.onChangeTrigger.asSignal(); + + KernelEvents.instance.onRedstone.handle(()->{ + if ((this.getAnalogInput() != this.analogInputState) || (this.bundleInputState == this.getBundledInput())){ + this.onChangeTrigger.trigger(null); + } + }); } public function getAddr():String { return this.addr; + updateState(); + } + + private function updateState() { + this.analogInputState = cc.Redstone.getAnalogInput(this.addr); + this.bundleInputState = cc.Redstone.getBundledInput(this.addr); } public inline function setOutput(on:Bool):Void { + this.analogInputState = 15; cc.Redstone.setOutput(this.addr,on); } @@ -30,6 +53,7 @@ class Redstone implements IPeripheral { } public inline function setAnalogOutput(strength:Int): Void { + this.analogInputState = strength; cc.Redstone.setAnalogOutput(this.addr,strength); } @@ -42,6 +66,7 @@ class Redstone implements IPeripheral { } public inline function setBundledOutput(output: BundleMask) { + this.bundleInputState = output; cc.Redstone.setBundledOutput(this.addr,output); }