gamejam2024/core/src/com/monjaro/gamejam/Game.java

100 lines
2.2 KiB
Java
Raw Normal View History

2024-04-20 12:58:06 +01:00
package com.monjaro.gamejam;
import com.badlogic.gdx.ApplicationAdapter;
2024-04-20 14:40:06 +01:00
import com.badlogic.gdx.Gdx;
2024-04-20 16:15:42 +01:00
import com.badlogic.gdx.Input;
2024-04-20 12:58:06 +01:00
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
2024-04-20 14:56:13 +01:00
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
2024-04-20 12:58:06 +01:00
public class Game extends ApplicationAdapter {
2024-04-20 14:40:06 +01:00
2024-04-20 14:56:13 +01:00
private final Set<Actor> actors = new HashSet<>();
2024-04-20 15:45:42 +01:00
private final List<Die> dice = new ArrayList<>();
2024-04-20 14:40:06 +01:00
private SpriteBatch batch;
private Texture img;
2024-04-20 14:56:13 +01:00
private final static int TICKS_PER_SECOND = 60;
2024-04-20 14:40:06 +01:00
private double tickProgress = 0;
2024-04-20 12:58:06 +01:00
@Override
2024-04-20 14:40:06 +01:00
public void create() {
2024-04-20 12:58:06 +01:00
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
2024-04-20 15:45:42 +01:00
2024-04-20 16:38:07 +01:00
Face.setBlankFaceSprite(new Texture("blank_die_face.png"));
Face.setPipSprite(new Texture("pip.png"));
for (int i = 1; i <= 5; i++) {
dice.add(new Die((i*80), 20, 64, 64));
2024-04-20 15:45:42 +01:00
}
2024-04-20 12:58:06 +01:00
}
2024-04-20 14:40:06 +01:00
public void tick() {
2024-04-20 16:15:42 +01:00
processInput();
2024-04-20 14:56:13 +01:00
actors.forEach(Actor::tick);
2024-04-20 16:15:42 +01:00
}
2024-04-20 16:15:42 +01:00
public void processInput() {
Input input = Gdx.input;
if (input.isKeyJustPressed(Input.Keys.R)) { //reroll dice that aren't locked
dice.stream().filter(d -> !d.isLocked()).forEach(Die::roll);
2024-04-20 16:15:42 +01:00
}
2024-04-20 16:34:16 +01:00
for (int i = 0; i < dice.size(); i++) { //lock dice, iterating over for each keycode
Die die = dice.get(i); //die iterator is looking at
int keyCode = Input.Keys.NUM_1 + i; //keycode for the current die, 1, 2...9, 0 on keyboard
if (input.isKeyJustPressed(keyCode)) { //if key corresponding to die has been pressed
die.setLocked(!die.isLocked()); //flip lock state
}
}
2024-04-20 14:40:06 +01:00
}
2024-04-20 12:58:06 +01:00
@Override
2024-04-20 14:40:06 +01:00
public void render() {
tickProgress += Gdx.graphics.getDeltaTime() * TICKS_PER_SECOND;
2024-04-20 14:40:06 +01:00
while (tickProgress >= 1) { //tick as many times as needed
tick();
tickProgress--;
}
2024-04-20 14:56:13 +01:00
ScreenUtils.clear(0, 0, 0, 1);
2024-04-20 12:58:06 +01:00
batch.begin();
2024-04-20 14:56:13 +01:00
actors.forEach(a -> a.render(batch));
2024-04-20 15:45:42 +01:00
//TODO debug
for (Die die : dice) {
2024-04-20 16:38:07 +01:00
die.render(batch);
2024-04-20 15:45:42 +01:00
}
//-----
2024-04-20 12:58:06 +01:00
batch.end();
}
@Override
2024-04-20 14:40:06 +01:00
public void dispose() {
2024-04-20 12:58:06 +01:00
batch.dispose();
img.dispose();
}
2024-04-20 14:40:06 +01:00
2024-04-20 14:56:13 +01:00
private void addActor(Actor actor) {
actors.add(actor);
}
private void removeActor(Actor actor) {
actors.remove(actor);
}
2024-04-20 12:58:06 +01:00
}