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:
parent
915afaa7cb
commit
e2438b4005
3 changed files with 32 additions and 72 deletions
|
|
@ -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),
|
/* 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
|
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 */
|
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 boolean levelProgress = false; // this is used to make the transition from the Game menu to the game level look cool
|
||||||
public int fadeAnimation = 0;
|
public int fadeAnimation = 0;
|
||||||
|
|
||||||
|
|
@ -107,13 +107,13 @@ public class Game extends JPanel implements ActionListener {
|
||||||
public int actionTimer = 0;
|
public int actionTimer = 0;
|
||||||
|
|
||||||
// defines the initial player variables
|
// defines the initial player variables
|
||||||
public int playerX = 423;
|
public double playerX = 423;
|
||||||
public int playerY = 310;
|
public double playerY = 310;
|
||||||
public double playerXVelocity = 0;
|
public double playerXVelocity = 0;
|
||||||
public double playerYVelocity = 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 friction = 0.6;
|
||||||
public final double maxSpeed = 6.0;
|
public final double maxSpeed = 6.0;
|
||||||
public final double gravity = 1.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 playerSizeX = 14*4;
|
||||||
public int playerSizeY = 18*4;
|
public int playerSizeY = 18*4;
|
||||||
public String playerDirection = "none";
|
public String playerFacing = "forward";
|
||||||
public BufferedImage playerCurrentSprite;
|
public BufferedImage playerCurrentSprite;
|
||||||
public int playerWalkingModifier = 5;
|
|
||||||
public boolean playerOnFeet = true;
|
public boolean playerOnFeet = true;
|
||||||
public boolean playerActive = true;
|
public boolean playerActive = false;
|
||||||
public int walkTick;
|
public int walkTick;
|
||||||
public static int ticksPerFrame = 8;
|
public static int ticksPerFrame = 8;
|
||||||
public static int[] walkCycle = {1, 0, 2, 0};
|
public static int[] walkCycle = {1, 0, 2, 0};
|
||||||
|
|
|
||||||
|
|
@ -11,31 +11,26 @@ public class input implements KeyListener {
|
||||||
public void keyPressed(KeyEvent e) { // input
|
public void keyPressed(KeyEvent e) { // input
|
||||||
if (m.playerActive) {
|
if (m.playerActive) {
|
||||||
switch (e.getKeyCode()) {
|
switch (e.getKeyCode()) {
|
||||||
case KeyEvent.VK_W:
|
case KeyEvent.VK_W, KeyEvent.VK_UP -> m.upHeld = true;
|
||||||
m.playerDirection = "backward";
|
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> m.leftHeld = true;
|
||||||
break;
|
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> m.downHeld = true;
|
||||||
case KeyEvent.VK_A, KeyEvent.VK_LEFT:
|
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> m.rightHeld = true;
|
||||||
m.playerDirection = "left";
|
case KeyEvent.VK_SPACE -> {
|
||||||
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:
|
|
||||||
if (m.playerOnFeet) {
|
if (m.playerOnFeet) {
|
||||||
m.playerYVelocity = m.jumpSpeed;
|
m.playerYVelocity = m.jumpSpeed;
|
||||||
m.playerOnFeet = false;
|
m.playerOnFeet = false;
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch (e.getKeyCode()) {
|
switch (e.getKeyCode()) {
|
||||||
case KeyEvent.VK_ESCAPE -> System.exit(0);
|
case KeyEvent.VK_ESCAPE -> System.exit(0);
|
||||||
case KeyEvent.VK_CONTROL -> System.out.println("X+Y="+m.playerX+"+"+m.playerY);
|
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
|
public void keyReleased(KeyEvent e) { // what happens when you let go of a key
|
||||||
if (m.playerActive) {
|
if (m.playerActive) {
|
||||||
switch (e.getKeyCode()) {
|
switch (e.getKeyCode()) {
|
||||||
case KeyEvent.VK_W:
|
case KeyEvent.VK_W, KeyEvent.VK_UP -> m.upHeld = false;
|
||||||
m.playerDirection = "none";
|
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> m.leftHeld = false;
|
||||||
break;
|
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> m.downHeld = false;
|
||||||
case KeyEvent.VK_A, KeyEvent.VK_LEFT:
|
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> m.rightHeld = false;
|
||||||
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; */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,20 +60,12 @@ public class update {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (m.playerActive) {
|
if (m.playerActive) {
|
||||||
boolean moving = false;
|
if (m.leftHeld && !m.rightHeld) { m.playerFacing = "left"; }
|
||||||
if (m.playerDirection.equals("right")) {
|
else if (m.rightHeld && !m.leftHeld) { m.playerFacing = "right"; }
|
||||||
m.playerX += m.playerWalkingModifier;
|
else if (m.upHeld && !m.downHeld) { m.playerFacing = "backward"; }
|
||||||
moving = true;
|
else if (m.downHeld && !m.upHeld) { m.playerFacing = "forward"; }
|
||||||
} else if (m.playerDirection.equals("left")) {
|
|
||||||
m.playerX -= m.playerWalkingModifier;
|
boolean moving = m.leftHeld != m.rightHeld || m.upHeld || m.downHeld;
|
||||||
moving = true;
|
|
||||||
} else if (m.playerDirection.equals("forward")) {
|
|
||||||
moving = true;
|
|
||||||
} else if (m.playerDirection.equals("backward")) {
|
|
||||||
moving = true;
|
|
||||||
} else {
|
|
||||||
moving = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int frame = 0;
|
int frame = 0;
|
||||||
if (moving) {
|
if (moving) {
|
||||||
|
|
@ -82,6 +74,7 @@ public class update {
|
||||||
} else {
|
} else {
|
||||||
m.walkTick = 0;
|
m.walkTick = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m.leftHeld && !m.rightHeld) {
|
if (m.leftHeld && !m.rightHeld) {
|
||||||
m.playerXVelocity -= m.acceleration;
|
m.playerXVelocity -= m.acceleration;
|
||||||
} else if (m.rightHeld && !m.leftHeld) {
|
} else if (m.rightHeld && !m.leftHeld) {
|
||||||
|
|
@ -100,9 +93,8 @@ public class update {
|
||||||
m.playerY += m.playerYVelocity;
|
m.playerY += m.playerYVelocity;
|
||||||
|
|
||||||
// collisions
|
// collisions
|
||||||
|
|
||||||
double groundY = getGroundY(m);
|
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.playerY = (int) groundY;
|
||||||
m.playerYVelocity = 0;
|
m.playerYVelocity = 0;
|
||||||
m.playerOnFeet = true;
|
m.playerOnFeet = true;
|
||||||
|
|
@ -110,19 +102,8 @@ public class update {
|
||||||
m.playerOnFeet = false;
|
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
|
// 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
|
// first ship
|
||||||
if (m.playerX < -m.playerSizeX || m.playerX > m.windowWidth) { m.level = 4; }
|
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);
|
g.drawString("could be safe from Ratticus (I'm scared)", 30, m.windowHeight/2-160+350);
|
||||||
}
|
}
|
||||||
if (m.playerActive) {
|
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();
|
Toolkit.getDefaultToolkit().sync();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue