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