aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/viewer/InputHandler.java
blob: 9d5aa9faf9dc551ab4f9502854c0a9067cf081bf (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package fr.umlv.java.wallj.viewer;

import fr.umlv.java.wallj.board.TileVec2;
import fr.umlv.java.wallj.event.*;
import fr.umlv.zen5.ApplicationContext;
import fr.umlv.zen5.Event;
import fr.umlv.zen5.KeyboardKey;
import org.jbox2d.common.Vec2;

import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;


/**
 * Treats the inputs from the keyboard and mouse provided by Zen 5 and creates Events meaningful for the game.
 *
 * @author Adam NAILI
 */
public final class InputHandler {
  private final ApplicationContext applicationContext;

  /**
   * @param applicationContext the Zen5 application context
   */
  public InputHandler(ApplicationContext applicationContext) {
    this.applicationContext = Objects.requireNonNull(applicationContext);
  }

  /**
   * @return the list of events converted from Zen 5 events to game events
   */
  public List<fr.umlv.java.wallj.event.Event> getEvents() {
    LinkedList<fr.umlv.java.wallj.event.Event> events = new LinkedList<>();
    fr.umlv.zen5.Event event = applicationContext.pollEvent();
    if (event != null) {
      Event.Action action = event.getAction();
      Point2D.Float location = event.getLocation();
      if (location != null) { //Mouse Handling
        if (action == Event.Action.POINTER_DOWN) {
          Vec2 mouseLocation = new Vec2(location.x, location.y);
          TileVec2 mouseTileLocation = TileVec2.of(mouseLocation);
          events.add(new MoveRobotOrder(mouseTileLocation));
        }
      }
      KeyboardKey keyboardKey = event.getKey();
      if (keyboardKey != null) { //Keyboard Handling
        if (action == Event.Action.KEY_PRESSED) {
          switch (keyboardKey) {
            case SPACE:
              events.add(new BombSetupOrder());
              break;
            case R:
              events.add(new ConfirmOrder());
              break;
            case Q:
              events.add(new QuitGameOrder());
              break;
            case S:
              events.add(new SimulationStartOrder());
              break;
          }
        }
      }
    }
    return events;
  }
}