Move hologram-related fields to proper class. Breaks existing database saves!

This commit is contained in:
Simon 2018-11-27 14:38:33 -05:00
parent b361583252
commit 54ed05b86c
5 changed files with 132 additions and 98 deletions

View File

@ -15,39 +15,14 @@ import org.bson.Document;
import org.json.simple.JSONObject;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.*;
public class Cubot extends GameObject implements Updatable, ControllableUnit, MessageReceiver,
Attackable, Rechargeable, HardwareHost {
private static final char MAP_INFO = 0x0080;
/**
* Hologram value that is displayed
* <br>TODO: Move to CubotHologram class
*/
private int hologram = 0;
/**
* Hologram string that is displayed
* <br>TODO: Move to CubotHologram class
*/
private String hologramString = "";
/**
* Hologram mode that was set during this tick
* <br>TODO: Move to CubotHologram class
*/
private HologramMode hologramMode = HologramMode.CLEARED;
/**
* Hologram mode at the end of the last tick
* <br>TODO: Move to CubotHologram class
*/
private HologramMode lastHologramMode = HologramMode.CLEARED;
/**
* Hologram color code. Format is handled by the client
* <br>TODO: Move to CubotHologram class
*/
private int hologramColor = 0;
/**
* Hit points
@ -148,29 +123,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
*/
private CPU cpu;
/**
* Display mode of the hologram hardware
* <br>TODO: move this inside CubotHologram class
*/
public enum HologramMode {
/**
* Display nothing
*/
CLEARED,
/**
* Display value as hexadecimal in format 0x0000
*/
HEX,
/**
* Display string
*/
STRING,
/**
* Display value as decimal
*/
DEC
}
public enum ConsoleMode {
/**
* Used by the ComPort hardware - clears the console screen (client-side)
@ -247,8 +199,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
currentAction = Action.IDLE;
//Same principle for hologram
lastHologramMode = hologramMode;
hologramMode = HologramMode.CLEARED;
//And the console
lastConsoleMode = consoleMode;
@ -260,6 +210,10 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
//And the status..
lastStatus = currentStatus;
currentStatus = 0;
for (HardwareModule module : hardwareAddresses.values()) {
module.update();
}
}
@Override
@ -272,16 +226,19 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
json.put("hp", hp);
json.put("shield", shield);
json.put("action", lastAction.ordinal());
json.put("holo", hologram);
json.put("holoStr", hologramString);
json.put("holoMode", lastHologramMode.ordinal());
json.put("holoC", hologramColor);
json.put("energy", energy);
if (parent != null) {
json.put("parent", parent.getUsername()); //Only used client-side for now
}
for (HardwareModule module : hardwareAddresses.values()) {
JSONObject hwJson = module.jsonSerialise();
if (hwJson != null) {
json.put(module.getClass().getName(), hwJson);
}
}
return json;
}
@ -293,10 +250,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
dbObject.put("hp", hp);
dbObject.put("shield", shield);
dbObject.put("action", lastAction.ordinal());
dbObject.put("holo", hologram);
dbObject.put("holoStr", hologramString);
dbObject.put("holoMode", lastHologramMode.ordinal());
dbObject.put("holoC", hologramColor);
dbObject.put("energy", energy);
if (parent != null) {
@ -331,17 +284,17 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
clearKeyboardBuffer();
consoleMessagesBuffer.clear();
lastConsoleMessagesBuffer.clear();
hologramColor = 0;
currentStatus = 0;
lastStatus = 0;
addStatus(CubotStatus.FACTORY_NEW);
for (HardwareModule module : hardwareAddresses.values()) {
module.reset();
}
}
@Override
public boolean onDeadCallback() {
//TODO make death event instead
// LogManager.LOGGER.info(getParent().getUsername() + "'s Cubot died");
reset();
//Teleport to spawn point
@ -401,15 +354,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
return currentAction;
}
public void setHologram(int hologram) {
this.hologram = hologram;
}
public void setHologramString(String hologramString) {
this.hologramString = hologramString;
}
public int getEnergy() {
return energy;
}
@ -494,10 +438,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
return false;
}
public void setHologramMode(HologramMode hologramMode) {
this.hologramMode = hologramMode;
}
@Override
public void setAction(Action action) {
currentAction = action;
@ -527,10 +467,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
this.consoleMode = consoleMode;
}
public void setHologramColor(int hologramColor) {
this.hologramColor = hologramColor;
}
public void addStatus(CubotStatus status) {
currentStatus |= status.val;

View File

@ -3,15 +3,14 @@ package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document;
import org.json.simple.JSONObject;
public class CubotHologram extends CubotHardwareModule {
/**
* Hardware ID (Should be unique)
*/
static final char HWID = 0x0009;
public static final int DEFAULT_ADDRESS = 9;
private static final int HOLO_CLEAR = 0;
@ -22,12 +21,27 @@ public class CubotHologram extends CubotHardwareModule {
private static final int STR_MAX_LEN = 8;
private int displayValue = 0;
private String displayString = "";
private HologramMode mode = HologramMode.CLEARED;
private HologramMode lastMode = HologramMode.CLEARED;
/**
* Hologram color code. Format is handled by the client
*/
private int displayColor = 0;
public CubotHologram(Cubot cubot) {
super(cubot);
}
public CubotHologram(Document document, ControllableUnit cubot) {
super(document, cubot);
displayValue = document.getInteger("value");
displayColor = document.getInteger("color");
displayString = document.getString("string");
mode = HologramMode.values()[document.getInteger("mode")];
}
@Override
@ -36,11 +50,10 @@ public class CubotHologram extends CubotHardwareModule {
char a = getCpu().getRegisterSet().getRegister("A").getValue();
if (a == HOLO_CLEAR) {
cubot.setHologramMode(Cubot.HologramMode.CLEARED);
mode = HologramMode.CLEARED;
} else if (a == HOLO_DISPLAY_HEX) {
char b = getCpu().getRegisterSet().getRegister("B").getValue();
cubot.setHologram(b);
cubot.setHologramMode(Cubot.HologramMode.HEX);
displayValue = getCpu().getRegisterSet().getRegister("B").getValue();
mode = HologramMode.HEX;
} else if (a == HOLO_DISPLAY_STRING) {
char x = getCpu().getRegisterSet().getRegister("X").getValue();
//Display zero-terminated string starting at X (max 8 chars)
@ -58,13 +71,12 @@ public class CubotHologram extends CubotHardwareModule {
}
}
cubot.setHologramString(holoString.toString());
cubot.setHologramMode(Cubot.HologramMode.STRING);
displayString = holoString.toString();
mode = HologramMode.STRING;
} else if (a == HOLO_DISPLAY_DEC) {
//Display decimal number
char b = getCpu().getRegisterSet().getRegister("B").getValue();
cubot.setHologram(b);
cubot.setHologramMode(Cubot.HologramMode.DEC);
displayValue = getCpu().getRegisterSet().getRegister("B").getValue();
mode = HologramMode.DEC;
} else if (a == HOLO_DISPLAY_COLOR) {
@ -72,7 +84,7 @@ public class CubotHologram extends CubotHardwareModule {
int b = getCpu().getRegisterSet().getRegister("B").getValue();
int c = getCpu().getRegisterSet().getRegister("C").getValue();
cubot.setHologramColor((c | (b << 16))); //B:C
displayColor = (c | (b << 16)); //B:C
}
}
@ -83,4 +95,69 @@ public class CubotHologram extends CubotHardwareModule {
return HWID;
}
@Override
public Document mongoSerialise() {
Document document = super.mongoSerialise();
document.put("color", displayColor);
document.put("value", displayValue);
document.put("string", displayString);
document.put("mode", lastMode.ordinal());
return document;
}
@Override
public JSONObject debugJsonSerialise() {
JSONObject json = jsonSerialise();
json.put("lastmode", mode);
return json;
}
@Override
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();
json.put("color", displayColor);
json.put("value", displayValue);
json.put("string", displayString);
json.put("mode", lastMode.ordinal());
return json;
}
private enum HologramMode {
/**
* Display nothing
*/
CLEARED,
/**
* Display value as hexadecimal in format 0x0000
*/
HEX,
/**
* Display string
*/
STRING,
/**
* Display value as decimal
*/
DEC
}
@Override
public void reset() {
displayValue = 0;
displayColor = 0;
displayString = "";
mode = HologramMode.CLEARED;
}
@Override
public void update() {
lastMode = mode;
mode = HologramMode.CLEARED;
}
}

View File

@ -2,11 +2,13 @@ package net.simon987.server.assembly;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
import org.json.simple.JSONObject;
public abstract class HardwareModule implements MongoSerializable {
public abstract class HardwareModule implements MongoSerializable, JSONSerialisable {
private CPU cpu;
@ -35,6 +37,25 @@ public abstract class HardwareModule implements MongoSerializable {
@Override
public String toString() {
return String.format("{%s}", getClass().getSimpleName());
JSONObject hwJson = jsonSerialise();
return String.format("{%s: {%s}}", getClass().getSimpleName(), hwJson == null ? "" : hwJson);
}
public void reset() {
}
public void update() {
}
@Override
public JSONObject jsonSerialise() {
return null;
}
@Override
public JSONObject debugJsonSerialise() {
return null;
}
}

View File

@ -1023,7 +1023,8 @@ var Cubot = (function (_super) {
this.makeLaserAttack();
}
this.updateDirection();
this.updateHologram(json.holoMode, json.holoC, json.holo, json.holoStr);
var holoHw = json["net.simon987.cubotplugin.CubotHologram"];
this.updateHologram(holoHw.mode, holoHw.color, holoHw.value, holoHw.string);
this.setShield(this.shield > 0);
};
Cubot.prototype.updateHologram = function (holoMode, holoColor, holoValue, holoStr) {
@ -1579,7 +1580,6 @@ var FluidTile = (function (_super) {
}(Tile));
var MagneticTile = (function (_super) {
__extends(MagneticTile, _super);
function MagneticTile(x, y) {
var _this = _super.call(this, x, y, config.magneticSprite, 0) || this;
_this.baseTint = 0xFFFFFF;
@ -1588,7 +1588,6 @@ var MagneticTile = (function (_super) {
_this.tileType = "Magnetic tile";
return _this;
}
MagneticTile.prototype.onHover = function () {
mar.game.add.tween(this).to({isoZ: this.baseZ + 30}, 200, Phaser.Easing.Quadratic.InOut, true);
mar.tileIndicator.tileX = this.tileX;

View File

@ -326,7 +326,8 @@ class Cubot extends GameObject {
}
this.updateDirection();
this.updateHologram(json.holoMode, json.holoC, json.holo, json.holoStr);
let holoHw = json["net.simon987.cubotplugin.CubotHologram"];
this.updateHologram(holoHw.mode, holoHw.color, holoHw.value, holoHw.string);
//Update shield
this.setShield(this.shield > 0)