Skip to content

Commit

Permalink
feat: added system for signal lights in STW
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Oct 23, 2023
1 parent 18db0b8 commit f20451d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/troblecodings/signals/core/OSSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.troblecodings.signals.core;

import com.troblecodings.signals.signalbox.MainSignalIdentifier.SignalState;

@FunctionalInterface
public interface OSSupplier<T> {

T get(final SignalState type);

}
32 changes: 22 additions & 10 deletions src/main/java/com/troblecodings/signals/enums/EnumGuiMode.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.troblecodings.signals.enums;

import java.util.function.Supplier;

import com.troblecodings.core.ReadBuffer;
import com.troblecodings.guilib.ecs.entitys.render.UILines;
import com.troblecodings.guilib.ecs.entitys.render.UITexture;
import com.troblecodings.signals.core.OSSupplier;
import com.troblecodings.signals.guis.UISignalBoxTile;
import com.troblecodings.signals.signalbox.MainSignalIdentifier.SignalState;

public enum EnumGuiMode {
STRAIGHT(new float[] {
Expand All @@ -14,29 +14,41 @@ public enum EnumGuiMode {
0, 0.5f, 0.5f, 1
}), END(new float[] {
0.9f, 0.2f, 0.9f, 0.8f
}), PLATFORM(() -> new UILines(new float[] {
}), PLATFORM((state) -> new UILines(new float[] {
0, 0.15f, 1, 0.15f
}, 3)), BUE(new float[] {
0.3f, 0, 0.3f, 1, 0.7f, 0, 0.7f, 1
}), HP(0), VP(1), RS(2), RA10(3), SH2(4),
IN_CONNECTION(() -> new UITexture(UISignalBoxTile.ARROW_ICON)),
OUT_CONNECTION(() -> new UITexture(UISignalBoxTile.ARROW_ICON));
}), HP(0, true), VP(1, true), RS(2, true), RA10(3), SH2(4),
IN_CONNECTION((_u) -> new UITexture(UISignalBoxTile.ARROW_ICON)),
OUT_CONNECTION((_u) -> new UITexture(UISignalBoxTile.ARROW_ICON));

/**
* Naming
*/

public final Supplier<Object> consumer;
public final OSSupplier<Object> consumer;

private EnumGuiMode(final int id) {
this(() -> new UITexture(UISignalBoxTile.ICON, id * 0.2, 0, id * 0.2 + 0.2, 0.5));
this((_u) -> new UITexture(UISignalBoxTile.ICON, id * 0.2, 0, id * 0.2 + 0.2, 0.5));
}

private EnumGuiMode(final int id, final boolean unused) {
this((state) -> {
if (state.equals(SignalState.RED)) {
return new UITexture(UISignalBoxTile.SIGNALS, id * 2 * 0.1666, 0,
id * 2 * 0.1666 + 0.1666, 1);
} else {
return new UITexture(UISignalBoxTile.SIGNALS, (id + 0.5) * 2 * 0.1666, 0,
(id + 0.5) * 2 * 0.1666 + 0.1666, 1);
}
});
}

private EnumGuiMode(final float[] array) {
this(() -> new UILines(array, 2));
this((_u) -> new UILines(array, 2));
}

private EnumGuiMode(final Supplier<Object> consumer) {
private EnumGuiMode(final OSSupplier<Object> consumer) {
this.consumer = consumer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

public class ContainerSignalBox extends ContainerBase implements UIClientSync {

protected final List<ModeIdentifier> greenSignals = new ArrayList<>();
protected final Map<Point, List<ModeSet>> greenSignals = new HashMap<>();
protected final Map<BlockPos, List<SubsidiaryState>> possibleSubsidiaries = new HashMap<>();
protected final Map<Point, Map<ModeSet, SubsidiaryEntry>> enabledSubsidiaryTypes = new HashMap<>();
protected final List<Map.Entry<Point, Point>> nextPathways = new ArrayList<>();
Expand All @@ -48,6 +48,7 @@ public class ContainerSignalBox extends ContainerBase implements UIClientSync {
private SignalBoxTileEntity tile;
private Consumer<String> infoUpdates;
private Consumer<List<SignalBoxNode>> colorUpdates;
private Consumer<List<Point>> signalUpdates;

public ContainerSignalBox(final GuiInfo info) {
super(info);
Expand Down Expand Up @@ -157,7 +158,10 @@ public void deserializeClient(final ReadBuffer buffer) {
}
final int greenSignalsSize = buffer.getInt();
for (int i = 0; i < greenSignalsSize; i++) {
greenSignals.add(ModeIdentifier.of(buffer));
final ModeIdentifier identifier = ModeIdentifier.of(buffer);
final List<ModeSet> greenSignals = this.greenSignals
.computeIfAbsent(identifier.point, _u -> new ArrayList<>());
greenSignals.add(identifier.mode);
}
update();
break;
Expand Down Expand Up @@ -196,16 +200,24 @@ public void deserializeClient(final ReadBuffer buffer) {
break;
}
case SET_SIGNALS: {
final List<Point> pointUpdates = new ArrayList<>();
final int redSignalSize = buffer.getByteToUnsignedInt();
for (int i = 0; i < redSignalSize; i++) {
greenSignals.remove(ModeIdentifier.of(buffer));
final ModeIdentifier identifier = ModeIdentifier.of(buffer);
greenSignals.remove(identifier.point);
pointUpdates.add(identifier.point);
}
final int greenSignalSize = buffer.getByteToUnsignedInt();
for (int i = 0; i < greenSignalSize; i++) {
final ModeIdentifier modeIdentifier = ModeIdentifier.of(buffer);
if (!greenSignals.contains(modeIdentifier))
greenSignals.add(modeIdentifier);
if (!greenSignals.containsKey(modeIdentifier.point)) {
final List<ModeSet> greenSignals = this.greenSignals
.computeIfAbsent(modeIdentifier.point, _u -> new ArrayList<>());
greenSignals.add(modeIdentifier.mode);
}
pointUpdates.add(modeIdentifier.point);
}
signalUpdates.accept(pointUpdates);
break;
}
default:
Expand Down Expand Up @@ -411,4 +423,8 @@ protected void setInfoConsumer(final Consumer<String> consumer) {
protected void setColorUpdater(final Consumer<List<SignalBoxNode>> updater) {
this.colorUpdates = updater;
}

protected void setSignalUpdater(final Consumer<List<Point>> updater) {
this.signalUpdates = updater;
}
}
17 changes: 16 additions & 1 deletion src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public GuiSignalBox(final GuiInfo info) {
this.container = (ContainerSignalBox) info.base;
container.setInfoConsumer(this::infoUpdate);
container.setColorUpdater(this::applyColorChanges);
container.setSignalUpdater(this::updateSignals);
this.info = info;
}

Expand All @@ -130,6 +131,19 @@ public void infoUpdate(final String errorString) {
return;
}

public void updateSignals(final List<Point> updated) {
updated.forEach(point -> {
final UISignalBoxTile tile = allTiles.get(point);
tile.setGreenSignals(container.greenSignals.getOrDefault(point, new ArrayList<>()));
tile.getNode().forEach(mode -> {
if (mode.mode.equals(EnumGuiMode.HP) || mode.mode.equals(EnumGuiMode.VP)
|| mode.mode.equals(EnumGuiMode.RS)) {
tile.updateModeSet(mode);
}
});
});
}

protected void resetTileSelection() {
colors.values().forEach(color -> color.getParent().remove(color));
colors.clear();
Expand Down Expand Up @@ -806,7 +820,8 @@ private void initializeFieldTemplate(final BiConsumer<UIEntity, UISignalBoxTile>
if (node == null) {
node = new SignalBoxNode(name);
}
final UISignalBoxTile sbt = new UISignalBoxTile(node);
final UISignalBoxTile sbt = new UISignalBoxTile(node,
container.greenSignals.getOrDefault(name, new ArrayList<>()));
if (!node.isEmpty())
allTiles.put(name, sbt);
tile.add(sbt);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/troblecodings/signals/guis/SidePanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.troblecodings.signals.guis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -168,7 +169,7 @@ public void updateNextNode(final int selection, final int rotation) {
final SignalBoxNode node = new SignalBoxNode(new Point(-1, -1));
final EnumGuiMode modes = EnumGuiMode.values()[selection];
node.add(new ModeSet(modes, Rotation.values()[rotation]));
final UISignalBoxTile sbt = new UISignalBoxTile(node);
final UISignalBoxTile sbt = new UISignalBoxTile(node, new ArrayList<>());
preview.add(sbt);
preview.add(new UIBorder(new UIEntity().getBasicTextColor()));

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/troblecodings/signals/guis/UIMenu.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.troblecodings.signals.guis;

import java.util.ArrayList;
import java.util.function.BiConsumer;

import com.troblecodings.guilib.ecs.entitys.DrawInfo;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void postDraw(final DrawInfo info) {
preview.add(new UIColor(0xFFAFAFAF));
final SignalBoxNode node = new SignalBoxNode(new Point(-1, -1));
node.add(new ModeSet(mode, Rotation.values()[this.rotation]));
final UISignalBoxTile sbt = new UISignalBoxTile(node);
final UISignalBoxTile sbt = new UISignalBoxTile(node, new ArrayList<>());
preview.add(sbt);
preview.setHeight(20);
preview.setWidth(20);
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/com/troblecodings/signals/guis/UISignalBoxTile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.troblecodings.signals.guis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.troblecodings.guilib.ecs.entitys.UIComponent;
Expand All @@ -11,6 +13,7 @@
import com.troblecodings.guilib.ecs.entitys.transform.UIIndependentTranslate;
import com.troblecodings.guilib.ecs.entitys.transform.UIRotate;
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.signalbox.MainSignalIdentifier.SignalState;
import com.troblecodings.signals.signalbox.ModeSet;
import com.troblecodings.signals.signalbox.Point;
import com.troblecodings.signals.signalbox.SignalBoxNode;
Expand All @@ -24,17 +27,32 @@ public class UISignalBoxTile extends UIComponentEntity {
"gui/textures/symbols.png");
public static final ResourceLocation ARROW_ICON = new ResourceLocation(OpenSignalsMain.MODID,
"gui/textures/connection.png");
public static final ResourceLocation SIGNALS = new ResourceLocation(OpenSignalsMain.MODID,
"gui/textures/signals.png");

private SignalBoxNode node;
private final Map<ModeSet, UIEntity> setToEntity = new HashMap<>();
private final List<ModeSet> greenSignals;

public UISignalBoxTile(final SignalBoxNode node) {
public UISignalBoxTile(final SignalBoxNode node, final List<ModeSet> greenSignals) {
super(new UIEntity());
this.node = node;
this.greenSignals = new ArrayList<>(greenSignals);
if (this.node != null)
this.node.forEach(this::localAdd);
}

public void setGreenSignals(final List<ModeSet> list) {
greenSignals.clear();
greenSignals.addAll(list);
}

public void updateModeSet(final ModeSet mode) {
localRemove(mode);
localAdd(mode);
update();
}

public void setNode(final SignalBoxNode node) {
if (this.node != null)
this.node.forEach(this::localRemove);
Expand All @@ -51,7 +69,8 @@ private void localAdd(final ModeSet modeSet) {
entity.add(rotation);
}
entity.add(new UIIndependentTranslate(0, 0, 1));
entity.add((UIComponent) modeSet.mode.consumer.get());
entity.add((UIComponent) modeSet.mode.consumer
.get(greenSignals.contains(modeSet) ? SignalState.GREEN : SignalState.RED));
this.entity.add(entity);
setToEntity.put(modeSet, entity);
this.entity.setVisible(!setToEntity.isEmpty());
Expand Down

0 comments on commit f20451d

Please sign in to comment.