Save file
This commit is contained in:
parent
e2365edb51
commit
a7aa6a579e
@ -7,11 +7,17 @@
|
|||||||
* Spielbarkeit
|
* Spielbarkeit
|
||||||
** DONE User Interaktion mit Maus oder Tastatur
|
** DONE User Interaktion mit Maus oder Tastatur
|
||||||
** DONE Ziel und Verbesserungsmöglichkeit (z.B. durch Punkte)
|
** DONE Ziel und Verbesserungsmöglichkeit (z.B. durch Punkte)
|
||||||
*** TODO Zombies random spawn
|
*** DONE Zombies random spawn
|
||||||
*** TODO reales level
|
*** TODO reales level
|
||||||
vllt https://limezu.itch.io/modernexteriors
|
vllt https://limezu.itch.io/modernexteriors
|
||||||
** TODO Persistierung des Spielstandes (abspeichern)
|
** TODO Persistierung des Spielstandes (abspeichern)
|
||||||
|
*** DONE only 1 save or pick save
|
||||||
|
only one save
|
||||||
|
*** TODO dont show full intro when save exists
|
||||||
* Dokumentation
|
* Dokumentation
|
||||||
** TODO Kurze Präsentation (10 min) im letzten Labortermin (eine pro Gruppe)
|
** TODO Kurze Präsentation (10 min) im letzten Labortermin (eine pro Gruppe)
|
||||||
** DONE JavaDoc-Dokumentation
|
** DONE JavaDoc-Dokumentation
|
||||||
** TODO Klassendiagramm
|
** TODO Klassendiagramm
|
||||||
|
|
||||||
|
* Spaß
|
||||||
|
** level system
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import processing.core.PApplet;
|
|||||||
*/
|
*/
|
||||||
public class Enemy extends Fightable implements Health, Interactable {
|
public class Enemy extends Fightable implements Health, Interactable {
|
||||||
int health;
|
int health;
|
||||||
int maxHealth = 20;
|
int maxHealth;
|
||||||
|
|
||||||
Enemy(Fenster window) {
|
Enemy(Fenster window) {
|
||||||
super(window);
|
super(window);
|
||||||
@ -13,6 +13,16 @@ public class Enemy extends Fightable implements Health, Interactable {
|
|||||||
this.health = maxHealth;
|
this.health = maxHealth;
|
||||||
this.collidable = true;
|
this.collidable = true;
|
||||||
this.color = window.color(200, 0, 0);
|
this.color = window.color(200, 0, 0);
|
||||||
|
maxHealth = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
Enemy(Fenster window, int maxHealth) {
|
||||||
|
super(window);
|
||||||
|
this.type = "Z";
|
||||||
|
this.maxHealth = maxHealth;
|
||||||
|
this.health = maxHealth;
|
||||||
|
this.collidable = true;
|
||||||
|
this.color = window.color(200, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void damage(int damage) {
|
public void damage(int damage) {
|
||||||
|
|||||||
@ -2,9 +2,8 @@ import processing.core.PApplet;
|
|||||||
import processing.core.PFont;
|
import processing.core.PFont;
|
||||||
import processing.sound.*;
|
import processing.sound.*;
|
||||||
|
|
||||||
import java.awt.event.KeyEvent;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -12,6 +11,8 @@ import java.util.Arrays;
|
|||||||
*/
|
*/
|
||||||
public class Fenster extends PApplet {
|
public class Fenster extends PApplet {
|
||||||
|
|
||||||
|
Score save;
|
||||||
|
FileHandleUtil savefile = FileHandleUtil.getInstance();
|
||||||
Player player;
|
Player player;
|
||||||
Tiles tiles;
|
Tiles tiles;
|
||||||
|
|
||||||
@ -52,7 +53,9 @@ public class Fenster extends PApplet {
|
|||||||
public void settings() {
|
public void settings() {
|
||||||
size(800, 800);
|
size(800, 800);
|
||||||
|
|
||||||
|
setupPlayer();
|
||||||
classSetup();
|
classSetup();
|
||||||
|
|
||||||
tiles.settings();
|
tiles.settings();
|
||||||
|
|
||||||
|
|
||||||
@ -86,7 +89,7 @@ public class Fenster extends PApplet {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw() {
|
public void draw() {
|
||||||
System.out.println(frameRate);
|
// System.out.println(frameRate);
|
||||||
|
|
||||||
if(player.pos.get().x > width) {
|
if(player.pos.get().x > width) {
|
||||||
translate(-800, 0);
|
translate(-800, 0);
|
||||||
@ -144,6 +147,14 @@ public class Fenster extends PApplet {
|
|||||||
if (key == 'w') {
|
if (key == 'w') {
|
||||||
dead();
|
dead();
|
||||||
}
|
}
|
||||||
|
if (key == 'u') {
|
||||||
|
try {
|
||||||
|
closeGame();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
exit();
|
||||||
|
}
|
||||||
startScreen.keyPressed();
|
startScreen.keyPressed();
|
||||||
gameScreen.keyPressed();
|
gameScreen.keyPressed();
|
||||||
fightScreen.keyPressed();
|
fightScreen.keyPressed();
|
||||||
@ -156,8 +167,32 @@ public class Fenster extends PApplet {
|
|||||||
deathScreen.mousePressed();
|
deathScreen.mousePressed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void classSetup() {
|
void setupPlayer() {
|
||||||
|
try {
|
||||||
|
List<Score> allSaves = savefile.readLatestScores();
|
||||||
|
if (allSaves.size() == 0) {
|
||||||
player = new Player(this);
|
player = new Player(this);
|
||||||
|
} else {
|
||||||
|
save = allSaves.getLast();
|
||||||
|
player = new Player(this, save.getScore(), save.getPosition(), save.getHealth());
|
||||||
|
System.out.println(save.getPosition().x + " : " + save.getPosition().y);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
player = new Player(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void closeGame() throws IOException {
|
||||||
|
Score score = new Score(1, player.experience, player.pos.get_grid(), player.health);
|
||||||
|
try {
|
||||||
|
savefile.addScore(score);
|
||||||
|
} catch (IOException e) {
|
||||||
|
savefile.createFile();
|
||||||
|
savefile.addScore(score);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void classSetup() {
|
||||||
tiles = new Tiles(this, 40.0f);
|
tiles = new Tiles(this, 40.0f);
|
||||||
startScreen = new StartScreen(this);
|
startScreen = new StartScreen(this);
|
||||||
gameScreen = new GameScreen(this);
|
gameScreen = new GameScreen(this);
|
||||||
@ -174,6 +209,7 @@ public class Fenster extends PApplet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
|
player = new Player(this);
|
||||||
classSetup();
|
classSetup();
|
||||||
soundTrack.play();
|
soundTrack.play();
|
||||||
deathScreen.isState = false;
|
deathScreen.isState = false;
|
||||||
|
|||||||
94
src/FileHandleUtil.java
Normal file
94
src/FileHandleUtil.java
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public final class FileHandleUtil {
|
||||||
|
|
||||||
|
private static FileHandleUtil INSTANCE;
|
||||||
|
final String FILE_PATH = "./savefile.txt";
|
||||||
|
|
||||||
|
private FileHandleUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FileHandleUtil getInstance() {
|
||||||
|
if(INSTANCE == null) {
|
||||||
|
INSTANCE = new FileHandleUtil();
|
||||||
|
}
|
||||||
|
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* reads all scores from file
|
||||||
|
* @return scores sorted by rank
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public List<Score> readScores() throws IOException {
|
||||||
|
List<Score> scores = new ArrayList<Score>();
|
||||||
|
List<String> lines = Files.readAllLines(Paths.get(FILE_PATH));
|
||||||
|
for (String line : lines) {
|
||||||
|
scores.add(Score.fromString(line));
|
||||||
|
}
|
||||||
|
return scores;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* reads all scores from file
|
||||||
|
* @return scores sorted by datetime
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public List<Score> readLatestScores() throws IOException {
|
||||||
|
List<Score> scores = this.readScores();
|
||||||
|
|
||||||
|
scores.sort(Comparator.comparing(Score::getDate));
|
||||||
|
|
||||||
|
return scores;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* adds a score, reorganizes the rank ids and writes to file
|
||||||
|
* @param score
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public void addScore(Score score) throws IOException {
|
||||||
|
List<Score> scores = this.readScores();
|
||||||
|
scores.set(0, score);
|
||||||
|
|
||||||
|
scores.sort(Comparator.comparing(Score::getScore).thenComparing(Comparator.comparing(Score::getDate)));
|
||||||
|
|
||||||
|
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, false));
|
||||||
|
|
||||||
|
for (int i = 0; i < scores.size(); i++) {
|
||||||
|
scores.get(i).setRank(i+1);
|
||||||
|
writer.write(scores.get(i).toString());
|
||||||
|
writer.newLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* delete savefile
|
||||||
|
* @return boolean if file deleted
|
||||||
|
*/
|
||||||
|
public boolean deleteFile() {
|
||||||
|
try {
|
||||||
|
Files.delete(Paths.get(FILE_PATH));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createFile() throws IOException {
|
||||||
|
Files.createFile(Paths.get(FILE_PATH));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,4 +1,5 @@
|
|||||||
import processing.core.PApplet;
|
import processing.core.PApplet;
|
||||||
|
import processing.core.PVector;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -22,11 +23,13 @@ public class Player extends Fightable implements Health {
|
|||||||
this.skills.add(new Tritt(window, (Fightable) window.player));
|
this.skills.add(new Tritt(window, (Fightable) window.player));
|
||||||
}
|
}
|
||||||
|
|
||||||
Player(Fenster window, int exp, Position pos, int level) {
|
Player(Fenster window, int exp, PVector position, int health) {
|
||||||
super(window);
|
super(window);
|
||||||
this.experience = exp;
|
this.experience = exp;
|
||||||
this.pos = pos;
|
this.type = "@";
|
||||||
this.level = level;
|
this.level = exp/100;
|
||||||
|
this.health = health;
|
||||||
|
pos.set_on_grid((int) position.x, (int) position.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void damage(int damage) {
|
public void damage(int damage) {
|
||||||
|
|||||||
@ -11,6 +11,11 @@ public class Position {
|
|||||||
Position(Fenster window) {
|
Position(Fenster window) {
|
||||||
this.window = window;
|
this.window = window;
|
||||||
}
|
}
|
||||||
|
Position(Fenster window, PVector position) {
|
||||||
|
this.window = window;
|
||||||
|
this.x = position.x;
|
||||||
|
this.y = position.y;
|
||||||
|
}
|
||||||
|
|
||||||
public PVector get() {
|
public PVector get() {
|
||||||
return new PVector(x, y);
|
return new PVector(x, y);
|
||||||
|
|||||||
@ -1,28 +0,0 @@
|
|||||||
import java.io.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class for the savefile
|
|
||||||
*/
|
|
||||||
public class Save {
|
|
||||||
|
|
||||||
private static Save INSTANCE;
|
|
||||||
private String file = "./save";
|
|
||||||
|
|
||||||
private Save() {}
|
|
||||||
|
|
||||||
public static Save getInstance() {
|
|
||||||
if(INSTANCE == null) {
|
|
||||||
INSTANCE = new Save();
|
|
||||||
}
|
|
||||||
return INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save() {
|
|
||||||
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
|
|
||||||
new FileOutputStream(file), "utf-8"))) {
|
|
||||||
writer.write("something");
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
86
src/Score.java
Normal file
86
src/Score.java
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
import processing.core.PVector;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
public class Score {
|
||||||
|
|
||||||
|
private int rank;
|
||||||
|
private PVector position;
|
||||||
|
private int health;
|
||||||
|
private LocalDateTime date;
|
||||||
|
private int score;
|
||||||
|
|
||||||
|
public Score() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Score(int score) {
|
||||||
|
this.score = score;
|
||||||
|
this.date = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Score(int rank, int score, PVector position, int health) {
|
||||||
|
this.rank = rank;
|
||||||
|
this.score = score;
|
||||||
|
this.position = position;
|
||||||
|
this.health = health;
|
||||||
|
this.date = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Score(int rank, LocalDateTime date, int score, PVector position, int health) {
|
||||||
|
this.rank = rank;
|
||||||
|
this.date = date;
|
||||||
|
this.score = score;
|
||||||
|
this.position = position;
|
||||||
|
this.health = health;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRank() {
|
||||||
|
return rank;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRank(int rank) {
|
||||||
|
this.rank = rank;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDateTime getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDate(LocalDateTime date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getScore() {
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PVector getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHealth() {
|
||||||
|
return health;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(int score) {
|
||||||
|
this.score = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return rank + ";" + date.format(formatter) + ";" + score + ";" + position.x + ";" + position.y + ";" + health;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Score fromString(String line) {
|
||||||
|
String[] parts = line.split(";");
|
||||||
|
int rank = Integer.parseInt(parts[0]);
|
||||||
|
LocalDateTime date = LocalDateTime.parse(parts[1], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
int score = Integer.parseInt(parts[2]);
|
||||||
|
PVector position = new PVector(Float.parseFloat(parts[3]), Float.parseFloat(parts[4]));
|
||||||
|
int health = Integer.parseInt(parts[5]);
|
||||||
|
return new Score(rank, date, score, position, health);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -106,9 +106,11 @@ public class Tiles implements Drawable {
|
|||||||
|
|
||||||
void spawn_zombie() {
|
void spawn_zombie() {
|
||||||
if(num_enemies < 3) {
|
if(num_enemies < 3) {
|
||||||
tileMap[rand.nextInt(100)][rand.nextInt(100)].character = new Enemy(window);
|
tileMap[rand.nextInt(100)][rand.nextInt(100)].character = new Enemy(window, 20);
|
||||||
num_enemies++;
|
num_enemies++;
|
||||||
|
System.out.println("Number of Zombies: " + num_enemies);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Character get(int x,int y) {
|
public Character get(int x,int y) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user