瀏覽代碼

Menü - Key Bindings abspeichern und laden

Hudriwudri 5 年之前
父節點
當前提交
b68360374f

+ 8 - 0
resources/config.txt

@@ -0,0 +1,8 @@
+Up Key: 87
+Down Key: 83
+Left Key: 65
+Right Key: 68
+Zoom In Key: 73
+Zoom Out Key: 79
+Confirm Key: 257
+Escape Key: 256

+ 8 - 7
src/pathgame/PathGame.java

@@ -19,10 +19,11 @@ import pathgame.tilemap.TileMap;
 
 public class PathGame implements IGame
 {
+
     private final Gamestate gamestate = new Gamestate();
 
     private final Level level = new Level();
-    
+
     private final PlayerRenderer playerRenderer = new PlayerRenderer();
     private final TileMapRenderer mapRenderer = new TileMapRenderer();
 
@@ -52,7 +53,7 @@ public class PathGame implements IGame
             }
         }
         menu.tick(gamestate, level);
-  
+
         if(Keys.TEST_KEY.getTime() == 1)
         {
             level.nextLevel();
@@ -65,13 +66,13 @@ public class PathGame implements IGame
     {
         TileMap map = level.getMap();
         Player player = level.getPlayer();
-        
+
         float zoomRestrictionX = r.getViewWidth() / (map.getWidth() * TileRenderer.TILE_SIZE);
         float zoomRestrictionY = (r.getViewHeight() - HUDRenderer.OFFSET_Y) / (map.getHeight() * TileRenderer.TILE_SIZE);
-    
+
         cam.limitScale(Math.max(zoomRestrictionX, zoomRestrictionY));
         float interScale = cam.getInterpolatedScale(lag);
-        
+
         mapRenderer.setScale(interScale);
 
         float offX = getMapOffsetX(map, player, r, lag, interScale);
@@ -124,9 +125,9 @@ public class PathGame implements IGame
         }
         return cam.getCamOffsetY(offY, minOffY, lag, interScale);
     }
-    
+
     @Override
     public void onStop()
     {
     }
-}
+}

+ 61 - 8
src/pathgame/gameplay/Keys.java

@@ -1,22 +1,75 @@
 package pathgame.gameplay;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
 import me.hammerle.snuviengine.api.KeyBinding;
 import me.hammerle.snuviengine.api.KeyHandler;
 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 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"
+    };
+
+    static
+    {
+        String line[] = new String[KEYNAMES.length];
+        File f = new File("resources/config.txt");
+        System.out.println(f.getName());
+        if(f.exists())
+        {
+            FileReader reader = null;
+            try
+            {
+                reader = new FileReader(f);
+                BufferedReader bReader = new BufferedReader(reader);
+                for(int i = 0; i < KEYNAMES.length; ++i)
+                {
+                    try
+                    {
+                        line[i] = bReader.readLine();
+                    }
+                    catch(IOException ex)
+                    {
+                    }
+                }
+            }
+            catch(FileNotFoundException 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
+        };
+    }
 }

+ 1 - 1
src/pathgame/gameplay/menu/BaseMenu.java

@@ -8,7 +8,7 @@ public abstract class BaseMenu
 {
     private final int id;
     private int index = 0;
-    public abstract MenuOption[] getOptions();
+    public abstract MenuButton[] getOptions();
     private int returnId;
     
     public BaseMenu(int id)

+ 7 - 8
src/pathgame/gameplay/menu/CharacterMenu.java

@@ -5,27 +5,26 @@ import pathgame.gameplay.PlayerAbilities;
 
 public class CharacterMenu extends BaseMenu
 {
-    private final MenuOption[] options;
+
+    private final MenuButton[] options;
 
     public CharacterMenu(int id, int mainId)
     {
         super(id);
-
-        options = new MenuOption[PlayerAbilities.ABILITIES.length + 1];
-
+        options = new MenuButton[PlayerAbilities.ABILITIES.length + 1];
         for(int i = 0; i < options.length - 1; ++i)
         {
             options[i] = getAbilityOption(PlayerAbilities.ABILITIES[i]);
         }
-        options[options.length - 1] = new MenuOption("Back", (gamestate) ->
+        options[options.length - 1] = new MenuButton("Back", (gamestate) ->
         {
             setReturnId(mainId);
         });
     }
 
-    private static MenuOption getAbilityOption(PlayerAbilities pa)
+    private static MenuButton getAbilityOption(PlayerAbilities pa)
     {
-        return new MenuOption(pa.getName(), (gamestate, level) ->
+        return new MenuButton(pa.getName(), (gamestate, level) ->
         {
             level.reset();
             level.getPlayer().setAbilities(pa);
@@ -34,7 +33,7 @@ public class CharacterMenu extends BaseMenu
     }
 
     @Override
-    public MenuOption[] getOptions()
+    public MenuButton[] getOptions()
     {
         return options;
     }

+ 6 - 6
src/pathgame/gameplay/menu/EscMenu.java

@@ -5,23 +5,23 @@ import pathgame.gameplay.Gamestates;
 
 public class EscMenu extends BaseMenu
 {
-    private final MenuOption[] options;
+    private final MenuButton[] options;
     
     public EscMenu(int id, int mainId)
     {
         super(id);
         
-        options = new MenuOption[] 
+        options = new MenuButton[] 
         {
-            new MenuOption("Continue", (gamestate) -> 
+            new MenuButton("Continue", (gamestate) -> 
             {
                 gamestate.setState(Gamestates.GAMEPLAY);
             }),
-            new MenuOption("Main Menu", (gamestate) -> 
+            new MenuButton("Main Menu", (gamestate) -> 
             {
                 setReturnId(mainId);
             }),
-            new MenuOption("Exit", (gamestate) -> 
+            new MenuButton("Exit", (gamestate) -> 
             {
                 Engine.stop();
             }),
@@ -29,7 +29,7 @@ public class EscMenu extends BaseMenu
     }
 
     @Override
-    public MenuOption[] getOptions()
+    public MenuButton[] getOptions()
     {
         return options;
     }

+ 6 - 6
src/pathgame/gameplay/menu/MainMenu.java

@@ -4,23 +4,23 @@ import me.hammerle.snuviengine.api.Engine;
 
 public class MainMenu extends BaseMenu
 {
-    private final MenuOption[] options;
+    private final MenuButton[] options;
     
     public MainMenu(int id, int optionsId, int charaterId)
     {
         super(id);
         
-        options = new MenuOption[] 
+        options = new MenuButton[] 
         {
-            new MenuOption("Start", (gamestate) -> 
+            new MenuButton("Start", (gamestate) -> 
             {
                 setReturnId(charaterId);
             }),
-            new MenuOption("Options", (gamestate) -> 
+            new MenuButton("Options", (gamestate) -> 
             {
                 setReturnId(optionsId);
             }),
-            new MenuOption("Exit", (gamestate) -> 
+            new MenuButton("Exit", (gamestate) -> 
             {
                 Engine.stop();
             })
@@ -28,7 +28,7 @@ public class MainMenu extends BaseMenu
     }
 
     @Override
-    public MenuOption[] getOptions()
+    public MenuButton[] getOptions()
     {
         return options;
     }

+ 1 - 1
src/pathgame/gameplay/menu/Menu.java

@@ -49,7 +49,7 @@ public class Menu
         }
     }
     
-    public MenuOption[] getOptions()
+    public MenuButton[] getOptions()
     {
         return menus[currentIndex].getOptions();
     }

+ 3 - 3
src/pathgame/gameplay/menu/MenuOption.java → src/pathgame/gameplay/menu/MenuButton.java

@@ -5,18 +5,18 @@ import java.util.function.Consumer;
 import pathgame.gameplay.Gamestate;
 import pathgame.gameplay.Level;
 
-public class MenuOption
+public class MenuButton
 {
     private final String name;
     private final BiConsumer<Gamestate, Level> r;
     
-    public MenuOption(String name, BiConsumer<Gamestate, Level> r)
+    public MenuButton(String name, BiConsumer<Gamestate, Level> r)
     {
         this.name = name;
         this.r = r;
     }
     
-    public MenuOption(String name, Consumer<Gamestate> r)
+    public MenuButton(String name, Consumer<Gamestate> r)
     {
         this(name, (gamestate, level) -> r.accept(gamestate));
     }

+ 48 - 40
src/pathgame/gameplay/menu/OptionMenu.java

@@ -1,59 +1,67 @@
 package pathgame.gameplay.menu;
 
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import me.hammerle.snuviengine.api.KeyBinding;
 import me.hammerle.snuviengine.api.KeyHandler;
 import pathgame.gameplay.Keys;
 
 public class OptionMenu extends BaseMenu
 {
-    private final MenuOption[] options; 
-    
+
+    private final MenuButton[] options;
+
     public OptionMenu(int id, int mainId)
     {
         super(id);
-        
-        options = new MenuOption[] 
+        int menuLength = Keys.KEYS.length;
+        options = new MenuButton[menuLength + 1];
+        for(int i = 0; i < menuLength; ++i)
         {
-            new MenuOption("Up Key", (gamestate) -> 
-            {
-                KeyHandler.rebind(Keys.UP_KEY);
-            }),
-            new MenuOption("Down Key", (gamestate) -> 
-            {
-                KeyHandler.rebind(Keys.DOWN_KEY);
-            }),
-            new MenuOption("Left Key", (gamestate) -> 
-            {
-                KeyHandler.rebind(Keys.LEFT_KEY);
-            }),
-            new MenuOption("Right Key", (gamestate) -> 
-            {
-                KeyHandler.rebind(Keys.RIGHT_KEY);
-            }),
-            new MenuOption("Escape Key", (gamestate) -> 
-            {
-                KeyHandler.rebind(Keys.ESCAPE_KEY);
-            }),
-            new MenuOption("Confirm Key", (gamestate) -> 
-            {
-                KeyHandler.rebind(Keys.CONFIRM_KEY);
-            }),
-            new MenuOption("Zoom In Key", (gamestate) -> 
+            options[i] = getButton(Keys.KEYNAMES[i], Keys.KEYS[i]);
+        }
+        options[menuLength] = new MenuButton("Back to Main Menu", (gamestate) ->
+        {
+            File f = new File("resources/config.txt");
+            if(!f.exists())
             {
-                KeyHandler.rebind(Keys.ZOOM_IN_KEY);
-            }),
-            new MenuOption("Zoom Out Key", (gamestate) -> 
+                try
+                {
+                    f.createNewFile();
+                }
+                catch(IOException ex)
+                {
+                }
+            }
+            FileWriter writer = null;
+            try
             {
-                KeyHandler.rebind(Keys.ZOOM_OUT_KEY);
-            }),
-            new MenuOption("Back to Main Menu", (gamestate) -> 
+                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.close();
+            }
+            catch(IOException ex)
             {
-                setReturnId(mainId);
-            }),
-        };
+            }
+            setReturnId(mainId);
+        });
+    }
+
+    private MenuButton getButton(String name, KeyBinding key)
+    {
+        return new MenuButton(name, (gamestate) ->
+        {
+            KeyHandler.rebind(key);
+        });
     }
 
     @Override
-    public MenuOption[] getOptions()
+    public MenuButton[] getOptions()
     {
         return options;
     }
@@ -63,4 +71,4 @@ public class OptionMenu extends BaseMenu
     {
         return true;
     }
-}
+}

+ 6 - 18
src/pathgame/rendering/MenuRenderer.java

@@ -4,23 +4,11 @@ import me.hammerle.snuviengine.api.KeyBinding;
 import me.hammerle.snuviengine.api.Renderer;
 import pathgame.gameplay.Keys;
 import pathgame.gameplay.menu.Menu;
-import pathgame.gameplay.menu.MenuOption;
+import pathgame.gameplay.menu.MenuButton;
 
 public class MenuRenderer
 {
-    private final KeyBinding[] keys = new KeyBinding[]
-    {
-        Keys.UP_KEY,
-        Keys.DOWN_KEY,
-        Keys.LEFT_KEY,
-        Keys.RIGHT_KEY,
-        Keys.ESCAPE_KEY,
-        Keys.CONFIRM_KEY,
-        Keys.ZOOM_IN_KEY,
-        Keys.ZOOM_OUT_KEY
-    };
-
-    private final float keyWidths[] = new float[keys.length];
+    private final float keyWidths[] = new float[Keys.KEYS.length];
 
     public void renderTick(Renderer r, float lag, Menu menu)
     {
@@ -30,7 +18,7 @@ public class MenuRenderer
         r.updateMatrix();
         scale = 1.0f / scale;
 
-        MenuOption[] options = menu.getOptions();
+        MenuButton[] options = menu.getOptions();
 
         float lastGap = 10.0f;
         float baseBoxHeight = 300.0f * scale;
@@ -64,9 +52,9 @@ public class MenuRenderer
         else
         {
             float max = Float.MIN_VALUE;
-            for(int i = 0; i < keys.length; i++)
+            for(int i = 0; i < Keys.KEYS.length; i++)
             {
-                keyWidths[i] = r.getFontRenderer().getSize(getKeyName(keys[i])).getWidth();
+                keyWidths[i] = r.getFontRenderer().getSize(getKeyName(Keys.KEYS[i])).getWidth();
                 if(keyWidths[i] > max)
                 {
                     max = keyWidths[i];
@@ -77,7 +65,7 @@ public class MenuRenderer
             {
                 boolean active = menu.getActiveIndex() == i;
                 renderText(options[i].getName(), active, r, windowWidth, y, true);
-                r.getFontRenderer().drawString(windowWidth * 0.85f - max * 0.5f - keyWidths[i] * 0.5f, y, addColor(getKeyName(keys[i]), active));
+                r.getFontRenderer().drawString(windowWidth * 0.85f - max * 0.5f - keyWidths[i] * 0.5f, y, addColor(getKeyName(Keys.KEYS[i]), active));
                 y += step;
             }
             y += lastGap;