From 2a38a6828526970e945527f2590355dd0bf27b5c Mon Sep 17 00:00:00 2001 From: James <150948866+jameslaight@users.noreply.github.com> Date: Sat, 20 Apr 2024 14:56:13 +0100 Subject: [PATCH] modify game loop --- core/src/com/monjaro/gamejam/Actor.java | 4 +++- core/src/com/monjaro/gamejam/Dice.java | 7 ------- core/src/com/monjaro/gamejam/Die.java | 4 +++- core/src/com/monjaro/gamejam/Game.java | 28 ++++++++++++++++++++----- 4 files changed, 29 insertions(+), 14 deletions(-) delete mode 100644 core/src/com/monjaro/gamejam/Dice.java diff --git a/core/src/com/monjaro/gamejam/Actor.java b/core/src/com/monjaro/gamejam/Actor.java index 4b9aa3d..5172d3b 100644 --- a/core/src/com/monjaro/gamejam/Actor.java +++ b/core/src/com/monjaro/gamejam/Actor.java @@ -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); } diff --git a/core/src/com/monjaro/gamejam/Dice.java b/core/src/com/monjaro/gamejam/Dice.java deleted file mode 100644 index 54d016b..0000000 --- a/core/src/com/monjaro/gamejam/Dice.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.monjaro.gamejam; - -public class Dice { - static Texture faces; - - -} diff --git a/core/src/com/monjaro/gamejam/Die.java b/core/src/com/monjaro/gamejam/Die.java index 57d86b0..5d9b890 100644 --- a/core/src/com/monjaro/gamejam/Die.java +++ b/core/src/com/monjaro/gamejam/Die.java @@ -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) { } diff --git a/core/src/com/monjaro/gamejam/Game.java b/core/src/com/monjaro/gamejam/Game.java index 071881f..e441b05 100644 --- a/core/src/com/monjaro/gamejam/Game.java +++ b/core/src/com/monjaro/gamejam/Game.java @@ -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 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); + } + }