modify game loop

This commit is contained in:
James 2024-04-20 14:56:13 +01:00
parent 8e6527afa0
commit 2a38a68285
4 changed files with 29 additions and 14 deletions

View file

@ -1,9 +1,11 @@
package com.monjaro.gamejam;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public abstract class Actor {
public abstract void tick();
public abstract void render();
public abstract void render(SpriteBatch batch);
}

View file

@ -1,7 +0,0 @@
package com.monjaro.gamejam;
public class Dice {
static Texture faces;
}

View file

@ -1,5 +1,7 @@
package com.monjaro.gamejam;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Die extends Actor {
/*
@ -22,7 +24,7 @@ public class Die extends Actor {
}
@Override
public void render() {
public void render(SpriteBatch batch) {
}

View file

@ -5,13 +5,21 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import jdk.vm.ci.hotspot.JFR;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Game extends ApplicationAdapter {
private final Set<Actor> actors = new HashSet<>();
private SpriteBatch batch;
private Texture img;
private static int TICKS_PER_SECOND;
private final static int TICKS_PER_SECOND = 60;
private double tickProgress = 0;
@Override
@ -21,20 +29,22 @@ public class Game extends ApplicationAdapter {
}
public void tick() {
actors.forEach(Actor::tick);
}
@Override
public void render() {
Gdx.graphics.getDeltaTime();
tickProgress += Gdx.graphics.getDeltaTime() / TICKS_PER_SECOND;
while (tickProgress >= 1) { //tick as many times as needed
tick();
tickProgress--;
}
ScreenUtils.clear(1, 0, 0, 1);
ScreenUtils.clear(0, 0, 0, 1);
batch.begin();
batch.draw(img, 0, 0);
actors.forEach(a -> a.render(batch));
batch.end();
}
@ -44,4 +54,12 @@ public class Game extends ApplicationAdapter {
img.dispose();
}
private void addActor(Actor actor) {
actors.add(actor);
}
private void removeActor(Actor actor) {
actors.remove(actor);
}
}