From f2a3ba9fd6d6420aa00c8ab010e0ec729bb1eb6c Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Mon, 23 May 2022 17:30:44 -0500 Subject: [PATCH] Isolate the draw loop Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- src/sig/RabiClone.java | 30 ++++++++++-------------------- src/sig/engine/GameEntity.java | 6 ++++++ src/sig/engine/Object.java | 34 ++++++++++++++++++++++++++++++++++ src/sig/objects/Player.java | 29 +++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 src/sig/engine/GameEntity.java create mode 100644 src/sig/engine/Object.java create mode 100644 src/sig/objects/Player.java diff --git a/src/sig/RabiClone.java b/src/sig/RabiClone.java index 634d6aa..8b70afe 100644 --- a/src/sig/RabiClone.java +++ b/src/sig/RabiClone.java @@ -2,14 +2,20 @@ package sig; import javax.swing.JFrame; import java.awt.event.KeyEvent; +import java.util.ArrayList; +import java.util.List; import sig.engine.Panel; +import sig.objects.Player; +import sig.engine.Object; public class RabiClone { public static final String PROGRAM_NAME="Sig's Java Project Template"; public static int UPCOUNT=0; public static Panel p; + + public static List OBJ = new ArrayList(); public static void main(String[] args) { JFrame f = new JFrame(PROGRAM_NAME); @@ -26,33 +32,17 @@ public class RabiClone { p.render(); + OBJ.add(new Player(p)); + long lastGameTime = System.nanoTime(); while (true) { long timePassed = System.nanoTime()-lastGameTime; lastGameTime=System.nanoTime(); double updateMult = timePassed/1000000000d; - if (KeyHeld(KeyEvent.VK_UP)) { - //System.out.println("Up Held: "+UPCOUNT+++" (+"+(float)(timePassed/1000000f)+"ms)"); - p.nanaY-=16*updateMult; - } - if (KeyHeld(KeyEvent.VK_DOWN)) { - //System.out.println("Up Held: "+UPCOUNT+++" (+"+(float)(timePassed/1000000f)+"ms)"); - p.nanaY+=16*updateMult; - } - if (KeyHeld(KeyEvent.VK_RIGHT)) { - //System.out.println("Up Held: "+UPCOUNT+++" (+"+(float)(timePassed/1000000f)+"ms)"); - p.nanaX+=16*updateMult; - System.out.println(p.nanaX); + for (Object o : OBJ) { + o.update(updateMult); } - if (KeyHeld(KeyEvent.VK_LEFT)) { - //System.out.println("Up Held: "+UPCOUNT+++" (+"+(float)(timePassed/1000000f)+"ms)"); - p.nanaX-=16*updateMult; - } - } } - private static Boolean KeyHeld(int key) { - return p.KEYS.getOrDefault(key,false); - } } diff --git a/src/sig/engine/GameEntity.java b/src/sig/engine/GameEntity.java new file mode 100644 index 0000000..df2cd1c --- /dev/null +++ b/src/sig/engine/GameEntity.java @@ -0,0 +1,6 @@ +package sig.engine; + +public interface GameEntity { + public void update(double updateMult); + public void draw(int[] p); +} diff --git a/src/sig/engine/Object.java b/src/sig/engine/Object.java new file mode 100644 index 0000000..c858e25 --- /dev/null +++ b/src/sig/engine/Object.java @@ -0,0 +1,34 @@ +package sig.engine; + +public abstract class Object implements GameEntity{ + double x,y; + Sprite spr; + Panel panel; + + protected Object(Panel panel) { + this.panel=panel; + } + + public double getX() { + return x; + } + public void setX(double x) { + this.x = x; + } + public double getY() { + return y; + } + public void setY(double y) { + this.y = y; + } + public Sprite getSprite() { + return spr; + } + public void setSprite(Sprite spr) { + this.spr = spr; + } + + protected boolean KeyHeld(int key) { + return panel.KEYS.getOrDefault(key,false); + } +} diff --git a/src/sig/objects/Player.java b/src/sig/objects/Player.java new file mode 100644 index 0000000..81c2a0e --- /dev/null +++ b/src/sig/objects/Player.java @@ -0,0 +1,29 @@ +package sig.objects; + +import sig.DrawLoop; +import sig.engine.Object; +import sig.engine.Panel; +import sig.engine.Sprite; +import java.awt.event.KeyEvent; + +public class Player extends Object{ + + public Player(Panel panel) { + super(panel); + this.setSprite(Sprite.NANA); + } + + + @Override + public void update(double updateMult) { + if (KeyHeld(KeyEvent.VK_RIGHT)) { + setX(getX()+32*updateMult); + } + } + + @Override + public void draw(int[] p) { + DrawLoop.Draw_Sprite(this.getX(), this.getY(), this.getSprite()); + } + +}