redstone change listner

This commit is contained in:
Djeeberjr 2022-03-12 22:23:17 +01:00
parent a75f86cbc1
commit 24d9268b4a

View File

@ -2,22 +2,45 @@ package kernel.peripherals;
import util.Color; import util.Color;
using tink.CoreApi;
typedef BundleMask = Color; typedef BundleMask = Color;
class Redstone implements IPeripheral { class Redstone implements IPeripheral {
public final onChange:Signal<Noise>;
private final onChangeTrigger:SignalTrigger<Noise> = Signal.trigger();
private final addr:Side; private final addr:Side;
private var analogInputState:Int;
private var bundleInputState:BundleMask;
@:allow(kernel.peripherals) @:allow(kernel.peripherals)
private function new(side: Side) { private function new(side: Side) {
this.addr = 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 { public function getAddr():String {
return this.addr; 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 { public inline function setOutput(on:Bool):Void {
this.analogInputState = 15;
cc.Redstone.setOutput(this.addr,on); cc.Redstone.setOutput(this.addr,on);
} }
@ -30,6 +53,7 @@ class Redstone implements IPeripheral {
} }
public inline function setAnalogOutput(strength:Int): Void { public inline function setAnalogOutput(strength:Int): Void {
this.analogInputState = strength;
cc.Redstone.setAnalogOutput(this.addr,strength); cc.Redstone.setAnalogOutput(this.addr,strength);
} }
@ -42,6 +66,7 @@ class Redstone implements IPeripheral {
} }
public inline function setBundledOutput(output: BundleMask) { public inline function setBundledOutput(output: BundleMask) {
this.bundleInputState = output;
cc.Redstone.setBundledOutput(this.addr,output); cc.Redstone.setBundledOutput(this.addr,output);
} }