From 2f5ded58ab917bedad360066470d978b58aedb25 Mon Sep 17 00:00:00 2001 From: Chara1236 Date: Sun, 5 Jun 2022 22:18:10 -0400 Subject: [PATCH] Enemy Causes Death --- saves/Level1.txt | 2 +- src/GamePanel.java | 31 +++++++++++++++++-------------- src/LevelManager.java | 23 ++++++++++++++++++++--- src/MapReader.java | 9 ++++++++- src/NonPlayer.java | 14 +++++++++++++- src/Player.java | 22 +++++++++++++++++----- 6 files changed, 76 insertions(+), 25 deletions(-) diff --git a/saves/Level1.txt b/saves/Level1.txt index ee931e3..f75a3e8 100644 --- a/saves/Level1.txt +++ b/saves/Level1.txt @@ -14,5 +14,5 @@ sssssssssssssssssd 1 zxc z sssssssssssssssssd sssssssssssssssssd qwwwwwwwwe sssssssssssssssssd qrsssssssstwe -sssssssssssssssssd qwwrssssssssssstwe qwe +sssssssssssssssssd qwwrssssssssssstwe !!! qwe ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3 \ No newline at end of file diff --git a/src/GamePanel.java b/src/GamePanel.java index a096de7..f8ce02f 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -27,15 +27,15 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ public Thread gameThread; public Image image; public Graphics graphics; - public Player player; + public static Player player; public BackgroundImage background; public int playerFrame, enemyFrame; // keeps track of how many ticks has elapsed since last frame change public int playerFrameCounter = 0; public int enemyFrameCounter = 0; - public BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11]; - public BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3]; + public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11]; + public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3]; public static ArrayListmap = new ArrayList(); public static ArrayListenemy = new ArrayList(); @@ -77,8 +77,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ e.printStackTrace(); } player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', playerSpriteArray); //create a player controlled player, set start location to middle of screenk - enemy.add(new NonPlayer(1200, 100, slimeSpriteArray, 50, 28)); - enemy.add(new NonPlayer(0, 100, slimeSpriteArray, 50, 28)); // the height of 35 is set because it is half of the original tile height (i.e., 70px) this.setFocusable(true); //make everything in this class appear on the screen this.addKeyListener(this); //start listening for keyboard input @@ -116,7 +114,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ enemy.get(i).draw(g, enemyFrame); } playerFrameCounter += player.draw(g, playerFrame); - g.drawString(camera.x+" "+map.get(0).x,100,100); + g.drawString(camera.x+" "+player.y,100,100); } //call the move methods in other classes to update positions @@ -131,12 +129,17 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ //handles all collision detection and responds accordingly public void checkCollision() { player.isGrounded = false; - for (NonPlayer n: enemy) { - n.isGrounded = false; - } for (Tile i : map) { i.update(); } + for (NonPlayer n: enemy) { + n.update(); + n.isGrounded = false; + if(n.collidePlayer(player)){ + player.alive = false; + } + } + //force player to remain on screen (For the most part) if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) { @@ -163,11 +166,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ //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(){ LevelManager.setLevel(1); - 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; @@ -198,11 +196,16 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ //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); + 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 diff --git a/src/LevelManager.java b/src/LevelManager.java index 664e934..c5e46f9 100644 --- a/src/LevelManager.java +++ b/src/LevelManager.java @@ -1,3 +1,7 @@ +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; +import java.io.IOException; + public class LevelManager { public static int level = 1; public static int xSpawn = 0; @@ -8,12 +12,25 @@ public class LevelManager { LevelManager.level = level; if(level == 1){ xSpawn = 0; - ySpawn = 600; + ySpawn = 300; filePath = "saves/Level1.txt"; } else if(level == 2){ - xSpawn = 0; - ySpawn = 600; + xSpawn = 200; + ySpawn = 100; filePath = "saves/Level2.txt"; } + try { + MapReader.inputMap(GamePanel.map, GamePanel.enemy,filePath); + } catch (IOException | SpriteException e) { + throw new RuntimeException(e); + } catch (UnsupportedAudioFileException e) { + throw new RuntimeException(e); + } catch (LineUnavailableException e) { + throw new RuntimeException(e); + } + GamePanel.player.reset(); + } + public static void nextLevel(){ + setLevel(level+1); } } diff --git a/src/MapReader.java b/src/MapReader.java index e76808f..18b6466 100644 --- a/src/MapReader.java +++ b/src/MapReader.java @@ -1,3 +1,5 @@ +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; import java.io.*; import java.util.ArrayList; import java.util.Scanner; @@ -15,7 +17,9 @@ public class MapReader { !: Slime Grass: */ - public static void inputMap(ArrayList map, String filePath) throws IOException, SpriteException { + public static void inputMap(ArrayList map, ArrayListenemy , String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { + enemy.clear(); + map.clear(); String file = FileManager.readFile(LevelManager.filePath); int x = -GamePanel.GAME_WIDTH/2 + Tile.length; int y = 0; @@ -59,6 +63,9 @@ public class MapReader { } else if(file.charAt(i)=='b'){ map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png"))); map.get(map.size()-1).collision = false; + } else if(file.charAt(i)=='!'){ + enemy.add(new NonPlayer(x, y, GamePanel.slimeSpriteArray, 50, 28)); + } x+=Tile.length; } diff --git a/src/NonPlayer.java b/src/NonPlayer.java index cf8da51..fd3a527 100644 --- a/src/NonPlayer.java +++ b/src/NonPlayer.java @@ -16,6 +16,9 @@ public class NonPlayer extends GenericSprite { public int currentXDirection, currentYDirection; public boolean isDead; + public int realX; + + // private final Sound bump; public BufferedImage[][][] spriteArray; @@ -40,6 +43,13 @@ public class NonPlayer extends GenericSprite { } return false; } + + public boolean collidePlayer(Player p){ + if(realX+npcWidth>p.x&&realX