final/src/GamePanel.java

409 lines
16 KiB
Java
Raw Normal View History

2022-05-30 19:46:53 +01:00
/* GamePanel class acts as the main "game loop" - continuously runs the game and calls whatever needs to be called
2022-05-30 19:36:18 +01:00
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.*;
2022-05-31 18:19:23 +01:00
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
2022-06-14 19:54:55 +01:00
import java.io.Serializable;
2022-05-31 18:19:41 +01:00
import java.util.ArrayList;
2022-06-03 18:50:13 +01:00
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
2022-05-30 19:36:18 +01:00
import javax.swing.*;
2022-06-14 19:54:55 +01:00
public class GamePanel extends JPanel implements Runnable, KeyListener, Serializable {
2022-05-30 19:36:18 +01:00
2022-05-30 19:46:53 +01:00
//dimensions of window
2022-06-02 18:33:04 +01:00
public static final int GAME_WIDTH = 1225;
public static final int GAME_HEIGHT = 630;
2022-05-30 19:46:53 +01:00
public transient JPanel gameFrame;
2022-06-06 18:32:13 +01:00
public transient Thread gameThread;
public transient Image image;
public transient Graphics graphics;
public Player player;
public BackgroundImage background;
2022-06-03 22:33:21 +01:00
public int playerFrame, enemyFrame;
2022-05-31 18:19:23 +01:00
// keeps track of how many ticks has elapsed since last frame change
2022-06-03 22:33:21 +01:00
public int playerFrameCounter = 0;
public int enemyFrameCounter = 0;
2022-06-08 18:36:47 +01:00
public boolean isPaused;
public PauseMenu pauseMenu;
2022-05-31 18:19:23 +01:00
2022-06-12 01:02:41 +01:00
public BufferedImageWrapper[][][] playerSpriteArray = new BufferedImageWrapper[2][2][11];
public BufferedImageWrapper[][][] slimeSpriteArray = new BufferedImageWrapper[2][2][3];
public BufferedImageWrapper[] explosionArray = new BufferedImageWrapper[9];
2022-05-30 19:46:53 +01:00
//public static ArrayList<Tile>map = new ArrayList<Tile>();
public Tile[][]map = new Tile[300][18];
public ArrayList<Middleware> middlewareArray = new ArrayList<Middleware>();
2022-06-08 19:32:31 +01:00
public ArrayList<Tile>particleTiles = new ArrayList<Tile>();
public ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
public ArrayList<StickyBomb>bombs = new ArrayList<>();
2022-06-12 01:02:41 +01:00
public BombDirectionShow bombDir = null;
public ArrayList<Particle>particles = new ArrayList<Particle>();
public Camera camera;
// image imports begin here
public BufferedImageWrapper backgroundImage = new BufferedImageWrapper(getImage("img/backgrounds/pointyMountains.png"));
public BufferedImageWrapper box = new BufferedImageWrapper(getImage("img/tiles/boxes/box.png"));
public BufferedImageWrapper boxCoin = new BufferedImageWrapper(getImage("img/tiles/boxes/boxCoin.png"));
public BufferedImageWrapper bomb;
2022-06-07 18:19:54 +01:00
2022-06-06 18:32:13 +01:00
public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
this.gameFrame = gameFrame;
2022-06-02 18:21:29 +01:00
camera = new Camera(0);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
2022-06-08 18:36:47 +01:00
pauseMenu = new PauseMenu(GAME_HEIGHT/2, 100, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 60), "Paused");
2022-06-03 22:33:21 +01:00
try {
// load player sprites from disk here
for (int i = 0; i < 11; i++) {
2022-05-31 18:19:23 +01:00
BufferedImage sprite = getImage(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(flipImageHorizontally(sprite));
playerSpriteArray[0][1][i] = new BufferedImageWrapper(flipImageHorizontally(sprite));
2022-05-31 18:19:23 +01:00
}
2022-06-08 18:36:47 +01:00
for (int i = 0; i < 9; i++) {
explosionArray[i] = new BufferedImageWrapper(getImage("img/misc/bomb/sonicExplosion0" + i + ".png"));
2022-06-08 18:36:47 +01:00
}
2022-06-03 22:33:21 +01:00
// load slime sprites from disk here
// these variables were not defined above because they are temporary variables
BufferedImageWrapper[] temporarySlimeArray = {new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk1.png")),
new BufferedImageWrapper(getImage("img/enemy/slime/slimeWalk2.png")),
new BufferedImageWrapper(getImage("img/enemy/slime/slimeDead.png"))};
BufferedImageWrapper[] flippedTemporarySlimeArray = {new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk1.png"))),
new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeWalk2.png"))),
new BufferedImageWrapper(flipImageHorizontally(getImage("img/enemy/slime/slimeDead.png")))};
2022-06-03 22:33:21 +01:00
// 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(getImage("img/misc/bomb.png"));
2022-06-03 22:33:21 +01:00
} catch (IOException e) {
e.printStackTrace();
2022-05-31 18:19:23 +01:00
}
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)
2022-06-15 03:00:27 +01:00
addUserInterface();
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
}
public void addUserInterface() {
2022-05-30 19:46:53 +01:00
this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input
2022-06-06 18:32:13 +01:00
// request focus when the CardLayout selects this game
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent cEvt) {
Component src = (Component) cEvt.getSource();
src.requestFocusInWindow();
}
2022-05-30 19:46:53 +01:00
2022-06-06 18:32:13 +01:00
});
2022-05-30 19:46:53 +01:00
//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(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
2022-06-12 22:28:27 +01:00
try {
player.mousePressed(e);
} catch (SpriteException | IOException ex) {
2022-06-12 22:28:27 +01:00
throw new RuntimeException(ex);
}
}
2022-06-09 19:19:23 +01:00
public void mouseReleased(MouseEvent e) {
player.mouseReleased(e);
}
2022-06-12 01:02:41 +01:00
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
player.mouseDragged(e);
}
2022-06-13 04:55:19 +01:00
public void mouseMoved(MouseEvent e) {
player.mouseMoved(e);
}
2022-06-12 01:02:41 +01:00
});
}
2022-05-30 19:46:53 +01:00
// startThread is to be called after the game has started to avoid any issues TODO: better explanation
public void startThread() {
2022-05-30 19:46:53 +01:00
//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();
2022-06-15 03:00:27 +01:00
System.out.println(gameThread);
2022-05-30 19:46:53 +01:00
}
//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();
2022-06-13 04:55:19 +01:00
try {
draw(graphics, playerFrame, enemyFrame);//update the positions of everything on the screen
} catch (IOException e) {
throw new RuntimeException(e);
}
2022-05-30 19:46:53 +01:00
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
2022-06-13 04:55:19 +01:00
public void draw(Graphics g, int playerFrame, int enemyFrame) throws IOException {
background.draw(g);
2022-06-08 18:36:47 +01:00
if (isPaused) {
// 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);
}
}
2022-05-31 18:19:41 +01:00
}
2022-06-05 22:37:09 +01:00
for(int i=0; i<enemy.size(); i++){
enemy.get(i).draw(g, enemyFrame);
2022-06-03 22:33:21 +01:00
}
playerFrameCounter += player.draw(g, playerFrame);
2022-06-09 05:07:44 +01:00
for(int i=0; i<bombs.size(); i++){
if(i<bombs.size()) {
if (bombs.get(i).erase) {
bombs.remove(i);
} else {
2022-06-12 01:02:41 +01:00
bombs.get(i).draw(g);
2022-06-09 05:07:44 +01:00
}
}
}
2022-06-08 16:23:57 +01:00
for(int i=0; i<particles.size(); i++){
2022-06-15 03:00:27 +01:00
// todo: find cause of particles being null
2022-06-08 19:32:31 +01:00
if(i<particles.size()) {
particles.get(i).draw(g);
particles.get(i).lifeSpan--;
if (particles.get(i).lifeSpan <= 0) {
particles.remove(i);
}
2022-06-08 16:23:57 +01:00
}
}
2022-06-12 01:02:41 +01:00
if(player.leftMouseDown){
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);
2022-06-12 01:02:41 +01:00
bombDir.draw(g);
}
2022-06-13 04:55:19 +01:00
if(player.holdingSteel){
String filePath = "";
if(player.canPlaceSteel){
filePath = "img/tiles/boxes/greenSteel.png";
} else {
filePath = "img/tiles/boxes/redSteel.png";
}
2022-06-13 18:53:58 +01:00
int x = (player.mouseX + camera.x + GAME_WIDTH / 2) / Tile.length;
2022-06-13 04:55:19 +01:00
double y = ((player.mouseY / Tile.length))*Tile.length;
2022-06-13 18:53:58 +01:00
g.drawImage(getImage(filePath),x*Tile.length - (GamePanel.GAME_WIDTH/2)-camera.x,(int)y,Tile.length,Tile.length,null);
2022-06-13 04:55:19 +01:00
}
// g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length)+" "+player.leftMouseDown,100,100);
// g.drawString(camera.x+" "+((player.mouseX+camera.x+GAME_WIDTH/2)/Tile.length)+" "+player.leftMouseDown,100,200);
g.drawImage(bomb.image,20,20,35,35,null);
2022-06-12 19:11:09 +01:00
g.drawString("X"+LevelManager.bombs,60,40);
2022-06-08 18:36:47 +01:00
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);
}
2022-05-30 19:46:53 +01:00
}
//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
2022-06-08 19:32:31 +01:00
public void move() throws IOException {
2022-05-31 18:19:23 +01:00
player.move();
2022-06-03 22:33:21 +01:00
for (NonPlayer n: enemy) {
n.move();
}
2022-06-09 05:07:44 +01:00
for(int i=0; i<bombs.size(); i++){
bombs.get(i).move();
}
2022-06-08 19:32:31 +01:00
for(int i=0; i<particles.size(); i++){
2022-06-15 02:35:05 +01:00
if(particles.get(i)!=null) {
particles.get(i).move();
}
2022-06-08 16:23:57 +01:00
}
2022-05-30 19:46:53 +01:00
}
//handles all collision detection and responds accordingly
2022-05-31 18:31:58 +01:00
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();
}
}
2022-05-31 18:19:41 +01:00
}
2022-06-06 03:18:10 +01:00
for (NonPlayer n: enemy) {
n.update();
n.isGrounded = false;
2022-06-12 04:48:57 +01:00
if(n.collidePlayer(player)&&!n.isDead){
2022-06-06 03:18:10 +01:00
player.alive = false;
}
}
2022-06-05 22:33:24 +01:00
//force player to remain on screen (For the most part)
2022-05-31 18:31:58 +01:00
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
2022-06-05 17:05:49 +01:00
player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
player.yVelocity = 0;
player.isGrounded = true;
2022-06-03 22:33:21 +01:00
}
if (player.x <= 0) {
player.x = 0;
}
if (player.x + Player.PLAYER_WIDTH >= GAME_WIDTH) {
player.x = GAME_WIDTH - Player.PLAYER_WIDTH;
}
2022-06-05 17:05:49 +01:00
for (NonPlayer n: enemy) {
2022-06-03 22:33:21 +01:00
if (n.y >= GAME_HEIGHT - n.npcHeight) {
n.y = GAME_HEIGHT - n.npcHeight;
n.yVelocity = 0;
n.isGrounded = true;
2022-05-31 18:31:58 +01:00
}
2022-05-30 19:36:18 +01:00
}
2022-06-05 17:05:49 +01:00
}
2022-05-30 19:46:53 +01:00
//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(){
2022-06-05 22:33:24 +01:00
LevelManager.setLevel(1);
2022-05-30 19:46:53 +01:00
//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;
while(true){ //this is the infinite game loop
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){
2022-06-14 18:34:18 +01:00
if (!isPaused && MenuPanel.gameStart) {
2022-06-08 18:36:47 +01:00
// only perform game functions if game is not paused
try {
move();
} catch (IOException e) {
throw new RuntimeException(e);
}
2022-06-08 18:36:47 +01:00
checkCollision();
2022-06-12 04:48:57 +01:00
updateEnemy();
2022-06-08 19:50:45 +01:00
try {
updateParticle();
} catch (IOException e) {
throw new RuntimeException(e);
}
2022-06-08 18:36:47 +01:00
if (playerFrameCounter > 5) {
// increment sprite image to be used and keeps it below 12
playerFrame = (playerFrame + 1) % 11;
playerFrameCounter -= 5;
2022-06-14 19:54:55 +01:00
// if the player has moved enough to justify a frame change, a new save will also be made
2022-06-15 03:00:27 +01:00
try {
2022-06-15 15:26:28 +01:00
FileManager.writeObjectToFile("local/game_state", new ArrayList()); // this is placeholder, replace with this
2022-06-15 03:00:27 +01:00
} catch (IOException e) {
e.printStackTrace();
}
2022-06-08 18:36:47 +01:00
}
2022-06-08 19:32:31 +01:00
}
2022-05-30 19:36:18 +01:00
repaint();
2022-05-30 19:46:53 +01:00
delta--;
}
2022-05-30 19:36:18 +01:00
}
2022-05-30 19:46:53 +01:00
}
2022-05-30 19:36:18 +01:00
2022-06-12 04:48:57 +01:00
public void updateEnemy(){
for(int i=0; i<enemy.size(); i++){
if(enemy.get(i).isDead){
2022-06-13 18:42:05 +01:00
// enemy.get(i).fadeCounter--;
2022-06-12 04:48:57 +01:00
if(enemy.get(i).fadeCounter<=0){
enemy.remove(i);
}
}
}
}
2022-06-08 19:32:31 +01:00
public void updateParticle() throws IOException {
for(Tile t: particleTiles){
2022-06-08 19:50:45 +01:00
if(GlobalState.randInt(1,10)==1) {
particles.add(new Particle(t.x + GlobalState.randInt(0, Tile.length), t.y + GlobalState.randInt(0, Tile.length / 2),
GlobalState.randInt(-3, 3), GlobalState.randInt(-5, 2), GlobalState.randInt(5, 9), "img/particles/LavaParticle.png"));
}
}
2022-06-08 19:32:31 +01:00
}
2022-05-31 18:19:23 +01:00
//if a key is pressed, we'll send it over to the Player class for processing
2022-05-30 19:46:53 +01:00
public void keyPressed(KeyEvent e){
e = UtilityFunction.intercept(e, middlewareArray);
2022-06-08 18:36:47 +01:00
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
isPaused = !isPaused;
} else {
player.keyPressed(e);
}
2022-05-30 19:46:53 +01:00
}
2022-05-30 19:36:18 +01:00
2022-05-31 18:19:23 +01:00
//if a key is released, we'll send it over to the Player class for processing
2022-05-30 19:46:53 +01:00
public void keyReleased(KeyEvent e){
e = UtilityFunction.intercept(e, middlewareArray);
2022-05-31 18:19:23 +01:00
player.keyReleased(e);
2022-06-06 03:18:10 +01:00
if(e.getKeyChar() == 'p'){
LevelManager.nextLevel();
}
2022-05-30 19:46:53 +01:00
}
2022-05-30 19:36:18 +01:00
2022-05-30 19:46:53 +01:00
//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){
2022-05-30 19:36:18 +01:00
2022-05-30 19:46:53 +01:00
}
2022-05-31 18:19:23 +01:00
2022-06-03 18:17:03 +01:00
public static BufferedImage getImage(String imageLocation) throws IOException {
2022-05-31 18:19:23 +01:00
return ImageIO.read(new File(imageLocation));
}
2022-06-03 18:09:59 +01:00
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;
}
2022-05-30 19:36:18 +01:00
}