aboutsummaryrefslogtreecommitdiff
path: root/src/gameplay/SongPlayer.java
blob: d80d40181e6e66bdcc4bf24e0dfdb6119eb40d65 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package gameplay;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

import javafx.geometry.Pos;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.animation.*;
import javafx.util.*;
import main.Difficulty;
import main.ScoreController;
import sound.AudioFilePlayer;



//hi aidan here are some objects you can use
// cntrl.setScore(0) - pass in int
// cntrl.getScore() - returns int
// d.bpm - int, defined in difficulty metadata
// lvl.colors - array of colors (size 5) for the block colors
// d.notes - File, notes.txt in the difficulty folder


public class SongPlayer extends Pane {
	private int bpm;
	Timer timer;
	final int TIME = 1500; // delay for notes falling down the screen

	main.ScoreController scoreCounter = new ScoreController();

	Rectangle goalPerfect = new Rectangle();
	HBox buttonBox = new HBox();
	VBox polish = new VBox();
	VBox place = new VBox();

	TButton dButton = new TButton(Color.RED, 50, 50, 5);
	Queue<NoteInfo> dSends = new LinkedList<NoteInfo>(); // Queue that dictates when to send the notes
	ArrayList<Block> dLane = new ArrayList<Block>(); // Array list containing all the notes currently on the field

	TButton fButton = new TButton(Color.BLUE, 50, 50, 5);
	Queue<NoteInfo> fSends = new LinkedList<NoteInfo>();
	ArrayList<Block> fLane = new ArrayList<Block>();

	TButton sButton = new TButton(Color.GREEN, 50, 50, 5);
	Queue<NoteInfo> spaceSends = new LinkedList<NoteInfo>();
	ArrayList<Block> spaceLane = new ArrayList<Block>();

	TButton jButton = new TButton(Color.PURPLE, 50, 50, 5);
	Queue<NoteInfo> jSends = new LinkedList<NoteInfo>();
	ArrayList<Block> jLane = new ArrayList<Block>();

	TButton kButton = new TButton(Color.YELLOW, 50, 50, 5);
	Queue<NoteInfo> kSends = new LinkedList<NoteInfo>();
	ArrayList<Block> kLane = new ArrayList<Block>();

	/**
	 * Establishes what the chart for the song is going to look like
	 * @throws FileNotFoundException
	 */
	public void loadSong(File notes) throws FileNotFoundException {
		Scanner scan = new Scanner(new File(notes.getPath()));
		try{
			while (scan.hasNext()) {
				String input = scan.next();
				if (input.charAt(0) == 'd') {
					dSends.add(new NoteInfo(Double.parseDouble(input.substring(1))));
				}
				else if (input.charAt(0) == 'f') {
					fSends.add(new NoteInfo(Double.parseDouble(input.substring(1))));
				}
				else if (input.charAt(0) == 's') {
					spaceSends.add(new NoteInfo(Double.parseDouble(input.substring(1))));
				}
				else if (input.charAt(0) == 'j') {
					jSends.add(new NoteInfo(Double.parseDouble(input.substring(1))));
				}
				else if (input.charAt(0) == 'k') {
					kSends.add(new NoteInfo(Double.parseDouble(input.substring(1))));
				}
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}

	public SongPlayer(main.Level lvl, Difficulty d, Pane p, ScoreController cntrl) {
		bpm = d.bpm;
		timer = new Timer(60);
		scoreCounter = cntrl;

		try {
			loadSong(d.notes);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		Rectangle field = new Rectangle(50, 50, new Color(0, 0, 0, 0.7));
		field.heightProperty().bind(super.heightProperty());
		field.widthProperty().bind(super.widthProperty());

		goalPerfect.heightProperty().bind(super.heightProperty().divide(32));
		goalPerfect.heightProperty().bind(super.widthProperty());

		dButton.setColor(lvl.colors[0]);
		fButton.setColor(lvl.colors[1]);
		sButton.setColor(lvl.colors[2]);
		jButton.setColor(lvl.colors[3]);
		kButton.setColor(lvl.colors[4]);
		genButton(dButton);
		genButton(fButton);
		genButton(sButton);
		genButton(jButton);
		genButton(kButton);
		
		gui.Driver.primaryStage.getScene().setOnKeyPressed(e -> {
			if (e.getCode() == KeyCode.D) {
				checkNote(dLane, dButton);
			}
			if (e.getCode() == KeyCode.F) {
				checkNote(fLane, fButton);
			}
			if (e.getCode() == KeyCode.SPACE) {
				checkNote(spaceLane, sButton);
			}
			if (e.getCode() == KeyCode.J) {
				checkNote(jLane, jButton);
			}
			if (e.getCode() == KeyCode.K) {
				checkNote(kLane, kButton);
			}
			System.out.println("Score: " + scoreCounter.getScore() + "\nCombo: " + scoreCounter.getCombo() + "\n");
		});

		// buttonBox.setStyle("-fx-padding: 0;" + "-fx-border-style: solid inside;"
		// 		+ "-fx-border-width: 0;" + "-fx-border-insets: 20;"
		// 		+ "-fx-background-color: black;" + "-fx-opacity: 0.67;");
		buttonBox.setAlignment(Pos.CENTER);
		buttonBox.getChildren().addAll(dButton, fButton, sButton, jButton, kButton);
		buttonBox.setSpacing(10);

		polish.getChildren().addAll(field);
		polish.setAlignment(Pos.BASELINE_CENTER);

		place.prefWidthProperty().bind(super.widthProperty());
		place.prefHeightProperty().bind(super.heightProperty());
		place.getChildren().addAll(buttonBox);
		place.setAlignment(Pos.BOTTOM_CENTER);
		place.setSpacing(10);

		StackPane root = new StackPane();
		root.getChildren().addAll(place);

		goalPerfect.setY(dButton.getY());
		super.getChildren().addAll(root, goalPerfect);

		sound.AudioFilePlayer music = new AudioFilePlayer("src/assets/BookBetrayal.wav");
		music.play();
		gameLoop.start();
	}

	/**
	 * Checks if a note should be sent at the current time, and sends the note if it
	 * needs to be
	 * 
	 * @param sends the queue to check
	 * @param lane  the lane to send the note to
	 * @param pos   the x pos of the note to be sent
	 * @param c     the color of the sent note
	 */
	public void sendNote(Queue<NoteInfo> sends, ArrayList<Block> lane, TButton button) {
		if (sends.peek() != null && timer.time() > sends.peek().getTime()-(TIME*bpm/60000)) {
			TranslateTransition anim = new TranslateTransition(Duration.millis(TIME));

			lane.add(new Block(button.getColor(), 50, 50, 5));
			int index = lane.size() - 1;
			sends.remove();
			lane.get(index).heightProperty().bind(super.widthProperty().divide(8));
			lane.get(index).widthProperty().bind(super.widthProperty().divide(8));
			lane.get(index).arcHeightProperty().bind(super.widthProperty().divide(25));
			lane.get(index).arcWidthProperty().bind(super.widthProperty().divide(25));
			lane.get(index).setX(button.getLayoutX());
			lane.get(index).setY(-lane.get(index).getHeight());
			anim.setByY(super.getHeight() + lane.get(index).getHeight());
			anim.setCycleCount(1);
			anim.setAutoReverse(false);
			anim.setNode(lane.get(lane.size() - 1));
			anim.play();

			anim.setOnFinished(e -> {
				if (super.getChildren().removeAll(anim.getNode())){
					scoreCounter.miss();
					FillTransition ft = new FillTransition(Duration.millis(500), button);
					ft.setFromValue(Color.RED);
					ft.setToValue(button.getFillColor());
					ft.play();
				}
			});
			super.getChildren().add(lane.get(lane.size() - 1));
		}
	}

	/**
	 * Sets up the given button
	 * 
	 * @param button
	 */
	private void genButton(TButton button) {
		button.heightProperty().bind(super.widthProperty().divide(8));
		button.widthProperty().bind(super.widthProperty().divide(8));
		button.arcHeightProperty().bind(super.widthProperty().divide(25));
		button.arcWidthProperty().bind(super.widthProperty().divide(25));
		button.setStrokeWidth(3);
	}

	/**
	 * The background test that is run on every frame of the game
	 */
	AnimationTimer gameLoop = new AnimationTimer() {

		@Override
		public void handle(long arg0) {
			sendNote(dSends, dLane, dButton);
			sendNote(fSends, fLane, fButton);
			sendNote(spaceSends, spaceLane, sButton);
			sendNote(jSends, jLane, jButton);
			sendNote(kSends, kLane, kButton);
		}
	};

	/**
	 * returns the pos in the lane array of the closest note to the goal
	 * 
	 * @param searchLane
	 * @return the position of the note
	 */
	private int getClosestNote(ArrayList<Block> searchLane) {
		int pos = 0;

		for (int i = 0; i < searchLane.size(); i++) {
			if (distanceToGoal(searchLane.get(i)) < distanceToGoal(searchLane.get(pos))) {
				pos = i;
			}
		}
		return pos;
	}

	/**
	 * Returns the distance to the goal of the given note
	 * 
	 * @param note
	 * @return
	 */
	private double distanceToGoal(Block note) {
		return Math.abs((super.getHeight() - note.getTranslateY()) - dButton.getY());
	}

	/**
	 * When the player hits the key, checks the quality of the hit
	 * @param lane the lane checking for a hit
	 * @param button the button checking for a hit
	 * @return 2 for a perfect hit, 1 for a good hit, 0 for a miss, and -1 if there are no notes to hit
	 */
	private int checkNote(ArrayList<Block> lane, TButton button) {
		double distance = distanceToGoal(lane.get(getClosestNote(lane)));
		if (lane.size() > 0 && distance < super.getHeight() / 3) {

			FillTransition ft = new FillTransition(Duration.millis(500), button);
			ft.setToValue(button.getFillColor());

			super.getChildren().removeAll(lane.get(getClosestNote(lane)));
			lane.remove(lane.get(getClosestNote(lane)));
			if (distance < super.getHeight() / 16) {
				ft.setFromValue(Color.WHITE);
				ft.play();
				scoreCounter.perfect();
				return 2;
			}
			if (distance < super.getHeight() / 5) {
				ft.setFromValue(Color.CYAN);
				ft.play();
				scoreCounter.good();
				return 1;
			}
			ft.setFromValue(Color.RED);
			ft.play();
			scoreCounter.miss();
			return 0;
		}
		return -1;
	}

}