fully revamped the movement system, also I once again left out level =3 and playeractive in variables so I fixed that and instead I made the "Delete" key set those variables to 3 and true

This commit is contained in:
Martin Dvořák 2026-10-23 00:18:39 +02:00
commit e2438b4005
3 changed files with 32 additions and 72 deletions

View file

@ -60,7 +60,7 @@ public class Game extends JPanel implements ActionListener {
/* defines the level which is used to determine what exactly shows (for example main menu is level 1),
levelProgress which is only used for the animation from level 1 to level 2
and fadeAnimation which is also used for the animation from level 1 to level 2 */
public int level = 3;
public int level = 1;
public boolean levelProgress = false; // this is used to make the transition from the Game menu to the game level look cool
public int fadeAnimation = 0;
@ -107,13 +107,13 @@ public class Game extends JPanel implements ActionListener {
public int actionTimer = 0;
// defines the initial player variables
public int playerX = 423;
public int playerY = 310;
public double playerX = 423;
public double playerY = 310;
public double playerXVelocity = 0;
public double playerYVelocity = 0;
public boolean leftHeld, rightHeld;
public boolean leftHeld, rightHeld, upHeld, downHeld;
public static final double acceleration = 0.01;
public static final double acceleration = 0.6;
public final double friction = 0.6;
public final double maxSpeed = 6.0;
public final double gravity = 1.0;
@ -122,11 +122,10 @@ public class Game extends JPanel implements ActionListener {
public int playerSizeX = 14*4;
public int playerSizeY = 18*4;
public String playerDirection = "none";
public String playerFacing = "forward";
public BufferedImage playerCurrentSprite;
public int playerWalkingModifier = 5;
public boolean playerOnFeet = true;
public boolean playerActive = true;
public boolean playerActive = false;
public int walkTick;
public static int ticksPerFrame = 8;
public static int[] walkCycle = {1, 0, 2, 0};

View file

@ -11,31 +11,26 @@ public class input implements KeyListener {
public void keyPressed(KeyEvent e) { // input
if (m.playerActive) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W:
m.playerDirection = "backward";
break;
case KeyEvent.VK_A, KeyEvent.VK_LEFT:
m.playerDirection = "left";
m.leftHeld = true;
break;
case KeyEvent.VK_S:
m.playerDirection = "forward";
break;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT:
m.playerDirection = "right";
m.rightHeld = true;
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_W, KeyEvent.VK_UP -> m.upHeld = true;
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> m.leftHeld = true;
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> m.downHeld = true;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> m.rightHeld = true;
case KeyEvent.VK_SPACE -> {
if (m.playerOnFeet) {
m.playerYVelocity = m.jumpSpeed;
m.playerOnFeet = false;
}
break;
}
}
}
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE -> System.exit(0);
case KeyEvent.VK_CONTROL -> System.out.println("X+Y="+m.playerX+"+"+m.playerY);
// THE LINE BELOW IS FOR ME WHEN DEBUGGING, I LEFT IT IN BECAUSE ITS IMPOSSIBLE TO KNOW ABOUT IT WITHOUT READING THE CODE LOL
case KeyEvent.VK_DELETE -> {
m.level = 3;
m.playerActive = true;
}
}
}
@ -46,25 +41,10 @@ public class input implements KeyListener {
public void keyReleased(KeyEvent e) { // what happens when you let go of a key
if (m.playerActive) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W:
m.playerDirection = "none";
break;
case KeyEvent.VK_A, KeyEvent.VK_LEFT:
m.playerDirection = "none";
m.leftHeld = false;
break;
case KeyEvent.VK_S:
m.playerDirection = "none";
break;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT:
m.playerDirection = "none";
m.rightHeld = false;
break;
/* ONLY FOR DEBUGGING BELOW
case KeyEvent.VK_A -> m.playerX -= m.playerWalkingModifier;
case KeyEvent.VK_D -> m.playerX += m.playerWalkingModifier;
case KeyEvent.VK_S -> m.playerY += 1;
case KeyEvent.VK_W -> m.playerY -= 1; */
case KeyEvent.VK_W, KeyEvent.VK_UP -> m.upHeld = false;
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> m.leftHeld = false;
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> m.downHeld = false;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> m.rightHeld = false;
}
}
}

View file

@ -60,20 +60,12 @@ public class update {
}
}
if (m.playerActive) {
boolean moving = false;
if (m.playerDirection.equals("right")) {
m.playerX += m.playerWalkingModifier;
moving = true;
} else if (m.playerDirection.equals("left")) {
m.playerX -= m.playerWalkingModifier;
moving = true;
} else if (m.playerDirection.equals("forward")) {
moving = true;
} else if (m.playerDirection.equals("backward")) {
moving = true;
} else {
moving = false;
}
if (m.leftHeld && !m.rightHeld) { m.playerFacing = "left"; }
else if (m.rightHeld && !m.leftHeld) { m.playerFacing = "right"; }
else if (m.upHeld && !m.downHeld) { m.playerFacing = "backward"; }
else if (m.downHeld && !m.upHeld) { m.playerFacing = "forward"; }
boolean moving = m.leftHeld != m.rightHeld || m.upHeld || m.downHeld;
int frame = 0;
if (moving) {
@ -82,6 +74,7 @@ public class update {
} else {
m.walkTick = 0;
}
if (m.leftHeld && !m.rightHeld) {
m.playerXVelocity -= m.acceleration;
} else if (m.rightHeld && !m.leftHeld) {
@ -100,9 +93,8 @@ public class update {
m.playerY += m.playerYVelocity;
// collisions
double groundY = getGroundY(m);
if (m.playerY >= groundY) {
if (m.playerY+10 >= groundY) { // the +10 is so it feels like you can jump the instant you touch the ground even tho you technically are a bit above it, just makes the movement feel snappier
m.playerY = (int) groundY;
m.playerYVelocity = 0;
m.playerOnFeet = true;
@ -110,19 +102,8 @@ public class update {
m.playerOnFeet = false;
}
/*
if (m.level == 3 && (m.playerY < 338 || (m.playerX >= 583 || m.playerX <= 274) && m.playerY < 366)) {
m.playerY += 10;
m.playerOnFeet = false;
} else if (m.level == 3 && m.playerX < 583 && m.playerX > 274) {
m.playerY = 338;
m.playerOnFeet = true;
} else {
m.playerOnFeet = true;
}*/
// update the player sprite based on direction and if he's walking and frame
m.playerCurrentSprite = spriteFor(m, m.playerDirection, frame);
m.playerCurrentSprite = spriteFor(m, m.playerFacing, frame);
// first ship
if (m.playerX < -m.playerSizeX || m.playerX > m.windowWidth) { m.level = 4; }
}
@ -274,7 +255,7 @@ public class update {
g.drawString("could be safe from Ratticus (I'm scared)", 30, m.windowHeight/2-160+350);
}
if (m.playerActive) {
g.drawImage(m.playerCurrentSprite, m.playerX, m.playerY, m.playerSizeX, m.playerSizeY, m);
g.drawImage(m.playerCurrentSprite, (int) m.playerX, (int) m.playerY, m.playerSizeX, m.playerSizeY, m);
}
Toolkit.getDefaultToolkit().sync();
}