-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameController.java
83 lines (72 loc) · 1.87 KB
/
GameController.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Name: GameController
* Beschreibung: Explaination
*
*
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
public class GameController implements KeyListener, ActionListener {
private List<GameModel> models;
/**
* constrcutor of the GameController;
* creates a new ArrayList which later holds all models
* */
public GameController() {
models = new ArrayList<>();
}
/**
* adds a view to the Controller and
* adds the KeyListeners implemented in the GameModel to the
* view of the game.
* @param view
* the view to which the KeyListeners are getting added to
* */
public void addView(GameView view) {
view.addKeyListener(this);
}
/**
* adds a GameModel to the the GameController's array
* of models
* @param model
* the model to add
* */
public void addModel(GameModel model) {
models.add(model);
}
/**
* the implemented KeyListener events;
* these all need to be added because of the implements keyword
* in the class declaration (simpler: because the KeyListeners need
* to be implemented).
* this is called for each GameModel and returns a keyCode
* which is later used in the GameModel's class which has implemented
* methods that react and do different actions for each key tap.
* @param e
* the KeyEvent which is used to extract the keyCode value
* */
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
for (GameModel gameModel : models) {
gameModel.keyPressed(keyCode);
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
for (GameModel gameModel : models) {
gameModel.keyReleased(keyCode);
}
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}