Ver código fonte

soul bottle and time highscore is saved, highscore is shown in level screen

Kajetan Johannes Hammerle 6 anos atrás
pai
commit
bdaf745fe5

+ 1 - 1
options.txt

@@ -12,4 +12,4 @@ key.left="Left"
 key.right="Right"
 key.run="Shift"
 key.up="Up"
-sound=true
+sound=false

+ 3 - 2
slot1.txt

@@ -1,2 +1,3 @@
-level.01-Parabola=true
-level.02-Out of Reach=true
+level.00-Tech Demo=true
+level.00-Tech Demo.bottles=3
+level.00-Tech Demo.time=4.125000000000025

+ 71 - 51
src/me/hammerle/supersnuvi/gamelogic/Level.java

@@ -8,7 +8,6 @@ import java.util.TreeSet;
 import java.util.stream.Collectors;
 import me.hammerle.supersnuvi.entity.Entity;
 import me.hammerle.supersnuvi.entity.EntityBuilder;
-import me.hammerle.supersnuvi.rendering.Game;
 import me.hammerle.supersnuvi.tiles.BottledSoulTile;
 import me.hammerle.supersnuvi.tiles.Location;
 import me.hammerle.supersnuvi.tiles.StartTile;
@@ -24,7 +23,7 @@ public final class Level
     private final boolean worldLoaded;
     private final LevelData data;
     
-    private final String name;
+    private String name;
     
     private final HashMap<Integer, Entity> entities;
     private Entity hero;
@@ -82,12 +81,12 @@ public final class Level
             spawns.add(new Point(5, 5));
         }
         
-        if(name.equals("00-Tech Demo"))
+        /*if(name.equals("00-Tech Demo"))
         {
             int index = data.getBackgroundIndex() + 1;
             data.setTile(index, 5, 5, 2);
             data.setTile(index, 30, 5, 1);
-        }
+        }*/
         
         resetLevel();
         
@@ -130,6 +129,7 @@ public final class Level
         state.resetTiles();
         data.activateEntities();
         souls = 0;
+        time = 0.0;
         shouldReset = false;
         done = false;
         Entity h = spawnHero();
@@ -164,11 +164,26 @@ public final class Level
         souls++;
     }
     
+    public int getCurrentBottles()
+    {
+        return souls;
+    }
+    
+    public int getMaxBottles()
+    {
+        return maxSouls;
+    }
+    
     public LevelData getData()
     {
         return data;
     }
     
+    public double getTime()
+    {
+        return time;
+    }  
+
     // -------------------------------------------------------------------------
     // tick
     // -------------------------------------------------------------------------
@@ -209,6 +224,56 @@ public final class Level
         }
     }   
     
+    public char[] formatBottles(int bottles)
+    {
+        char[] c = new char[5];
+        if(bottles <= 9)
+        {
+            c[0] = '0';
+            c[1] = (char) (bottles + '0');
+        }
+        else if(bottles > 99)
+        {
+            c[0] = 'X';
+            c[1] = 'X';
+        }
+        else
+        {
+            c[0] = (char) ((bottles / 10) + '0');
+            c[1] = (char) ((bottles % 10) + '0');
+        }
+        c[2] = '/';
+        if(maxSouls <= 9)
+        {
+            c[3] = '0';
+            c[4] = (char) (maxSouls + '0');
+        }
+        else if(maxSouls > 99)
+        {
+            c[3] = 'X';
+            c[4] = 'X';
+        }
+        else
+        {
+            c[3] = (char) ((maxSouls / 10) + '0');
+            c[4] = (char) ((maxSouls % 10) + '0');
+        }
+        return c;
+    }
+    
+    public char[] formatTime(double time)
+    {
+        if(time == -1.0)
+        {
+            return new char[] {'-', '-', '-', '-', '-'};
+        }
+        else if(time >= 999.9)
+        {
+            return new char[] {'9', '9', '9', '.', '9'};
+        }
+        return String.format("%05.1f", time).toCharArray();
+    }
+    
     public void render()
     {
         if(worldLoaded)
@@ -269,43 +334,10 @@ public final class Level
                 renderer.restore();
 
                 // soul rendering
-                char[] c = new char[5];
-                if(souls <= 9)
-                {
-                    c[0] = '0';
-                    c[1] = (char) (souls + '0');
-                }
-                else if(souls > 99)
-                {
-                    c[0] = 'X';
-                    c[1] = 'X';
-                }
-                else
-                {
-                    c[0] = (char) ((souls / 10) + '0');
-                    c[1] = (char) ((souls % 10) + '0');
-                }
-                c[2] = '/';
-                if(maxSouls <= 9)
-                {
-                    c[3] = '0';
-                    c[4] = (char) (maxSouls + '0');
-                }
-                else if(maxSouls > 99)
-                {
-                    c[3] = 'X';
-                    c[4] = 'X';
-                }
-                else
-                {
-                    c[3] = (char) ((maxSouls / 10) + '0');
-                    c[4] = (char) ((maxSouls % 10) + '0');
-                }
-
                 double x = 5;
                 renderer.drawFixedImage(BottledSoulTile.IMAGE, x, 0, line, line);
                 x += line;
-                renderer.drawText(x, y, c);
+                renderer.drawText(x, y, formatBottles(souls));
                 x += renderer.getTextWidth(5) + 10;
 
                 // energy rendering
@@ -335,19 +367,7 @@ public final class Level
                 x += leftX + 10;
 
                 // time rendering
-                if(time >= 999.9)
-                {
-                    c[0] = '9';
-                    c[1] = '9';
-                    c[2] = '9';
-                    c[3] = '.';
-                    c[4] = '9';
-                    renderer.drawText(x, y, c);
-                }
-                else
-                {
-                    renderer.drawText(x, y, String.format("%05.1f", time).toCharArray());
-                }
+                renderer.drawText(x, y, formatTime(time));
             }
             renderer.stopTextDrawing();
         }

+ 111 - 55
src/me/hammerle/supersnuvi/gamelogic/StateRenderer.java

@@ -185,7 +185,7 @@ public class StateRenderer
         return c;
     }
     
-    private static char[] getCharLine(char start, String s, char end, char spacer)
+    private static char[] getCharLine(char start, String s, char end)
     {
         char[] chars = new char[MENU_WIDTH];
         chars[0] = start;
@@ -195,69 +195,93 @@ public class StateRenderer
         {
             chars[i + 1] = s.charAt(i);
         }
-        chars[chars.length - 3] = spacer;
         return chars;
     }
     
-    private static final int MENU_WIDTH = 26;
+    private static char[] getLevelCharLine(char start, char mid, char end, char spacer)
+    {
+        char[] c = new char[MENU_WIDTH];
+        Arrays.fill(c, 1, c.length - 1, mid);
+        c[0] = start;
+        c[c.length - 1] = end;
+        c[c.length - 7] = spacer;
+        c[c.length - 13] = spacer;
+        return c;
+    }
+    
+    private static char[] getMoreLevelCharLine(char start, char mid, char end, char spacer)
+    {
+        char[] c = new char[MENU_WIDTH];
+        Arrays.fill(c, 1, c.length - 1, mid);
+        c[0] = start;
+        c[1] = '.';
+        c[2] = '.';
+        c[3] = '.';
+        c[c.length - 1] = end;
+        c[c.length - 7] = spacer;
+        c[c.length - 13] = spacer;
+        return c;
+    }
+    
+    private static final int MENU_WIDTH = 36;
     private static final int MENU_MAX = 5;
+    
     private static final char[] TABLE_TOP = getCharLine((char) 131, (char) 136, (char) 133, (char) 136);
-    private static final char[] TABLE_HEADING = getCharLine((char) 134, "Choose a Level ...", (char) 134, ' ');
-    private static final char[] TABLE_MID = getCharLine((char) 130, (char) 136, (char) 132, (char) 129);
-    private static final char[] TABLE_BOTTOM = getCharLine((char) 137, (char) 136, (char) 138, (char) 135);
-    private static final char[] TABLE_MORE = getCharLine((char) 134, "...", (char) 134, (char) 134);
+    private static final char[] TABLE_HEADING = getCharLine((char) 134, "Choose a Level ...", (char) 134);
+    private static final char[] TABLE_MID = getLevelCharLine((char) 130, (char) 136, (char) 132, (char) 129);
+    private static final char[] TABLE_BOTTOM = getLevelCharLine((char) 137, (char) 136, (char) 138, (char) 135);
+    private static final char[] TABLE_MORE = getMoreLevelCharLine((char) 134, ' ', (char) 134, (char) 134);
     
     private static final char[][] START_UP = new char[][] 
     {
         getCharLine((char) 131, (char) 136, (char) 133, (char) 136),
-        getCharLine((char) 134, "Super Snuvi", (char) 134, ' '),
+        getCharLine((char) 134, "Super Snuvi", (char) 134),
         getCharLine((char) 130, (char) 136, (char) 132, (char) 136),
-        getCharLine((char) 134, "Start Game", (char) 134, ' '),
-        getCharLine((char) 134, "Options", (char) 134, ' '),
-        getCharLine((char) 134, "Exit Game", (char) 134, ' '),
+        getCharLine((char) 134, "Start Game", (char) 134),
+        getCharLine((char) 134, "Options", (char) 134),
+        getCharLine((char) 134, "Exit Game", (char) 134),
         getCharLine((char) 137, (char) 136, (char) 138, (char) 136)
     };
     
     private static final char[][] SLOTS = new char[][] 
     {
         getCharLine((char) 131, (char) 136, (char) 133, (char) 136),
-        getCharLine((char) 134, "Choose a Savegame ...", (char) 134, ' '),
+        getCharLine((char) 134, "Choose a Savegame ...", (char) 134),
         getCharLine((char) 130, (char) 136, (char) 132, (char) 136),
-        getCharLine((char) 134, "Slot 1", (char) 134, ' '),
-        getCharLine((char) 134, "Slot 2", (char) 134, ' '),
-        getCharLine((char) 134, "Slot 3", (char) 134, ' '),
-        getCharLine((char) 134, "Back", (char) 134, ' '),
+        getCharLine((char) 134, "Slot 1", (char) 134),
+        getCharLine((char) 134, "Slot 2", (char) 134),
+        getCharLine((char) 134, "Slot 3", (char) 134),
+        getCharLine((char) 134, "Back", (char) 134),
         getCharLine((char) 137, (char) 136, (char) 138, (char) 136)
     };
     
     private static final char[][] OPTIONS = new char[][] 
     {
         getCharLine((char) 131, (char) 136, (char) 133, (char) 136),
-        getCharLine((char) 134, "Options", (char) 134, ' '),
+        getCharLine((char) 134, "Options", (char) 134),
         getCharLine((char) 130, (char) 136, (char) 132, (char) 136),
-        getCharLine((char) 134, "Sound", (char) 134, ' '),  
-        getCharLine((char) 134, "K: Up", (char) 134, ' '),
-        getCharLine((char) 134, "K: Down", (char) 134, ' '),
-        getCharLine((char) 134, "K: Left", (char) 134, ' '),
-        getCharLine((char) 134, "K: Rright", (char) 134, ' '),
-        getCharLine((char) 134, "K: Jump", (char) 134, ' '),
-        getCharLine((char) 134, "K: Run", (char) 134, ' '),
-        getCharLine((char) 134, "K: Back", (char) 134, ' '),
-        getCharLine((char) 134, "K: Enter", (char) 134, ' '),
-        getCharLine((char) 134, "K: Combat", (char) 134, ' '),
-        getCharLine((char) 134, "K: Switch Face", (char) 134, ' '),
-        getCharLine((char) 134, "K: Dash/Dodge", (char) 134, ' '),
-        getCharLine((char) 134, "K: Dash/Dodge", (char) 134, ' '),
-        getCharLine((char) 134, "K: Block", (char) 134, ' '),
-        getCharLine((char) 134, "K: Attack", (char) 134, ' '),
-        getCharLine((char) 134, "", (char) 134, ' '), // save
-        getCharLine((char) 134, "Back", (char) 134, ' '),
+        getCharLine((char) 134, "Sound", (char) 134),  
+        getCharLine((char) 134, "K: Up", (char) 134),
+        getCharLine((char) 134, "K: Down", (char) 134),
+        getCharLine((char) 134, "K: Left", (char) 134),
+        getCharLine((char) 134, "K: Rright", (char) 134),
+        getCharLine((char) 134, "K: Jump", (char) 134),
+        getCharLine((char) 134, "K: Run", (char) 134),
+        getCharLine((char) 134, "K: Back", (char) 134),
+        getCharLine((char) 134, "K: Enter", (char) 134),
+        getCharLine((char) 134, "K: Combat", (char) 134),
+        getCharLine((char) 134, "K: Switch Face", (char) 134),
+        getCharLine((char) 134, "K: Dash/Dodge", (char) 134),
+        getCharLine((char) 134, "K: Dash/Dodge", (char) 134),
+        getCharLine((char) 134, "K: Block", (char) 134),
+        getCharLine((char) 134, "K: Attack", (char) 134),
+        getCharLine((char) 134, "", (char) 134), // save
+        getCharLine((char) 134, "Back", (char) 134),
         getCharLine((char) 137, (char) 136, (char) 138, (char) 136)
     };
     
-    private static final char[] SAVE_OVERLAY = getCharLine(' ', "Save", ' ', ' ');
+    private static final char[] SAVE_OVERLAY = getCharLine(' ', "Save", ' ');
     
-            
     private static final int OPTION_OFFSET = 10;
             
     static
@@ -317,10 +341,31 @@ public class StateRenderer
             // doing that here to prevent concurent modification
             if(currentLevel.shouldFinish())
             {
+                String base = "level." + currentLevel.getName();
+                SimpleConfig sp = SAVE_SLOTS[slotScreenIndex];
+                
+                // save success
+                sp.set(base, true);
+                
+                // update time, if a new highscore was scored
+                double time = sp.getDouble(base + ".time", Integer.MAX_VALUE);
+                if(currentLevel.getTime() < time)
+                {
+                    sp.set(base + ".time", currentLevel.getTime());
+                }
+                
+                // update bottles, if a new highscore was scored
+                int bottles = sp.getInt(base + ".bottles", 0);
+                if(currentLevel.getCurrentBottles() > bottles)
+                {
+                    sp.set(base + ".bottles", currentLevel.getCurrentBottles());
+                }
+                
+                // final save
+                sp.save();
+                
                 currentLevel.resetLevel();
-                SAVE_SLOTS[slotScreenIndex].set("level." + currentLevel.getName(), true);
                 currentLevel = null;
-                SAVE_SLOTS[slotScreenIndex].save();
                 return;
             }
             
@@ -478,7 +523,7 @@ public class StateRenderer
         {
             case 0:
             {
-                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 3 / 2);
+                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 5 / 4);
                 double x = (renderer.getWidth() - renderer.getTextWidth(MENU_WIDTH)) * 0.5;
                 double y = (renderer.getHeight() - renderer.getTextHeight(7)) * 0.5;
                 double line = renderer.getTextHeight(1);
@@ -501,7 +546,7 @@ public class StateRenderer
             }
             case 1:
             {
-                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 3 / 2);
+                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 5 / 4);
                 double x = (renderer.getWidth() - renderer.getTextWidth(MENU_WIDTH)) * 0.5;
                 double y = (renderer.getHeight() - renderer.getTextHeight(7)) * 0.5;
                 double line = renderer.getTextHeight(1);
@@ -524,7 +569,7 @@ public class StateRenderer
             }
             case 2:
             {
-                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 3 / 2);
+                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 5 / 4);
                 double x = (renderer.getWidth() - renderer.getTextWidth(MENU_WIDTH)) * 0.5;
                 double y = (renderer.getHeight() - renderer.getTextHeight(OPTIONS.length)) * 0.5;
                 double line = renderer.getTextHeight(1);
@@ -607,7 +652,7 @@ public class StateRenderer
             case 3:
             {
                 // level screen rendering
-                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 3 / 2);
+                renderer.prepareTextDrawing(255, 255, 255, 1.0, MENU_WIDTH * 5 / 4);
 
                 int listLength = Math.min(levels.length, MENU_MAX);
                 double x = (renderer.getWidth() - renderer.getTextWidth(MENU_WIDTH)) * 0.5;
@@ -664,6 +709,8 @@ public class StateRenderer
     
     private double paintLevelName(double x, double y, double line, int from, int length)
     {
+        SimpleConfig sp = SAVE_SLOTS[slotScreenIndex];
+        
         char[] chars = new char[MENU_WIDTH];
         chars[0] = 134;
         chars[MENU_WIDTH - 1] = 134;
@@ -672,21 +719,24 @@ public class StateRenderer
         for(int j = from; j < length; j++)
         {
             String s = levels[j].getName();
-            int border = Math.min(MENU_WIDTH - 4, s.length());
+            int border = Math.min(MENU_WIDTH - 14, s.length());
             for(int i = 0; i < border; i++)
             {
                 chars[i + 1] = s.charAt(i);
             }
             Arrays.fill(chars, border + 1, MENU_WIDTH - 1, (char) 0);
-            chars[chars.length - 3] = 134;
-            if(SAVE_SLOTS[slotScreenIndex].getBoolean("level." + s, false))
-            {
-                chars[chars.length - 2] = 'x';
-            }
-            else
-            {
-                chars[chars.length - 2] = 'o';
-            }
+            chars[chars.length - 13] = 134;
+            chars[chars.length - 7] = 134;
+            
+            // bottles
+            char[] tmp = levels[j].formatBottles(sp.getInt("level." + s + ".bottles", 0));
+            int l = Math.min(5, tmp.length);
+            System.arraycopy(tmp, 0, chars, chars.length - 7 - l, l);
+            // time
+            tmp = levels[j].formatTime(sp.getDouble("level." + s + ".time", -1.0));
+            l = Math.min(5, tmp.length);
+            System.arraycopy(tmp, 0, chars, chars.length - 1 - l, l);
+            
             renderer.drawText(x, y, chars);
             y += line;
         }
@@ -700,13 +750,19 @@ public class StateRenderer
         renderer.fillRectangle(
                 x + renderer.getTextWidth(1),
                 y + renderer.getTextHeight(pos) - 1, 
-                renderer.getTextWidth(MENU_WIDTH - 4), 
+                renderer.getTextWidth(MENU_WIDTH - 14), 
+                line);
+        
+        renderer.fillRectangle(
+                x + renderer.getTextWidth(MENU_WIDTH - 12),
+                y + renderer.getTextHeight(pos) - 1, 
+                renderer.getTextWidth(5), 
                 line);
         
         renderer.fillRectangle(
-                x + renderer.getTextWidth(MENU_WIDTH - 2),
+                x + renderer.getTextWidth(MENU_WIDTH - 6),
                 y + renderer.getTextHeight(pos) - 1, 
-                renderer.getTextWidth(1), 
+                renderer.getTextWidth(5), 
                 line);
         renderer.restore();
     }