Skip to content

Commit

Permalink
Fixing Control Issues with double key pressed using new uodatePos
Browse files Browse the repository at this point in the history
  • Loading branch information
IamRishavDas committed Feb 20, 2024
1 parent 46e4106 commit 5049f16
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
16 changes: 9 additions & 7 deletions Controller/KeyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import java.awt.event.KeyListener;

import NewGame.GamePanel;
import Utils.Constants;

public class KeyController implements KeyListener{
GamePanel gamePanel;
private GamePanel gamePanel;
public KeyController(GamePanel gamePanel){
this.gamePanel = gamePanel;
}
Expand All @@ -21,18 +20,21 @@ public void keyTyped(KeyEvent e) {
public void keyPressed(KeyEvent e) {
int keyEvent = e.getKeyCode();
switch(keyEvent){
case KeyEvent.VK_RIGHT -> gamePanel.getGame().getPlayer().setDirection(Constants.Directions.RIGHT);
case KeyEvent.VK_LEFT -> gamePanel.getGame().getPlayer().setDirection(Constants.Directions.LEFT);
case KeyEvent.VK_UP -> gamePanel.getGame().getPlayer().setDirection(Constants.Directions.UP);
case KeyEvent.VK_DOWN -> gamePanel.getGame().getPlayer().setDirection(Constants.Directions.DOWN);
case KeyEvent.VK_RIGHT -> gamePanel.getGame().getPlayer().setRight(true);
case KeyEvent.VK_LEFT -> gamePanel.getGame().getPlayer().setLeft(true);
case KeyEvent.VK_UP -> gamePanel.getGame().getPlayer().setUp(true);
case KeyEvent.VK_DOWN -> gamePanel.getGame().getPlayer().setDown(true);
}
}

@Override
public void keyReleased(KeyEvent e) {
int keyEvent = e.getKeyCode();
switch(keyEvent){
case KeyEvent.VK_RIGHT, KeyEvent.VK_LEFT, KeyEvent.VK_UP, KeyEvent.VK_DOWN -> gamePanel.getGame().getPlayer().setMoving(false);
case KeyEvent.VK_RIGHT -> gamePanel.getGame().getPlayer().setRight(false);
case KeyEvent.VK_LEFT -> gamePanel.getGame().getPlayer().setLeft(false);
case KeyEvent.VK_UP -> gamePanel.getGame().getPlayer().setUp(false);
case KeyEvent.VK_DOWN -> gamePanel.getGame().getPlayer().setDown(false);
}
}
}
78 changes: 58 additions & 20 deletions Entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ public class Player extends Entity {
private BufferedImage[][] animations;
private int aniTick, aniSpeed = 15, aniIndex; // the lower aniSpeed leads to increase the animation speed
private int playerAction = Constants.PlayerConstants.IDLE;
private int playerDir = -1;

private boolean left, right, up, down;

private boolean moving = false;
private final int move = 1;
private final int playerSpeed = 1;

public Player(float x, float y) {
super(x, y);
loadAnimations();
}

public void update() {
updateAnimation();
setAnimation();
updatePos();
setAnimation();
updateAnimation();
}

// render the player
Expand Down Expand Up @@ -58,15 +60,6 @@ private void loadAnimations() {
}
}

public void setDirection(int direction){
this.playerDir = direction;
moving = true;
}

public void setMoving(boolean moving){
this.moving = moving;
}

public void setAnimation(){
if(moving){
playerAction = Constants.PlayerConstants.RUNNING;
Expand All @@ -76,13 +69,23 @@ public void setAnimation(){
}

public void updatePos(){
if(moving){
switch (playerDir) {
case Constants.Directions.LEFT -> x -= move;
case Constants.Directions.RIGHT -> x += move;
case Constants.Directions.UP -> y -= move;
case Constants.Directions.DOWN -> y += move;
}

moving = false; //default

if(left && !right){ // left
x -= playerSpeed;
moving = true;
}else if(!left && right){ //right
x += playerSpeed;
moving = true;
}

if(up && !down){ //up
y -= playerSpeed;
moving = true;
}else if(!up && down){ //down
y += playerSpeed;
moving = true;
}
}

Expand All @@ -96,4 +99,39 @@ private void updateAnimation(){
}
}
}

public boolean isLeft() {
return left;
}

public void setLeft(boolean left) {
this.left = left;
}

public boolean isRight() {
return right;
}

public void setRight(boolean right) {
this.right = right;
}

public boolean isUp() {
return up;
}

public void setUp(boolean up) {
this.up = up;
}

public boolean isDown() {
return down;
}

public void setDown(boolean down) {
this.down = down;
}



}
7 changes: 4 additions & 3 deletions NewGame/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class Game implements Runnable {
// UPS setter (Update per Sec)
private final int UPS = 200;

Player player;
// creating a player in Game
private Player player;

public Game() {
initClasses();
Expand All @@ -43,7 +44,7 @@ public void render(Graphics g){
player.render(g);
}

// game loop (fps counter)
// game loop (fps counter) && UPS counter
@Override
public void run() {
double timePerFrame = 1e9 / FPS;
Expand Down Expand Up @@ -89,6 +90,6 @@ public void run() {
}

public Player getPlayer(){
return player;
return this.player;
}
}

0 comments on commit 5049f16

Please sign in to comment.