added type to IPerph

This commit is contained in:
Djeeberjr 2023-06-26 19:05:48 +02:00
parent f124525d2d
commit 4dcc060e9a
8 changed files with 74 additions and 2 deletions

View File

@ -39,6 +39,10 @@ class Drive implements IPeripheral {
return this.addr;
}
public function getType():String {
return TYPE_NAME;
}
public inline function isDiskPresent(): Bool {
return this.native.isDiskPresent();
}

View File

@ -1,6 +1,9 @@
package kernel.peripherals;
class EnergyStorage implements IPeripheral{
import lib.exporter.ExportConfig;
import lib.exporter.IExportable;
class EnergyStorage implements IPeripheral implements IExportable{
public static inline final TYPE_NAME:String = "energyCell";
private final addr:String;
@ -22,4 +25,17 @@ class EnergyStorage implements IPeripheral{
public function getAddr():String {
return this.addr;
}
public function getType():String {
return TYPE_NAME;
}
public function export():ExportConfig {
return {
getDelegates: [
"energy" => _ -> Number(this.getEnergy()),
"capacity" => _ -> Number(this.getEnergyCapacity()),
],
}
}
}

View File

@ -2,4 +2,5 @@ package kernel.peripherals;
interface IPeripheral {
public function getAddr(): String;
public function getType(): String;
}

View File

@ -74,6 +74,10 @@ class Modem implements INetworkInterface implements IPeripheral {
return this.addr;
}
public function getType():String {
return TYPE_NAME;
}
public function getBaseRoutingCost():Int {
if (this.native.isWireless()){
return 2; // Prefere messages over cable

View File

@ -72,6 +72,31 @@ class Peripheral {
};
}
/**
Cast peripheral to a specific type.
This is a temporary solution, maybe forever.
**/
public function getFromType(addr: String, type: String): Null<IPeripheral> {
switch (type){
case Computer.TYPE_NAME:
return getComputer(addr);
case Screen.TYPE_NAME:
return getScreen(addr);
case Drive.TYPE_NAME:
return getDrive(addr);
case EnergyStorage.TYPE_NAME:
return getEnergyStorage(addr);
case Modem.TYPE_NAME:
return getModem(addr);
case Printer.TYPE_NAME:
return getPrinter(addr);
case "redstone":
return getRedstone(addr);
}
return null;
}
public function getScreen(addr: String): Null<Screen> {
var addr = safeGetAddr(addr, Screen.TYPE_NAME);
if (addr == null) return null;
@ -127,4 +152,14 @@ class Peripheral {
public function getAllEnergyStorages(): Array<EnergyStorage> {
return [ for (addr in findAddrByType(EnergyStorage.TYPE_NAME)) new EnergyStorage(addr)];
}
public function getComputer(addr: String): Null<Computer> {
var addr = safeGetAddr(addr, Computer.TYPE_NAME);
if (addr == null) return null;
return new Computer(addr);
}
public function getAllComputers(): Array<Computer> {
return [ for (addr in findAddrByType(Computer.TYPE_NAME)) new Computer(addr)];
}
}

View File

@ -19,6 +19,10 @@ class Printer implements IPeripheral {
return addr;
}
public function getType():String {
return TYPE_NAME;
}
public function write(text: String){
}

View File

@ -44,6 +44,7 @@ abstract BundleMask(Int) from cc.Colors.Color to cc.Colors.Color {
}
class Redstone implements IPeripheral implements IExportable {
public static inline final TYPE_NAME:String = "redstone"; // TODO: there is technically not a type for redstone.
public final onChange:Signal<Noise>;
@ -74,6 +75,10 @@ class Redstone implements IPeripheral implements IExportable {
return this.addr;
}
public function getType():String {
return TYPE_NAME;
}
private function updateState() {
this.analogInputState = cc.Redstone.getAnalogInput(this.addr);
this.bundleInputState = cc.Redstone.getBundledInput(this.addr);
@ -125,7 +130,6 @@ class Redstone implements IPeripheral implements IExportable {
public function export():ExportConfig {
return {
type: "redstone",
getDelegates: [
"input" => (_) -> {return Bool(this.getInput());},
"analog" => (_) -> {return Number(this.getAnalogInput());}

View File

@ -39,6 +39,10 @@ class Screen implements TermWriteable implements IPeripheral {
return this.addr;
}
public function getType():String {
return TYPE_NAME;
}
public function getTextScale():Float {
return nativ.getTextScale();
}