Isolate the draw loop

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent 121dc6acb6
commit f2a3ba9fd6
  1. 30
      src/sig/RabiClone.java
  2. 6
      src/sig/engine/GameEntity.java
  3. 34
      src/sig/engine/Object.java
  4. 29
      src/sig/objects/Player.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<Object> OBJ = new ArrayList<Object>();
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);
}
}

@ -0,0 +1,6 @@
package sig.engine;
public interface GameEntity {
public void update(double updateMult);
public void draw(int[] p);
}

@ -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);
}
}

@ -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());
}
}
Loading…
Cancel
Save