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

66 lines
1.3 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 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 jdk.vm.ci.hotspot.JFR;
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 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 14:40:06 +01:00
public void tick() {
2024-04-20 14:56:13 +01:00
actors.forEach(Actor::tick);
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() {
2024-04-20 14:56:13 +01:00
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 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
}