From 46e4106c1c9821acc27398f3d319dfa87289bc99 Mon Sep 17 00:00:00 2001 From: Rishav <140265067+IamRishavDas@users.noreply.github.com> Date: Tue, 20 Feb 2024 20:49:08 +0530 Subject: [PATCH] Making Different Player class for Player --- Controller/KeyController.java | 10 +-- Entities/Entity.java | 10 +++ Entities/Player.java | 99 +++++++++++++++++++++++++ NewGame/Game.java | 23 +++++- NewGame/GamePanel.java | 132 +++------------------------------- 5 files changed, 143 insertions(+), 131 deletions(-) create mode 100644 Entities/Entity.java create mode 100644 Entities/Player.java diff --git a/Controller/KeyController.java b/Controller/KeyController.java index e2f7c34..d329e41 100644 --- a/Controller/KeyController.java +++ b/Controller/KeyController.java @@ -21,10 +21,10 @@ public void keyTyped(KeyEvent e) { public void keyPressed(KeyEvent e) { int keyEvent = e.getKeyCode(); switch(keyEvent){ - case KeyEvent.VK_RIGHT -> gamePanel.setDirection(Constants.Directions.RIGHT); - case KeyEvent.VK_LEFT -> gamePanel.setDirection(Constants.Directions.LEFT); - case KeyEvent.VK_UP -> gamePanel.setDirection(Constants.Directions.UP); - case KeyEvent.VK_DOWN -> gamePanel.setDirection(Constants.Directions.DOWN); + 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); } } @@ -32,7 +32,7 @@ public void keyPressed(KeyEvent e) { 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.setMoving(false); + case KeyEvent.VK_RIGHT, KeyEvent.VK_LEFT, KeyEvent.VK_UP, KeyEvent.VK_DOWN -> gamePanel.getGame().getPlayer().setMoving(false); } } } diff --git a/Entities/Entity.java b/Entities/Entity.java new file mode 100644 index 0000000..4c6ccf1 --- /dev/null +++ b/Entities/Entity.java @@ -0,0 +1,10 @@ +package Entities; + +public abstract class Entity { + // only extending class can use it + protected float x, y; + public Entity(float x, float y){ + this.x = x; + this.y = y; + } +} diff --git a/Entities/Player.java b/Entities/Player.java new file mode 100644 index 0000000..c0f646d --- /dev/null +++ b/Entities/Player.java @@ -0,0 +1,99 @@ +package Entities; + +import java.awt.Graphics; +import java.awt.image.BufferedImage; +import java.io.InputStream; + +import javax.imageio.ImageIO; + +import Utils.Constants; + +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 moving = false; + private final int move = 1; + + public Player(float x, float y) { + super(x, y); + loadAnimations(); + } + + public void update() { + updateAnimation(); + setAnimation(); + updatePos(); + } + + // render the player + public void render(Graphics g) { + g.drawImage(animations[playerAction][aniIndex], (int)x, (int)y, 64*3, 40*3, null); + } + + private void loadAnimations() { + + String filePath = "/Resources/player_sprites.png"; + InputStream is = getClass().getResourceAsStream(filePath); + try { + BufferedImage image = ImageIO.read(is); + + animations = new BufferedImage[9][6]; + + for (int i = 0; i < animations.length; i++) { + for (int j = 0; j < animations[i].length; j++) { + animations[i][j] = image.getSubimage(j * 64, i * 40, 64, 40); + } + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + 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; + }else{ + playerAction = Constants.PlayerConstants.IDLE; + } + } + + 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; + } + } + } + + private void updateAnimation(){ + aniTick++; + if(aniTick >= aniSpeed){ + aniTick = 0; + aniIndex++; + if(aniIndex >= Constants.PlayerConstants.GetSpriteAmount(playerAction)){ + aniIndex = 0; + } + } + } +} diff --git a/NewGame/Game.java b/NewGame/Game.java index 1e615c9..7d856ea 100644 --- a/NewGame/Game.java +++ b/NewGame/Game.java @@ -1,5 +1,9 @@ package NewGame; +import java.awt.Graphics; + +import Entities.Player; + @SuppressWarnings("unused") public class Game implements Runnable { private GameFrame gameFrame; @@ -12,20 +16,31 @@ public class Game implements Runnable { // UPS setter (Update per Sec) private final int UPS = 200; + Player player; + public Game() { - gamePanel = new GamePanel(); + initClasses(); + gamePanel = new GamePanel(this); gameFrame = new GameFrame(gamePanel); gamePanel.requestFocus(); startGameLoop(); } + public void initClasses(){ + player = new Player(200, 200); + } + private void startGameLoop() { gameThread = new Thread(this); gameThread.start(); } public void update(){ - gamePanel.updateGame(); + player.update(); + } + + public void render(Graphics g){ + player.render(g); } // game loop (fps counter) @@ -72,4 +87,8 @@ public void run() { } } } + + public Player getPlayer(){ + return player; + } } \ No newline at end of file diff --git a/NewGame/GamePanel.java b/NewGame/GamePanel.java index caf15d7..7d6fb87 100644 --- a/NewGame/GamePanel.java +++ b/NewGame/GamePanel.java @@ -1,89 +1,32 @@ package NewGame; -import javax.imageio.ImageIO; + import javax.swing.JPanel; import Controller.KeyController; import Controller.MouseController; -import Utils.Constants; import java.awt.Dimension; import java.awt.Graphics; -import java.awt.image.BufferedImage; -import java.io.InputStream; public class GamePanel extends JPanel { - // load image - private BufferedImage image; - - // animations - - private int aniTick, aniSpeed = 15, aniIndex; // the lower aniSpeed leads to increase the animation speed - - // idle animations - private BufferedImage[][] animations; - - // current state of the player - private int playerAction = Constants.PlayerConstants.IDLE; - private int playerDir = -1; - private boolean moving = false; - private final int move = 5; - // panel dim private final int panelWidth = 1280; private final int panelHeight = 800; - // ball pos - private float posX = 50f; - private float posY = 50f; - - // // controlling speed - // private float dirX = 3f; - // private float dirY = 3f; + // using constructor initialize the game Component + private Game game; // adding panel attributes and listeners - public GamePanel() { - importImage(); - loadAnimations(); + public GamePanel(Game game) { + this.game = game; addKeyListener(new KeyController(this)); addMouseListener(new MouseController(this)); addMouseMotionListener(new MouseController(this)); setPanelSize(); } - public void updateGame(){ - updateAnimation(); - setAnimation(); - updatePos(); - } - - private void loadAnimations(){ - animations = new BufferedImage[9][6]; - - for(int i = 0; i < animations.length; i++) { - for(int j = 0; j < animations[i].length; j++) { - animations[i][j] = image.getSubimage(j*64, i*40, 64, 40); - } - } - } - - private void importImage(){ - String filePath = "/Resources/player_sprites.png"; - InputStream is = getClass().getResourceAsStream(filePath); - try { - image = ImageIO.read(is); - } catch (Exception e) { - e.printStackTrace(); - } finally{ - try { - is.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - // setting panel dimension private void setPanelSize(){ Dimension size = new Dimension(panelWidth,panelHeight); @@ -92,73 +35,14 @@ private void setPanelSize(){ this.setMaximumSize(size); } - 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; - }else{ - playerAction = Constants.PlayerConstants.IDLE; - } - } - - public void updatePos(){ - if(moving){ - switch (playerDir) { - case Constants.Directions.LEFT -> posX -= move; - case Constants.Directions.RIGHT -> posX += move; - case Constants.Directions.UP -> posY -= move; - case Constants.Directions.DOWN -> posY += move; - } - } - } - @Override public void paintComponent(Graphics g) { super.paintComponent(g); - g.drawImage(animations[playerAction][aniIndex], (int)posX, (int)posY, 64*3, 40*3, null); + game.render(g); } - private void updateAnimation(){ - aniTick++; - if(aniTick >= aniSpeed){ - aniTick = 0; - aniIndex++; - if(aniIndex >= Constants.PlayerConstants.GetSpriteAmount(playerAction)){ - aniIndex = 0; - } - } + public Game getGame(){ + return this.game; } - //chagning oval direction for bounce effect - // private void updateOval() { - - // posX += dirX; - // if (posX > (panelWidth - 100) || posX < 0){ - // dirX *= -1; - // color = getRandomColor(); - // } - - // posY += dirY; - // if (posY > (panelHeight - 100) || posY < 0){ - // dirY *= -1; - // color = getRandomColor(); - // } - // } - - // private Color getRandomColor(){ - // Random random = new Random(); - // int r = random.nextInt(256); - // int g = random.nextInt(256); - // int b = random.nextInt(256); - // return new Color(r,g,b); - // } - }