Skip to content

Commit

Permalink
created InputManager class to handle inputs and changed the inputlist…
Browse files Browse the repository at this point in the history
… to be

type KeyCode instead of string.
  • Loading branch information
ashish2199 committed Nov 24, 2017
1 parent 949587c commit d316c2f
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 35 deletions.
31 changes: 21 additions & 10 deletions src/bomberman/GameLoop.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
package bomberman;

import bomberman.constants.GlobalConstants;
import bomberman.entity.Entity;
import bomberman.gamecontroller.InputManager;
import bomberman.scenes.Sandbox;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.GraphicsContext;

public class GameLoop {


public class GameLoop
{
static double tickDuration;
final static long startNanoTime = System.nanoTime();
static double tickDuration;
final static long startNanoTime = System.nanoTime();

public static double getTickDuration() {
return tickDuration;
}

public static void start(GraphicsContext gc)
{
public static void start(GraphicsContext gc) {
GameState.gameStatus=GlobalConstants.GameStatus.Running;
new AnimationTimer() {
public void handle(long currentNanoTime) {
tickDuration = (currentNanoTime - startNanoTime) / 1000000000.0;
gc.clearRect(0, 0, 512, 512);
//TODO This will have to be something like, currentScene.getEntities()
for(Entity e : Sandbox.getEntities())
e.draw();
updateGame();
renderGame();
}
}.start();
}
}

public static void updateGame() {
InputManager.handlePlayerMovements();
}

public static void renderGame() {
for (Entity e : Sandbox.getEntities()) {
e.draw();
}
}

}
8 changes: 5 additions & 3 deletions src/bomberman/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
*/
package bomberman;

import bomberman.constants.GlobalConstants;
import java.util.Date;

/**
*
* @author ashish.padalkar
*/
public class GameState {
int level;
Date lastSaved;
boolean hasUnsavedChanges;
public static int level;
public static Date lastSaved;
public static boolean hasUnsavedChanges;
public static GlobalConstants.GameStatus gameStatus;
}
6 changes: 3 additions & 3 deletions src/bomberman/constants/GlobalConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class GlobalConstants {
public static String GAME_NAME = "BomberMan";
public static String GAME_VERSION = " v 0.1";
public static Color BACKGROUND_COLOR = Color.WHITE;
public static int PLAYER_X = 200;
public static int PLAYER_Y = 200;
public static int PLAYER_WIDTH = 18;
public static int PLAYER_HEIGHT = 21;
public static int PLAYER_SCALE = 2;

public static enum GameStatus{
Running,Paused,GameOver
}
}
12 changes: 5 additions & 7 deletions src/bomberman/entity/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,23 @@ public class Player implements MovingEntity, KillableEntity {
String name;

public Player() {
init();
init(0,0);
}

public Player(int posX, int posY) {
this();
init(posX, posY);
health = 100;
positionX = posX;
positionY = posY;
isAlive = true;
}

private void init() {
private void init(int x,int y) {
name = "Player";
playerBoundary = new RectBoundedBox(positionX, positionY, GlobalConstants.PLAYER_WIDTH, GlobalConstants.PLAYER_HEIGHT);

playerAnimations = new PlayerAnimations(this);

positionX = GlobalConstants.PLAYER_X;
positionY = GlobalConstants.PLAYER_Y;
positionX = x;
positionY = y;

currentSprite = playerAnimations.getPlayerIdleSprite();
}
Expand Down
27 changes: 16 additions & 11 deletions src/bomberman/gamecontroller/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package bomberman.gamecontroller;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
Expand All @@ -18,7 +19,7 @@
public class EventHandler {
public static char lastKeyPress;
public static char lastKeyReleased;
public static ArrayList<String> input = new ArrayList<String>();
public static ArrayList<KeyCode> inputList = new ArrayList<KeyCode>();

public static void attachEventHandlers(Scene s){
keyReleaseHanlder krh = new keyReleaseHanlder();
Expand All @@ -28,35 +29,39 @@ public static void attachEventHandlers(Scene s){
}

public boolean isKeyDown(KeyCode k) {
if( input.contains(k.toString())){
if( inputList.contains(k)){
return true;
}else{
return false;
}
}

public static List getInputList(){
return inputList;
}
}

class keyReleaseHanlder implements javafx.event.EventHandler<KeyEvent>{
public keyReleaseHanlder() {
}
@Override
public void handle(KeyEvent evt) {
System.out.println("The key released is : "+evt.getText()+" with keycode "+evt.getCode().getName());
//System.out.println("The key released is : "+evt.getText()+" with keycode "+evt.getCode().getName());

String code = evt.getCode().toString();
KeyCode code = evt.getCode();

if ( EventHandler.input.contains(code) )
EventHandler.input.remove( code );
if ( EventHandler.inputList.contains(code) )
EventHandler.inputList.remove( code );
}
}
class keyPressedHandler implements javafx.event.EventHandler<KeyEvent>{
@Override
public void handle(KeyEvent evt) {
System.out.println("The key pressed is : "+evt.getText()+" with keycode "+evt.getCode().getName());
String code = evt.getCode().toString();
//https://gamedevelopment.tutsplus.com/tutorials/introduction-to-javafx-for-game-development--cms-23835
//System.out.println("The key pressed is : "+evt.getText()+" with keycode "+evt.getCode().getName());
KeyCode code = evt.getCode();

// only add once... prevent duplicates
if ( !EventHandler.input.contains(code) )
EventHandler.input.add( code );
if ( !EventHandler.inputList.contains(code) )
EventHandler.inputList.add( code );
}
}
2 changes: 1 addition & 1 deletion src/bomberman/gamecontroller/GameVariables.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
* @author ashish.padalkar
*/
public class GameVariables {

}
49 changes: 49 additions & 0 deletions src/bomberman/gamecontroller/InputManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bomberman.gamecontroller;

import bomberman.constants.Direction;
import bomberman.entity.player.Player;
import bomberman.scenes.Sandbox;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.input.KeyCode;
/**
*
* @author Ashish
*/
public class InputManager {

public static void handlePlayerMovements(){
List keyboardInputs = EventHandler.getInputList();
Player player = Sandbox.getPlayer();
if(keyboardInputs.contains(KeyCode.UP) || keyboardInputs.contains(KeyCode.W)){
player.move(5,Direction.UP);
}
if(keyboardInputs.contains(KeyCode.DOWN) || keyboardInputs.contains(KeyCode.S)){
player.move(5,Direction.DOWN);
}
if(keyboardInputs.contains(KeyCode.LEFT) || keyboardInputs.contains(KeyCode.A)){
player.move(5,Direction.LEFT);
}
if(keyboardInputs.contains(KeyCode.RIGHT) || keyboardInputs.contains(KeyCode.D)){
player.move(5,Direction.RIGHT);
}
if( !keyboardInputs.contains(KeyCode.LEFT) &&
!keyboardInputs.contains(KeyCode.RIGHT) &&
!keyboardInputs.contains(KeyCode.UP) &&
!keyboardInputs.contains(KeyCode.DOWN) &&
!keyboardInputs.contains(KeyCode.W) &&
!keyboardInputs.contains(KeyCode.A) &&
!keyboardInputs.contains(KeyCode.S) &&
!keyboardInputs.contains(KeyCode.D)
)
{
player.move(0, Direction.DOWN);
}
}

}

0 comments on commit d316c2f

Please sign in to comment.