605 lines
24 KiB
Java
605 lines
24 KiB
Java
/* GamePanel class acts as the main "game loop" - continuously runs the game and calls whatever needs to be called
|
||
|
||
Child of JPanel because JPanel contains methods for drawing to the screen
|
||
|
||
Implements KeyListener interface to listen for keyboard input
|
||
|
||
Implements Runnable interface to use "threading" - let the game do two things at once
|
||
|
||
*/
|
||
import java.awt.*;
|
||
import java.awt.event.*;
|
||
import java.awt.image.BufferedImage;
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import javax.imageio.ImageIO;
|
||
import java.io.Serializable;
|
||
import java.lang.reflect.Array;
|
||
import java.nio.file.Files;
|
||
import java.nio.file.Path;
|
||
import java.util.ArrayList;
|
||
import java.util.ConcurrentModificationException;
|
||
import javax.sound.sampled.LineUnavailableException;
|
||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||
import javax.swing.*;
|
||
|
||
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
|
||
|
||
public class GamePanel extends JPanel implements Runnable, KeyListener, Serializable {
|
||
|
||
//dimensions of window
|
||
public static final int GAME_WIDTH = 1225;
|
||
public static final int GAME_HEIGHT = 630;
|
||
|
||
public static final int fireballSpeed = 5;
|
||
|
||
public static Font tutorialFont = new Font(Font.MONOSPACED, Font.BOLD, 36);
|
||
public static Font loreFont = new Font(Font.MONOSPACED, Font.ITALIC + Font.BOLD, 36);
|
||
public static Color tutorialColor = Color.darkGray;
|
||
public static Color loreColor = Color.lightGray;
|
||
|
||
public int bombCount;
|
||
|
||
public transient JPanel gameFrame;
|
||
|
||
public transient Thread gameThread;
|
||
public transient Image image;
|
||
public transient Graphics graphics;
|
||
public transient boolean isNewStart = false;
|
||
public Player player;
|
||
public BackgroundImage background, cloudOneBackground, cloudTwoBackground, cloudThreeBackground;
|
||
public int playerFrame, enemyFrame;
|
||
// keeps track of how many ticks has elapsed since last frame change
|
||
public int playerFrameCounter = 0;
|
||
public int timeSinceLastSave = 0;
|
||
public transient boolean isPaused;
|
||
public boolean isDialogue, waitForDialogue, mouseAlreadyTranslated;
|
||
public PauseMenu pauseMenu, pauseMenuExitOne, pauseMenuExitTwo, pauseMenuResume;
|
||
public DialogueMenu dialogueMenu;
|
||
public ArrayList<String> dialogueArray = new ArrayList<String>();
|
||
|
||
|
||
public BufferedImageWrapper[][][] playerSpriteArray = new BufferedImageWrapper[2][2][11];
|
||
public BufferedImageWrapper[][][] slimeSpriteArray = new BufferedImageWrapper[2][2][3];
|
||
public BufferedImageWrapper[] explosionArray = new BufferedImageWrapper[9];
|
||
|
||
//public static ArrayList<Tile>map = new ArrayList<Tile>();
|
||
|
||
public Tile[][]map = new Tile[1000][18];
|
||
public ArrayList<Middleware> middlewareArray = new ArrayList<Middleware>();
|
||
|
||
public ArrayList<Tile>particleTiles = new ArrayList<Tile>();
|
||
|
||
public ArrayList<Tile>shootingTiles = new ArrayList<Tile>();
|
||
|
||
public ArrayList<FireBall>fireballs = new ArrayList<FireBall>();
|
||
public ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
|
||
public ArrayList<StickyBomb>bombs = new ArrayList<>();
|
||
public BombDirectionShow bombDir = null;
|
||
public ArrayList<Particle>particles = new ArrayList<Particle>();
|
||
public Camera camera;
|
||
|
||
// image imports begin here
|
||
public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(("img/backgrounds/pointyMountains.png"));
|
||
public BufferedImageWrapper box = new BufferedImageWrapper(("img/tiles/boxes/box.png"));
|
||
public BufferedImageWrapper boxCoin = new BufferedImageWrapper(("img/tiles/boxes/boxCoin.png"));
|
||
public BufferedImageWrapper cloud1 = new BufferedImageWrapper(("img/backgrounds/cloud1.png"));
|
||
public BufferedImageWrapper cloud2 = new BufferedImageWrapper(("img/backgrounds/cloud2.png"));
|
||
public BufferedImageWrapper cloud3 = new BufferedImageWrapper(("img/backgrounds/cloud3.png"));
|
||
public BufferedImageWrapper bomb;
|
||
public BufferedImageWrapper narratorPortrait = new BufferedImageWrapper(("img/dialogue/Gunther.png"));
|
||
public BufferedImageWrapper villainPortrait = new BufferedImageWrapper(("img/dialogue/Bouncer.png"));
|
||
public String lastText;
|
||
public boolean isContinue;
|
||
public boolean isRunning;
|
||
public ArrayList<WallSign> tutorialSign = new ArrayList<WallSign>();
|
||
public ArrayList<WallSign> loreSign = new ArrayList<WallSign>();
|
||
|
||
|
||
public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
|
||
this.gameFrame = gameFrame;
|
||
camera = new Camera(0);
|
||
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
|
||
cloudOneBackground = new BackgroundImage(200, 200, cloud1, cloud1.image.getWidth(), cloud1.image.getHeight(), 5, camera);
|
||
cloudTwoBackground = new BackgroundImage(600, 250, cloud2, cloud2.image.getWidth(), cloud3.image.getHeight(), 5, camera);
|
||
cloudThreeBackground = new BackgroundImage(1000, 200, cloud3, cloud2.image.getWidth(), cloud3.image.getHeight(), 5, camera);
|
||
pauseMenu = new PauseMenu(GAME_HEIGHT/2, 100, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 60), "Paused", true);
|
||
pauseMenuExitOne = new PauseMenu(GAME_HEIGHT/2, 0, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 24), "Press ENTER to return", true);
|
||
pauseMenuExitTwo = new PauseMenu(GAME_HEIGHT/2, -20, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 24), "to the main menu", true);
|
||
pauseMenuResume = new PauseMenu(GAME_HEIGHT/2, -50, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 18), "(or press ESC to resume)", true);
|
||
dialogueMenu = new DialogueMenu(GAME_HEIGHT-100, 200, new Font(Font.MONOSPACED, Font.BOLD, 20), narratorPortrait, true);
|
||
dialogueArray.add("Did you ever hear the tragedy of Darth Plagueis The Wise?");
|
||
dialogueArray.add("I thought not.");
|
||
dialogueArray.add("It’s not a story the Jedi would tell you.");
|
||
dialogueArray.add("Darth Plagueis was a Dark Lord of the Sith, " +
|
||
"so powerful and so wise he could use the Force to influence " +
|
||
"the midichlorians to create life… " +
|
||
"He had such a knowledge of the dark side that " +
|
||
"he could even keep the ones he cared about from dying. " +
|
||
"The dark side of the Force is a pathway to many abilities some consider to be unnatural. " +
|
||
"He became so powerful… the only thing he was afraid of was losing his power, " +
|
||
"which eventually, of course, he did.");
|
||
dialogueArray.add("Unfortunately, he taught his apprentice everything he knew, then his apprentice killed him in his sleep.");
|
||
dialogueArray.add("Ironic.");
|
||
dialogueArray.add("He could save others from death, but not himself.");
|
||
try {
|
||
// load player sprites from disk here
|
||
for (int i = 0; i < 11; i++) {
|
||
String sprite = (String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));
|
||
|
||
playerSpriteArray[1][0][i] = new BufferedImageWrapper(sprite);
|
||
playerSpriteArray[1][1][i] = new BufferedImageWrapper(sprite);
|
||
playerSpriteArray[0][0][i] = new BufferedImageWrapper(sprite, true);
|
||
playerSpriteArray[0][1][i] = new BufferedImageWrapper(sprite, true);
|
||
}
|
||
for (int i = 0; i < 9; i++) {
|
||
explosionArray[i] = new BufferedImageWrapper(("img/misc/bomb/sonicExplosion0" + i + ".png"));
|
||
}
|
||
// load slime sprites from disk here
|
||
// these variables were not defined above because they are temporary variables
|
||
BufferedImageWrapper[] temporarySlimeArray = {new BufferedImageWrapper(("img/enemy/slime/slimeWalk1.png")),
|
||
new BufferedImageWrapper(("img/enemy/slime/slimeWalk2.png")),
|
||
new BufferedImageWrapper(("img/enemy/slime/slimeDead.png"))};
|
||
BufferedImageWrapper[] flippedTemporarySlimeArray = {new BufferedImageWrapper((("img/enemy/slime/slimeWalk1.png")), true),
|
||
new BufferedImageWrapper(("img/enemy/slime/slimeWalk2.png"), true),
|
||
new BufferedImageWrapper(("img/enemy/slime/slimeDead.png"), true)};
|
||
// please note that these sprites are reversed compared to the player sprites
|
||
slimeSpriteArray[0][0] = temporarySlimeArray;
|
||
slimeSpriteArray[0][1] = temporarySlimeArray;
|
||
slimeSpriteArray[1][0] = flippedTemporarySlimeArray;
|
||
slimeSpriteArray[1][1] = flippedTemporarySlimeArray;
|
||
|
||
// load bomb sprites
|
||
bomb = new BufferedImageWrapper(("img/misc/bomb.png"));
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, playerSpriteArray); //create a player controlled player, set start location to middle of screenk
|
||
// the height of 35 is set because it is half of the original tile height (i.e., 70px)
|
||
addUserInterface();
|
||
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
|
||
isNewStart = true;
|
||
isRunning = true;
|
||
}
|
||
|
||
public void addUserInterface() {
|
||
this.setFocusable(true); //make everything in this class appear on the screen
|
||
this.addKeyListener(this); //start listening for keyboard input
|
||
// request focus when the CardLayout selects this game
|
||
requestFocusable();
|
||
//add the MousePressed method from the MouseAdapter - by doing this we can listen for mouse input. We do this differently from the KeyListener because MouseAdapter has SEVEN mandatory methods - we only need one of them, and we don't want to make 6 empty methods
|
||
addMouseListener();
|
||
}
|
||
|
||
public void addMouseListener() {
|
||
addMouseListener(new MouseAdapter() {
|
||
public void mousePressed(MouseEvent e) {
|
||
try {
|
||
if (isDialogue || isPaused) {
|
||
mouseAlreadyTranslated = true;
|
||
keyPressed(new KeyEvent(new Component() {}, 0, 0, 0, KeyEvent.VK_ENTER, (char)KeyEvent.VK_ENTER));
|
||
} else {
|
||
player.mousePressed(e);
|
||
}
|
||
} catch (SpriteException | IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
public void mouseReleased(MouseEvent e) {
|
||
if (mouseAlreadyTranslated) {
|
||
mouseAlreadyTranslated = false;
|
||
} else if (!isDialogue && !isPaused) {
|
||
try {
|
||
player.mouseReleased(e);
|
||
} catch (IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
} catch (SpriteException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
});
|
||
addMouseMotionListener(new MouseAdapter() {
|
||
public void mouseDragged(MouseEvent e) {
|
||
try {
|
||
player.mouseDragged(e);
|
||
} catch (IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
} catch (SpriteException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
public void mouseMoved(MouseEvent e) {
|
||
player.mouseMoved(e);
|
||
}
|
||
});
|
||
}
|
||
|
||
public void requestFocusable() {
|
||
this.addComponentListener(new ComponentAdapter() {
|
||
|
||
@Override
|
||
public void componentShown(ComponentEvent cEvt) {
|
||
Component src = (Component) cEvt.getSource();
|
||
src.requestFocusInWindow();
|
||
}
|
||
|
||
});
|
||
}
|
||
|
||
// startThread is to be called after the game has started to avoid any issues TODO: better explanation
|
||
public void startThread() {
|
||
//make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
|
||
gameThread = new Thread(this);
|
||
gameThread.start();
|
||
//System.out.println(gameThread);
|
||
}
|
||
|
||
//paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself
|
||
public void paint(Graphics g){
|
||
//we are using "double buffering here" - if we draw images directly onto the screen, it takes time and the human eye can actually notice flashes of lag as each pixel on the screen is drawn one at a time. Instead, we are going to draw images OFF the screen, then simply move the image on screen as needed.
|
||
image = createImage(GAME_WIDTH, GAME_HEIGHT); //draw off screen
|
||
graphics = image.getGraphics();
|
||
try {
|
||
draw(graphics, playerFrame, enemyFrame);//update the positions of everything on the screen
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
} catch (UnsupportedAudioFileException e) {
|
||
throw new RuntimeException(e);
|
||
} catch (LineUnavailableException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
g.drawImage(image, 0, 0, this); //move the image on the screen
|
||
|
||
}
|
||
|
||
//call the draw methods in each class to update positions as things move
|
||
public void draw(Graphics g, int playerFrame, int enemyFrame) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
|
||
Color oldColor = g.getColor();
|
||
Font oldFont = g.getFont();
|
||
background.draw(g);
|
||
cloudOneBackground.draw(g);
|
||
cloudTwoBackground.draw(g);
|
||
cloudThreeBackground.draw(g);
|
||
for (WallSign w: tutorialSign) {
|
||
w.draw(g, tutorialColor);
|
||
}
|
||
for (WallSign w: loreSign) {
|
||
w.draw(g, loreColor);
|
||
}
|
||
// reset graphics color to black and font to system default
|
||
g.setColor(oldColor);
|
||
g.setFont(oldFont);
|
||
if (isPaused || isDialogue) {
|
||
// set player frame to 0 to prevent frame from changing when player moves
|
||
playerFrame = 0;
|
||
Graphics2D g2d = (Graphics2D)(g);
|
||
// remove extraneous details from game when paused
|
||
g2d.setPaint(Color.white);
|
||
}
|
||
//Don't want to draw off screen items
|
||
int xMin = Math.max(0,((this.camera.x+GAME_WIDTH)/Tile.length)-(GAME_WIDTH/(2*Tile.length))-5);
|
||
int xMax = Math.min(map.length, 7+xMin + GAME_WIDTH/Tile.length);
|
||
for(int i=xMin; i<xMax; i++){
|
||
for(int j=0; j<map[0].length; j++){
|
||
if(map[i][j]!=null) {
|
||
map[i][j].draw(g);
|
||
}
|
||
}
|
||
}
|
||
for(int i=0; i<enemy.size(); i++){
|
||
enemy.get(i).draw(g, enemyFrame);
|
||
}
|
||
playerFrameCounter += player.draw(g, playerFrame);
|
||
for(int i=0; i<bombs.size(); i++){
|
||
if(i<bombs.size()) {
|
||
if (bombs.get(i).erase) {
|
||
bombs.remove(i);
|
||
} else {
|
||
bombs.get(i).draw(g);
|
||
}
|
||
}
|
||
}
|
||
for(int i=0; i<fireballs.size(); i++){
|
||
if(fireballs.get(i)!=null) {
|
||
fireballs.get(i).draw(g);
|
||
}
|
||
if(fireballs.get(i).dead){
|
||
fireballs.remove(i);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
for(int i=0; i<particles.size(); i++){
|
||
// todo: find cause of particles being null
|
||
if(i<particles.size()&&particles.get(i)!=null) {
|
||
particles.get(i).draw(g);
|
||
particles.get(i).lifeSpan--;
|
||
if (particles.get(i).lifeSpan <= 0) {
|
||
particles.remove(i);
|
||
}
|
||
}
|
||
}
|
||
if(player.leftMouseDown&&LevelManager.bombs>0&&!player.holdingSteel){
|
||
|
||
bombDir = new BombDirectionShow(this.player.x + this.camera.x + WIDTH/2, this.player.y+HEIGHT/2,
|
||
(player.mouseX - this.player.x) / 20, (player.mouseY - this.player.y) / 10);
|
||
bombDir.draw(g);
|
||
}
|
||
|
||
if(player.holdingSteel){
|
||
String filePath = "";
|
||
if(player.canPlaceSteel){
|
||
filePath = "img/tiles/boxes/greenSteel.png";
|
||
} else {
|
||
filePath = "img/tiles/boxes/redSteel.png";
|
||
}
|
||
int x = (player.mouseX + camera.x + GAME_WIDTH / 2) / Tile.length;
|
||
double y = ((player.mouseY / Tile.length))*Tile.length;
|
||
g.drawImage(getImage(filePath),x*Tile.length - (GamePanel.GAME_WIDTH/2)-camera.x,(int)y,Tile.length,Tile.length,null);
|
||
}
|
||
|
||
|
||
g.drawImage(bomb.image,20,20,35,35,null);
|
||
g.drawString("X"+LevelManager.bombs,60,40);
|
||
if (isPaused) {
|
||
g.setColor(new Color(255, 255, 255, 100));
|
||
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
|
||
pauseMenu.draw(g, Color.white, Color.black);
|
||
pauseMenuExitOne.draw(g, new Color(0,0, 0, 0), Color.gray);
|
||
pauseMenuExitTwo.draw(g, new Color(0,0, 0, 0), Color.gray);
|
||
pauseMenuResume.draw(g, new Color(0,0, 0, 0), Color.gray);
|
||
} else if (isDialogue) {
|
||
g.setColor(new Color(255, 255, 255, 100));
|
||
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
|
||
try {
|
||
if (waitForDialogue) {
|
||
dialogueMenu.currentFrame = dialogueArray.get(0).length();
|
||
dialogueMenu.frameCounter = 0;
|
||
dialogueMenu.draw(g, dialogueArray.get(0), Color.white, Color.black);
|
||
} else if (dialogueMenu.draw(g, dialogueArray.get(0), Color.white, Color.black)) {
|
||
waitForDialogue = true;
|
||
}
|
||
} catch (IndexOutOfBoundsException e) {
|
||
isDialogue = false;
|
||
// throw new RuntimeException(e);
|
||
}
|
||
}
|
||
}
|
||
|
||
//call the move methods in other classes to update positions
|
||
//this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
|
||
public void move() throws IOException, UnsupportedAudioFileException, LineUnavailableException {
|
||
player.move();
|
||
for (NonPlayer n: enemy) {
|
||
n.move();
|
||
}
|
||
for(int i=0; i<bombs.size(); i++){
|
||
bombs.get(i).move();
|
||
}
|
||
for(int i=0; i<particles.size(); i++){
|
||
if(particles.get(i)!=null) {
|
||
particles.get(i).move();
|
||
}
|
||
}
|
||
for(int i=0; i<fireballs.size(); i++){
|
||
if(fireballs.get(i)!=null){
|
||
fireballs.get(i).move();
|
||
}
|
||
}
|
||
}
|
||
|
||
//handles all collision detection and responds accordingly
|
||
public void checkCollision() {
|
||
player.isGrounded = false;
|
||
for(int i=0; i<map.length; i++){
|
||
for(int j=0; j<map[0].length; j++){
|
||
if(map[i][j]!=null) {
|
||
map[i][j].update();
|
||
}
|
||
}
|
||
}
|
||
for (NonPlayer n: enemy) {
|
||
n.update();
|
||
n.isGrounded = false;
|
||
if(n.collidePlayer(player)&&!n.isDead){
|
||
player.alive = false;
|
||
}
|
||
}
|
||
|
||
//force player to remain on screen (For the most part)
|
||
|
||
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
|
||
player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
|
||
player.yVelocity = 0;
|
||
player.isGrounded = true;
|
||
}
|
||
if (player.x <= 0) {
|
||
player.x = 0;
|
||
}
|
||
if (player.x + Player.PLAYER_WIDTH >= GAME_WIDTH) {
|
||
player.x = GAME_WIDTH - Player.PLAYER_WIDTH;
|
||
}
|
||
|
||
for (NonPlayer n: enemy) {
|
||
if (n.y >= GAME_HEIGHT - n.npcHeight) {
|
||
n.y = GAME_HEIGHT - n.npcHeight;
|
||
n.yVelocity = 0;
|
||
n.isGrounded = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
|
||
public void run() {
|
||
if (isNewStart) {
|
||
LevelManager.setLevel(1);
|
||
isDialogue = true;
|
||
} else {
|
||
LevelManager.bombs = bombCount;
|
||
}
|
||
try {
|
||
player.resetNoSound();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
|
||
long lastTime = System.nanoTime();
|
||
double amountOfTicks = 60;
|
||
double ns = 1000000000/amountOfTicks;
|
||
double delta = 0;
|
||
long now;
|
||
int fireballCounter = 0;
|
||
while(isRunning){ //this is the game loop, terminates on game restart to prevent race conditions
|
||
now = System.nanoTime();
|
||
delta = delta + (now-lastTime)/ns;
|
||
lastTime = now;
|
||
|
||
//only move objects around and update screen if enough time has passed
|
||
if(delta >= 1){
|
||
if ((!isPaused && !isDialogue) && MenuPanel.gameStart) {
|
||
// only perform game functions if game is not paused or in dialogue
|
||
try {
|
||
move();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
} catch (UnsupportedAudioFileException e) {
|
||
throw new RuntimeException(e);
|
||
} catch (LineUnavailableException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
checkCollision();
|
||
updateEnemy();
|
||
if(fireballCounter<0){fireballCounter = 200;}
|
||
fireballCounter--;
|
||
if(fireballCounter == 0){
|
||
updateShootingBlock();
|
||
}
|
||
try {
|
||
updateParticle();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
if (playerFrameCounter > 5) {
|
||
// increment sprite image to be used and keeps it below 12
|
||
playerFrame = (playerFrame + 1) % 11;
|
||
playerFrameCounter -= 5;
|
||
}
|
||
}
|
||
// a new save is made every 100 ticks
|
||
timeSinceLastSave++;
|
||
if (timeSinceLastSave >= 100) {
|
||
timeSinceLastSave -= 100;
|
||
bombCount = LevelManager.bombs;
|
||
try {
|
||
// atomic save to prevent EOF errors
|
||
FileManager.writeObjectToFile("local\\temp_state.dat", this);
|
||
Files.move(Path.of("local", "temp_state.dat"), Path.of("local", "game_state.dat"), ATOMIC_MOVE);
|
||
} catch (IOException | ConcurrentModificationException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
repaint();
|
||
delta--;
|
||
}
|
||
//System.out.println(Thread.currentThread());
|
||
}
|
||
|
||
}
|
||
|
||
public void updateEnemy(){
|
||
for(int i=0; i<enemy.size(); i++){
|
||
if(enemy.get(i).isDead){
|
||
// enemy.get(i).fadeCounter--;
|
||
if(enemy.get(i).fadeCounter<=0){
|
||
enemy.remove(i);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public void updateShootingBlock(){
|
||
for(Tile i: shootingTiles){
|
||
if(i.shootingDir.equals("left")){
|
||
fireballs.add(new FireBall(i.x-20,i.y+Tile.length/2-4,-fireballSpeed,0,"left",8,16));
|
||
} else if(i.shootingDir.equals("up")){
|
||
fireballs.add(new FireBall(i.x+Tile.length/2,i.y-20,0,-fireballSpeed,"up",16,8));
|
||
} else if(i.shootingDir.equals("right")){
|
||
fireballs.add(new FireBall(i.x+Tile.length+20,i.y+Tile.length/2-4,fireballSpeed,0,"right",8,16));
|
||
} else if(i.shootingDir.equals("down")){
|
||
fireballs.add(new FireBall(i.x+Tile.length/2,i.y+Tile.length+10,0,fireballSpeed,"down",16,8));
|
||
}
|
||
}
|
||
}
|
||
public void updateParticle() throws IOException {
|
||
if(particles.size()<10) {
|
||
for (int i = 0; i < particleTiles.size(); i++) {
|
||
if (UtilityFunction.randInt(1, 20) == 1) {
|
||
particles.add(new Particle(particleTiles.get(i).x + UtilityFunction.randInt(0, Tile.length), particleTiles.get(i).y + UtilityFunction.randInt(0, Tile.length / 2),
|
||
UtilityFunction.randInt(-3, 3), UtilityFunction.randInt(-5, 2), UtilityFunction.randInt(5, 9), "img/particles/LavaParticle.png"));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//if a key is pressed, we'll send it over to the Player class for processing
|
||
public void keyPressed(KeyEvent e){
|
||
e = UtilityFunction.intercept(e, middlewareArray);
|
||
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||
isPaused = !isPaused;
|
||
System.out.println(isPaused);
|
||
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||
if (isPaused && (e.getSource() == null)) {
|
||
((CardLayout)gameFrame.getLayout()).show(gameFrame, "menu");
|
||
} else if (!waitForDialogue) {
|
||
waitForDialogue = true;
|
||
} else {
|
||
dialogueMenu.currentFrame = 0;
|
||
dialogueMenu.frameCounter = 0;
|
||
dialogueArray.remove(0);
|
||
waitForDialogue = false;
|
||
}
|
||
} else {
|
||
try {
|
||
player.keyPressed(e);
|
||
} catch (IOException ex) {
|
||
throw new RuntimeException(ex);
|
||
}
|
||
}
|
||
}
|
||
|
||
//if a key is released, we'll send it over to the Player class for processing
|
||
public void keyReleased(KeyEvent e){
|
||
e = UtilityFunction.intercept(e, middlewareArray);
|
||
player.keyReleased(e);
|
||
if(e.getKeyChar() == 'p'){
|
||
LevelManager.nextLevel();
|
||
|
||
}
|
||
}
|
||
|
||
//left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface
|
||
public void keyTyped(KeyEvent e){
|
||
|
||
}
|
||
|
||
public static BufferedImage getImage(String imageLocation) throws IOException {
|
||
return ImageIO.read(new File(imageLocation));
|
||
}
|
||
|
||
public static BufferedImage flipImageHorizontally(BufferedImage originalImage) {
|
||
BufferedImage flippedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
|
||
for (int x = 0; x < originalImage.getWidth(); x++) {
|
||
for (int y = 0; y < originalImage.getHeight(); y++) {
|
||
// -1 is added to prevent off-by-one errors
|
||
flippedImage.setRGB(x, y, originalImage.getRGB(originalImage.getWidth()-x-1, y));
|
||
}
|
||
}
|
||
return flippedImage;
|
||
}
|
||
|
||
} |