Browse Source

Code Refactoring (Key Rebindings)

Hudriwudri 5 years ago
parent
commit
6c02aa3247

BIN
lib/SnuviEngine.jar


BIN
resources/config.bin


BIN
resources/config.txt


+ 6 - 1
src/pathgame/PathGame.java

@@ -40,7 +40,7 @@ public class PathGame implements IGame
     @Override
     public void tick()
     {
-        cam.tick(level);
+        cam.tick(level, gamestate);
 
         level.getMap().tick();
         mapRenderer.tick();
@@ -64,6 +64,11 @@ public class PathGame implements IGame
     @Override
     public void renderTick(Renderer r, float lag)
     {
+        if(gamestate.is(Gamestates.MENU))
+        {
+            lag = 0.0f;
+        }
+        
         TileMap map = level.getMap();
         Player player = level.getPlayer();
 

+ 2 - 2
src/pathgame/gameplay/Camera.java

@@ -13,7 +13,7 @@ public class Camera
     private float camOffsetX = 0.0f;
     private float camOffsetY = 0.0f;
 
-    public void tick(Level level)
+    public void tick(Level level, Gamestate gamestate)
     {
         lastCamOffsetX = camOffsetX;
         lastCamOffsetY = camOffsetY;
@@ -28,7 +28,7 @@ public class Camera
             scale /= 1.1f;
         }
 
-        if(!level.getPlayer().isMoving())
+        if(!level.getPlayer().isMoving() && gamestate.is(Gamestates.GAMEPLAY))
         {
             if(Keys.CAM_UP_KEY.isDown())
             {

+ 1 - 1
src/pathgame/gameplay/Gamestate.java

@@ -14,7 +14,7 @@ public class Gamestate
         return state;
     }
     
-    public boolean isGamestate(Gamestates gamestate)
+    public boolean is(Gamestates gamestate)
     {
         return state == gamestate;
     }

+ 25 - 41
src/pathgame/gameplay/Keys.java

@@ -1,9 +1,8 @@
 package pathgame.gameplay;
 
-import java.io.BufferedReader;
+import java.io.DataInputStream;
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
+import java.io.FileInputStream;
 import java.io.IOException;
 import me.hammerle.snuviengine.api.KeyBinding;
 import me.hammerle.snuviengine.api.KeyHandler;
@@ -12,64 +11,49 @@ import org.lwjgl.glfw.GLFW;
 public class Keys
 {
 
-    public static final KeyBinding UP_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_W);
-    public static final KeyBinding DOWN_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_S);
-    public static final KeyBinding LEFT_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_A);
-    public static final KeyBinding RIGHT_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_D);
-    public static final KeyBinding ZOOM_IN_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_I);
-    public static final KeyBinding ZOOM_OUT_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_O);
-    public static final KeyBinding CONFIRM_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_ENTER);
-    public static final KeyBinding ESCAPE_KEY;// = KeyHandler.register(GLFW.GLFW_KEY_ESCAPE);
+    public static final KeyBinding UP_KEY = KeyHandler.register(GLFW.GLFW_KEY_W);
+    public static final KeyBinding DOWN_KEY = KeyHandler.register(GLFW.GLFW_KEY_S);
+    public static final KeyBinding LEFT_KEY = KeyHandler.register(GLFW.GLFW_KEY_A);
+    public static final KeyBinding RIGHT_KEY = KeyHandler.register(GLFW.GLFW_KEY_D);
+    public static final KeyBinding ZOOM_IN_KEY = KeyHandler.register(GLFW.GLFW_KEY_I);
+    public static final KeyBinding ZOOM_OUT_KEY = KeyHandler.register(GLFW.GLFW_KEY_O);
+    public static final KeyBinding CONFIRM_KEY = KeyHandler.register(GLFW.GLFW_KEY_ENTER);
+    public static final KeyBinding ESCAPE_KEY = KeyHandler.register(GLFW.GLFW_KEY_ESCAPE);
     public static final KeyBinding CAM_UP_KEY = KeyHandler.register(GLFW.GLFW_KEY_UP);
     public static final KeyBinding CAM_DOWN_KEY = KeyHandler.register(GLFW.GLFW_KEY_DOWN);
     public static final KeyBinding CAM_LEFT_KEY = KeyHandler.register(GLFW.GLFW_KEY_LEFT);
     public static final KeyBinding CAM_RIGHT_KEY = KeyHandler.register(GLFW.GLFW_KEY_RIGHT);
     public static final KeyBinding TEST_KEY = KeyHandler.register(GLFW.GLFW_KEY_T);
 
-    public static final KeyBinding[] KEYS;
+    public static final KeyBinding[] KEYS = new KeyBinding[]
+    {
+        UP_KEY, DOWN_KEY, LEFT_KEY, RIGHT_KEY, ZOOM_IN_KEY, ZOOM_OUT_KEY, 
+        CONFIRM_KEY, ESCAPE_KEY, CAM_UP_KEY, CAM_DOWN_KEY, CAM_LEFT_KEY, 
+        CAM_RIGHT_KEY
+    };
+
     public static final String[] KEYNAMES =
     {
-        "Up Key", "Down Key", "Left Key", "Right Key", "Zoom In Key", "Zoom Out Key", "Confirm Key", "Escape Key", //"Cam Up Key", "Cam Down Key", "Cam Left Key", "Cam Right Key"
+        "Up Key", "Down Key", "Left Key", "Right Key", "Zoom In Key",
+        "Zoom Out Key", "Confirm Key", "Escape Key", "Cam Up Key", 
+        "Cam Down Key", "Cam Left Key", "Cam Right Key"
     };
 
     static
     {
-        String line[] = new String[KEYNAMES.length];
-        File f = new File("resources/config.txt");
-        System.out.println(f.getName());
+        File f = new File("resources/config.bin");
         if(f.exists())
         {
-            FileReader reader = null;
-            try
+            try(DataInputStream reader = new DataInputStream(new FileInputStream(f)))
             {
-                reader = new FileReader(f);
-                BufferedReader bReader = new BufferedReader(reader);
-                for(int i = 0; i < KEYNAMES.length; ++i)
+                for(int i = 0; i < KEYS.length; ++i)
                 {
-                    try
-                    {
-                        line[i] = bReader.readLine();
-                    }
-                    catch(IOException ex)
-                    {
-                    }
+                    KeyHandler.rebind(KEYS[i], reader.readInt());
                 }
             }
-            catch(FileNotFoundException ex)
+            catch(IOException ex)
             {
             }
         }
-        UP_KEY = KeyHandler.register(Integer.parseInt(line[0].substring(line[0].indexOf(":") + 2, line[0].length())));
-        DOWN_KEY = KeyHandler.register(Integer.parseInt(line[1].substring(line[1].indexOf(":") + 2, line[1].length())));
-        LEFT_KEY = KeyHandler.register(Integer.parseInt(line[2].substring(line[2].indexOf(":") + 2, line[2].length())));
-        RIGHT_KEY = KeyHandler.register(Integer.parseInt(line[3].substring(line[3].indexOf(":") + 2, line[3].length())));
-        ZOOM_IN_KEY = KeyHandler.register(Integer.parseInt(line[4].substring(line[4].indexOf(":") + 2, line[4].length())));
-        ZOOM_OUT_KEY = KeyHandler.register(Integer.parseInt(line[5].substring(line[5].indexOf(":") + 2, line[5].length())));
-        CONFIRM_KEY = KeyHandler.register(Integer.parseInt(line[6].substring(line[6].indexOf(":") + 2, line[6].length())));
-        ESCAPE_KEY = KeyHandler.register(Integer.parseInt(line[7].substring(line[7].indexOf(":") + 2, line[7].length())));
-        KEYS = new KeyBinding[]
-        {
-            UP_KEY, DOWN_KEY, LEFT_KEY, RIGHT_KEY, ZOOM_IN_KEY, ZOOM_OUT_KEY, CONFIRM_KEY, ESCAPE_KEY, //CAM_UP_KEY, CAM_DOWN_KEY, CAM_LEFT_KEY, CAM_RIGHT_KEY
-        };
     }
 }

+ 8 - 15
src/pathgame/gameplay/menu/BaseMenu.java

@@ -1,5 +1,6 @@
 package pathgame.gameplay.menu;
 
+import me.hammerle.snuviengine.api.KeyBinding;
 import pathgame.gameplay.Gamestate;
 import pathgame.gameplay.Keys;
 import pathgame.gameplay.Level;
@@ -29,16 +30,16 @@ public abstract class BaseMenu
     public int tick(Gamestate gamestate, Level level)
     {
         returnId = id;
-        if(isUpPressed())
+        if(isKeyPressed(Keys.UP_KEY) || isKeyPressed(Keys.CAM_UP_KEY))
         {
             int length = getOptions().length;
             index = ((index - 1) + length) % length;
         }
-        else if(isDownPressed())
+        else if(isKeyPressed(Keys.DOWN_KEY) || isKeyPressed(Keys.CAM_DOWN_KEY))
         {
             index = (index + 1) % getOptions().length;
         }
-        else if(isEnterPressed())
+        else if(isKeyPressed(Keys.CONFIRM_KEY))
         {
             getOptions()[index].run(gamestate, level);
         }
@@ -54,23 +55,15 @@ public abstract class BaseMenu
         returnId = id;
     }
             
-    private boolean isUpPressed()
+    public void resetIndex()
     {
-        return Keys.UP_KEY.isDown() && Keys.UP_KEY.getTime() == 1 || (Keys.UP_KEY.getTime() >= 20 && Keys.UP_KEY.getTime() % 5 == 1);
+        index = 0;
     }
     
-    private boolean isDownPressed()
+    private boolean isKeyPressed(KeyBinding key)
     {
-        return Keys.DOWN_KEY.isDown() && Keys.DOWN_KEY.getTime() == 1 || (Keys.DOWN_KEY.getTime() >= 20 && Keys.DOWN_KEY.getTime() % 5 == 1);
+        return key.getTime() == 1 || (key.getTime() >= 20 && key.getTime() % 5 == 1);
     }
     
-    private boolean isEnterPressed()
-    {
-        return Keys.CONFIRM_KEY.isDown() && Keys.CONFIRM_KEY.getTime() == 1;
-    }
     
-    public void resetIndex()
-    {
-        index = 0;
-    }
 }

+ 5 - 18
src/pathgame/gameplay/menu/OptionMenu.java

@@ -1,7 +1,8 @@
 package pathgame.gameplay.menu;
 
 import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
+import java.io.DataOutputStream;
 import java.io.IOException;
 import me.hammerle.snuviengine.api.KeyBinding;
 import me.hammerle.snuviengine.api.KeyHandler;
@@ -23,27 +24,13 @@ public class OptionMenu extends BaseMenu
         }
         options[menuLength] = new MenuButton("Back to Main Menu", (gamestate) ->
         {
-            File f = new File("resources/config.txt");
-            if(!f.exists())
+            File f = new File("resources/config.bin");
+            try(DataOutputStream writer = new DataOutputStream(new FileOutputStream(f)))
             {
-                try
-                {
-                    f.createNewFile();
-                }
-                catch(IOException ex)
-                {
-                }
-            }
-            FileWriter writer = null;
-            try
-            {
-                writer = new FileWriter(f);
-                int x = 0;
                 for(int i = 0; i < Keys.KEYS.length; ++i)
                 {
-                    writer.append(Keys.KEYNAMES[i] + ": " + String.valueOf(Keys.KEYS[i].getKey()) + "\n");
+                    writer.writeInt(Keys.KEYS[i].getKey());
                 }
-                writer.close();
             }
             catch(IOException ex)
             {