From a1bb41f8a2035cb55ff5de53016aeecc562f03c4 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sun, 13 Mar 2022 22:29:25 +0100 Subject: [PATCH] redstone bundle mask improvement --- src/kernel/peripherals/Redstone.hx | 44 ++++++++++++++++++++++++++++-- src/util/Color.hx | 3 +- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/kernel/peripherals/Redstone.hx b/src/kernel/peripherals/Redstone.hx index 25b7ef6..711c5a6 100644 --- a/src/kernel/peripherals/Redstone.hx +++ b/src/kernel/peripherals/Redstone.hx @@ -1,10 +1,45 @@ package kernel.peripherals; +import haxe.ds.ReadOnlyArray; import util.Color; 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 { + var components: Array = []; + var mask = 1; + for (i in 0...16){ + if ((this & mask) > 0 ){ + components.push(mask); + } + mask = mask << 1; + } + + return components; + } +} class Redstone implements IPeripheral { @@ -22,16 +57,19 @@ class Redstone implements IPeripheral { this.addr = side; this.onChange = this.onChangeTrigger.asSignal(); + updateState(); + 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); } }); + } public function getAddr():String { return this.addr; - updateState(); } private function updateState() { diff --git a/src/util/Color.hx b/src/util/Color.hx index b1445fb..8d95af0 100644 --- a/src/util/Color.hx +++ b/src/util/Color.hx @@ -21,7 +21,8 @@ enum abstract Color(Int) from cc.Colors.Color to cc.Colors.Color { var Black = 0x8000; @:op(A + B) + @:op(A | B) public inline function combine(rhs: Color):BundleMask { - return this + rhs; + return this | rhs; } }