redstone bundle mask improvement

This commit is contained in:
Djeeberjr 2022-03-13 22:29:25 +01:00
parent 57bf49bea9
commit a1bb41f8a2
2 changed files with 43 additions and 4 deletions

View File

@ -1,10 +1,45 @@
package kernel.peripherals; package kernel.peripherals;
import haxe.ds.ReadOnlyArray;
import util.Color; import util.Color;
using tink.CoreApi; using tink.CoreApi;
typedef BundleMask = Color; abstract BundleMask(Int) from cc.Colors.Color to cc.Colors.Color {
public inline function new(i:Int) {
this = i;
}
@:from
public static function fromColor(c: Color) {
return new BundleMask(c);
}
@:op(A + B)
@:op(A | B)
public inline function combine(rhs: BundleMask):BundleMask {
return this | rhs;
}
@:op(A + B)
@:op(A | B)
public inline function combineWithColor(rhs: Color):BundleMask {
return this | rhs;
}
public function getComponents(): ReadOnlyArray<Color> {
var components: Array<Color> = [];
var mask = 1;
for (i in 0...16){
if ((this & mask) > 0 ){
components.push(mask);
}
mask = mask << 1;
}
return components;
}
}
class Redstone implements IPeripheral { class Redstone implements IPeripheral {
@ -22,16 +57,19 @@ class Redstone implements IPeripheral {
this.addr = side; this.addr = side;
this.onChange = this.onChangeTrigger.asSignal(); this.onChange = this.onChangeTrigger.asSignal();
updateState();
KernelEvents.instance.onRedstone.handle(()->{ KernelEvents.instance.onRedstone.handle(()->{
if ((this.getAnalogInput() != this.analogInputState) || (this.bundleInputState == this.getBundledInput())){ if ((this.getAnalogInput() != this.analogInputState) || (this.bundleInputState != this.getBundledInput())){
updateState();
this.onChangeTrigger.trigger(null); this.onChangeTrigger.trigger(null);
} }
}); });
} }
public function getAddr():String { public function getAddr():String {
return this.addr; return this.addr;
updateState();
} }
private function updateState() { private function updateState() {

View File

@ -21,7 +21,8 @@ enum abstract Color(Int) from cc.Colors.Color to cc.Colors.Color {
var Black = 0x8000; var Black = 0x8000;
@:op(A + B) @:op(A + B)
@:op(A | B)
public inline function combine(rhs: Color):BundleMask { public inline function combine(rhs: Color):BundleMask {
return this + rhs; return this | rhs;
} }
} }