build tab: use a struct for tracking buildables

pull/1/head
Bronwen 2023-01-03 02:03:53 -05:00
parent cf77644f4b
commit 43c035ba5b
1 changed files with 35 additions and 31 deletions

View File

@ -18,7 +18,6 @@ import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.engine.scenegraph.IMouseCaptureArea;
import xyz.valnet.hadean.gameobjects.BottomBar;
import xyz.valnet.hadean.gameobjects.Camera;
import xyz.valnet.hadean.gameobjects.Terrain;
import xyz.valnet.hadean.gameobjects.inputlayer.BuildLayer;
import xyz.valnet.hadean.gameobjects.inputlayer.SelectionLayer;
import xyz.valnet.hadean.input.Button;
@ -31,7 +30,6 @@ import xyz.valnet.hadean.interfaces.ISelectable;
import xyz.valnet.hadean.interfaces.ISelectionChangeListener;
import xyz.valnet.hadean.util.Assets;
import xyz.valnet.hadean.util.Layers;
import xyz.valnet.hadean.util.Pair;
import xyz.valnet.hadean.util.SmartBoolean;
public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCaptureArea, IButtonListener {
@ -39,7 +37,6 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
private SelectionLayer selection;
private BuildLayer buildLayer;
private Camera camera;
private Terrain terrain;
private SmartBoolean opened;
private int width = 200;
@ -51,13 +48,19 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
private String selectedCategory = "";
private transient Constructor<? extends IBuildable> selectedBuildable = null;
private transient Map<String, List<Pair<String, Constructor<? extends IBuildable>>>> buildables = null;
private transient Map<Button, Constructor<? extends IBuildable>> buildableButtons = null;
private transient BuildableRecord selectedBuildable = null;
private transient Map<String, List<BuildableRecord>> buildables = null;
private transient Map<Button, BuildableRecord> buildableButtons = null;
private int height = 0;
private String selectedBuildableName = "";
private record BuildableRecord(
String name,
Constructor<? extends IBuildable> constructor,
int type
) {
}
private void calculateBuildables() {
try {
@ -81,10 +84,11 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
}
String category = annotation.category();
String name = annotation.name();
int type = annotation.type();
if(!buildables.containsKey(category))
buildables.put(category, new ArrayList<Pair<String, Constructor<? extends IBuildable>>>());
buildables.get(category).add(new Pair<String, Constructor<? extends IBuildable>>(name, constructor));
buildables.put(category, new ArrayList<BuildableRecord>());
buildables.get(category).add(new BuildableRecord(name, constructor, type));
System.out.println("Added " + category + " / " + name);
}
@ -107,7 +111,7 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
// draw the currently selected build item
Assets.flat.pushColor(new Vector4f(1f, 1f, 1f, 1.0f));
Vector2i topLeft = camera.world2screen(x, y).asInt();
Assets.font.drawString(selectedBuildableName, topLeft.x, topLeft.y - 20);
Assets.font.drawString(selectedBuildable.name, topLeft.x, topLeft.y - 20);
Assets.flat.swapColor(new Vector4f(1f, 1f, 1f, 0.5f));
for(int i = 0; i < w; i ++) for(int j = 0; j < h; j ++) {{
camera.draw(Layers.BUILD_INTERACTABLE, Assets.checkerBoard, x + i, y + j);
@ -122,7 +126,6 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
buildLayer = get(BuildLayer.class);
selection = get(SelectionLayer.class);
camera = get(Camera.class);
terrain = get(Terrain.class);
opened = new SmartBoolean(false, new SmartBoolean.IListener() {
@ -142,22 +145,23 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
selection.subscribe(this);
}
buildables = new HashMap<String, List<Pair<String, Constructor<? extends IBuildable>>>>();
buildableButtons = new HashMap<Button, Constructor<? extends IBuildable>>();
buildables = new HashMap<String, List<BuildableRecord>>();
buildableButtons = new HashMap<Button, BuildableRecord>();
calculateBuildables();
}
private List<Button> categoryButtons = new ArrayList<Button>();
private void selectBuildable(String name, Constructor<? extends IBuildable> constructor) {
if(selectedBuildable != null && constructor == null) {
buildLayer.deactiveate();
} else if (selectedBuildable == null && constructor != null) {
activateBuildLayer();
}
selectedBuildable = constructor;
selectedBuildableName = name;
private void deselectBuilding() {
if(selectedBuildable != null) buildLayer.deactiveate();
selectedBuildable = null;
}
private void selectBuildable(BuildableRecord buildableRecord) {
if(buildableRecord == null) deselectBuilding();
if (selectedBuildable == null) activateBuildLayer();
selectedBuildable = buildableRecord;
}
private void activateBuildLayer() {
@ -174,7 +178,7 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
public void build(int x1, int y1, int x2, int y2) {
if(selectedBuildable == null) return;
try {
IBuildable building = selectedBuildable.newInstance();
IBuildable building = selectedBuildable.constructor.newInstance();
if(building instanceof GameObject) {
add((GameObject) building);
}
@ -192,7 +196,7 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
opened.set(false);
buildLayer.deactiveate();
} else {
selectBuildable("", null);
deselectBuilding();
}
}
});
@ -278,7 +282,7 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
if(!opened.value()) return;
List<Pair<String, Constructor<? extends IBuildable>>> categoryBuildables = buildables.get(selectedCategory);
List<BuildableRecord> categoryBuildables = buildables.get(selectedCategory);
if(categoryBuildables == null) return;
int left = width + padding * 2;
@ -286,16 +290,16 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
int buttonWidth = 100;
int top = 576 - BottomBar.bottomBarHeight - padding - buttonHeight;
int i = 0;
for(Pair<String, Constructor<? extends IBuildable>> nameConstructorPair : categoryBuildables) {
for(BuildableRecord buildableRecord : categoryBuildables) {
int x = left + (buttonWidth + padding) * i;
int y = top;
int w = buttonWidth;
int h = buttonHeight;
i ++;
Button btn = new SimpleButton(nameConstructorPair.first(), x, y, w, h, Layers.GENERAL_UI_INTERACTABLE);
Button btn = new SimpleButton(buildableRecord.name, x, y, w, h, Layers.GENERAL_UI_INTERACTABLE);
btn.registerClickListener(this);
add(btn);
buildableButtons.put(btn, nameConstructorPair.second());
buildableButtons.put(btn, buildableRecord);
}
}
@ -303,11 +307,11 @@ public class BuildTab extends Tab implements ISelectionChangeListener, IMouseCap
public void click(Button target) {
if(categoryButtons.contains(target)) {
selectedCategory = target.getText();
selectBuildable("", null);
deselectBuilding();
constructItemButtons();
} else if(buildableButtons.containsKey(target)) {
Constructor<? extends IBuildable> newConstructor = buildableButtons.get(target);
selectBuildable(target.getText(), newConstructor);
BuildableRecord newBuildableRecord = buildableButtons.get(target);
selectBuildable(newBuildableRecord);
}
}