Skip to content

Commit

Permalink
save system (not load)
Browse files Browse the repository at this point in the history
  • Loading branch information
MutantPiggieGolem1 committed Jul 23, 2022
1 parent 0984416 commit e38e37d
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 54 deletions.
14 changes: 14 additions & 0 deletions saves.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"1658608215536": {
"health": 100,
"state": "bottomfloor",
"inventory": [],
"version": 1
},
"1658608520186": {
"health": 100,
"state": "bottomfloor",
"inventory": [],
"version": 1
}
}
52 changes: 44 additions & 8 deletions src/main/java/hugone/Area.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
Expand Down Expand Up @@ -54,12 +55,17 @@ public Area(String id) throws JSONException {
JSONArray furnituredata = data.getJSONArray("furniture");
for (int i = 0; i < furnituredata.length(); i++) {
JSONObject furn = furnituredata.getJSONObject(i);
Furniture furni = new Furniture(furn, this);
this.furniture.add(furni);
switch (furn.getString("objectid")) {
case "exit":
this.furniture.add(new Exit(furn, this));
break;
case "save":
this.furniture.add(new Savepoint(furn, this));
break;
default:
this.furniture.add(new Furniture(furn, this));
}
}

JSONObject exitdata = data.getJSONObject("exit");
this.furniture.add(new Exit(exitdata, this));
}

public void init() {
Expand Down Expand Up @@ -150,18 +156,18 @@ public String getNext() {

class Furniture implements InteractableObject {
public final String id;
protected Area area;
protected final Area area;
private Image image;
private String item;
private Dialogues dialogue;
protected Dialogues dialogue;
private Rectangle pos;
private boolean collide;
private boolean interacted = false;

public Furniture(JSONObject data, Area area) throws JSONException {
this.area = area;

this.id = data.has("objectid") ? data.getString("objectid") : null;
this.id = data.getString("objectid");
this.item = data.has("item") ? data.getString("item") : null;
this.collide = data.has("collide") ? data.getBoolean("collide") : true;
if (data.has("dialogue")) {
Expand All @@ -176,6 +182,14 @@ public Furniture(JSONObject data, Area area) throws JSONException {
this.pos = new Rectangle(location.getInt(0), location.getInt(1), dimension.getInt(0), this.image.getHeight());
}

protected Furniture(String id, Rectangle pos, Image image, boolean collide, Area area) {
this.id = id;
this.pos = pos;
this.image = image;
this.collide = collide;
this.area = area;
}

public boolean collidesWith(Rectangle r) {
return this.collide && this.pos.intersects(r);
}
Expand Down Expand Up @@ -205,4 +219,26 @@ public void onInteraction() {
this.area.setDialogue(App.story.getDialogue("cant_exit"));
}
}
}

class Savepoint extends Furniture {
public Savepoint(JSONObject data, Area area) {
super(data.getString("objectid"), new Rectangle(
data.getJSONArray("location").getInt(0),
data.getJSONArray("location").getInt(1),
10, 10
), Constants.SAVEICON, true, area);
this.dialogue = Constants.SAVEDIALOGUE;
}

@Override
public void onInteraction() {
try {
App.story.save();
this.area.setDialogue(this.dialogue);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
}
25 changes: 13 additions & 12 deletions src/main/java/hugone/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ public class Character {

protected int health = -1;
protected Rectangle pos = new Rectangle();
public final HashMap<MoveState, MoveState> movemap = new HashMap<MoveState, MoveState>();
public static final HashMap<MoveState, MoveState> movemap = new HashMap<MoveState, MoveState>();
static {
movemap.put(MoveState.STOP, MoveState.STOP);
movemap.put(MoveState.MOVE0, MoveState.MOVE1);
movemap.put(MoveState.MOVE1, MoveState.MOVE2);
movemap.put(MoveState.MOVE2, MoveState.MOVE0);
movemap.put(MoveState.RUN1, MoveState.RUN2);
movemap.put(MoveState.RUN2, MoveState.RUN1);
}

private MoveState lastmovestate = MoveState.STOP;

Expand Down Expand Up @@ -67,22 +75,15 @@ public Character(String id) throws JSONException {
}
}
}
Image eximg = this.directions.get(Direction.UP).get(MoveState.STOP);
int h = Constants.CHARACTERSIZE;
if (eximg != null) h = eximg.getHeight();
this.pos = new Rectangle(0,0,Constants.CHARACTERSIZE,h);
}
Image eximg = this.directions.get(Direction.UP).get(MoveState.STOP);
int h = Constants.CHARACTERSIZE;
if (eximg != null) h = eximg.getHeight();
this.pos = new Rectangle(0,0,Constants.CHARACTERSIZE,h);

if (data.has("health")) {
this.health = data.getInt("health");
}

movemap.put(MoveState.STOP, MoveState.STOP);
movemap.put(MoveState.MOVE0, MoveState.MOVE1);
movemap.put(MoveState.MOVE1, MoveState.MOVE2);
movemap.put(MoveState.MOVE2, MoveState.MOVE0);
movemap.put(MoveState.RUN1, MoveState.RUN2);
movemap.put(MoveState.RUN2, MoveState.RUN1);
}

protected void setName(String name) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/hugone/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;

import hugone.util.Image;

public class Constants {
public static enum GameState {
MENU,
Expand Down Expand Up @@ -85,6 +87,8 @@ public static interface Feature {
public static final double FPS = REFRESHRATE; // 60.0 usually
public static final double TPS = 20.0;

public static final Image SAVEICON = new Image("save.png");
public static final Dialogues SAVEDIALOGUE = new Dialogues("save");
public class Battle {
public static final int MINNOTEMOVE = 2;
public static final int HITMARGIN = 150;
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/hugone/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class Player extends Character {
private boolean spacedown = false;

public ArrayList<String> inventory = new ArrayList<String>();
public int money;
// private ArrayList<Character> followers = new ArrayList<Character>();

public Player(String name) throws JSONException {
Expand Down Expand Up @@ -86,11 +85,6 @@ public void reccieveKeyPress(KeyEvent e, KeyPress p) {

public void render(Graphics2D g) {
super.render(this.direction, this.movestate, g);
// MoveState ms = this.movestate;
// for (Character c : this.followers.toArray(new Character[]{})) {
// ms = movemap.get(ms);
// c.render(this.direction, ms, g);
// }
}

public boolean spaceDown() {
Expand Down Expand Up @@ -123,7 +117,7 @@ public void moveLoop() {
} // dont move while stopped

if (Math.round(this.framenum % (this.sprinting ? 1.4 : 2)) == 0L)
this.movestate = super.movemap.get(this.movestate); // update the move state
this.movestate = Character.movemap.get(this.movestate); // update the move state
this.framenum++;

Rectangle goal = this.facingTowards(true);
Expand All @@ -146,6 +140,5 @@ public void stopMovement() {
public void respawn() {
this.health = 100;
this.inventory = new ArrayList<String>();
this.money -= 10;
}
}
20 changes: 20 additions & 0 deletions src/main/java/hugone/Story.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package hugone;

import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import hugone.Constants.Feature;
import hugone.Constants.GameState;
import hugone.util.Utils;

public class Story {
public Player player;
Expand All @@ -21,6 +24,7 @@ public class Story {

private String current; // id of current
public String checkpoint; // id of last died
public final int version;

public final JSONObject data;

Expand All @@ -32,6 +36,7 @@ public Story(String filename) {
}
s.close();
this.data = new JSONObject(out);
this.version = this.data.getInt("version");
}

public void init() throws JSONException {
Expand Down Expand Up @@ -125,4 +130,19 @@ public Feature getCurrent() {
return null;
}
}

private static final String savedir = "saves.json";
public void save() throws IOException {
JSONObject out = new JSONObject()
.put("health",this.player.health)
.put("inventory",this.player.inventory)
.put("state",this.current)
.put("checkpoint",this.checkpoint)
.put("version",this.version);
String filecontent = Utils.readFile(savedir);
JSONObject savedata = filecontent == null ? new JSONObject() : new JSONObject(new JSONTokener(filecontent));
savedata.put(""+System.currentTimeMillis(), out);
if (Utils.writeFile(savedir, savedata.toString())) return;
throw new IOException("Could not save game data.");
}
}
22 changes: 22 additions & 0 deletions src/main/java/hugone/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package hugone.util;

import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -125,4 +129,22 @@ public static double expoDelta(double cur, double goal) {
return goal; // snap
return (int) (cur + (goal - cur) * mul);
}

public static String readFile(String filepath) {
try (InputStream s = new FileInputStream(filepath)) {
return new String(s.readAllBytes(), StandardCharsets.UTF_8);
} catch (Exception e) {
return null;
}
}

public static boolean writeFile(String filepath, String toWrite) {
try (FileWriter writer = new FileWriter(filepath)) {
writer.write(toWrite);
writer.close();
return true;
} catch (Exception e) {
return false;
}
}
}
Binary file added src/main/resources/save.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/saved.wav
Binary file not shown.
49 changes: 36 additions & 13 deletions src/main/resources/story.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
}
},
"characters": {
"SYSTEM": {
"name": "System",
"emotions": {
"HAPPY": "SYSTEM_HAPPY.png"
}
},
"PLAYER": {
"name": "mc",
"health": 100,
Expand Down Expand Up @@ -165,19 +171,26 @@
"collide": true,
"dialogue": "explore_closet_1",
"item": "wallet"
},
{
"objectid": "exit",
"image": "door_1.png",
"location": [
200,
300
],
"dimensions": [
50,
100
]
},
{
"objectid": "save",
"location": [
400, 400
]
}
],
"exit": {
"location": [
200,
300
],
"dimensions": [
50,
100
],
"image": "door_1.png"
}
]
}
},
"battles": {
Expand Down Expand Up @@ -231,6 +244,16 @@
"textbox": "textbox.png"
}
]
},
"save": {
"lines": [{
"character": "SYSTEM",
"emotion": "HAPPY",
"line": "Game Saved!",
"audio": "saved.wav",
"textbox": "saved.png"
}]
}
}
},
"version": 1
}
Loading

0 comments on commit e38e37d

Please sign in to comment.