From a981aa43ac01bcba6126961eb51e17108214afd4 Mon Sep 17 00:00:00 2001 From: qwet1235 Date: Fri, 25 Nov 2022 01:17:02 -0500 Subject: [PATCH] removed gravity. was able to figure out why the player was squished; i corrected it and also centralized the hitradius/attackradius of the player --- .idea/misc.xml | 2 +- src/main/java/Entities/Player.java | 108 ++++++++------------------ src/main/java/gamestates/Playing.java | 57 ++++++++------ 3 files changed, 67 insertions(+), 100 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 370437b..955f2f8 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,5 +4,5 @@ - + \ No newline at end of file diff --git a/src/main/java/Entities/Player.java b/src/main/java/Entities/Player.java index eee5d10..1beb2ed 100644 --- a/src/main/java/Entities/Player.java +++ b/src/main/java/Entities/Player.java @@ -16,18 +16,11 @@ public class Player extends Creature { private int aniTick, aniIndex, aniSpeed = 25; private int playerAction = IDLE; private boolean moving = false, attacking = false, hit = false; - private boolean left, up, right, down, jump; + private boolean left, up, right, down; private float playerSpeed = 1.0f * Game.SCALE; private int[][] lvlData; - private float xDrawOffset = 21 * Game.SCALE; - private float yDrawOffset = 4 * Game.SCALE; - - // Jumping / Gravity - private float airSpeed = 0f; - private float gravity = 0.04f * Game.SCALE; - private float jumpSpeed = -2.25f * Game.SCALE; - private float fallSpeedAfterCollision = 0.5f * Game.SCALE; - private boolean inAir = false; + private float xDrawOffset = 17 * Game.SCALE; + private float yDrawOffset = 16 * Game.SCALE; // StatusBarUI private BufferedImage statusBarImg; @@ -80,7 +73,7 @@ public void update() { } updateAttackBox(); - + updatePos(); if (attacking) checkAttack(); @@ -110,7 +103,6 @@ public void render(Graphics g, int lvlOffset) { (int) (hitRadius.x - xDrawOffset) - lvlOffset + flipX, (int) (hitRadius.y - yDrawOffset), width * flipW, height, null); drawHitRadius(g, lvlOffset); -// drawHitbox(g, lvlOffset); drawAttackRadius(g, lvlOffset); drawUI(g); @@ -148,24 +140,20 @@ private void updateAnimationTick() { private void setAnimation() { int startAni = playerAction; - if (moving) + if (moving) { playerAction = RUNNING; - else if (hit) { - playerAction = HIT; - } - else + } else { playerAction = IDLE; - - if (inAir) { - if (airSpeed < 0) - playerAction = JUMP; - else - playerAction = FALLING; } if (attacking) { playerAction = ATTACK; } + + if (hit) { + playerAction = HIT; + } + if (startAni != playerAction) resetAniTick(); } @@ -178,59 +166,31 @@ private void resetAniTick() { private void updatePos() { moving = false; - if (jump) - jump(); - - if (!inAir) - if ((!left && !right) || (right && left)) - return; - float xSpeed = 0; + float ySpeed = 0; if (left) { xSpeed -= playerSpeed; flipX = width; flipW = -1; - } - if (right) { + moving = true; + }else if (right) { xSpeed += playerSpeed; flipX = 0; flipW = 1; + moving = true; + } + if (up) { + ySpeed -= playerSpeed; + moving = true; + } else if (down) { + ySpeed += playerSpeed; + moving = true; } - if (!inAir) - if (!IsEntityOnFloor(hitRadius, lvlData)) - inAir = true; - - if (inAir) { - if (CanMoveHere(hitRadius.x, hitRadius.y + airSpeed, hitRadius.width, hitRadius.height, lvlData)) { - hitRadius.y += airSpeed; - airSpeed += gravity; - updateXPos(xSpeed); - } else { - hitRadius.y = GetEntityYPosUnderRoofOrAboveFloor(hitRadius, airSpeed); - if (airSpeed > 0) - resetInAir(); - else - airSpeed = fallSpeedAfterCollision; - updateXPos(xSpeed); - } - - } else - updateXPos(xSpeed); - moving = true; - } - - private void jump() { - if (inAir) - return; - inAir = true; - airSpeed = jumpSpeed; - } + updateXPos(xSpeed); + updateYPos(ySpeed); - private void resetInAir() { - inAir = false; - airSpeed = 0; } @@ -243,6 +203,15 @@ private void updateXPos(float xSpeed) { } + private void updateYPos(float ySpeed) { + if (CanMoveHere(hitRadius.x, hitRadius.y + ySpeed, hitRadius.width, hitRadius.height, lvlData)) { + hitRadius.y += ySpeed; + } else { + hitRadius.y = GetEntityYPosUnderRoofOrAboveFloor(hitRadius, ySpeed); + } + + } + public void changeHealth(int value) { currentHealth += value; @@ -273,8 +242,6 @@ private void loadAnimations() { public void loadLvlData(int[][] lvlData) { this.lvlData = lvlData; - if (!IsEntityOnFloor(hitRadius, lvlData)) - inAir = true; } @@ -321,13 +288,9 @@ public void setDown(boolean down) { this.down = down; } - public void setJump(boolean jump) { - this.jump = jump; - } - public void resetAll() { resetDirBooleans(); - inAir = false; + resetAniTick(); attacking = false; moving = false; playerAction = IDLE; @@ -335,8 +298,5 @@ public void resetAll() { hitRadius.x = x; hitRadius.y = y; - - if (!IsEntityOnFloor(hitRadius, lvlData)) - inAir = true; } } \ No newline at end of file diff --git a/src/main/java/gamestates/Playing.java b/src/main/java/gamestates/Playing.java index 8cc129c..da52bdb 100644 --- a/src/main/java/gamestates/Playing.java +++ b/src/main/java/gamestates/Playing.java @@ -51,7 +51,7 @@ public Playing(Game game) { private void initClasses() { // initialize all classes required to run the game levelManager = new LevelManager(game); enemyManager = new EnemyManager(this); - player = new Player(200, 200, (int) (64 * Game.SCALE), (int) (40 * Game.SCALE), this); + player = new Player(200, 200, (int) (64 * Game.SCALE), (int) (64 * Game.SCALE), this); player.loadLvlData(levelManager.getCurrentLevel().getLevelData()); pauseOverlay = new PauseOverlay(this); gameOverOverlay = new GameOverOverlay(this); @@ -131,9 +131,6 @@ public void setGameOver(boolean gameOver) { @Override public void mouseClicked(MouseEvent e) { - if (!gameOver) - if (e.getButton() == MouseEvent.BUTTON1) - player.setAttacking(true); } @Override @@ -142,18 +139,24 @@ public void keyPressed(KeyEvent e) { gameOverOverlay.keyPressed(e); else switch (e.getKeyCode()) { - case KeyEvent.VK_A: - player.setLeft(true); - break; - case KeyEvent.VK_D: - player.setRight(true); - break; - case KeyEvent.VK_SPACE: - player.setJump(true); - break; - case KeyEvent.VK_ESCAPE: - paused = !paused; - break; + case KeyEvent.VK_W: + player.setUp(true); + break; + case KeyEvent.VK_A: + player.setLeft(true); + break; + case KeyEvent.VK_S: + player.setDown(true); + break; + case KeyEvent.VK_D: + player.setRight(true); + break; + case KeyEvent.VK_SPACE: + player.setAttacking(true); + break; + case KeyEvent.VK_ESCAPE: + paused = !paused; + break; } } @@ -161,15 +164,19 @@ public void keyPressed(KeyEvent e) { public void keyReleased(KeyEvent e) { if (!gameOver) switch (e.getKeyCode()) { - case KeyEvent.VK_A: - player.setLeft(false); - break; - case KeyEvent.VK_D: - player.setRight(false); - break; - case KeyEvent.VK_SPACE: - player.setJump(false); - break; + case KeyEvent.VK_W: + player.setUp(false); + break; + case KeyEvent.VK_A: + player.setLeft(false); + break; + case KeyEvent.VK_S: + player.setDown(false); + break; + case KeyEvent.VK_D: + player.setRight(false); + break; + } }