mirror of
https://github.com/jokerx3/HeatUp
synced 2026-07-27 11:29:46 +02:00
Highscores sind jetzt verfuegbar und koennen am Rundenende veraendert
werden. Serialisierung funktioniert auch schon.
This commit is contained in:
@@ -1,16 +1,143 @@
|
|||||||
package de.heatup.highscore;
|
package de.heatup.highscore;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
|
||||||
|
|
||||||
import de.heatup.cfg.Config;
|
import de.heatup.cfg.Config;
|
||||||
import de.heatup.logging.Logger;
|
import de.heatup.logging.Logger;
|
||||||
|
|
||||||
public class HighscoreHandler {
|
public class HighscoreHandler {
|
||||||
private int mapHash;
|
private int mapHash;
|
||||||
|
private MapHighscores mapHighscores;
|
||||||
/**
|
/**
|
||||||
* Es muss vorher eine Karte geladen sein
|
* Es muss vorher eine Karte geladen sein
|
||||||
*/
|
*/
|
||||||
public HighscoreHandler(String mapString) {
|
public HighscoreHandler(String mapString) {
|
||||||
mapHash = mapString.hashCode();
|
mapHash = mapString.hashCode();
|
||||||
// load file for serialization
|
|
||||||
|
try {
|
||||||
|
FileInputStream fileIn = new FileInputStream("highscore.dat");
|
||||||
|
ObjectInputStream in = new ObjectInputStream(fileIn);
|
||||||
|
mapHighscores = (MapHighscores) in.readObject();
|
||||||
|
in.close();
|
||||||
|
fileIn.close();
|
||||||
|
Logger.write("loaded highscore.dat");
|
||||||
|
} catch (FileNotFoundException fe) {
|
||||||
|
mapHighscores = new MapHighscores();
|
||||||
|
FileOutputStream fileOut = null;
|
||||||
|
try {
|
||||||
|
fileOut = new FileOutputStream("highscore.dat");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
ObjectOutputStream out = null;
|
||||||
|
try {
|
||||||
|
out = new ObjectOutputStream(fileOut);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
out.writeObject(mapHighscores);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
fileOut.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
Logger.write("created new highscore.dat");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FileInputStream fileIn = null;
|
||||||
|
try {
|
||||||
|
fileIn = new FileInputStream("highscore.dat");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
ObjectInputStream in = null;
|
||||||
|
try {
|
||||||
|
in = new ObjectInputStream(fileIn);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
mapHighscores = (MapHighscores) in.readObject();
|
||||||
|
} catch (ClassNotFoundException e1) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e1.printStackTrace();
|
||||||
|
} catch (IOException e1) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
fileIn.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
Logger.write("loaded highscore.dat");
|
||||||
|
} catch (IOException e) {
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
} catch (ClassNotFoundException ce) {
|
||||||
|
Logger.write("MapHighscore class not found");
|
||||||
|
ce.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
public int getCurrentMapHash() {
|
||||||
|
return mapHash;
|
||||||
|
}
|
||||||
|
public void setHighscore(int mapHash, String name, int score) {
|
||||||
|
mapHighscores.setHighscore(mapHash, name, score);
|
||||||
|
}
|
||||||
|
public ArrayList<ScoreEntry> getHighscores(int mapHash) {
|
||||||
|
return mapHighscores.getHighscores(mapHash);
|
||||||
|
}
|
||||||
|
public boolean saveHighscores() {
|
||||||
|
try {
|
||||||
|
FileOutputStream fileOut = new FileOutputStream("highscore.dat");
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(fileOut);
|
||||||
|
out.writeObject(mapHighscores);
|
||||||
|
out.close();
|
||||||
|
fileOut.close();
|
||||||
|
Logger.write("saved highscore.dat");
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Logger.write("error saving highscore.dat");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-9
@@ -5,28 +5,28 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class MapHighscore implements Serializable {
|
public class MapHighscores implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private Map<Integer, ArrayList<Score>> highscoreOfAllMaps = new HashMap<Integer, ArrayList<Score>>();
|
private Map<Integer, ArrayList<ScoreEntry>> highscoreOfAllMaps = new HashMap<Integer, ArrayList<ScoreEntry>>();
|
||||||
|
|
||||||
public ArrayList<Score> getHighscores(int mapHash) {
|
protected ArrayList<ScoreEntry> getHighscores(int mapHash) {
|
||||||
if (highscoreOfAllMaps.get(mapHash) == null) {
|
if (highscoreOfAllMaps.get(mapHash) == null) {
|
||||||
ArrayList<Score> mapHighscores = new ArrayList<Score>();
|
ArrayList<ScoreEntry> mapHighscores = new ArrayList<ScoreEntry>();
|
||||||
for (int n = 0; n<10; n++) {
|
for (int n = 0; n<10; n++) {
|
||||||
mapHighscores.add(new Score());
|
mapHighscores.add(new ScoreEntry());
|
||||||
}
|
}
|
||||||
highscoreOfAllMaps.put(mapHash, mapHighscores);
|
highscoreOfAllMaps.put(mapHash, mapHighscores);
|
||||||
}
|
}
|
||||||
return highscoreOfAllMaps.get(mapHash);
|
return highscoreOfAllMaps.get(mapHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHighscore(int mapHash, String name, int score) {
|
protected void setHighscore(int mapHash, String name, int score) {
|
||||||
|
|
||||||
ArrayList<Score> mapHighscores = highscoreOfAllMaps.get(mapHash);
|
ArrayList<ScoreEntry> mapHighscores = highscoreOfAllMaps.get(mapHash);
|
||||||
|
|
||||||
ArrayList<Score> newMapHighscores = new ArrayList<Score>();
|
ArrayList<ScoreEntry> newMapHighscores = new ArrayList<ScoreEntry>();
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ public class MapHighscore implements Serializable {
|
|||||||
if (mapHighscores.get(i).getScore() > score) {
|
if (mapHighscores.get(i).getScore() > score) {
|
||||||
newMapHighscores.add(mapHighscores.get(i));
|
newMapHighscores.add(mapHighscores.get(i));
|
||||||
} else {
|
} else {
|
||||||
newMapHighscores.add(new Score(name, score));
|
newMapHighscores.add(new ScoreEntry(name, score));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
+3
-3
@@ -2,19 +2,19 @@ package de.heatup.highscore;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
public class Score implements Serializable {
|
public class ScoreEntry implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private int score;
|
private int score;
|
||||||
|
|
||||||
public Score() {
|
public ScoreEntry() {
|
||||||
name = "default";
|
name = "default";
|
||||||
score = 2342;
|
score = 2342;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Score(String name, int score) {
|
public ScoreEntry(String name, int score) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.score = score;
|
this.score = score;
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ public class MapEngine {
|
|||||||
|
|
||||||
private static String mapString = "";
|
private static String mapString = "";
|
||||||
|
|
||||||
private final HighscoreHandler mapHighscore;
|
private HighscoreHandler highscoreHandler;
|
||||||
|
|
||||||
HashMap<Character, ArrayList<Point>> allMapTiles = new HashMap<Character, ArrayList<Point>>();
|
HashMap<Character, ArrayList<Point>> allMapTiles = new HashMap<Character, ArrayList<Point>>();
|
||||||
ArrayList<Character> availableHardwareTileTypes = new ArrayList<Character>();
|
ArrayList<Character> availableHardwareTileTypes = new ArrayList<Character>();
|
||||||
@@ -46,9 +46,8 @@ public class MapEngine {
|
|||||||
Scanner input = null;
|
Scanner input = null;
|
||||||
MapLoader ml = new MapLoader();
|
MapLoader ml = new MapLoader();
|
||||||
input = new Scanner(ml.loadMap(mapNumber));
|
input = new Scanner(ml.loadMap(mapNumber));
|
||||||
|
|
||||||
this.createTiles(input);
|
this.createTiles(input);
|
||||||
mapHighscore = new HighscoreHandler(mapString);
|
highscoreHandler = new HighscoreHandler(mapString);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Öffnet einen File Chooser um eine Custom Map auszuwählen, welche anschließend geladen wird.
|
* Öffnet einen File Chooser um eine Custom Map auszuwählen, welche anschließend geladen wird.
|
||||||
@@ -63,7 +62,10 @@ public class MapEngine {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
this.createTiles(input);
|
this.createTiles(input);
|
||||||
mapHighscore = new HighscoreHandler(mapString);
|
highscoreHandler = new HighscoreHandler(mapString);
|
||||||
|
}
|
||||||
|
public HighscoreHandler getHighscoreHandler() {
|
||||||
|
return highscoreHandler;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Erstellt aus einem gegebenen Scanner, welche eine Datei einliest,
|
* Erstellt aus einem gegebenen Scanner, welche eine Datei einliest,
|
||||||
@@ -260,4 +262,7 @@ public class MapEngine {
|
|||||||
gamePanel.add(currentStaticObject);
|
gamePanel.add(currentStaticObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public int getMapHash() {
|
||||||
|
return highscoreHandler.getCurrentMapHash();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class Main extends JFrame implements Runnable {
|
|||||||
|
|
||||||
private boolean running = false;
|
private boolean running = false;
|
||||||
private static boolean pause = false;
|
private static boolean pause = false;
|
||||||
private boolean fullscreen = true;
|
private boolean fullscreen = false;
|
||||||
|
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import java.awt.Point;
|
|||||||
|
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
import de.heatup.highscore.ScoreEntry;
|
||||||
|
import de.heatup.logging.Logger;
|
||||||
import de.heatup.mapengine.MapEngine;
|
import de.heatup.mapengine.MapEngine;
|
||||||
import de.heatup.mapengine.SpawnAssistant;
|
import de.heatup.mapengine.SpawnAssistant;
|
||||||
import de.heatup.objects.ImageLoader;
|
import de.heatup.objects.ImageLoader;
|
||||||
@@ -86,6 +88,20 @@ public class PanelBuilder {
|
|||||||
}
|
}
|
||||||
me.addAllToPanel(gamePanel);
|
me.addAllToPanel(gamePanel);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Highscore tests
|
||||||
|
*/
|
||||||
|
|
||||||
|
// for (ScoreEntry score : me.getHighscoreHandler().getHighscores(me.getMapHash())) {
|
||||||
|
// Logger.write(score.getName()+" "+score.getScore());
|
||||||
|
// }
|
||||||
|
// me.getHighscoreHandler().setHighscore(me.getMapHash(), "Daniel", 3134);
|
||||||
|
// me.getHighscoreHandler().saveHighscores();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ende Highscore tests
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
for (Player movingObject : instancedObjects.getObjects()) {
|
for (Player movingObject : instancedObjects.getObjects()) {
|
||||||
Point spawnPoint = SpawnAssistant.getPathLocationOnGrid();
|
Point spawnPoint = SpawnAssistant.getPathLocationOnGrid();
|
||||||
|
|||||||
Reference in New Issue
Block a user