ashish2199 / Aidos

A beginner friendly project with aim of creating our own version of Bomberman.
GNU General Public License v3.0
41 stars 27 forks source link

EventHandler #37

Closed ItsStolas closed 6 years ago

ItsStolas commented 6 years ago

I've been thinking over EventHandler for input.

If the make it more generic like my proposed PR, we run into the problem of implementing KeyPressed and KeyReleased (KeyIsPressed is fine, though).

Maybe we could, inside @ashish2199 suggest update() for the whole game, put for 1 update loop which keys were pressed and which were released, then wipe them in the next loop. Each entity can then watch those lists to events that need KeyPressed (like placing a bomb).

Eg:

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());

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

        if ( EventHandler.input.contains(code) ) {
            EventHandler.input.remove( code );
                EventHandler.RELEASED_KEYS.add( 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
        // only add once... prevent duplicates
        if ( !EventHandler.input.contains(code) ) {
            EventHandler.input.add( code );
                EventHandler.PRESSED_KEYS.add( code);
        }

    }

and at the END of hypothetical update() loop (after all entities have run their update()'s, just

EventHandler.PRESSED_KEYS.clear()
EventHandler.RELEASED_KEYS.clear()

Thoughts?

ItsStolas commented 6 years ago

A better idea would be some way to notify the entities that a button has been pressed maybe. Can entities subscribe to events from the scene?

ashish2199 commented 6 years ago

I would like to know how this will make the movement smoother ? What other benefits do you think this will provide ?

Will it not be save as current implementation if we keep adding and clearing the inputs list on every update of gameloop ( animationtimer ).

ItsStolas commented 6 years ago

Well currently there’s a delay when you try to move your character, and if you press space while running your character will stop. I thought this way would handle those better.

StewartWallace1115 commented 6 years ago

From what I read online, it looks like the most common solution to the problem. Have you tested it yet? Instead of using an arraylist, you can use a set data structure to avoid checking for duplicates in the list. I believe the linkedhashset will work, since it maintains insertion order.

ItsStolas commented 6 years ago

Not yet I wanted to give someone else a chance to contribute and implement the update() to entities. Feel free to take a stab at it :)

ItsStolas commented 6 years ago

I’ve implemented the list, but it’s still in a PR. Just haven’t implemented the update to read from list, to be clear. Also the hashset is a great idea, it would also be a tiny bit faster haha.

ashish2199 commented 6 years ago

I have tried to implement the player movement using this new type of input handling in #39 I must say the animation is really very smooth now. Also please see if it looks good enough to be merged with master.

ItsStolas commented 6 years ago

Nice work :)