added buildings and refactored buttons

This commit is contained in:
DaniTheSkunk 2022-11-08 01:39:56 +00:00
parent 31f8b55648
commit cb33e97f0f
6 changed files with 175 additions and 29 deletions

View File

@ -0,0 +1,56 @@
package com.danitheskunk.skunkgame;
import com.danitheskunk.skunkworks.Vec2i;
public class Building {
private final String name;
private final String displayName;
private int count;
private Button button;
private final Cost cost;
public Building(String name, String displayName, Cost cost) {
this.name = name;
this.displayName = displayName;
this.cost = cost;
this.count = 0;
this.button = new Button(Vec2i.ZERO, displayName);
this.button.onClick(() -> {
if(canAfford()) {
buy();
}
});
}
public void setButtonPos(Vec2i pos) {
button.setPos(pos);
}
public boolean canAfford() {
return cost.canAfford();
}
public void buy() {
if(cost.canAfford()) {
cost.use();
count += 1;
button.setCount(count);
}
}
public boolean isButtonVisible() {
return count > 0 || canAfford();
}
public Button getButton() {
return button;
}
public String getName() {
return name;
}
public int getCount() {
return count;
}
}

View File

@ -7,9 +7,9 @@ import com.danitheskunk.skunkworks.gfx.vt.Terminal;
import java.util.concurrent.Callable;
public class Button {
private final Vec2i pos;
private Vec2i pos;
private static final Vec2i SIZE = new Vec2i(32, 3);
private final Recti rect;
private Recti rect;
private final String label;
private int count;
private Runnable clickCallback;
@ -49,4 +49,9 @@ public class Button {
public void setCount(int count) {
this.count = count;
}
public void setPos(Vec2i pos) {
this.pos = pos;
this.rect = new Recti(this.pos, SIZE);
}
}

View File

@ -0,0 +1,24 @@
package com.danitheskunk.skunkgame;
public class Cost {
private final CostElement[] elements;
public Cost(CostElement[] elements) {
this.elements = elements;
}
public boolean canAfford() {
for(var element : elements) {
if(element.getAmount() > SkunkGame.getRes(element.getRes())) {
return false;
}
}
return true;
}
public void use() {
for(var element : elements) {
SkunkGame.addRes(element.getRes(), -element.getAmount());
}
}
}

View File

@ -0,0 +1,19 @@
package com.danitheskunk.skunkgame;
public class CostElement {
private final String res;
private final double amount;
public CostElement(String res, double amount) {
this.res = res;
this.amount = amount;
}
public String getRes() {
return res;
}
public double getAmount() {
return amount;
}
}

View File

@ -1,7 +0,0 @@
package com.danitheskunk.skunkgame;
public class Resources {
public double wood;
public int den;
}

View File

@ -1,7 +1,6 @@
package com.danitheskunk.skunkgame;
import com.danitheskunk.skunkworks.BaseGame;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.WindowStretchMode;
import com.danitheskunk.skunkworks.gfx.Color;
@ -9,57 +8,102 @@ import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.vt.Terminal;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
public class SkunkGame extends BaseGame {
private Terminal term;
private double timeSinceLastUpdate;
private double timePerUpdate;
private Resources res;
public static Dictionary<String, Double> res;
private List<Building> buildings;
public static Vec2i mousePos;
private List<Button> buttons;
private List<Button> actionButtons;
public SkunkGame() {
super(new Vec2i(1280, 700), "SkunkGame v0.0.0");
window.setStretchMode(WindowStretchMode.ASPECT);
var font = window.loadFontTileset("fonts\\ega-8x14.png");
term = new Terminal(new Vec2i(160, 50), font);
res = new Resources();
res = new Hashtable<>();
buildings = new ArrayList<>();
timePerUpdate = 1.0;
buttons = new ArrayList<>();
actionButtons = new ArrayList<>();
res.put("wood", 0.0);
addBuilding(
"den",
"Den",
new CostElement[]{new CostElement("wood", 10)}
);
var woodButton = new Button(new Vec2i(31, 0), "Gather Wood");
woodButton.onClick(() -> res.wood += 1.0);
buttons.add(woodButton);
woodButton.onClick(() -> addRes("wood", 1.0));
actionButtons.add(woodButton);
var denButton = new Button(new Vec2i(31, 4), "Den");
denButton.onClick(() -> {
if(res.wood >= 10.0) {
res.wood -= 10.0;
res.den += 1;
denButton.setCount(res.den);
}
});
buttons.add(denButton);
}
private void drawScreen() {
term.clear(Color.BLACK);
drawResources();
term.drawVerticalSingleLine(new Vec2i(30, 0), term.getSize().getY());
var buttons = new ArrayList<Button>();
buttons.addAll(actionButtons);
for(var building : buildings) {
if(building.isButtonVisible()) {
buttons.add(building.getButton());
}
}
int i = 0;
for(var button : buttons) {
button.setPos(Vec2i.add(new Vec2i(31, 0),
Vec2i.mul(new Vec2i(0, 3), i)
));
button.render(term);
++i;
}
}
private void drawResources() {
term.drawString(new Vec2i(0, 0), "Wood:");
term.drawString(
new Vec2i(18, 0),
String.format("%d", (int) res.wood)
term.drawString(new Vec2i(18, 0),
String.format("%d", (int) getRes("wood"))
);
}
public static double getRes(String name) {
return res.get(name);
}
public static void setRes(String name, double count) {
res.put(name, count);
}
public static void addRes(String name, double count) {
setRes(name, getRes(name) + count);
}
private void addBuilding(
String name, String displayName, CostElement[] cost
) {
buildings.add(new Building(name, displayName, new Cost(cost)));
}
private Building getBuilding(String name) {
//todo: optimise
for(var building : buildings) {
if(building.getName().equals(name)) {
return building;
}
}
return null;
}
private void tick() {
}
@ -79,9 +123,14 @@ public class SkunkGame extends BaseGame {
}
mousePos = Vec2i.div(window.getMousePos(), term.getFullCharSize());
if(window.isMouseClicked(0)) {
for(var button : buttons) {
for(var button : actionButtons) {
button.processClick(mousePos);
}
for(var building : buildings) {
if(building.isButtonVisible()) {
building.getButton().processClick(mousePos);
}
}
}
}