Commenting player class, fixed steel bug

master
Chara1236 2022-06-20 20:32:40 -04:00
parent a94409dd12
commit 55a19bf5f5
11 changed files with 95 additions and 44 deletions

View File

@ -13,6 +13,7 @@
2950 550 you can blow up boxes 2950 550 you can blow up boxes
500 200 Press WASD to move and R to restart 500 200 Press WASD to move and R to restart
500 250 S makes you fall down faster 500 250 S makes you fall down faster
500 300 P skips the level
4250 100 You can pickup/placedown steel using right click 4250 100 You can pickup/placedown steel using right click
4250 150 Hold right click to see your pickup/placedown range 4250 150 Hold right click to see your pickup/placedown range
4250 200 You can't jump or throw bombs while while holding steel 4250 200 You can't jump or throw bombs while while holding steel

View File

@ -1,4 +1,4 @@
qwmwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwmwwwwwwwmmwwwwwwmwwwmmwwwwwmwwwwwmmwwwwwwwwwwwwwwe qwwwwmwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwmwwwwwwwmmwwwwwwmwwwmmwwwwwmwwwwwmmwwwwwwwwwwwwwwe
a mb !! b d a mb !! b d
a bbbbbbo hmmmmmmmmmmmmmm1 a bbbbbbo hmmmmmmmmmmmmmm1
a b h + a b h +

View File

@ -0,0 +1 @@
$Empty

1
saves/Level6-signs.txt Normal file
View File

@ -0,0 +1 @@
600 100 YOU WIN THE GAME! Have fun with these bombs

18
saves/Level6.txt Normal file
View File

@ -0,0 +1,18 @@
qwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwe
s d
s d
s d
s d
s d
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
sbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbd
zxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc

Binary file not shown.

BIN
sound/land.wav Normal file

Binary file not shown.

View File

@ -607,7 +607,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
e = UtilityFunction.intercept(e, middlewareArray); e = UtilityFunction.intercept(e, middlewareArray);
player.keyReleased(e); player.keyReleased(e);
// pressing the P key skips to the next level // pressing the P key skips to the next level
if(e.getKeyCode() == KeyEvent.VK_P){ if(e.getKeyCode() == KeyEvent.VK_P && !isDialogue){
LevelManager.nextLevel(); LevelManager.nextLevel();
try { try {
player.resetNoSound(); player.resetNoSound();

View File

@ -52,6 +52,12 @@ public class LevelManager implements Serializable {
ySpawn = 350; ySpawn = 350;
filePath = "saves/Level5.txt"; filePath = "saves/Level5.txt";
bombs = 1; bombs = 1;
} else if(level == 6){
//-1100/460
xSpawn = 0;
ySpawn = 50;
filePath = "saves/Level6.txt";
bombs = 999999;
} }
try { try {
// load map into GamePanel // load map into GamePanel
@ -116,6 +122,8 @@ public class LevelManager implements Serializable {
bombs = 5; bombs = 5;
} else if(GameFrame.game.level == 5){ } else if(GameFrame.game.level == 5){
bombs = 1; bombs = 1;
} else if(GameFrame.game.level == 6){
bombs = 999999;
} }
} }
// overloaded setLevel that accepts only a level argument // overloaded setLevel that accepts only a level argument

View File

@ -18,7 +18,6 @@ public class Player extends GenericSprite {
public static final int steelReachRange = 4*Tile.length; public static final int steelReachRange = 4*Tile.length;
public int lastXDirection, lastYDirection, lastFrame; public int lastXDirection, lastYDirection, lastFrame;
public static final int walkSpeedCap = 5;
public boolean alive; public boolean alive;
private transient Sound jump = new Sound("sound/jump.wav"); private transient Sound jump = new Sound("sound/jump.wav");
@ -42,8 +41,9 @@ public class Player extends GenericSprite {
public boolean leftClickPlacedSteel; public boolean leftClickPlacedSteel;
public Player(int x, int y, BufferedImageWrapper[][][] sprites) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public Player(int x, int y, BufferedImageWrapper[][][] sprites) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
//Creates the generic sprite via super function
super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH); super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
// jump = new Sound("sound/jump.wav"); //Sets default state of player
spriteArray = sprites; spriteArray = sprites;
alive = true; alive = true;
isPlayer = true; isPlayer = true;
@ -59,7 +59,8 @@ public class Player extends GenericSprite {
// moves paddle when key is pressed //Allows the plays to move depending on which keys are pressed.
//Or reset if R is pressed
public void keyPressed(KeyEvent e) throws IOException { public void keyPressed(KeyEvent e) throws IOException {
if(e.getKeyCode() == KeyEvent.VK_D){ if(e.getKeyCode() == KeyEvent.VK_D){
rightPressed = true; rightPressed = true;
@ -78,7 +79,7 @@ public class Player extends GenericSprite {
} }
} }
// stops moving paddle when key is released //Stops movement when player releases keys
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_D){ if(e.getKeyCode() == KeyEvent.VK_D){
rightPressed = false; rightPressed = false;
@ -94,36 +95,42 @@ public class Player extends GenericSprite {
} }
} }
//Checks if the player overlaps with a tile
public boolean collide(Tile tile, double x, double y){ public boolean collide(Tile tile, double x, double y){
if(tile==null){return false;} if(tile==null){return false;}
if(!tile.collision){ if(!tile.collision){
return false; return false;
} }
if(x+WIDTH>tile.realX&&x<tile.realX+Tile.length&&y-tile.y<Tile.length&&tile.y-y<HEIGHT){ return x + WIDTH > tile.realX && x < tile.realX + Tile.length && y - tile.y < Tile.length && tile.y - y < HEIGHT;
return true;
}
return false;
} }
// calls parent
//Checks if the player can change their x position by some amount, and y position by amount.
//If any tiles overlap the player, it can't update.
//Kills the player if they touch lava and sets the game to the next level if they finish.
public boolean canUpdate(double x, double y) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public boolean canUpdate(double x, double y) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
if(this.y+y<=-(HEIGHT+Tile.length)){ if(this.y+y<=-(HEIGHT+Tile.length)){
return false; return false;
} }
boolean canUpdate = true; boolean canUpdate = true;
//To reduce lag, we only check the tiles near by, which the boundaries are set
//By these 4 variables.
int lowX = Math.max(0, ((GameFrame.game.camera.x+GamePanel.GAME_WIDTH)/Tile.length)-4); int lowX = Math.max(0, ((GameFrame.game.camera.x+GamePanel.GAME_WIDTH)/Tile.length)-4);
int highX = Math.min(lowX + 8, GameFrame.game.map.length); int highX = Math.min(lowX + 8, GameFrame.game.map.length);
int lowY = Math.max(0,(this.y/Tile.length)-6); int lowY = Math.max(0,(this.y/Tile.length)-6);
int highY = Math.min(lowY + 12, GameFrame.game.map[0].length); int highY = Math.min(lowY + 12, GameFrame.game.map[0].length);
//Actually checks the blocks around and if they collide with the player.
for(int i=lowX; i<highX; i++) { for(int i=lowX; i<highX; i++) {
for (int j = lowY; j < highY; j++) { for (int j = lowY; j < highY; j++) {
if (GameFrame.game.map != null) { if (GameFrame.game.map != null) {
if (collide(GameFrame.game.map[i][j], this.x + x, this.y + y)) { if (collide(GameFrame.game.map[i][j], this.x + x, this.y + y)) {
//Next level if the block is the finish line
if (GameFrame.game.map[i][j].isFinish) { if (GameFrame.game.map[i][j].isFinish) {
LevelManager.nextLevel(); LevelManager.nextLevel();
GameFrame.game.player.resetNoSound(); GameFrame.game.player.resetNoSound();
return true; return true;
} }
//Reset the level if the player touches lava
if (GameFrame.game.map[i][j].kills) { if (GameFrame.game.map[i][j].kills) {
GameFrame.game.player.reset(); GameFrame.game.player.reset();
return true; return true;
@ -139,15 +146,8 @@ public class Player extends GenericSprite {
} }
//Checks if player can place steel, and changes the canPlaceSteel variable to true or false
public void updatePlaceSteel(int x, int y) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public void updatePlaceSteel(int x, int y) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
if(this.y<0){
canPlaceSteel = false;
return;
}
if(canUpdate(0,2)){
canPlaceSteel = false;
return;
}
canPlaceSteel = true; canPlaceSteel = true;
boolean adjacent = false; boolean adjacent = false;
int realX = x*Tile.length-GameFrame.game.camera.x; int realX = x*Tile.length-GameFrame.game.camera.x;
@ -156,24 +156,33 @@ public class Player extends GenericSprite {
double hypo = Math.sqrt(xDis*xDis+yDis*yDis); double hypo = Math.sqrt(xDis*xDis+yDis*yDis);
int xx = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length; int xx = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length;
int yy = (mouseY / Tile.length); int yy = (mouseY / Tile.length);
//System.out.println(hypo +" "+xDis + " "+ realX); //If you are off the screen, you can't place
if(this.y<0){
canPlaceSteel = false;
return;
}
//If you are falling, you can't place
if(canUpdate(0,2)){
canPlaceSteel = false;
return;
}
//If the block is over an enemy, you can't place
for(NonPlayer e: GameFrame.game.enemy){ for(NonPlayer e: GameFrame.game.enemy){
int TileX = xx*Tile.length; int TileX = xx*Tile.length;
int TileY = yy*Tile.length; int TileY = yy*Tile.length;
int ex = (e.x+GamePanel.GAME_WIDTH/2); int ex = (e.x+GamePanel.GAME_WIDTH/2);
//System.out.println((xx*Tile.length)+" "+(yy*Tile.length)+" ");
//System.out.print(" "+ (e.x+GamePanel.GAME_WIDTH/2) + " "+e.y+" ");
if(TileX<=ex+e.WIDTH&&ex<=TileX+Tile.length&&TileY<=e.y+e.HEIGHT&&e.y<=TileY+Tile.length){ if(TileX<=ex+e.WIDTH&&ex<=TileX+Tile.length&&TileY<=e.y+e.HEIGHT&&e.y<=TileY+Tile.length){
canPlaceSteel = false; canPlaceSteel = false;
return; return;
} }
} }
if(Math.abs(xDis)<(WIDTH+Tile.length)/2&&Math.abs(yDis)<(HEIGHT+Tile.length)/2){ //If the block is over the player, you can't place
if(Math.abs(xDis)<(WIDTH+Tile.length)/2+5&&Math.abs(yDis)<(HEIGHT+Tile.length)/2){
canPlaceSteel = false; return; canPlaceSteel = false; return;
} }
//If the block isn't adjacent to any tile, you can't place
int[][]check = {{1,0},{0,1},{-1,0},{0,-1}}; int[][]check = {{1,0},{0,1},{-1,0},{0,-1}};
for(int[]a: check){ for(int[]a: check){
try{ try{
@ -181,31 +190,40 @@ public class Player extends GenericSprite {
adjacent = true; adjacent = true;
break; break;
} }
} catch(Exception e){ } catch(Exception ignored){
} }
} }
if(!adjacent){canPlaceSteel = false; return;} if(!adjacent){canPlaceSteel = false; return;}
//If the block is out of range, you can't place
if(hypo>steelReachRange){canPlaceSteel = false; return;} if(hypo>steelReachRange){canPlaceSteel = false; return;}
//If there is a non-replacable block, you can't place
if(GameFrame.game.map[x][y]!=null&&!GameFrame.game.map[x][y].replaceAble){canPlaceSteel = false; return;} if(GameFrame.game.map[x][y]!=null&&!GameFrame.game.map[x][y].replaceAble){canPlaceSteel = false; return;}
//If you can't reach an area because of blocks in the way, you can't place.
if(!canReach(xx,yy)){canPlaceSteel = false; return;}; if(!canReach(xx,yy)){canPlaceSteel = false; return;};
//System.out.println(realX);
} }
//Moves the player with physics.
public void move() throws IOException, UnsupportedAudioFileException, LineUnavailableException { public void move() throws IOException, UnsupportedAudioFileException, LineUnavailableException {
// mouseX = MouseInfo.getPointerInfo().getLocation().x; //To prevent spamming steel block, we have pickup delay for the steel blocks
// mouseY = MouseInfo.getPointerInfo().getLocation().y;
pickupDelay = Math.max(0,pickupDelay - 1); pickupDelay = Math.max(0,pickupDelay - 1);
int Tilex = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length; int Tilex = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length;
int Tiley = (mouseY / Tile.length); int Tiley = (mouseY / Tile.length);
//If the player is holding steel, we update to see if they can updatePlaceSteel.
if(holdingSteel){ if(holdingSteel){
updatePlaceSteel(Tilex,Tiley); updatePlaceSteel(Tilex,Tiley);
} }
//Stops player from glitching into corner
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
GameFrame.game.camera.x += -Math.signum(xVelocity); GameFrame.game.camera.x += -Math.signum(xVelocity);
} }
//Prevents player from passing tiles in the x-direction
if(!canUpdate(xVelocity, 0)){ if(!canUpdate(xVelocity, 0)){
int updateAmount = 0; int updateAmount = 0;
if(xVelocity>0){ if(xVelocity>0){
@ -221,12 +239,14 @@ public class Player extends GenericSprite {
} }
//xVelocity = 0; //xVelocity = 0;
} }
//Prevents player from passing blocks from the y direction
if(!canUpdate(0, yVelocity)){ if(!canUpdate(0, yVelocity)){
if(yVelocity>0){ if(yVelocity>0){
while(canUpdate(0,1)){ while(canUpdate(0,1)){
y+=1; y+=1;
} }
isGrounded = true; isGrounded = true;
} else if(yVelocity<0){ } else if(yVelocity<0){
while(canUpdate(0,-1)){ while(canUpdate(0,-1)){
y-=1; y-=1;
@ -234,6 +254,7 @@ public class Player extends GenericSprite {
} }
yVelocity = 0; yVelocity = 0;
} }
//If the player can update their position due to nothing being in the way, update their position.
if(canUpdate(xVelocity, yVelocity)) { if(canUpdate(xVelocity, yVelocity)) {
y = y + (int) yVelocity; y = y + (int) yVelocity;
GameFrame.game.camera.x = GameFrame.game.camera.x + (int) xVelocity; GameFrame.game.camera.x = GameFrame.game.camera.x + (int) xVelocity;
@ -245,6 +266,8 @@ public class Player extends GenericSprite {
} }
//Give the player xVelocity if they press left or right
//Add particles if the player is walking
if(rightPressed){ if(rightPressed){
if(isGrounded){ if(isGrounded){
addParticle(-1); addParticle(-1);
@ -261,6 +284,9 @@ public class Player extends GenericSprite {
xVelocity -= 1; xVelocity -= 1;
} }
} }
//Checks if the player can jump
//Conditions: They press up, they are on the ground, they are not holding steel
if(upPressed&&isGrounded&&!holdingSteel){ if(upPressed&&isGrounded&&!holdingSteel){
y-=1; y-=1;
isGrounded = false; isGrounded = false;
@ -278,6 +304,7 @@ public class Player extends GenericSprite {
} }
xVelocity *= 0.93; xVelocity *= 0.93;
//Adds gravity, and allows player to fall down faster.
if(!isGrounded) { if(!isGrounded) {
yVelocity += 0.3; yVelocity += 0.3;
if(downPressed){ if(downPressed){
@ -285,14 +312,17 @@ public class Player extends GenericSprite {
} }
} }
//If the player is dead, reset map
if(!alive){ if(!alive){
alive = true; alive = true;
reset(); reset();
} }
//Speed limit to player
capSpeed(); capSpeed();
} }
//Spawn the player with the death sound
public void reset() throws UnsupportedAudioFileException, LineUnavailableException, IOException { public void reset() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
SoundWrapper.playSound("sound/OOF.wav"); SoundWrapper.playSound("sound/OOF.wav");
holdingSteel = false; holdingSteel = false;
@ -303,6 +333,8 @@ public class Player extends GenericSprite {
LevelManager.setBombs(); LevelManager.setBombs();
} }
//Spawns the player without sound
public void resetNoSound() throws IOException { public void resetNoSound() throws IOException {
holdingSteel = false; holdingSteel = false;
LevelManager.setLevel(GameFrame.game.level, true); LevelManager.setLevel(GameFrame.game.level, true);
@ -311,8 +343,11 @@ public class Player extends GenericSprite {
GameFrame.game.bombs.clear(); GameFrame.game.bombs.clear();
LevelManager.setBombs(); LevelManager.setBombs();
} }
//Handles events when the player presses a mouse button.
//Left for throwing bombs, and right for picking and placing steel
public void mousePressed(MouseEvent e) throws SpriteException, IOException { public void mousePressed(MouseEvent e) throws SpriteException, IOException {
canReach(1,1); //canReach(1,1);
mouseX = e.getX(); mouseX = e.getX();
mouseY = e.getY(); mouseY = e.getY();
if(e.getButton()==MouseEvent.BUTTON1) { if(e.getButton()==MouseEvent.BUTTON1) {

View File

@ -6,19 +6,6 @@ public class SoundWrapper implements Serializable {
transient public Sound sound; transient public Sound sound;
public String soundString; public String soundString;
public static Sound grass;
static {
try {
grass = new Sound("sound/grass.wav");
} catch (UnsupportedAudioFileException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (LineUnavailableException e) {
throw new RuntimeException(e);
}
}
// please note that not as many constructors were implemented as BufferedImage, as this class was created before most sounds were added; // please note that not as many constructors were implemented as BufferedImage, as this class was created before most sounds were added;
// as such, backwards compatibility was not needed // as such, backwards compatibility was not needed