cc-haxe/src/kernel/peripherals/Peripherals.hx

92 lines
2.4 KiB
Haxe
Raw Normal View History

2021-12-20 00:55:30 +00:00
package kernel.peripherals;
2022-03-12 16:23:09 +00:00
import haxe.ds.ReadOnlyArray;
2021-12-20 00:55:30 +00:00
import kernel.peripherals.Modem;
import kernel.peripherals.Screen;
using lua.Table;
using Lambda;
2022-03-05 01:41:30 +00:00
using tink.CoreApi;
2021-12-20 00:55:30 +00:00
/**
Class responseable for retrieving peripherals.
**/
class Peripheral {
2022-02-24 18:47:27 +00:00
/**
Depends on: KernelEvents
**/
2022-02-21 00:50:19 +00:00
public static var instance:Peripheral;
2022-02-24 18:47:27 +00:00
private var screens: Array<Screen> = [];
private var modes: Array<Modem> = [];
2022-03-05 02:01:26 +00:00
private var drives:Array<Drive> = [];
2022-03-12 21:03:32 +00:00
private var redstone:Array<Redstone> = [];
2022-02-24 18:47:27 +00:00
2022-02-21 00:50:19 +00:00
@:allow(kernel.Init)
2022-02-24 18:47:27 +00:00
private function new() {
KernelEvents.instance.onPeripheral.handle(this.updatePeripherals);
KernelEvents.instance.onPeripheralDetach.handle(this.updatePeripherals);
updatePeripherals();
2022-03-12 21:03:32 +00:00
redstone = [ for(side in [Side.Top,Side.Right,Side.Left,Side.Front,Side.Bottom,Side.Back]) new Redstone(side) ];
2022-02-24 18:47:27 +00:00
}
private function updatePeripherals() {
findScreens();
findModems();
2022-03-05 02:06:41 +00:00
findDrives();
2022-02-24 18:47:27 +00:00
}
2021-12-20 00:55:30 +00:00
/**
Get all connected screens.
**/
2022-03-12 16:23:09 +00:00
public function getScreens():ReadOnlyArray<Screen> {
2022-02-24 18:47:27 +00:00
return this.screens;
2021-12-20 00:55:30 +00:00
}
2022-02-24 18:47:27 +00:00
private function findScreens():Void {
var allScreens = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "monitor");
this.screens = allScreens.map(s -> return new Screen((cc.Peripheral.wrap(s) : Dynamic), s));
}
2021-12-20 00:55:30 +00:00
2022-02-24 18:47:27 +00:00
public function getScreen(addr: String): Screen {
return this.screens.find(item -> item.getAddr() == addr);
2021-12-20 00:55:30 +00:00
}
/**
Get all connected modems.
**/
2022-03-12 16:23:09 +00:00
public function getModems():ReadOnlyArray<Modem> {
2022-02-24 18:47:27 +00:00
return this.modes;
2021-12-20 00:55:30 +00:00
}
2022-03-05 01:41:30 +00:00
public function getModem(addr: String): Null<Modem> {
return this.modes.find(item -> item.getAddr() == addr);
}
2022-02-24 18:47:27 +00:00
private function findModems():Void {
var allModems = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "modem");
this.modes = allModems.map(s -> return new Modem((cc.Peripheral.wrap(s) : Dynamic), s));
2021-12-20 00:55:30 +00:00
}
2022-03-05 02:01:26 +00:00
/**
Get all connected drives.
**/
2022-03-12 16:23:09 +00:00
public function getDrives(): ReadOnlyArray<Drive> {
2022-03-05 02:01:26 +00:00
return this.drives;
}
public function getDrive(addr: String): Null<Drive> {
return this.drives.find(item -> item.getAddr() == addr);
}
private function findDrives() {
var allDrives = cc.Peripheral.getNames().toArray().filter(s -> cc.Peripheral.getType(s) == "drive");
this.drives = allDrives.map(s -> return new Drive((cc.Peripheral.wrap(s) : Dynamic), s));
}
2022-03-12 21:03:32 +00:00
public function getRedstone(side: String): Redstone {
return this.redstone.find(item -> item.getAddr() == side);
}
2021-12-20 00:55:30 +00:00
}