Fixed serialization bugs, bomb bug during dialogue
parent
6fac8e9a59
commit
e49c4cf4e8
|
@ -46,7 +46,6 @@ public class DialogueMenu extends TextBox implements Serializable {
|
||||||
for (String s: newText) {
|
for (String s: newText) {
|
||||||
currentLineWidth += metrics.stringWidth(s + " ");
|
currentLineWidth += metrics.stringWidth(s + " ");
|
||||||
if (currentLineWidth - metrics.stringWidth(" ") < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*5)) {
|
if (currentLineWidth - metrics.stringWidth(" ") < (GamePanel.GAME_WIDTH - PORTRAIT_WIDTH - PADDING*5)) {
|
||||||
System.out.println(s + " " + currentLineWidth);
|
|
||||||
lines.set(lastLineIndex, lines.get(lastLineIndex) + s + " ");
|
lines.set(lastLineIndex, lines.get(lastLineIndex) + s + " ");
|
||||||
} else {
|
} else {
|
||||||
currentLineWidth = metrics.stringWidth(s);
|
currentLineWidth = metrics.stringWidth(s);
|
||||||
|
|
|
@ -28,15 +28,17 @@ public class FileManager {
|
||||||
|
|
||||||
public static Object readObjectFromFile(String fileLocation, List<String> allowedObject) throws IOException, ClassNotFoundException {
|
public static Object readObjectFromFile(String fileLocation, List<String> allowedObject) throws IOException, ClassNotFoundException {
|
||||||
ObjectInputStream objectStream;
|
ObjectInputStream objectStream;
|
||||||
|
Object o;
|
||||||
FileInputStream fileStream = new FileInputStream(fileLocation);
|
FileInputStream fileStream = new FileInputStream(fileLocation);
|
||||||
if (!allowedObject.contains("Any")) {
|
if (!allowedObject.contains("Any")) {
|
||||||
objectStream = new SafeObjectInputStream(fileStream, allowedObject);
|
objectStream = new SafeObjectInputStream(fileStream, allowedObject);
|
||||||
} else {
|
} else {
|
||||||
objectStream = new ObjectInputStream(fileStream);
|
objectStream = new ObjectInputStream(fileStream);
|
||||||
}
|
}
|
||||||
|
o = objectStream.readObject();
|
||||||
objectStream.close();
|
objectStream.close();
|
||||||
fileStream.close();
|
fileStream.close();
|
||||||
return objectStream.readObject();
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void writeObjectToFile(String fileLocation, Object o) throws IOException {
|
public static void writeObjectToFile(String fileLocation, Object o) throws IOException {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import java.io.Serializable;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.ConcurrentModificationException;
|
||||||
import javax.sound.sampled.LineUnavailableException;
|
import javax.sound.sampled.LineUnavailableException;
|
||||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
@ -42,7 +43,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
// keeps track of how many ticks has elapsed since last frame change
|
// keeps track of how many ticks has elapsed since last frame change
|
||||||
public int playerFrameCounter = 0;
|
public int playerFrameCounter = 0;
|
||||||
public int enemyFrameCounter = 0;
|
public int enemyFrameCounter = 0;
|
||||||
public boolean isPaused, isDialogue, waitForDialogue;
|
public boolean isPaused, isDialogue, waitForDialogue, mouseAlreadyTranslated;
|
||||||
public PauseMenu pauseMenu;
|
public PauseMenu pauseMenu;
|
||||||
public DialogueMenu dialogueMenu;
|
public DialogueMenu dialogueMenu;
|
||||||
public ArrayList<String> dialogueArray = new ArrayList<String>();
|
public ArrayList<String> dialogueArray = new ArrayList<String>();
|
||||||
|
@ -143,13 +144,22 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
try {
|
try {
|
||||||
player.mousePressed(e);
|
if (isDialogue || isPaused) {
|
||||||
|
mouseAlreadyTranslated = true;
|
||||||
|
keyPressed(new KeyEvent(new Component() {}, 0, 0, 0, KeyEvent.VK_ENTER));
|
||||||
|
} else {
|
||||||
|
player.mousePressed(e);
|
||||||
|
}
|
||||||
} catch (SpriteException | IOException ex) {
|
} catch (SpriteException | IOException ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void mouseReleased(MouseEvent e) {
|
public void mouseReleased(MouseEvent e) {
|
||||||
player.mouseReleased(e);
|
if (mouseAlreadyTranslated) {
|
||||||
|
mouseAlreadyTranslated = false;
|
||||||
|
} else if (!isDialogue && !isPaused) {
|
||||||
|
player.mouseReleased(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -265,7 +275,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
|
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
|
||||||
try {
|
try {
|
||||||
if (waitForDialogue) {
|
if (waitForDialogue) {
|
||||||
System.out.println(dialogueArray);
|
|
||||||
dialogueMenu.currentFrame = dialogueArray.get(0).length();
|
dialogueMenu.currentFrame = dialogueArray.get(0).length();
|
||||||
dialogueMenu.frameCounter = 0;
|
dialogueMenu.frameCounter = 0;
|
||||||
dialogueMenu.draw(g, dialogueArray.get(0), Color.white, Color.black);
|
dialogueMenu.draw(g, dialogueArray.get(0), Color.white, Color.black);
|
||||||
|
@ -274,7 +283,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
}
|
}
|
||||||
} catch (IndexOutOfBoundsException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
isDialogue = false;
|
isDialogue = false;
|
||||||
throw new RuntimeException(e);
|
// throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -385,7 +394,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
// atomic save to prevent EOF errors
|
// atomic save to prevent EOF errors
|
||||||
FileManager.writeObjectToFile("local\\temp_state.dat", this);
|
FileManager.writeObjectToFile("local\\temp_state.dat", this);
|
||||||
Files.move(Path.of("local", "temp_state.dat"), Path.of("local", "game_state.dat"), ATOMIC_MOVE);
|
Files.move(Path.of("local", "temp_state.dat"), Path.of("local", "game_state.dat"), ATOMIC_MOVE);
|
||||||
} catch (IOException e) {
|
} catch (IOException | ConcurrentModificationException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
repaint();
|
repaint();
|
||||||
|
@ -421,10 +430,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
|
||||||
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||||
isPaused = !isPaused;
|
isPaused = !isPaused;
|
||||||
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||||
dialogueMenu.currentFrame = 0;
|
if (!waitForDialogue) {
|
||||||
dialogueMenu.frameCounter = 0;
|
waitForDialogue = true;
|
||||||
dialogueArray.remove(0);
|
} else {
|
||||||
waitForDialogue = false;
|
dialogueMenu.currentFrame = 0;
|
||||||
|
dialogueMenu.frameCounter = 0;
|
||||||
|
dialogueArray.remove(0);
|
||||||
|
waitForDialogue = false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
player.keyPressed(e);
|
player.keyPressed(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,9 +28,9 @@ public class Player extends GenericSprite {
|
||||||
public static int mouseX;
|
public static int mouseX;
|
||||||
public static int mouseY;
|
public static int mouseY;
|
||||||
|
|
||||||
public boolean leftMouseDown;
|
public transient boolean leftMouseDown;
|
||||||
|
|
||||||
public boolean rightMouseDown;
|
public transient boolean rightMouseDown;
|
||||||
boolean holdingSteel;
|
boolean holdingSteel;
|
||||||
|
|
||||||
boolean canPlaceSteel;
|
boolean canPlaceSteel;
|
||||||
|
|
Loading…
Reference in New Issue