Compare commits
7 Commits
f15850b022
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
5dbf7fcf1f
|
|||
|
70eaa9514a
|
|||
|
befb117d85
|
|||
|
54a2ee3022
|
|||
|
ce3f590715
|
|||
|
39fc7d549a
|
|||
|
8902e28d31
|
@@ -4,6 +4,8 @@ import de.miaurizius.jgame2d.aStar.PathFinder;
|
||||
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||
import de.miaurizius.jgame2d.core.enums.Map;
|
||||
import de.miaurizius.jgame2d.core.handlers.*;
|
||||
import de.miaurizius.jgame2d.data.Config;
|
||||
import de.miaurizius.jgame2d.data.SaveLoad;
|
||||
import de.miaurizius.jgame2d.entity.Entity;
|
||||
import de.miaurizius.jgame2d.entity.Player;
|
||||
import de.miaurizius.jgame2d.environment.EnvironmentManager;
|
||||
@@ -61,6 +63,7 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
public PathFinder pFinder = new PathFinder(this);
|
||||
public EnvironmentManager eManager = new EnvironmentManager(this);
|
||||
public MiniMap mapMan = new MiniMap(this);
|
||||
public SaveLoad saveLoad = new SaveLoad(this);
|
||||
Thread gameThread;
|
||||
|
||||
// ENTITY AND OBJECT
|
||||
@@ -263,18 +266,16 @@ public class GamePanel extends JPanel implements Runnable {
|
||||
fScreenWidth = Boot.window.getWidth();
|
||||
fScreenHeight = Boot.window.getHeight();
|
||||
}
|
||||
public void retry() {
|
||||
public void resetGame(boolean restart) {
|
||||
player.setDefaultPositions();
|
||||
player.restoreLife();
|
||||
player.restoreStatus();
|
||||
assetSetter.setNPC();
|
||||
assetSetter.setMonster();
|
||||
}
|
||||
public void restart() {
|
||||
|
||||
if(!restart) return;
|
||||
player.setDefaultValues();
|
||||
assetSetter.setObject();
|
||||
assetSetter.setNPC();
|
||||
assetSetter.setMonster();
|
||||
assetSetter.setITiles();
|
||||
eManager.lighting.resetDay();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -736,6 +736,7 @@ public class UI {
|
||||
if(commandNum == 1) {
|
||||
graphics2d.drawString(">", textX-25, textY);
|
||||
if(panel.keyH.spacePressed) optionState = OptionState.OVERVIEW;
|
||||
panel.resetGame(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.miaurizius.jgame2d.core.handlers;
|
||||
|
||||
import de.miaurizius.jgame2d.core.Boot;
|
||||
import de.miaurizius.jgame2d.core.GamePanel;
|
||||
import de.miaurizius.jgame2d.core.enums.Direction;
|
||||
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||
@@ -7,6 +8,7 @@ import de.miaurizius.jgame2d.core.enums.Map;
|
||||
import de.miaurizius.jgame2d.entity.Entity;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class EventHandler {
|
||||
|
||||
@@ -104,10 +106,15 @@ public class EventHandler {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.player.attackCancel = true;
|
||||
panel.playSE(2);
|
||||
panel.ui.currentDialogue = "You drank the holy water.\nYour life has been recovered!\nBut all monsters respawned... \nGood luck!";
|
||||
panel.player.life = panel.player.maxLife;
|
||||
panel.ui.currentDialogue = "You saved your progress!";
|
||||
//panel.player.life = panel.player.maxLife;
|
||||
canTouchEvent = false;
|
||||
panel.assetSetter.setMonster();
|
||||
try {
|
||||
panel.saveLoad.save();
|
||||
} catch (Exception e) {
|
||||
Boot.logger.log(Level.SEVERE, "Saving game failed!", e);
|
||||
}
|
||||
}
|
||||
private void changeMap(Map map, int col, int row) {
|
||||
panel.gameState = GameState.TRANSITION;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.miaurizius.jgame2d.core.handlers;
|
||||
|
||||
import de.miaurizius.jgame2d.core.Boot;
|
||||
import de.miaurizius.jgame2d.core.GamePanel;
|
||||
import de.miaurizius.jgame2d.core.UI;
|
||||
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||
@@ -7,6 +8,7 @@ import de.miaurizius.jgame2d.core.enums.Map;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class KeyHandler implements KeyListener {
|
||||
|
||||
@@ -34,7 +36,15 @@ public class KeyHandler implements KeyListener {
|
||||
panel.playMusic(0);
|
||||
break;
|
||||
case 1:
|
||||
// add later
|
||||
System.out.printf("Loading config...%n");
|
||||
try {
|
||||
panel.config.load();
|
||||
panel.saveLoad.load();
|
||||
panel.gameState = GameState.PLAY;
|
||||
panel.playMusic(0);
|
||||
} catch (Exception e) {
|
||||
Boot.logger.log(Level.SEVERE, "Failed to load config", e);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
System.exit(0);
|
||||
@@ -152,12 +162,12 @@ public class KeyHandler implements KeyListener {
|
||||
if(panel.ui.commandNum == 0) {
|
||||
panel.gameState = GameState.PLAY;
|
||||
panel.playMusic(0);
|
||||
panel.retry();
|
||||
panel.resetGame(false);
|
||||
}
|
||||
if(panel.ui.commandNum == 1) {
|
||||
panel.gameState = GameState.TITLE;
|
||||
panel.ui.commandNum = 0;
|
||||
panel.restart();
|
||||
panel.resetGame(true);
|
||||
panel.stopMusic();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
package de.miaurizius.jgame2d.core;
|
||||
package de.miaurizius.jgame2d.data;
|
||||
|
||||
import de.miaurizius.jgame2d.core.GamePanel;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.HashMap;
|
||||
@@ -14,6 +16,7 @@ public class Config {
|
||||
this.panel = panel;
|
||||
for (Setting option : Setting.values()) settings.put(option.name, null);
|
||||
new File("gamedata").mkdirs();
|
||||
new File("gamedata/save").mkdirs();
|
||||
new File("gamedata/config").createNewFile();
|
||||
}
|
||||
|
||||
28
src/de/miaurizius/jgame2d/data/DataStorage.java
Normal file
28
src/de/miaurizius/jgame2d/data/DataStorage.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package de.miaurizius.jgame2d.data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DataStorage implements Serializable {
|
||||
|
||||
// PLAYER STATS
|
||||
int level;
|
||||
int maxLife;
|
||||
int life;
|
||||
int maxMana;
|
||||
int strength;
|
||||
int dexterity;
|
||||
int attack;
|
||||
int defense;
|
||||
int exp;
|
||||
int nextLevelExp;
|
||||
int coins;
|
||||
int worldX, worldY;
|
||||
|
||||
// PLAYER INVENTORY
|
||||
List<String> itemNames = new ArrayList<>();
|
||||
List<Integer> itemAmounts = new ArrayList<>();
|
||||
|
||||
|
||||
}
|
||||
82
src/de/miaurizius/jgame2d/data/SaveLoad.java
Normal file
82
src/de/miaurizius/jgame2d/data/SaveLoad.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package de.miaurizius.jgame2d.data;
|
||||
|
||||
import de.miaurizius.jgame2d.core.GamePanel;
|
||||
import de.miaurizius.jgame2d.entity.Entity;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class SaveLoad {
|
||||
|
||||
GamePanel panel;
|
||||
|
||||
public SaveLoad(GamePanel panel) {
|
||||
this.panel = panel;
|
||||
}
|
||||
|
||||
public Entity getObject(String itemName) {
|
||||
Entity obj;
|
||||
try {
|
||||
obj = (Entity) Class.forName(itemName).getDeclaredConstructor().newInstance(panel);
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
|
||||
InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void save() throws IOException {
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("gamedata/save/savegame.dat"));
|
||||
DataStorage ds = new DataStorage();
|
||||
|
||||
// PLAYER STATS
|
||||
ds.level = panel.player.level;
|
||||
ds.maxLife = panel.player.maxLife;
|
||||
ds.life = panel.player.life;
|
||||
ds.maxMana = panel.player.maxMana;
|
||||
ds.strength = panel.player.strength;
|
||||
ds.dexterity = panel.player.dexterity;
|
||||
ds.attack = panel.player.attack;
|
||||
ds.defense = panel.player.defense;
|
||||
ds.exp = panel.player.exp;
|
||||
ds.nextLevelExp = panel.player.nextLevelExp;
|
||||
ds.coins = panel.player.coins;
|
||||
ds.worldX = panel.player.worldX;
|
||||
ds.worldY = panel.player.worldY;
|
||||
|
||||
// PLAYER INVENTORY
|
||||
for(int i = 0; i < panel.player.inventory.size(); i++) {
|
||||
ds.itemNames.add(panel.player.inventory.get(i).getClass().getName());
|
||||
ds.itemAmounts.add(panel.player.inventory.get(i).amt);
|
||||
}
|
||||
|
||||
// WRITE
|
||||
oos.writeObject(ds);
|
||||
}
|
||||
|
||||
public void load() throws ClassNotFoundException, IOException {
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("gamedata/save/savegame.dat"));
|
||||
DataStorage ds = (DataStorage) ois.readObject();
|
||||
|
||||
panel.player.level = ds.level;
|
||||
panel.player.maxLife = ds.maxLife;
|
||||
panel.player.life = ds.life;
|
||||
panel.player.maxMana = ds.maxMana;
|
||||
panel.player.strength = ds.strength;
|
||||
panel.player.dexterity = ds.dexterity;
|
||||
panel.player.attack = ds.attack;
|
||||
panel.player.defense = ds.defense;
|
||||
panel.player.exp = ds.exp;
|
||||
panel.player.nextLevelExp = ds.nextLevelExp;
|
||||
panel.player.coins = ds.coins;
|
||||
panel.player.worldX = ds.worldX;
|
||||
panel.player.worldY = ds.worldY;
|
||||
|
||||
for(int i = 0; i < ds.itemNames.size(); i++) {
|
||||
Entity obj = getObject(ds.itemNames.get(i));
|
||||
obj.amt = ds.itemAmounts.get(i);
|
||||
panel.player.inventory.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@@ -80,7 +80,7 @@ public class Entity {
|
||||
public Entity currentShield;
|
||||
public Entity currentLight;
|
||||
public Projectile projectile;
|
||||
public ArrayList<Entity> inventory = new ArrayList<>();
|
||||
public List<Entity> inventory = new ArrayList<>();
|
||||
public final int maxInvSize = 20;
|
||||
|
||||
// ITEM ATTRIBUTES
|
||||
@@ -327,10 +327,10 @@ public class Entity {
|
||||
int nextWorldY = user.getTopY();
|
||||
|
||||
switch(user.direction) {
|
||||
case UP -> nextWorldY = user.getTopY()-1;
|
||||
case DOWN -> nextWorldY = user.getBottomY()+1;
|
||||
case LEFT -> nextWorldX = user.getLeftX()-1;
|
||||
case RIGHT -> nextWorldX = user.getRightX()+1;
|
||||
case UP -> nextWorldY = user.getTopY()-panel.player.speed;
|
||||
case DOWN -> nextWorldY = user.getBottomY()+panel.player.speed;
|
||||
case LEFT -> nextWorldX = user.getLeftX()-panel.player.speed;
|
||||
case RIGHT -> nextWorldX = user.getRightX()+panel.player.speed;
|
||||
}
|
||||
int col = nextWorldX / panel.tileSize;
|
||||
int row = nextWorldY / panel.tileSize;
|
||||
|
||||
@@ -311,9 +311,14 @@ public class Player extends Entity {
|
||||
worldY = panel.tileSize * 21;
|
||||
direction = Direction.DOWN;
|
||||
}
|
||||
public void restoreLife() {
|
||||
public void restoreStatus() {
|
||||
life = maxLife;
|
||||
invincible = false;
|
||||
transparent = false;
|
||||
attacking = false;
|
||||
guarding = false;
|
||||
knockback = false;
|
||||
lightUpdated = true;
|
||||
}
|
||||
public void getSleepingImage(BufferedImage image) {
|
||||
down1 = image;
|
||||
@@ -346,6 +351,7 @@ public class Player extends Entity {
|
||||
coins = 500;
|
||||
currentWeapon = new SwordNormalObj(panel);
|
||||
currentShield = new ShieldWoodObj(panel);
|
||||
currentLight = null;
|
||||
projectile = new FireballObj(panel);
|
||||
attack = getAttack();
|
||||
defense = getDefense();
|
||||
|
||||
@@ -75,6 +75,11 @@ public class Lighting {
|
||||
}
|
||||
}
|
||||
}
|
||||
public void resetDay() {
|
||||
dayState = DayState.DAY;
|
||||
filterAlpha = 0f;
|
||||
dayCount = 0;
|
||||
}
|
||||
|
||||
// ...
|
||||
public void setLightSource() {
|
||||
|
||||
Reference in New Issue
Block a user