209 lines
7.9 KiB
Java
209 lines
7.9 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.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import javax.imageio.ImageIO;
|
|
import java.util.ArrayList;
|
|
import java.util.Scanner;
|
|
import javax.sound.sampled.LineUnavailableException;
|
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
import javax.swing.*;
|
|
|
|
public class GamePanel extends JPanel implements Runnable, KeyListener{
|
|
|
|
//dimensions of window
|
|
public static final int GAME_WIDTH = 1225;
|
|
public static final int GAME_HEIGHT = 630;
|
|
|
|
public Thread gameThread;
|
|
public Image image;
|
|
public Graphics graphics;
|
|
public Player player;
|
|
public BackgroundImage background;
|
|
public int frame;
|
|
// keeps track of how many ticks has elapsed since last frame change
|
|
public int frameCounter = 0;
|
|
|
|
public BufferedImage[][][] spriteArray = new BufferedImage[2][2][11];
|
|
|
|
public static ArrayList<Tile>map = new ArrayList<Tile>();
|
|
|
|
public static Camera camera;
|
|
|
|
// image imports begin here
|
|
public BufferedImage backgroundImage = getImage("img/backgrounds/pointyMountains.png");
|
|
public BufferedImage box = getImage("img/tiles/boxes/box.png");
|
|
public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png");
|
|
|
|
public GamePanel() throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
|
|
camera = new Camera(0);
|
|
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10);
|
|
for (int i = 0; i < 11; i++) {
|
|
try {
|
|
BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0')));
|
|
|
|
spriteArray[1][0][i] = sprite;
|
|
spriteArray[1][1][i] = sprite;
|
|
spriteArray[0][0][i] = flipImageHorizontally(sprite);
|
|
spriteArray[0][1][i] = flipImageHorizontally(sprite);
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //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)
|
|
map.add(new SingleTile(1000, 500, box));
|
|
map.add(new SingleTile(700, 400, boxCoin));
|
|
map.add(new SingleTile(1000, 300, boxCoin));
|
|
map.add(new Tile(700, 200));
|
|
this.setFocusable(true); //make everything in this class appear on the screen
|
|
this.addKeyListener(this); //start listening for keyboard input
|
|
|
|
//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) {
|
|
player.mousePressed(e);
|
|
}
|
|
});
|
|
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
|
|
|
|
//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();
|
|
}
|
|
|
|
//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();
|
|
draw(graphics, frame);//update the positions of everything on the screen
|
|
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 frame){
|
|
background.draw(g);
|
|
frameCounter += player.draw(g, frame);
|
|
for(Tile i: map){
|
|
i.draw(g);
|
|
}
|
|
g.drawString(camera.x+" "+map.get(0).x,100,100);
|
|
}
|
|
|
|
//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(){
|
|
player.move();
|
|
}
|
|
|
|
//handles all collision detection and responds accordingly
|
|
public void checkCollision() {
|
|
player.isGrounded = false;
|
|
|
|
for (Tile i : map) {
|
|
i.collide(player);
|
|
}
|
|
//force player to remain on screen
|
|
if (player.y <= 0) {
|
|
player.y = 0;
|
|
}
|
|
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 <= 0) {
|
|
player.x = 0;
|
|
}
|
|
if (player.x + Player.PLAYER_WIDTH >= GAME_WIDTH) {
|
|
player.x = GAME_WIDTH - Player.PLAYER_WIDTH;
|
|
}
|
|
}
|
|
|
|
|
|
//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(){
|
|
try {
|
|
MapReader.inputMap(map, "saves/Level1.txt");
|
|
} catch (IOException | SpriteException 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;
|
|
|
|
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){
|
|
move();
|
|
checkCollision();
|
|
repaint();
|
|
if (frameCounter > 5) {
|
|
// increment sprite image to be used and keeps it below 12
|
|
frame = (frame + 1) % 11;
|
|
frameCounter -= 5;
|
|
}
|
|
delta--;
|
|
}
|
|
}
|
|
}
|
|
|
|
//if a key is pressed, we'll send it over to the Player class for processing
|
|
public void keyPressed(KeyEvent e){
|
|
player.keyPressed(e);
|
|
}
|
|
|
|
//if a key is released, we'll send it over to the Player class for processing
|
|
public void keyReleased(KeyEvent e){
|
|
player.keyReleased(e);
|
|
}
|
|
|
|
//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;
|
|
}
|
|
|
|
|
|
|
|
} |