enhance chest object handling and save/load functionality
This commit is contained in:
@@ -36,7 +36,8 @@ public class AssetSetter {
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*12;
|
||||
i++;
|
||||
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i] = new ChestObj(panel, new KeyObj(panel));
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i] = new ChestObj(panel);
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i].setLoot(new KeyObj(panel));
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i].worldX = panel.tileSize*30;
|
||||
panel.obj[Map.OVERWORLD.getIndex()][i].worldY = panel.tileSize*29;
|
||||
i++;
|
||||
|
||||
@@ -23,6 +23,14 @@ public class DataStorage implements Serializable {
|
||||
// PLAYER INVENTORY
|
||||
List<String> itemNames = new ArrayList<>();
|
||||
List<Integer> itemAmounts = new ArrayList<>();
|
||||
int currentWeaponSlot;
|
||||
int currentShieldSlot;
|
||||
|
||||
// MAP OBJECTS
|
||||
String[][] mapObjectNames;
|
||||
int[][] mapObjectWorldX;
|
||||
int[][] mapObjectWorldY;
|
||||
String[][] mapObjectLootNames;
|
||||
boolean[][] mapObjectOpened;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.miaurizius.jgame2d.data;
|
||||
|
||||
import de.miaurizius.jgame2d.core.GamePanel;
|
||||
import de.miaurizius.jgame2d.core.enums.Map;
|
||||
import de.miaurizius.jgame2d.entity.Entity;
|
||||
|
||||
import java.io.*;
|
||||
@@ -17,7 +18,7 @@ public class SaveLoad {
|
||||
public Entity getObject(String itemName) {
|
||||
Entity obj;
|
||||
try {
|
||||
obj = (Entity) Class.forName(itemName).getDeclaredConstructor().newInstance(panel);
|
||||
obj = (Entity) Class.forName(itemName).getDeclaredConstructor(GamePanel.class).newInstance(panel);
|
||||
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException |
|
||||
InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -50,6 +51,29 @@ public class SaveLoad {
|
||||
ds.itemAmounts.add(panel.player.inventory.get(i).amt);
|
||||
}
|
||||
|
||||
// PLAYER EQUIPMENT
|
||||
ds.currentWeaponSlot = panel.player.getCurrentWeaponSlot();
|
||||
ds.currentShieldSlot = panel.player.getCurrentShieldSlot();
|
||||
|
||||
// MAP OBJECTS
|
||||
ds.mapObjectNames = new String[Map.values().length][panel.obj[1].length];
|
||||
ds.mapObjectWorldX = new int[Map.values().length][panel.obj[1].length];
|
||||
ds.mapObjectWorldY = new int[Map.values().length][panel.obj[1].length];
|
||||
ds.mapObjectLootNames = new String[Map.values().length][panel.obj[1].length];
|
||||
ds.mapObjectOpened = new boolean[Map.values().length][panel.obj[1].length];
|
||||
|
||||
for(int mapNum = 0; mapNum < Map.values().length; mapNum++) {
|
||||
for(int i = 0; i < panel.obj[mapNum].length; i++) {
|
||||
if(panel.obj[mapNum][i] != null) {
|
||||
ds.mapObjectNames[mapNum][i] = panel.obj[mapNum][i].getClass().getName();
|
||||
ds.mapObjectWorldX[mapNum][i] = panel.obj[mapNum][i].worldX;
|
||||
ds.mapObjectWorldY[mapNum][i] = panel.obj[mapNum][i].worldY;
|
||||
if(panel.obj[mapNum][i].loot != null) ds.mapObjectLootNames[mapNum][i] = panel.obj[mapNum][i].loot.getClass().getName();
|
||||
ds.mapObjectOpened[mapNum][i] = panel.obj[mapNum][i].opened;
|
||||
} else ds.mapObjectNames[mapNum][i] = "NA";
|
||||
}
|
||||
}
|
||||
|
||||
// WRITE
|
||||
oos.writeObject(ds);
|
||||
}
|
||||
@@ -72,10 +96,33 @@ public class SaveLoad {
|
||||
panel.player.worldX = ds.worldX;
|
||||
panel.player.worldY = ds.worldY;
|
||||
|
||||
|
||||
// PLAYER INVENTORY
|
||||
panel.player.inventory.clear();
|
||||
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);
|
||||
panel.player.inventory.add(getObject(ds.itemNames.get(i)));
|
||||
panel.player.inventory.get(i).amt = ds.itemAmounts.get(i);
|
||||
}
|
||||
|
||||
// PLAYER EQUIPMENT
|
||||
panel.player.currentWeapon = panel.player.inventory.get(ds.currentWeaponSlot);
|
||||
panel.player.currentShield = panel.player.inventory.get(ds.currentShieldSlot);
|
||||
panel.player.getAttack();
|
||||
panel.player.getDefense();
|
||||
panel.player.getPlayerAttackImage();
|
||||
|
||||
// MAP OBJECTS
|
||||
for(int mapNum = 0; mapNum < Map.values().length; mapNum++) {
|
||||
for (int i = 0; i < ds.mapObjectNames[mapNum].length; i++) {
|
||||
if (!ds.mapObjectNames[mapNum][i].equals("NA")) {
|
||||
panel.obj[mapNum][i] = getObject(ds.mapObjectNames[mapNum][i]);
|
||||
panel.obj[mapNum][i].worldX = ds.mapObjectWorldX[mapNum][i];
|
||||
panel.obj[mapNum][i].worldY = ds.mapObjectWorldY[mapNum][i];
|
||||
if (ds.mapObjectLootNames[mapNum][i] != null) panel.obj[mapNum][i].loot = getObject(ds.mapObjectLootNames[mapNum][i]);
|
||||
panel.obj[mapNum][i].opened = ds.mapObjectOpened[mapNum][i];
|
||||
if(panel.obj[mapNum][i].opened) panel.obj[mapNum][i].down1 = panel.obj[mapNum][i].image2;
|
||||
} else panel.obj[mapNum][i] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -95,6 +95,8 @@ public class Entity {
|
||||
public boolean stackable;
|
||||
public int amt = 1;
|
||||
public float lightRadius;
|
||||
public boolean opened;
|
||||
public Entity loot;
|
||||
|
||||
public Entity(GamePanel panel) {
|
||||
this.panel = panel;
|
||||
@@ -354,6 +356,7 @@ public class Entity {
|
||||
target.speed += knockbackVal;
|
||||
target.knockback = true;
|
||||
}
|
||||
public void setLoot(Entity loot) {}
|
||||
|
||||
// PARTICLE SETUP
|
||||
public Color getParticleColor() {
|
||||
|
||||
@@ -369,6 +369,18 @@ public class Player extends Entity {
|
||||
public int getDefense() {
|
||||
return defense = dexterity * currentShield.defenseValue;
|
||||
}
|
||||
public int getCurrentWeaponSlot() {
|
||||
for(int i = 0; i < inventory.size(); i++) {
|
||||
if(inventory.get(i) == currentWeapon) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public int getCurrentShieldSlot() {
|
||||
for(int i = 0; i < inventory.size(); i++) {
|
||||
if(inventory.get(i) == currentShield) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public void getPlayerImage() {
|
||||
up1 = initEntitySprites("player/boy_up_1");
|
||||
up2 = initEntitySprites("player/boy_up_2");
|
||||
|
||||
@@ -8,13 +8,10 @@ import de.miaurizius.jgame2d.entity.Entity;
|
||||
public class ChestObj extends Entity {
|
||||
|
||||
GamePanel panel;
|
||||
Entity loot;
|
||||
boolean opened;
|
||||
|
||||
public ChestObj(GamePanel panel, Entity loot) {
|
||||
public ChestObj(GamePanel panel) {
|
||||
super(panel);
|
||||
this.panel = panel;
|
||||
this.loot = loot;
|
||||
type = EntityType.OBSTACLE;
|
||||
name = "chest";
|
||||
|
||||
@@ -31,11 +28,16 @@ public class ChestObj extends Entity {
|
||||
solidAreaDefaultY = solidArea.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoot(Entity loot) {
|
||||
this.loot = loot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interact() {
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
|
||||
if(opened) {
|
||||
if(this.opened) {
|
||||
panel.ui.currentDialogue = "It's already empty...";
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user