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;
using tink.CoreApi;
typedef BundleMask = Color;
class Redstone implements IPeripheral {
public final onChange:Signal<Noise>;
private final onChangeTrigger:SignalTrigger<Noise> = 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);
}