added resources and buttons

This commit is contained in:
DaniTheSkunk 2022-11-08 00:46:08 +00:00
parent e10f9ae829
commit 31f8b55648
4 changed files with 138 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/out/

View File

@ -0,0 +1,52 @@
package com.danitheskunk.skunkgame;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.gfx.vt.Terminal;
import java.util.concurrent.Callable;
public class Button {
private final Vec2i pos;
private static final Vec2i SIZE = new Vec2i(32, 3);
private final Recti rect;
private final String label;
private int count;
private Runnable clickCallback;
public Button(Vec2i pos, String label) {
this.pos = pos;
this.label = label;
this.rect = new Recti(pos, SIZE);
this.count = 0;
}
public void render(Terminal term) {
var str = label;
if(count > 0) {
str = String.format("%s (%d)", label, count);
}
boolean hover = rect.contains(SkunkGame.mousePos);
if(hover) {
term.drawBoxDouble(rect);
} else {
term.drawBoxSingle(rect);
}
var off = (rect.getWidth() - str.length()) / 2;
term.drawString(Vec2i.add(rect.getPos(), new Vec2i(off, 1)), str);
}
public void onClick(Runnable callback) {
clickCallback = callback;
}
public void processClick(Vec2i pos) {
if(rect.contains(pos) && clickCallback != null) {
clickCallback.run();
}
}
public void setCount(int count) {
this.count = count;
}
}

View File

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

View File

@ -1,11 +1,88 @@
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;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.vt.Terminal;
import java.util.ArrayList;
import java.util.List;
public class SkunkGame extends BaseGame {
private Terminal term;
private double timeSinceLastUpdate;
private double timePerUpdate;
private Resources res;
public static Vec2i mousePos;
private List<Button> buttons;
public SkunkGame() {
super(new Vec2i(960, 540), "SkunkGame v0.0.0");
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();
timePerUpdate = 1.0;
buttons = new ArrayList<>();
var woodButton = new Button(new Vec2i(31, 0), "Gather Wood");
woodButton.onClick(() -> res.wood += 1.0);
buttons.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());
for(var button : buttons) {
button.render(term);
}
}
private void drawResources() {
term.drawString(new Vec2i(0, 0), "Wood:");
term.drawString(
new Vec2i(18, 0),
String.format("%d", (int) res.wood)
);
}
private void tick() {
}
@Override
protected void render(IRenderContext rc) {
drawScreen();
rc.drawTerminal(term);
}
@Override
protected void update(double delta) {
timeSinceLastUpdate -= delta;
if(timeSinceLastUpdate <= 0.0) {
tick();
timeSinceLastUpdate += timePerUpdate;
}
mousePos = Vec2i.div(window.getMousePos(), term.getFullCharSize());
if(window.isMouseClicked(0)) {
for(var button : buttons) {
button.processClick(mousePos);
}
}
}
public static void main(String[] args) {