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 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent c115ff4a7e
commit 6ffcf0be3b
  1. 105
      src/sig/RabiClone.java
  2. 1
      src/sig/objects/LevelRenderer.java
  3. 71
      src/sig/objects/Player.java
  4. 7
      src/sig/utils/TimeUtils.java

@ -14,7 +14,6 @@ import sig.engine.Point;
import sig.map.Maps; import sig.map.Maps;
import sig.objects.ConfigureControls; import sig.objects.ConfigureControls;
import sig.objects.EditorRenderer; import sig.objects.EditorRenderer;
import sig.objects.Erinoah;
import sig.objects.LevelRenderer; import sig.objects.LevelRenderer;
import sig.objects.Player; import sig.objects.Player;
import sig.engine.Key; import sig.engine.Key;
@ -25,18 +24,19 @@ import sig.engine.PaletteColor;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
public class RabiClone{ public class RabiClone {
public static final String PROGRAM_NAME="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 Panel p;
public static JFrame f; public static JFrame f;
public static List<Object> OBJ = new ArrayList<Object>(); public static List<Object> OBJ = new ArrayList<Object>();
public static int BASE_WIDTH=512; public static int BASE_WIDTH = 512;
public static int BASE_HEIGHT=288; public static int BASE_HEIGHT = 288;
public static int SIZE_MULTIPLIER=1; public static int SIZE_MULTIPLIER = 1;
public static Point MOUSE_POS; public static Point MOUSE_POS;
public static PaletteColor BACKGROUND_COLOR = PaletteColor.DARK_ORCHID; public static PaletteColor BACKGROUND_COLOR = PaletteColor.DARK_ORCHID;
@ -46,35 +46,35 @@ public class RabiClone{
public static Player player; public static Player player;
public static Maps CURRENT_MAP = Maps.WORLD1; 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(); public static long lastControllerScan = System.currentTimeMillis();
static long lastUpdate=System.nanoTime(); static long lastUpdate = System.nanoTime();
final static long TARGET_FRAMETIME = 8333333l; 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) { public static void main(String[] args) {
Key.InitializeKeyConversionMap(); Key.InitializeKeyConversionMap();
f = new JFrame(PROGRAM_NAME); f = new JFrame(PROGRAM_NAME);
f.setResizable(false); f.setResizable(false);
f.setUndecorated(true); f.setUndecorated(true);
f.setSize(BASE_WIDTH,BASE_HEIGHT); //1024x576 (64x64) f.setSize(BASE_WIDTH, BASE_HEIGHT); // 1024x576 (64x64)
ChooseBestRatio(); ChooseBestRatio();
p = new Panel(f); p = new Panel(f);
MOUSE_POS=p.mousePos; MOUSE_POS = p.mousePos;
p.init(); p.init();
f.add(p); f.add(p);
f.addKeyListener(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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); f.setVisible(true);
@ -87,13 +87,13 @@ public class RabiClone{
long dt = 0; long dt = 0;
CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().getControllers(); CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().getControllers();
while (true) {
dt += System.nanoTime()-lastGameTime;
lastGameTime=System.nanoTime();
while (true) {
dt += System.nanoTime() - lastGameTime;
lastGameTime = System.nanoTime();
while (dt>=(1/244d)*1000000000l) { while (dt >= (1 / 244d) * 1000000000l) {
final double updateMult = 1/244d; final double updateMult = 1 / 244d;
handleGameControllers(); handleGameControllers();
KeyBind.poll(); KeyBind.poll();
@ -101,7 +101,7 @@ public class RabiClone{
if (Key.isKeyHeld(KeyEvent.VK_F1)) { if (Key.isKeyHeld(KeyEvent.VK_F1)) {
if (level_renderer instanceof EditorRenderer) { if (level_renderer instanceof EditorRenderer) {
OBJ.remove(level_renderer); OBJ.remove(level_renderer);
OBJ.add(level_renderer=new LevelRenderer(p)); OBJ.add(level_renderer = new LevelRenderer(p));
StartGame(); StartGame();
} }
} }
@ -109,73 +109,78 @@ public class RabiClone{
if (!(level_renderer instanceof EditorRenderer)) { if (!(level_renderer instanceof EditorRenderer)) {
OBJ.clear(); OBJ.clear();
ResetGame(); ResetGame();
OBJ.add(level_renderer=new EditorRenderer(p)); OBJ.add(level_renderer = new EditorRenderer(p));
} }
} }
if (Key.isKeyHeld(KeyEvent.VK_F3)) { if (Key.isKeyHeld(KeyEvent.VK_F3)) {
OBJ.clear(); OBJ.clear();
ResetGame(); 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) { if (Key.isKeyHeld(KeyEvent.VK_F5) && System.currentTimeMillis() - lastControllerScan > 5000) {
CONTROLLERS=ControllerEnvironment.getDefaultEnvironment().rescanControllers(); CONTROLLERS = ControllerEnvironment.getDefaultEnvironment().rescanControllers();
System.out.println(Arrays.toString(CONTROLLERS)); System.out.println(Arrays.toString(CONTROLLERS));
lastControllerScan=System.currentTimeMillis(); lastControllerScan = System.currentTimeMillis();
} }
for (int i=0;i<OBJ.size();i++) { for (int i = 0; i < OBJ.size(); i++) {
OBJ.get(i).update(updateMult); OBJ.get(i).update(updateMult);
if (OBJ.get(i).isMarkedForDeletion()) { if (OBJ.get(i).isMarkedForDeletion()) {
OBJ.remove(i--); OBJ.remove(i--);
} }
} }
dt-=(1/244d)*1000000000l; dt -= (1 / 244d) * 1000000000l;
TIME += (1 / 244d) * 1000000000l;
// System.out.println(TIME);
} }
if (dt<(1/244d)*1000000000l) { if (dt < (1 / 244d) * 1000000000l) {
lastReportedTime=System.currentTimeMillis(); lastReportedTime = System.currentTimeMillis();
} else } else {
if (System.currentTimeMillis()-lastReportedTime>5000) { if (System.currentTimeMillis() - lastReportedTime > 5000) {
System.out.println("WARNING! Game is lagging behind! Frames Behind: "+(dt/((1/244d)*1000000000l))); System.out.println(
lastReportedTime=System.currentTimeMillis(); "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() { private static void handleGameControllers() {
for (int i=0;i<CONTROLLERS.length;i++) { for (int i = 0; i < CONTROLLERS.length; i++) {
if (CONTROLLERS[i].getType()==Controller.Type.KEYBOARD||CONTROLLERS[i].getType()==Controller.Type.MOUSE) { if (CONTROLLERS[i].getType() == Controller.Type.KEYBOARD
|| CONTROLLERS[i].getType() == Controller.Type.MOUSE) {
continue; continue;
} }
if (!CONTROLLERS[i].poll()) { if (!CONTROLLERS[i].poll()) {
Controller[] newArr = new Controller[CONTROLLERS.length-1]; Controller[] newArr = new Controller[CONTROLLERS.length - 1];
for (int j=0;j<CONTROLLERS.length;j++) { for (int j = 0; j < CONTROLLERS.length; j++) {
if (j!=i) { if (j != i) {
newArr[(j>i?j-1:j)]=CONTROLLERS[i]; newArr[(j > i ? j - 1 : j)] = CONTROLLERS[i];
} }
} }
CONTROLLERS=newArr; CONTROLLERS = newArr;
} }
} }
} }
private static void ResetGame() { private static void ResetGame() {
player=null; player = null;
level_renderer=null; level_renderer = null;
control_settings_menu=null; control_settings_menu = null;
//System.gc(); // System.gc();
} }
private static void StartGame() { private static void StartGame() {
//System.gc(); // System.gc();
OBJ.add(player = new Player(p)); OBJ.add(player = new Player(p));
OBJ.add(new Erinoah(p)); //OBJ.add(new Erinoah(p));
} }
private static void ChooseBestRatio() { private static void ChooseBestRatio() {
while (f.getWidth()*(SIZE_MULTIPLIER+1)<Toolkit.getDefaultToolkit().getScreenSize().getWidth()) { while (f.getWidth() * (SIZE_MULTIPLIER + 1) < Toolkit.getDefaultToolkit().getScreenSize().getWidth()) {
SIZE_MULTIPLIER++; SIZE_MULTIPLIER++;
} }
f.setSize(f.getWidth()*SIZE_MULTIPLIER,(int)((f.getWidth()*SIZE_MULTIPLIER)/1.77777777778d)); f.setSize(f.getWidth() * SIZE_MULTIPLIER, (int) ((f.getWidth() * SIZE_MULTIPLIER) / 1.77777777778d));
} }
} }

@ -42,6 +42,7 @@ public class LevelRenderer extends Object{
if (RabiClone.player!=null) { if (RabiClone.player!=null) {
Draw_Animated_Object(RabiClone.player,RabiClone.player.facing_direction?Transform.HORIZONTAL:Transform.NONE); Draw_Animated_Object(RabiClone.player,RabiClone.player.facing_direction?Transform.HORIZONTAL:Transform.NONE);
Draw_Text(4,4,new String(RabiClone.player.x_velocity),Font.PROFONT_12); Draw_Text(4,4,new String(RabiClone.player.x_velocity),Font.PROFONT_12);
Draw_Text(4,4+Font.PROFONT_12.getGlyphHeight(),new String(RabiClone.player.slide_time3),Font.PROFONT_12);
} }
} }

@ -10,34 +10,35 @@ import sig.map.Map;
import sig.map.Tile; import sig.map.Tile;
import sig.map.View; import sig.map.View;
import sig.objects.actor.State; import sig.objects.actor.State;
import sig.utils.TimeUtils;
public class Player extends AnimatedObject{ public class Player extends AnimatedObject{
final static double GRAVITY = 2600; final static double GRAVITY = 1300;
final static double NORMAL_FRICTION = 9600; final static double NORMAL_FRICTION = 6400;
final static double NORMAL_JUMP_VELOCITY = -450; final static double NORMAL_JUMP_VELOCITY = -300;
final static boolean LEFT = false; final static boolean LEFT = false;
final static boolean RIGHT = true; final static boolean RIGHT = true;
final static int jump_fall_AnimationWaitTime = 200; final static long jump_fall_AnimationWaitTime = TimeUtils.millisToNanos(200);
final static int slide_AnimationWaitTime = 100; final static long slide_AnimationWaitTime = TimeUtils.millisToNanos(100);
final static int slide_duration = 700; final static long slide_duration = TimeUtils.millisToNanos(700);
final static double WALKING_SPEED_LIMIT = 246; final static double WALKING_SPEED_LIMIT = 164;
double y_acceleration = GRAVITY; double y_acceleration = GRAVITY;
double y_acceleration_limit = 150; double y_acceleration_limit = 100;
double x_acceleration = 0; double x_acceleration = 0;
double x_acceleration_limit = 150; double x_acceleration_limit = 100;
double x_velocity = 0; double x_velocity = 0;
double x_velocity_limit = 369; double x_velocity_limit = 246;
double y_velocity = 7.5; double y_velocity = 5;
double y_velocity_limit = 750; double y_velocity_limit = 500;
double sliding_velocity = 246; double sliding_velocity = 164;
double sliding_acceleration = 180; double sliding_acceleration = 120;
double horizontal_drag = 3000; double horizontal_drag = 2000;
double horizontal_friction = NORMAL_FRICTION; double horizontal_friction = NORMAL_FRICTION;
double horizontal_air_drag = 1200; double horizontal_air_drag = 800;
double horizontal_air_friction = 270; double horizontal_air_friction = 180;
double jump_velocity = NORMAL_JUMP_VELOCITY; double jump_velocity = NORMAL_JUMP_VELOCITY;
@ -54,12 +55,14 @@ public class Player extends AnimatedObject{
boolean spacebarReleased = true; boolean spacebarReleased = true;
boolean facing_direction = RIGHT; boolean facing_direction = RIGHT;
long spacebarPressed = System.currentTimeMillis(); long spacebarPressed = RabiClone.TIME;
long jump_slide_fall_StartAnimationTimer = -1; long jump_slide_fall_StartAnimationTimer = -1;
long slide_time = -1; long slide_time = -1;
int jumpHoldTime = 150; long slide_time2 = -1;
long slide_time3 = 0;
long jumpHoldTime = TimeUtils.millisToNanos(150);
final static int slideBufferTime = 200; final static long slideBufferTime = TimeUtils.millisToNanos(200);
long slidePressed = -1; long slidePressed = -1;
public Player(Panel panel) { public Player(Panel panel) {
@ -80,16 +83,16 @@ public class Player extends AnimatedObject{
break; break;
case FALLING: case FALLING:
if(prvState!=State.FALLING){ if(prvState!=State.FALLING){
jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); jump_slide_fall_StartAnimationTimer = RabiClone.TIME;
setAnimatedSpr(Sprite.ERINA_JUMP_FALL1); setAnimatedSpr(Sprite.ERINA_JUMP_FALL1);
} }
if(System.currentTimeMillis()-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){ if(RabiClone.TIME-jump_slide_fall_StartAnimationTimer>jump_fall_AnimationWaitTime){
setAnimatedSpr(Sprite.ERINA_JUMP_FALL); setAnimatedSpr(Sprite.ERINA_JUMP_FALL);
jump_slide_fall_StartAnimationTimer=-1; jump_slide_fall_StartAnimationTimer=-1;
} }
break; break;
case IDLE: case IDLE:
if (System.currentTimeMillis()-slidePressed<=slideBufferTime) { if (RabiClone.TIME-slidePressed<=slideBufferTime) {
performSlide(); performSlide();
break; break;
} }
@ -116,23 +119,23 @@ public class Player extends AnimatedObject{
//jump_velocity=-500; //jump_velocity=-500;
} }
if(jump_slide_fall_StartAnimationTimer==-1){ if(jump_slide_fall_StartAnimationTimer==-1){
jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); jump_slide_fall_StartAnimationTimer = RabiClone.TIME;
setAnimatedSpr(Sprite.ERINA_JUMP_RISE1); 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); setAnimatedSpr(Sprite.ERINA_JUMP_RISE);
} }
break; break;
case SLIDE: case SLIDE:
horizontal_friction=0; horizontal_friction=0;
if(jump_slide_fall_StartAnimationTimer==-1){ if(jump_slide_fall_StartAnimationTimer==-1){
jump_slide_fall_StartAnimationTimer = System.currentTimeMillis(); jump_slide_fall_StartAnimationTimer = RabiClone.TIME;
setAnimatedSpr(Sprite.ERINA_SLIDE1); 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); setAnimatedSpr(Sprite.ERINA_SLIDE);
} }
if(System.currentTimeMillis()-slide_time>slide_duration){ if(RabiClone.TIME-slide_time>slide_duration){
if(KeyHeld(Action.MOVE_LEFT)){ if(KeyHeld(Action.MOVE_LEFT)){
facing_direction=LEFT; facing_direction=LEFT;
} }
@ -140,6 +143,7 @@ public class Player extends AnimatedObject{
facing_direction=RIGHT; facing_direction=RIGHT;
} }
state=State.IDLE; state=State.IDLE;
slide_time3 = (System.nanoTime()-slide_time2);
} }
if (KeyHeld(Action.MOVE_LEFT)&&!KeyHeld(Action.MOVE_RIGHT)) { if (KeyHeld(Action.MOVE_LEFT)&&!KeyHeld(Action.MOVE_RIGHT)) {
if (facing_direction==LEFT&&x_velocity>-sliding_velocity*1.5|| if (facing_direction==LEFT&&x_velocity>-sliding_velocity*1.5||
@ -162,7 +166,7 @@ public class Player extends AnimatedObject{
break; break;
} }
prvState = state; prvState = state;
if (KeyHeld(Action.JUMP)&&System.currentTimeMillis()-spacebarPressed<jumpHoldTime) { if (KeyHeld(Action.JUMP)&&RabiClone.TIME-spacebarPressed<jumpHoldTime) {
y_velocity=jump_velocity; y_velocity=jump_velocity;
} }
} }
@ -197,7 +201,7 @@ public class Player extends AnimatedObject{
break; break;
case FALLING: case FALLING:
if (a==Action.SLIDE||a==Action.FALL) { if (a==Action.SLIDE||a==Action.FALL) {
slidePressed=System.currentTimeMillis(); slidePressed=RabiClone.TIME;
//System.out.println("Queue up slide."); //System.out.println("Queue up slide.");
} }
case JUMP: case JUMP:
@ -205,7 +209,7 @@ public class Player extends AnimatedObject{
jumpCount=0; jumpCount=0;
y_velocity = jump_velocity; y_velocity = jump_velocity;
spacebarReleased=false; spacebarReleased=false;
spacebarPressed=System.currentTimeMillis(); spacebarPressed=RabiClone.TIME;
} }
break; break;
case SLIDE: case SLIDE:
@ -223,7 +227,7 @@ public class Player extends AnimatedObject{
jumpCount--; jumpCount--;
y_velocity = jump_velocity; y_velocity = jump_velocity;
spacebarReleased=false; spacebarReleased=false;
spacebarPressed=System.currentTimeMillis(); spacebarPressed=RabiClone.TIME;
//System.out.println("Jump"); //System.out.println("Jump");
} }
} }
@ -241,7 +245,8 @@ public class Player extends AnimatedObject{
private void performSlide() { private void performSlide() {
slide_time = System.currentTimeMillis(); slide_time = RabiClone.TIME;
slide_time2 = System.nanoTime();
if(facing_direction){ if(facing_direction){
x_velocity=sliding_velocity; x_velocity=sliding_velocity;
} }

@ -0,0 +1,7 @@
package sig.utils;
public class TimeUtils {
public static long millisToNanos(long millis) {
return millis*1000000L;
}
}
Loading…
Cancel
Save