Coverage Summary for Class: GameInputHandler (com.mozarellabytes.kroy.Utilities)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
GameInputHandler | 0% (0/ 1) | 0% (0/ 15) | 0% (0/ 101) |
1 package com.mozarellabytes.kroy.Utilities;
2
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input;
5 import com.badlogic.gdx.InputProcessor;
6 import com.badlogic.gdx.math.Vector2;
7 import com.badlogic.gdx.math.Vector3;
8 import com.mozarellabytes.kroy.Entities.FireTruck;
9 import com.mozarellabytes.kroy.Entities.Fortress;
10 import com.mozarellabytes.kroy.Screens.GameScreen;
11
12 public class GameInputHandler implements InputProcessor {
13
14 /** The game screen that this input handler controls */
15 private final GameScreen gameScreen;
16
17 /** The graphical user interface - contains the buttons */
18 private final GUI gui;
19
20 /** Constructs the GameInputHandler
21 *
22 * @param gameScreen The game screen that this input handler controls
23 * @param gui The graphical user interface - contains the buttons
24 */
25 public GameInputHandler(GameScreen gameScreen, GUI gui) {
26 this.gameScreen = gameScreen;
27 this.gui = gui;
28 }
29
30 /** Called when a key was pressed
31 *
32 * This handles toggling sound, the control screen, the pause
33 * screen and makes the fire trucks attack a fortress that is
34 * within it's range
35 *
36 * @param keycode one of the constants in {@link Input.Keys}
37 * @return whether the input was processed */
38 @Override
39 public boolean keyDown(int keycode) {
40 switch (keycode) {
41 case Input.Keys.ESCAPE:
42 Gdx.app.exit();
43 System.exit(1);
44 break;
45 case Input.Keys.A:
46 System.out.println(gameScreen.gameState.getTrucksInAttackRange());
47 if (SoundFX.music_enabled && gameScreen.gameState.getTrucksInAttackRange() > 0) {
48 SoundFX.playTruckAttack();
49 }
50 for (FireTruck truck: gameScreen.getStation().getTrucks()){
51 truck.setAttacking(true);
52 }
53 break;
54 case Input.Keys.C:
55 gameScreen.toControlScreen();
56 break;
57 case Input.Keys.S:
58 gui.clickedSoundButton();
59 gui.changeSound();
60 gui.idleSoundButton();
61 break;
62 case Input.Keys.P:
63 gui.clickedPauseButton();
64 gameScreen.changeState();
65 }
66 return true;
67 }
68
69 @Override
70 public boolean keyUp(int keycode) {
71 if (this.gameScreen.getState().equals(GameScreen.PlayState.PLAY)) {
72 if (keycode == Input.Keys.A) {
73 SoundFX.stopTruckAttack();
74 for (FireTruck truck : gameScreen.getStation().getTrucks()) {
75 truck.setAttacking(false);
76 }
77 }
78 }
79 return true;
80 }
81
82 @Override
83 public boolean keyTyped(char character) {
84 return false;
85 }
86
87 /** Checks whether the user clicks on a firetruck, fortress, button or the end
88 * of a firetrucks path
89 * @param screenX The x coordinate, origin is in the upper left corner
90 * @param screenY The y coordinate, origin is in the upper left corner
91 * @param pointer the pointer for the event.
92 * @param button the button
93 * @return whether the input was processed */
94 @Override
95 public boolean touchDown(int screenX, int screenY, int pointer, int button) {
96 Vector2 clickCoordinates = generateClickCoordinates(screenX, screenY);
97 if (this.gameScreen.getState().equals(GameScreen.PlayState.PLAY)) {
98 if (gameScreen.isRoad((int) clickCoordinates.x, (int) clickCoordinates.y)) {
99 if (gameScreen.checkClick(clickCoordinates)) {
100 gameScreen.selectedTruck.resetPath();
101 gameScreen.selectedTruck.addTileToPath(clickCoordinates);
102 } else if (!gameScreen.checkTrailClick(clickCoordinates) && !checkFortressClick(clickCoordinates)) {
103 gameScreen.selectedTruck = null;
104 gameScreen.selectedEntity = null;
105 }
106 } else {
107 checkFortressClick(clickCoordinates);
108 }
109 }
110 checkButtonClick(new Vector2(screenX, Gdx.graphics.getHeight() - screenY));
111 return true;
112 }
113
114 /** The user draws a path for the fire truck, if the path is valid the coordinate
115 * positions are added to the trucks path
116 * @param pointer the pointer for the event.
117 * @return whether the input was processed */
118 @Override
119 public boolean touchDragged(int screenX, int screenY, int pointer) {
120 if (this.gameScreen.getState().equals(GameScreen.PlayState.PLAY)) {
121 if (gameScreen.selectedTruck != null) {
122 Vector2 clickCoordinates = generateClickCoordinates(screenX, screenY);
123 gameScreen.selectedTruck.addTileToPath(clickCoordinates);
124 }
125 }
126 return true;
127 }
128
129 /** Check if a user clicks up on a button or if the user draws multiple
130 * trucks to end on the same tile
131 *
132 * @param pointer the pointer for the event.
133 * @param button the button
134 * @return whether the input was processed */
135 @Override
136 public boolean touchUp(int screenX, int screenY, int pointer, int button) {
137 if (this.gameScreen.getState().equals(GameScreen.PlayState.PLAY)) {
138 if (gameScreen.selectedTruck != null) {
139 if (!gameScreen.selectedTruck.trailPath.isEmpty()) {
140 if (doTrucksHaveSameLastTile()){
141 giveTrucksDifferentLastTiles(gameScreen.selectedTruck);
142 }
143 }
144 gameScreen.selectedTruck.setMoving(true);
145 }
146 }
147 checkButtonUnclick(screenX, screenY);
148 return true;
149 }
150
151
152 @Override
153 public boolean mouseMoved(int screenX, int screenY) {
154 return false;
155 }
156
157 @Override
158 public boolean scrolled(int amount) {
159 return false;
160 }
161
162 /** Checks if the user has drawn more than one truck to the same end tile.
163 *
164 * @return <code> true </code> If more than one truck has the same end tile
165 * * <code> false </code> Otherwise
166 */
167 private boolean doTrucksHaveSameLastTile() {
168 for (FireTruck truck : gameScreen.getStation().getTrucks()) {
169 if (!truck.equals(gameScreen.selectedTruck)) {
170 if (!truck.getPath().isEmpty() && !truck.getTrailPath().isEmpty()){
171 if (truck.trailPath.last().equals(gameScreen.selectedTruck.trailPath.last())){
172 return true;
173 }
174 } else if (truck.getPosition().equals(gameScreen.selectedTruck.trailPath.last())) {
175 return true;
176 }
177 }
178 }
179 return false;
180 }
181
182 /** The method for giving trucks that have the same end tiles adjacent end tiles
183 * so that they do not end up on the same tile
184 * @param selectedTruck the truck that has to be moved so the two trucks end up
185 * on different tiles
186 */
187 private void giveTrucksDifferentLastTiles(FireTruck selectedTruck){
188 selectedTruck.trailPath.removeLast();
189 while (!selectedTruck.trailPath.isEmpty() && !selectedTruck.trailPath.last().equals(selectedTruck.path.last())) {
190 selectedTruck.path.removeLast();
191 }
192 }
193
194 /** Maps the position of where the user clicked on the screen to the tile that they clicked on
195 *
196 * @param screenX The x coordinate, origin is in the upper left corner
197 * @param screenY The y coordinate, origin is in the upper left corner
198 * @return a Vector2 containing the tile that the user clicked on
199 */
200 private Vector2 generateClickCoordinates(int screenX, int screenY){
201 Vector2 clickCoordinates = new Vector2(screenX, screenY);
202 Vector3 position = gameScreen.getCamera().unproject(new Vector3(clickCoordinates.x, clickCoordinates.y, 0));
203 return new Vector2((int) position.x, (int) position.y);
204 }
205
206
207 /** Checks if the user clicked on the home, pause or sound button
208 * and changes the sprite accordingly
209 * @param position2d The tile that was clicked
210 */
211 private void checkButtonClick(Vector2 position2d){
212 if (gui.getHomeButton().contains(position2d)) {
213 gui.clickedHomeButton();
214 } else if (gui.getPauseButton().contains(position2d)){
215 gui.clickedPauseButton();
216 } else if (gui.getSoundButton().contains(position2d)) {
217 gui.clickedSoundButton();
218 } else if (gui.getInfoButton().contains(position2d)) {
219 gui.clickedInfoButton();
220 }
221 }
222
223 /** Checks if user clicked on a fortress, if it did this fortress
224 * becomes the selected entity meaning its stats will be rendered
225 * in the top right hand corner
226 * @param position2d the tile that was clicked
227 * @return <code> true </code> If a fortress has been clicked on
228 * <code> false </code> Otherwise
229 */
230 private boolean checkFortressClick(Vector2 position2d) {
231 for (Fortress fortress : gameScreen.getFortresses()) {
232 if (fortress.getArea().contains(position2d)) {
233 gameScreen.selectedEntity = fortress;
234 return true;
235 }
236 }
237 gameScreen.selectedTruck = null;
238 gameScreen.selectedEntity = null;
239 return false;
240 }
241
242 /** Checks if the user has lifted the mouse over a button and triggers the
243 * appropriate action
244 * @param screenX The x coordinate, origin is in the upper left corner
245 * @param screenY The y coordinate, origin is in the upper left corner
246 */
247 private void checkButtonUnclick(int screenX, int screenY){
248 Vector2 screenCoords = new Vector2(screenX, Gdx.graphics.getHeight() - screenY);
249
250 if (gui.getHomeButton().contains(screenCoords)) {
251 gameScreen.toHomeScreen();
252 } else {
253 gui.idleHomeButton();
254 }
255
256 if (gui.getSoundButton().contains(screenCoords)){
257 gui.changeSound();
258 } else {
259 gui.idleSoundButton();
260 }
261
262 if (gui.getPauseButton().contains(screenCoords)){
263 gameScreen.changeState();
264 } else {
265 gui.idlePauseButton();
266 }
267
268 if (gui.getInfoButton().contains(screenCoords)){
269 gameScreen.toControlScreen();
270 }
271 }
272 }