Skip to content

Commit

Permalink
Making Different Player class for Player
Browse files Browse the repository at this point in the history
  • Loading branch information
IamRishavDas committed Feb 20, 2024
1 parent 811a5d8 commit 46e4106
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 131 deletions.
10 changes: 5 additions & 5 deletions Controller/KeyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ 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);
}
}

@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.setMoving(false);
case KeyEvent.VK_RIGHT, KeyEvent.VK_LEFT, KeyEvent.VK_UP, KeyEvent.VK_DOWN -> gamePanel.getGame().getPlayer().setMoving(false);
}
}
}
10 changes: 10 additions & 0 deletions Entities/Entity.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
99 changes: 99 additions & 0 deletions Entities/Player.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
23 changes: 21 additions & 2 deletions NewGame/Game.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package NewGame;

import java.awt.Graphics;

import Entities.Player;

@SuppressWarnings("unused")
public class Game implements Runnable {
private GameFrame gameFrame;
Expand All @@ -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)
Expand Down Expand Up @@ -72,4 +87,8 @@ public void run() {
}
}
}

public Player getPlayer(){
return player;
}
}
132 changes: 8 additions & 124 deletions NewGame/GamePanel.java
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
// }

}

0 comments on commit 46e4106

Please sign in to comment.