Skip to content

Commit

Permalink
removed gravity. was able to figure out why the player was squished; …
Browse files Browse the repository at this point in the history
…i corrected it and also centralized the hitradius/attackradius of the player
  • Loading branch information
qwet1235 committed Nov 25, 2022
1 parent 243c30f commit a981aa4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 34 additions & 74 deletions src/main/java/Entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +73,7 @@ public void update() {
}

updateAttackBox();

updatePos();
if (attacking)
checkAttack();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;

}

Expand All @@ -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;

Expand Down Expand Up @@ -273,8 +242,6 @@ private void loadAnimations() {

public void loadLvlData(int[][] lvlData) {
this.lvlData = lvlData;
if (!IsEntityOnFloor(hitRadius, lvlData))
inAir = true;

}

Expand Down Expand Up @@ -321,22 +288,15 @@ 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;
currentHealth = maxHealth;

hitRadius.x = x;
hitRadius.y = y;

if (!IsEntityOnFloor(hitRadius, lvlData))
inAir = true;
}
}
57 changes: 32 additions & 25 deletions src/main/java/gamestates/Playing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -142,34 +139,44 @@ 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;
}
}

@Override
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;

}

}
Expand Down

0 comments on commit a981aa4

Please sign in to comment.