aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl/xblast/client/KeyboardEventHandler.java
blob: bebc4a521b3f9873e940ed69f7a7ebcef1122d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package ch.epfl.xblast.client;

import ch.epfl.xblast.PlayerAction;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author Timothée FLOURE (257420)
 */
public class KeyboardEventHandler extends KeyAdapter{
    private final Map<Integer,PlayerAction> playerActionMap;
    private final Consumer consumer;

    /**
     * Instantiates a new KeyboardEventHandler.
     *
     * @param playerActionMap the map of key codes to PlayerAction.
     * @param consumer consumer related to the EventHandler
     */
    public KeyboardEventHandler(Map<Integer,PlayerAction> playerActionMap, Consumer<PlayerAction> consumer) {
        this.playerActionMap = playerActionMap;
        this.consumer = consumer;
    }

    /**
     * Executes the command related to the given keycode.
     *
     * @param e event (pressed key)
     */
    @Override
    public void keyTyped(KeyEvent e) {
        if (playerActionMap.containsKey(e.getKeyCode()))
            consumer.accept(playerActionMap.get(e.getKeyCode()));
    }
}