Save file

This commit is contained in:
Makussu 2024-06-09 21:17:06 +02:00
parent e2365edb51
commit a7aa6a579e
9 changed files with 254 additions and 40 deletions

View File

@ -7,11 +7,17 @@
* Spielbarkeit
** DONE User Interaktion mit Maus oder Tastatur
** DONE Ziel und Verbesserungsmöglichkeit (z.B. durch Punkte)
*** TODO Zombies random spawn
*** DONE Zombies random spawn
*** TODO reales level
vllt https://limezu.itch.io/modernexteriors
** TODO Persistierung des Spielstandes (abspeichern)
*** DONE only 1 save or pick save
only one save
*** TODO dont show full intro when save exists
* Dokumentation
** TODO Kurze Präsentation (10 min) im letzten Labortermin (eine pro Gruppe)
** DONE JavaDoc-Dokumentation
** TODO Klassendiagramm
* Spaß
** level system

View File

@ -5,7 +5,7 @@ import processing.core.PApplet;
*/
public class Enemy extends Fightable implements Health, Interactable {
int health;
int maxHealth = 20;
int maxHealth;
Enemy(Fenster window) {
super(window);
@ -13,6 +13,16 @@ public class Enemy extends Fightable implements Health, Interactable {
this.health = maxHealth;
this.collidable = true;
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) {

View File

@ -2,9 +2,8 @@ import processing.core.PApplet;
import processing.core.PFont;
import processing.sound.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.IOException;
import java.util.List;
/**
@ -12,6 +11,8 @@ import java.util.Arrays;
*/
public class Fenster extends PApplet {
Score save;
FileHandleUtil savefile = FileHandleUtil.getInstance();
Player player;
Tiles tiles;
@ -52,7 +53,9 @@ public class Fenster extends PApplet {
public void settings() {
size(800, 800);
setupPlayer();
classSetup();
tiles.settings();
@ -86,7 +89,7 @@ public class Fenster extends PApplet {
@Override
public void draw() {
System.out.println(frameRate);
// System.out.println(frameRate);
if(player.pos.get().x > width) {
translate(-800, 0);
@ -144,6 +147,14 @@ public class Fenster extends PApplet {
if (key == 'w') {
dead();
}
if (key == 'u') {
try {
closeGame();
} catch (IOException e) {
throw new RuntimeException(e);
}
exit();
}
startScreen.keyPressed();
gameScreen.keyPressed();
fightScreen.keyPressed();
@ -156,8 +167,32 @@ public class Fenster extends PApplet {
deathScreen.mousePressed();
}
void classSetup() {
void setupPlayer() {
try {
List<Score> allSaves = savefile.readLatestScores();
if (allSaves.size() == 0) {
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);
startScreen = new StartScreen(this);
gameScreen = new GameScreen(this);
@ -174,6 +209,7 @@ public class Fenster extends PApplet {
}
public void reset() {
player = new Player(this);
classSetup();
soundTrack.play();
deathScreen.isState = false;

94
src/FileHandleUtil.java Normal file
View 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));
}
}

View File

@ -1,4 +1,5 @@
import processing.core.PApplet;
import processing.core.PVector;
import java.util.ArrayList;
import java.util.List;
@ -22,11 +23,13 @@ public class Player extends Fightable implements Health {
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);
this.experience = exp;
this.pos = pos;
this.level = level;
this.type = "@";
this.level = exp/100;
this.health = health;
pos.set_on_grid((int) position.x, (int) position.y);
}
public void damage(int damage) {

View File

@ -11,6 +11,11 @@ public class Position {
Position(Fenster window) {
this.window = window;
}
Position(Fenster window, PVector position) {
this.window = window;
this.x = position.x;
this.y = position.y;
}
public PVector get() {
return new PVector(x, y);

View File

@ -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
View 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);
}
}

View File

@ -106,9 +106,11 @@ public class Tiles implements Drawable {
void spawn_zombie() {
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++;
System.out.println("Number of Zombies: " + num_enemies);
}
}
public Character get(int x,int y) {