-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloController.java
190 lines (169 loc) · 6.27 KB
/
HelloController.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
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
package com.example.gamepbo;
import javafx.animation.AnimationTimer;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class HelloController {
@FXML public Button buttonMulai;
@FXML public Button buttonSkor;
@FXML public TextField boxNama;
@FXML public ToggleGroup kesulitan;
@FXML public AnchorPane boxCanvas;
@FXML public Canvas canvasGame;
@FXML public Label labelSkor;
@FXML public Label labelLevel;
@FXML public AnchorPane boxKonfirmasi;
@FXML public Label labelKonfirmasi;
@FXML public Button buttonLanjut;
@FXML public Button buttonKeluar;
@FXML public Button buttonHapus;
@FXML public Label labelWaktu;
@FXML public StackPane boxTabel;
@FXML public Button buttonOkeSkor;
@FXML public AnchorPane boxSkor;
private GraphicsContext gc;
private ArrayList<Sprite> kumpulanMusuh;
private String kesulitanString;
private int skor;
private int level;
private DBConnector db;
static class LongValue
{
public long value;
public LongValue(long i)
{
value = i;
}
}
static class seconds extends TimerTask {
private int seconds;
public seconds(int seconds) {
this.seconds = seconds;
}
public int getSeconds() {
return seconds;
}
@Override
public void run() { seconds--; }
}
public HelloController(){
kumpulanMusuh = new ArrayList<>();
db = new DBConnector();
kesulitanString = "Mudah";
}
public void buttonMulaiClicked(ActionEvent e) {
RadioButton rb = (RadioButton) kesulitan.getSelectedToggle();
kesulitanString = rb.getText();
boxCanvas.setVisible(true);
boxCanvas.setDisable(false);
level = 1;
skor = 0;
mulaiLevel(kesulitanString);
}
public void mulaiLevel(String k){
if (kumpulanMusuh.size() != 0) kumpulanMusuh.removeAll(kumpulanMusuh);
labelLevel.setText("Level " + level);
gc = canvasGame.getGraphicsContext2D();
Image musuhImg = new Image(String.valueOf(getClass().getResource("musuh.png")), 35, 35, true, true);
for(int i = 0; i < level; i++){
Sprite musuh = new Sprite();
musuh.setImage(musuhImg);
musuh.setVelocity(0,0);
switch (k){
case "Sedang" -> musuh.addVelocity(Math.random() * 100 - 50,Math.random() * 100 - 50);
case "Sulit" -> musuh.addVelocity(Math.random() * 200 - 100,Math.random() * 200 - 100);
}
kumpulanMusuh.add(musuh);
}
gc.drawImage(musuhImg, 0,0);
LongValue lastNanoTime = new LongValue(System.nanoTime());
Timer timer = new Timer();
seconds sec = new seconds(5+level*2);
timer.scheduleAtFixedRate(sec, 0, 1000);
new AnimationTimer () {
@Override
public void handle(long currentNanoTime) {
// calculate time since last update.
double elapsedTime = (currentNanoTime - lastNanoTime.value) / 1000000000.0;
lastNanoTime.value = currentNanoTime;
labelWaktu.setText(String.format("%02d:%02d", sec.getSeconds() / 60, sec.getSeconds() % 60));
gc.clearRect(0,0, canvasGame.getWidth(), canvasGame.getHeight());
for(Sprite musuh : kumpulanMusuh){
musuh.update(elapsedTime);
musuh.render(gc);
}
// menang
if(kumpulanMusuh.isEmpty()){
boxKonfirmasi.setDisable(false);
boxKonfirmasi.setVisible(true);
buttonLanjut.setDisable(false);
labelKonfirmasi.setText("Kamu Menang!");
timer.cancel();
stop();
}
// kalah
if (sec.getSeconds() <= 0) {
boxKonfirmasi.setDisable(false);
boxKonfirmasi.setVisible(true);
buttonLanjut.setDisable(true);
labelKonfirmasi.setText("Waktu Habis!");
timer.cancel();
stop();
}
}
}.start();
}
public void canvasOnClick(MouseEvent e){
Point2D mousePoint = new Point2D(e.getX(), e.getY());
for(Sprite musuh : kumpulanMusuh){
if(musuh.getBoundary().contains(mousePoint)){
kumpulanMusuh.remove(musuh);
skor++;
break;
}
}
labelSkor.setText(String.valueOf(skor));
}
public void buttonLanjutClicked(ActionEvent e){
boxKonfirmasi.setVisible(false);
boxKonfirmasi.setDisable(true);
level++;
mulaiLevel(kesulitanString);
}
public void buttonKeluarClicked(ActionEvent e){
boxKonfirmasi.setVisible(false);
boxKonfirmasi.setDisable(true);
boxCanvas.setVisible(false);
boxCanvas.setDisable(true);
kumpulanMusuh.removeAll(kumpulanMusuh);
db.insert("INSERT INTO data VALUES ('"+boxNama.getText()+"', '"+kesulitanString+"', '"+skor+"', '"+level+"')");
}
public void buttonSkorClicked(ActionEvent e){
RadioButton rb = (RadioButton) kesulitan.getSelectedToggle();
kesulitanString = rb.getText();
db.start("SELECT * FROM data WHERE kesulitan='"+kesulitanString+"' ORDER BY skor DESC LIMIT 10");
boxTabel.getChildren().remove(boxTabel.getChildren().size()-1);
boxTabel.getChildren().add(db.getTable());
boxSkor.setDisable(false);
boxSkor.setVisible(true);
}
public void buttonOkeSkorClicked(ActionEvent e){
boxSkor.setVisible(false);
boxSkor.setDisable(true);
}
public void buttonHapusClicked(ActionEvent e){
db.insert("TRUNCATE TABLE data");
boxSkor.setVisible(false);
boxSkor.setDisable(true);
}
}