DEADSOFTWARE

Complete game save
authorfred-boy <fred-boy@protonmail.com>
Tue, 24 Apr 2018 14:33:30 +0000 (21:33 +0700)
committerfred-boy <fred-boy@protonmail.com>
Tue, 24 Apr 2018 14:33:30 +0000 (21:33 +0700)
14 files changed:
core/src/ru/deadsoftware/cavecraft/AppState.java
core/src/ru/deadsoftware/cavecraft/GameScreen.java
core/src/ru/deadsoftware/cavecraft/InputHandlerMenu.java
core/src/ru/deadsoftware/cavecraft/Renderer.java
core/src/ru/deadsoftware/cavecraft/game/GameInput.java
core/src/ru/deadsoftware/cavecraft/game/GamePhysics.java
core/src/ru/deadsoftware/cavecraft/game/GameProc.java
core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
core/src/ru/deadsoftware/cavecraft/game/GameSaver.java [new file with mode: 0644]
core/src/ru/deadsoftware/cavecraft/game/GameWorld.java
core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java [deleted file]
core/src/ru/deadsoftware/cavecraft/game/objects/Player.java
core/src/ru/deadsoftware/cavecraft/menu/MenuRenderer.java
core/src/ru/deadsoftware/cavecraft/menu/objects/Button.java

index e38e701ff2999a032cf101d8c82dad1dce11bcaa..233bbb6da85522744ac53226aad5d71dfaebf18a 100644 (file)
@@ -1,11 +1,11 @@
 package ru.deadsoftware.cavecraft;
 
 public enum AppState {
-    GOTO_GAME,
+    GOTO_NEW_GAME,
+    GOTO_LOAD_GAME,
     GOTO_MENU,
 
     MENU_MAIN,
-    MENU_GENWORLD,
 
     GAME_PLAY,
     GAME_CREATIVE_INV
index 1c6ad795349289701f3a2bfaeb29503a799f0fba..682319d184b3e7cbf1a562f8350ff4dead48e7bd 100644 (file)
@@ -3,6 +3,7 @@ package ru.deadsoftware.cavecraft;
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Screen;
 import ru.deadsoftware.cavecraft.game.GameProc;
+import ru.deadsoftware.cavecraft.game.GameSaver;
 import ru.deadsoftware.cavecraft.menu.MenuRenderer;
 
 public class GameScreen implements Screen {
@@ -38,7 +39,6 @@ public class GameScreen implements Screen {
 
     @Override
     public void show() {
-
     }
 
     @Override
@@ -53,13 +53,20 @@ public class GameScreen implements Screen {
                 menu();
                 break;
 
-            case GOTO_GAME:
+            case GOTO_NEW_GAME:
                 gameProc = new GameProc();
                 renderer = gameProc.renderer;
                 Gdx.input.setInputProcessor(new InputHandlerGame(gameProc));
                 CaveGame.STATE = AppState.GAME_PLAY;
                 break;
 
+            case GOTO_LOAD_GAME:_GAME:
+                gameProc = GameSaver.load();
+                renderer = gameProc.renderer;
+                Gdx.input.setInputProcessor(new InputHandlerGame(gameProc));
+                CaveGame.STATE = AppState.GAME_PLAY;
+                break;
+
             case GOTO_MENU:
                 renderer = menuRenderer;
                 Gdx.input.setInputProcessor(new InputHandlerMenu(menuRenderer));
index 75aa9a9d14f6db0a7358de12008a8ab47386d5dc..232e217c028c3154e56e821b12dcb1325157f446 100644 (file)
@@ -36,8 +36,8 @@ public class InputHandlerMenu implements InputProcessor{
     public boolean touchUp(int screenX, int screenY, int pointer, int mb) {
         screenX *= renderer.getWidth()/GameScreen.getWidth();
         screenY *= renderer.getHeight()/GameScreen.getHeight();
-        for (Button button : renderer.buttons) {
-            if (button.getRect().contains(screenX, screenY)) renderer.buttonClicked(button);
+        for (Button button : renderer.menuMainButtons) {
+            if (button.getRect().contains(screenX, screenY) && button.getType()>0) renderer.buttonClicked(button);
         }
         return false;
     }
index 928d1fbc88c44263f92154103f4959532b0c3add..43ea03e4a39a4df24555792331479627a237457f 100644 (file)
@@ -41,6 +41,12 @@ public abstract class Renderer {
         Assets.minecraftFont.draw(spriteBatch, str, x, y);
     }
 
+    public void drawString(String str) {
+        Assets.minecraftFont.draw(spriteBatch, str,
+                getWidth()/2 - Assets.getStringWidth(str)/2,
+                getHeight()/2 - Assets.getStringHeight(str)/2);
+    }
+
     public abstract void render();
 
 }
index 10e0e43f0047bad021e6f5b29120b8afc38f6586..cddeb4d27158d95eb635e896ecc09ff1db0d14db 100644 (file)
@@ -2,8 +2,10 @@ package ru.deadsoftware.cavecraft.game;
 
 import com.badlogic.gdx.Input;
 import com.badlogic.gdx.utils.TimeUtils;
-import ru.deadsoftware.cavecraft.*;
 import ru.deadsoftware.cavecraft.AppState;
+import ru.deadsoftware.cavecraft.Assets;
+import ru.deadsoftware.cavecraft.CaveGame;
+import ru.deadsoftware.cavecraft.Items;
 
 public class GameInput {
 
@@ -78,10 +80,11 @@ public class GameInput {
                 break;
 
             case Input.Keys.N:
-                CaveGame.STATE = AppState.GOTO_GAME;
+                CaveGame.STATE = AppState.GOTO_NEW_GAME;
                 break;
 
             case Input.Keys.ESCAPE: case Input.Keys.BACK:
+                GameSaver.save(gameProc);
                 CaveGame.STATE = AppState.GOTO_MENU;
                 break;
         }
index 3c2e9ec718f1653526f93b70edf30dda8928054b..07587e688129906f76e67401b01f8afd15b01778 100644 (file)
@@ -97,7 +97,7 @@ public class GamePhysics {
         if (pl.position.x+pl.texWidth/2<0) pl.position.x+=gameProc.world.getWidth()*16;
         if (pl.position.x+pl.texWidth/2>gameProc.world.getWidth()*16) pl.position.x-=gameProc.world.getWidth()*16;
         if (pl.position.y > gameProc.world.getHeight()*16) {
-            pl.position = gameProc.world.getSpawnPoint(0).cpy();
+            pl.position = gameProc.world.getSpawnPoint().cpy();
         }
         if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
             pl.moveY.add(0, -8);
index a2107fa956fd9bc0948d2e02fd0f770b511b1183..4fe96c6595274d6e97db4086e3d21ec665034b53 100644 (file)
@@ -2,25 +2,25 @@ package ru.deadsoftware.cavecraft.game;
 
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.Input;
-import com.badlogic.gdx.utils.Array;
 import com.badlogic.gdx.utils.TimeUtils;
 import ru.deadsoftware.cavecraft.*;
-import ru.deadsoftware.cavecraft.AppState;
 import ru.deadsoftware.cavecraft.game.mobs.Mob;
-import ru.deadsoftware.cavecraft.game.mobs.Pig;
 import ru.deadsoftware.cavecraft.game.objects.Player;
 
-public class GameProc {
+import java.io.Serializable;
+import java.util.ArrayList;
+
+public class GameProc implements Serializable{
 
     public static double RUN_TIME = 0;
 
     public Player player;
 
-    public Array<Mob> mobs;
+    public ArrayList<Mob> mobs;
 
-    public GameWorld world;
-    public GameRenderer renderer;
-    public GamePhysics physics;
+    public transient GameWorld world;
+    public transient GameRenderer renderer;
+    public transient GamePhysics physics;
 
     public int cursorX, cursorY;
     public int invSlot;
@@ -33,11 +33,11 @@ public class GameProc {
 
     public GameProc() {
         world = new GameWorld();
-        if (WorldSaver.exists()) {
-            world.load();
-        } else {
-            world.generate(1024, 256);
-        }
+        world.generate(8,256);
+        player = new Player(world.getSpawnPoint());
+        mobs = new ArrayList<Mob>();
+        physics = new GamePhysics(this);
+        if (!CaveGame.TOUCH) ctrlMode = 1;
         if (CaveGame.TOUCH) {
             renderer = new GameRenderer(this,320,
                     320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
@@ -45,13 +45,7 @@ public class GameProc {
             renderer = new GameRenderer(this,480,
                     480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
         }
-        physics = new GamePhysics(this);
-        player = new Player(world.getSpawnPoint(0));
-        mobs = new Array<Mob>();
-        for (int i=0; i<world.getWidth(); i+=64) {
-            mobs.add(new Pig(i*16, (int)world.getSpawnPoint(i).y, world));
-        }
-        if (!CaveGame.TOUCH) ctrlMode = 1;
+        GameSaver.save(this);
     }
 
     public void resetRenderer() {
index 480a63531184b715558da6ff13a22db5abd5f0f2..2db89c345912f1bca64c97a241b4b4798f9e6de6 100644 (file)
@@ -2,15 +2,15 @@ package ru.deadsoftware.cavecraft.game;
 
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.GL20;
-import com.badlogic.gdx.graphics.OrthographicCamera;
-import com.badlogic.gdx.graphics.g2d.SpriteBatch;
-import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
 import com.badlogic.gdx.math.Vector2;
-import ru.deadsoftware.cavecraft.*;
+import ru.deadsoftware.cavecraft.Assets;
+import ru.deadsoftware.cavecraft.CaveGame;
+import ru.deadsoftware.cavecraft.Items;
+import ru.deadsoftware.cavecraft.Renderer;
 import ru.deadsoftware.cavecraft.game.mobs.Mob;
 import ru.deadsoftware.cavecraft.game.objects.Player;
 
-public class GameRenderer extends Renderer{
+public class GameRenderer extends Renderer {
 
     private GameProc gameProc;
 
diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java b/core/src/ru/deadsoftware/cavecraft/game/GameSaver.java
new file mode 100644 (file)
index 0000000..070b5c2
--- /dev/null
@@ -0,0 +1,138 @@
+package ru.deadsoftware.cavecraft.game;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.files.FileHandle;
+import ru.deadsoftware.cavecraft.CaveGame;
+import ru.deadsoftware.cavecraft.Items;
+
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.nio.ByteBuffer;
+
+public class GameSaver {
+
+    private static final int VERSION = 0;
+
+    private static int[][] fMap, bMap;
+    private static int readIndex;
+
+    private static int bytesInt(byte[] bytes) {
+        ByteBuffer wrapped = ByteBuffer.wrap(bytes);
+        int res = wrapped.getInt(readIndex);
+        readIndex+=4;
+        return res;
+    }
+
+    private static void writeInt(FileHandle file, int i, boolean append) {
+        byte[] bytes = ByteBuffer.allocate(4).putInt(i).array();
+        file.writeBytes(bytes, append);
+    }
+    
+    private static void saveMap(FileHandle file, int[][] map) {
+        int rl,bl;
+        int width = map.length;
+        int height = map[0].length;
+        writeInt(file, VERSION, false);
+        writeInt(file, width, true);
+        writeInt(file, height, true);
+        for (int y=0; y<map[0].length; y++) {
+            bl = map[0][y];
+            rl=0;
+            for (int x=0; x<map.length; x++) {
+                if (map[x][y]!=bl || x==map.length-1) {
+                    if (x==map.length-1) rl++;
+                    writeInt(file, rl, true);
+                    writeInt(file, bl, true);
+                    System.out.printf("%d. Run:%d; Block:%s\n",y, rl, Items.BLOCKS.getKeyAt(bl));
+                    rl=0;
+                    bl=map[x][y];
+                }
+                rl++;
+            }
+        }
+    }
+
+    private static int[][] loadMap(FileHandle file) throws Exception {
+        int[][] map = null;
+        int ver, width, height;
+        int rl,bl;
+        byte[] data = file.readBytes();
+        readIndex = 0;
+        ver = bytesInt(data);
+        if (VERSION == ver) {
+            width = bytesInt(data);
+            height = bytesInt(data);
+            map = new int[width][height];
+            for (int y=0; y<height; y++) {
+                for (int x=0; x<width; x+=rl) {
+                    rl = bytesInt(data);
+                    bl = bytesInt(data);
+                    System.out.printf("%d. Run:%d; Block:%s\n",y, rl, Items.BLOCKS.getKeyAt(bl));
+                    for (int i=x; i<x+rl; i++) map[i][y] = bl;
+                }
+            }
+        } else throw new Exception("version mismatch");
+        return map;
+    }
+
+    public static int[][] getLoadedForeMap() {
+        return fMap;
+    }
+
+    public static int[][] getLoadedBackMap() {
+        return bMap;
+    }
+
+    public static void loadMap() {
+        try {
+            fMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"));
+            bMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"));
+        } catch (Exception e) {
+            Gdx.app.error("GameSaver",e.getMessage(),e);
+        }
+    }
+
+    public static GameProc load() {
+        FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
+        GameProc gameProc = null;
+        try {
+            ObjectInputStream in = new ObjectInputStream(file.read());
+            int ver = in.readInt();
+            if (VERSION == ver) gameProc = (GameProc)in.readObject();
+                else throw new Exception("version mismatch");
+            in.close();
+        } catch (Exception e) {
+            Gdx.app.error("GameSaver",e.getMessage(),e);
+        }
+        gameProc.world = new GameWorld();
+        gameProc.world.load();
+        gameProc.physics = new GamePhysics(gameProc);
+        gameProc.resetRenderer();
+        fMap = null;
+        bMap = null;
+        return gameProc;
+    }
+
+    public static void save(GameProc gameProc) {
+        FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/");
+        file.mkdirs();
+        file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
+        try {
+            ObjectOutputStream out = new ObjectOutputStream(file.write(false));
+            out.writeInt(VERSION);
+            out.writeObject(gameProc);
+            out.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), gameProc.world.getFullForeMap());
+        saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), gameProc.world.getFullBackMap());
+    }
+
+    public static boolean exists() {
+        return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav").exists() &&
+                Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() &&
+                Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists());
+    }
+
+}
index 4ed875de4e4f87b705d7301f6c80645293948fe1..92973e18192e64de711c2408af2abe0af075c0b2 100644 (file)
@@ -19,15 +19,19 @@ public class GameWorld {
         return HEIGHT;
     }
 
+    public int[][] getFullForeMap() {
+        return foreMap;
+    }
+
+    public int[][] getFullBackMap() {
+        return backMap;
+    }
+
     public int getForeMap(int x, int y) {
         int map = 0;
         try {
-            if (x<0) {
-                x = x % (getWidth());
-                x = getWidth()- Math.abs(x);
-            } else if (x>0) {
-                x = x % (getWidth());
-            }
+            x = x%getWidth();
+            if (x<0) x=getWidth()-Math.abs(x);
             map = foreMap[x][y];
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld",e.toString());
@@ -37,12 +41,8 @@ public class GameWorld {
 
     public void setForeMap(int x, int y, int value) {
         try {
-            if (x<0) {
-                x = x % (getWidth());
-                x = getWidth()- Math.abs(x);
-            } else if (x>0) {
-                x = x % (getWidth());
-            }
+            x = x%getWidth();
+            if (x<0) x=getWidth()-Math.abs(x);
             foreMap[x][y] = value;
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld", e.toString());
@@ -52,12 +52,8 @@ public class GameWorld {
     public int getBackMap(int x, int y) {
         int map = 0;
         try {
-            if (x<0) {
-                x = x % (getWidth());
-                x = getWidth()- Math.abs(x);
-            } else if (x>0) {
-                x = x % (getWidth());
-            }
+            x = x%getWidth();
+            if (x<0) x=getWidth()-Math.abs(x);
             map = backMap[x][y];
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld",e.toString());
@@ -67,12 +63,8 @@ public class GameWorld {
 
     public void setBackMap(int x, int y, int value) {
         try {
-            if (x<0) {
-                x = x % (getWidth());
-                x = getWidth()- Math.abs(x);
-            } else if (x>0) {
-                x = x % (getWidth());
-            }
+            x = x%getWidth();
+            if (x<0) x=getWidth()-Math.abs(x);
             backMap[x][y] = value;
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld", e.toString());
@@ -91,8 +83,8 @@ public class GameWorld {
         }
     }
 
-    public Vector2 getSpawnPoint(int x) {
-        int y=0;
+    public Vector2 getSpawnPoint() {
+        int x=0,y=0;
         while (true) {
             y++;
             if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
@@ -109,17 +101,12 @@ public class GameWorld {
         foreMap = WorldGen.getForeMap();
         backMap = WorldGen.getBackMap();
         WorldGen.clear();
-        save();
-    }
-
-    public void save() {
-        WorldSaver.save(foreMap, backMap);
     }
 
     public void load() {
-        WorldSaver.load();
-        foreMap = WorldSaver.getLoadedForeMap();
-        backMap = WorldSaver.getLoadedBackMap();
+        GameSaver.loadMap();
+        foreMap = GameSaver.getLoadedForeMap();
+        backMap = GameSaver.getLoadedBackMap();
         WIDTH = foreMap.length;
         HEIGHT = foreMap[0].length;
     }
diff --git a/core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java b/core/src/ru/deadsoftware/cavecraft/game/WorldSaver.java
deleted file mode 100644 (file)
index 41967a2..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-package ru.deadsoftware.cavecraft.game;
-
-import com.badlogic.gdx.Gdx;
-import com.badlogic.gdx.files.FileHandle;
-import ru.deadsoftware.cavecraft.CaveGame;
-
-import java.nio.ByteBuffer;
-
-public class WorldSaver {
-
-    private static final int VERSION = 0;
-
-    private static int[][] fMap, bMap;
-    private static int readIndex;
-
-    private static int bytesInt(byte[] bytes) {
-        ByteBuffer wrapped = ByteBuffer.wrap(bytes);
-        int res = wrapped.getInt(readIndex);
-        readIndex+=4;
-        return res;
-    }
-
-    private static void writeInt(FileHandle file, int i, boolean append) {
-        byte[] bytes = ByteBuffer.allocate(4).putInt(i).array();
-        file.writeBytes(bytes, append);
-    }
-    
-    private static void saveMap(FileHandle file, int[][] map) {
-        int width = map.length;
-        int height = map[0].length;
-        writeInt(file, VERSION, false);
-        writeInt(file, width, true);
-        writeInt(file, height, true);
-        for (int y=0; y<map[0].length; y++) {
-            int bl = map[0][y];
-            int rl = 1;
-            for (int x=0; x<map.length; x++) {
-                if (map[x][y]!=bl || x==map.length-1) {
-                    writeInt(file, rl, true);
-                    writeInt(file, bl, true);
-                    rl=1;
-                    bl=map[x][y];
-                } else {
-                    rl++;
-                }
-            }
-        }
-    }
-
-    private static int[][] loadMap(FileHandle file) {
-        int[][] map = null;
-        int ver, width, height;
-        int rl,bl;
-        byte[] data = file.readBytes();
-        readIndex = 0;
-        ver = bytesInt(data);
-        if (VERSION <= ver) {
-            width = bytesInt(data);
-            height = bytesInt(data);
-            map = new int[width][height];
-            for (int y=0; y<height; y++) {
-                for (int x=0; x<width; x+=rl) {
-                    rl = bytesInt(data);
-                    bl = bytesInt(data);
-                    for (int i=x; i<x+rl; i++) map[i][y] = bl;
-                }
-            }
-        }
-        return map;
-    }
-
-    public static int[][] getLoadedForeMap() {
-        return fMap;
-    }
-
-    public static int[][] getLoadedBackMap() {
-        return bMap;
-    }
-
-    public static void load() {
-        fMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"));
-        bMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"));
-    }
-
-    public static void save(int[][] foreMap, int[][] backMap) {
-        Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/").mkdirs();
-        saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), foreMap);
-        saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), backMap);
-    }
-
-    public static boolean exists() {
-        return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() &&
-                Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists());
-    }
-
-}
index 415ed3d5d46cbef214ac6a847583b71fc15a33e6..9e2f3dd55238b18d6890794ecf7260b029e28729 100644 (file)
@@ -1,12 +1,11 @@
 package ru.deadsoftware.cavecraft.game.objects;
 
-import com.badlogic.gdx.math.RandomXS128;
 import com.badlogic.gdx.math.Rectangle;
 import com.badlogic.gdx.math.Vector2;
-import ru.deadsoftware.cavecraft.Assets;
-import ru.deadsoftware.cavecraft.game.WorldGen;
 
-public class Player {
+import java.io.Serializable;
+
+public class Player implements Serializable {
 
     public static int ANIM_SPEED = 6;
 
index aa2897db09265c1471b64578ba6cf7c4063ee0b7..4f3e3b16e3fd605e61fe0f1e9f274e3b008b61c2 100644 (file)
@@ -3,23 +3,32 @@ package ru.deadsoftware.cavecraft.menu;
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.utils.Array;
 import ru.deadsoftware.cavecraft.*;
-import ru.deadsoftware.cavecraft.AppState;
+import ru.deadsoftware.cavecraft.game.GameSaver;
 import ru.deadsoftware.cavecraft.menu.objects.Button;
 
 public class MenuRenderer extends Renderer {
 
-    public Array<Button> buttons;
+    public Array<Button> menuMainButtons;
 
     public MenuRenderer(int width) {
         super(width,width*((float) GameScreen.getHeight()/GameScreen.getWidth()));
-        buttons = new Array<Button>();
-        buttons.add(new Button("Play", getWidth()/2-100, getHeight()/4));
-        buttons.add(new Button("Quit", getWidth()/2-100, getHeight()/4+30));
+        menuMainButtons = new Array<Button>();
+        menuMainButtons.add(new Button("New game", getWidth()/2-100, getHeight()/4));
+        menuMainButtons.add(new Button("Load game", getWidth()/2-100, getHeight()/4+30,GameSaver.exists()?1:0));
+        menuMainButtons.add(new Button("Quit", getWidth()/2-100, getHeight()/4+60));
     }
 
     public void buttonClicked(Button button) {
-        if (button.getLabel().toLowerCase().equals("play")) {
-            CaveGame.STATE = AppState.GOTO_GAME;
+        if (button.getLabel().toLowerCase().equals("new game")) {
+            spriteBatch.begin();
+            drawGenWorld();
+            spriteBatch.end();
+            CaveGame.STATE = AppState.GOTO_NEW_GAME;
+        } else if (button.getLabel().toLowerCase().equals("load game")) {
+            spriteBatch.begin();
+            drawGenWorld();
+            spriteBatch.end();
+            CaveGame.STATE = AppState.GOTO_LOAD_GAME;
         } else if (button.getLabel().toLowerCase().equals("quit")) {
             Gdx.app.exit();
         }
@@ -32,6 +41,29 @@ public class MenuRenderer extends Renderer {
                 (button.getX()+button.getWidth()/2)-Assets.getStringWidth(button.getLabel())/2,
                 (button.getY()+button.getHeight()/2)-Assets.getStringHeight(button.getLabel())/2);
     }
+    
+    private void drawMenuMain() {
+        for (Button button : menuMainButtons) {
+            if (button.getType()>0) {
+                if (button.getRect().contains(Gdx.input.getX()*getWidth()/GameScreen.getWidth(),
+                        Gdx.input.getY()*getHeight()/GameScreen.getHeight()))
+                    button.setType(2);
+                else button.setType(1);
+            }
+            drawButton(button);
+        }
+    }
+
+    private void drawGenWorld() {
+        for (int x=0; x<=getWidth()/16; x++)
+            for (int y=0; y<=getHeight()/16; y++) {
+                spriteBatch.draw(Items.BLOCKS.get("dirt").getTexture(), x * 16, y * 16);
+                spriteBatch.draw(Assets.shade,x*16,y*16);
+            }
+        spriteBatch.draw(Assets.gameLogo, getWidth()/2-Assets.gameLogo.getWidth()/2, 0);
+        drawString("Generating World...");
+        spriteBatch.flush();
+    }
 
     @Override
     public void render() {
@@ -42,15 +74,11 @@ public class MenuRenderer extends Renderer {
                 spriteBatch.draw(Assets.shade,x*16,y*16);
             }
         spriteBatch.draw(Assets.gameLogo, getWidth()/2-Assets.gameLogo.getWidth()/2, 0);
-        for (Button button : buttons) {
-            if (button.getRect().contains(Gdx.input.getX()*getWidth()/GameScreen.getWidth(),
-                    Gdx.input.getY()*getHeight()/GameScreen.getHeight()) && button.getType()>0) {
-                button.setType(2);
-            } else {
-                button.setType(1);
-            }
-            drawButton(button);
+
+        switch (CaveGame.STATE) {
+            case MENU_MAIN: drawMenuMain(); break;
         }
+
         drawString("CaveCraft "+CaveGame.VERSION,0,
                 getHeight()-Assets.getStringHeight("CaveCraft "+CaveGame.VERSION)*1.5f);
         spriteBatch.end();
index b6810a704547ddda85a72200e8559a94473b0c04..d08370ef8506b3fa0b8d369dca58b0c83a5b5b45 100644 (file)
@@ -18,6 +18,10 @@ public class Button {
         this(label, x, y, width, heigth, 1);
     }
 
+    public Button(String label, float x, float y, int type) {
+        this(label, x, y, 200, 20, type);
+    }
+
     public Button(String label, float x, float y) {
         this(label, x, y, 200, 20, 1);
     }