From 6ffcf0be3b57277a920ae71f95c3faa506533329 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Thu, 9 Jun 2022 23:34:52 -0500 Subject: [PATCH] Added a hack due to timing values updating slower without a print Java maps Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com> Co-authored-by: sigonasr2 --- src/sig/RabiClone.java | 109 +++++++++++++++-------------- src/sig/objects/LevelRenderer.java | 1 + src/sig/objects/Player.java | 71 ++++++++++--------- src/sig/utils/TimeUtils.java | 7 ++ 4 files changed, 103 insertions(+), 85 deletions(-) create mode 100644 src/sig/utils/TimeUtils.java diff --git a/src/sig/RabiClone.java b/src/sig/RabiClone.java index d953147..733f59a 100644 --- a/src/sig/RabiClone.java +++ b/src/sig/RabiClone.java @@ -14,7 +14,6 @@ import sig.engine.Point; import sig.map.Maps; import sig.objects.ConfigureControls; import sig.objects.EditorRenderer; -import sig.objects.Erinoah; import sig.objects.LevelRenderer; import sig.objects.Player; import sig.engine.Key; @@ -25,18 +24,19 @@ import sig.engine.PaletteColor; import java.awt.Toolkit; import java.awt.event.KeyEvent; -public class RabiClone{ - public static final String PROGRAM_NAME="RabiClone"; +public class RabiClone { + public static final String PROGRAM_NAME = "RabiClone"; + public static final String BLANK = "\0"; - public static int UPCOUNT=0; + public static int UPCOUNT = 0; public static Panel p; public static JFrame f; public static List OBJ = new ArrayList(); - public static int BASE_WIDTH=512; - public static int BASE_HEIGHT=288; - public static int SIZE_MULTIPLIER=1; + public static int BASE_WIDTH = 512; + public static int BASE_HEIGHT = 288; + public static int SIZE_MULTIPLIER = 1; public static Point MOUSE_POS; public static PaletteColor BACKGROUND_COLOR = PaletteColor.DARK_ORCHID; @@ -46,35 +46,35 @@ public class RabiClone{ public static Player player; public static Maps CURRENT_MAP = Maps.WORLD1; - public static Controller[] CONTROLLERS = new Controller[]{}; + public static Controller[] CONTROLLERS = new Controller[] {}; public static long lastControllerScan = System.currentTimeMillis(); - static long lastUpdate=System.nanoTime(); + static long lastUpdate = System.nanoTime(); final static long TARGET_FRAMETIME = 8333333l; - static long lastReportedTime=System.currentTimeMillis(); + static long lastReportedTime = System.currentTimeMillis(); + public static long TIME = 0; public static void main(String[] args) { Key.InitializeKeyConversionMap(); - - f = new JFrame(PROGRAM_NAME); f.setResizable(false); f.setUndecorated(true); - f.setSize(BASE_WIDTH,BASE_HEIGHT); //1024x576 (64x64) + f.setSize(BASE_WIDTH, BASE_HEIGHT); // 1024x576 (64x64) ChooseBestRatio(); p = new Panel(f); - MOUSE_POS=p.mousePos; - + MOUSE_POS = p.mousePos; + p.init(); - + f.add(p); f.addKeyListener(p); - f.setLocation((int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth()-f.getWidth())/2), (int)((Toolkit.getDefaultToolkit().getScreenSize().getHeight()-f.getHeight())/2)); + f.setLocation((int) ((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - f.getWidth()) / 2), + (int) ((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - f.getHeight()) / 2)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); @@ -87,13 +87,13 @@ public class RabiClone{ long dt = 0; CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().getControllers(); + while (true) { - dt += System.nanoTime()-lastGameTime; - lastGameTime=System.nanoTime(); + dt += System.nanoTime() - lastGameTime; + lastGameTime = System.nanoTime(); - - while (dt>=(1/244d)*1000000000l) { - final double updateMult = 1/244d; + while (dt >= (1 / 244d) * 1000000000l) { + final double updateMult = 1 / 244d; handleGameControllers(); KeyBind.poll(); @@ -101,7 +101,7 @@ public class RabiClone{ if (Key.isKeyHeld(KeyEvent.VK_F1)) { if (level_renderer instanceof EditorRenderer) { OBJ.remove(level_renderer); - OBJ.add(level_renderer=new LevelRenderer(p)); + OBJ.add(level_renderer = new LevelRenderer(p)); StartGame(); } } @@ -109,73 +109,78 @@ public class RabiClone{ if (!(level_renderer instanceof EditorRenderer)) { OBJ.clear(); ResetGame(); - OBJ.add(level_renderer=new EditorRenderer(p)); + OBJ.add(level_renderer = new EditorRenderer(p)); } } if (Key.isKeyHeld(KeyEvent.VK_F3)) { OBJ.clear(); ResetGame(); - OBJ.add(control_settings_menu=new ConfigureControls(p)); + OBJ.add(control_settings_menu = new ConfigureControls(p)); } - if (Key.isKeyHeld(KeyEvent.VK_F5)&&System.currentTimeMillis()-lastControllerScan>5000) { - CONTROLLERS=ControllerEnvironment.getDefaultEnvironment().rescanControllers(); + if (Key.isKeyHeld(KeyEvent.VK_F5) && System.currentTimeMillis() - lastControllerScan > 5000) { + CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().rescanControllers(); System.out.println(Arrays.toString(CONTROLLERS)); - lastControllerScan=System.currentTimeMillis(); + lastControllerScan = System.currentTimeMillis(); } - for (int i=0;i5000) { - System.out.println("WARNING! Game is lagging behind! Frames Behind: "+(dt/((1/244d)*1000000000l))); - lastReportedTime=System.currentTimeMillis(); + if (dt < (1 / 244d) * 1000000000l) { + lastReportedTime = System.currentTimeMillis(); + } else { + if (System.currentTimeMillis() - lastReportedTime > 5000) { + System.out.println( + "WARNING! Game is lagging behind! Frames Behind: " + (dt / ((1 / 244d) * 1000000000l))); + lastReportedTime = System.currentTimeMillis(); + } } + System.out.print(BLANK); //This is hackish. Removing this slows down the game by about 30%. The timer runs slower. ??? } } - private static void handleGameControllers() { - for (int i=0;ii?j-1:j)]=CONTROLLERS[i]; + Controller[] newArr = new Controller[CONTROLLERS.length - 1]; + for (int j = 0; j < CONTROLLERS.length; j++) { + if (j != i) { + newArr[(j > i ? j - 1 : j)] = CONTROLLERS[i]; } } - CONTROLLERS=newArr; + CONTROLLERS = newArr; } } } private static void ResetGame() { - player=null; - level_renderer=null; - control_settings_menu=null; - //System.gc(); + player = null; + level_renderer = null; + control_settings_menu = null; + // System.gc(); } private static void StartGame() { - //System.gc(); + // System.gc(); OBJ.add(player = new Player(p)); - OBJ.add(new Erinoah(p)); + //OBJ.add(new Erinoah(p)); } private static void ChooseBestRatio() { - while (f.getWidth()*(SIZE_MULTIPLIER+1)jump_fall_AnimationWaitTime){ + if(RabiClone.TIME-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){ setAnimatedSpr(Sprite.ERINA_JUMP_FALL); jump_slide_fall_StartAnimationTimer=-1; } break; case IDLE: - if (System.currentTimeMillis()-slidePressed<=slideBufferTime) { + if (RabiClone.TIME-slidePressed<=slideBufferTime) { performSlide(); break; } @@ -116,23 +119,23 @@ public class Player extends AnimatedObject{ //jump_velocity=-500; } if(jump_slide_fall_StartAnimationTimer==-1){ - jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); + jump_slide_fall_StartAnimationTimer = RabiClone.TIME; setAnimatedSpr(Sprite.ERINA_JUMP_RISE1); } - if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){ + if(RabiClone.TIME-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){ setAnimatedSpr(Sprite.ERINA_JUMP_RISE); } break; case SLIDE: horizontal_friction=0; if(jump_slide_fall_StartAnimationTimer==-1){ - jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); + jump_slide_fall_StartAnimationTimer = RabiClone.TIME; setAnimatedSpr(Sprite.ERINA_SLIDE1); } - if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>slide_AnimationWaitTime){ + if(RabiClone.TIME-jump_slide_fall_StartAnimationTimer>slide_AnimationWaitTime){ setAnimatedSpr(Sprite.ERINA_SLIDE); } - if(System.currentTimeMillis()-slide_time>slide_duration){ + if(RabiClone.TIME-slide_time>slide_duration){ if(KeyHeld(Action.MOVE_LEFT)){ facing_direction=LEFT; } @@ -140,6 +143,7 @@ public class Player extends AnimatedObject{ facing_direction=RIGHT; } state=State.IDLE; + slide_time3 = (System.nanoTime()-slide_time2); } if (KeyHeld(Action.MOVE_LEFT)&&!KeyHeld(Action.MOVE_RIGHT)) { if (facing_direction==LEFT&&x_velocity>-sliding_velocity*1.5|| @@ -162,7 +166,7 @@ public class Player extends AnimatedObject{ break; } prvState = state; - if (KeyHeld(Action.JUMP)&&System.currentTimeMillis()-spacebarPressed