-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformcontroler.java
348 lines (284 loc) · 11.7 KB
/
Platformcontroler.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package application;
import application.main.Plyr;
import application.main.Stick;
import javafx.animation.AnimationTimer;
import javafx.fxml.FXML;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
abstract class Platform {
@FXML
protected static int width;
public Platform(int width) {
this.width = width;
// Set other properties of the platformShape as needed
}
static int getWidth() {
return width;// Get the width of the platform
}
public void setWidth(int width) {
this.width = width;
}
}
class EasyPlatform extends Platform {
public Stick stick;
protected int width = 50;
protected Rectangle easy_platform_rec;
protected static int distanceBetweenPlatforms = 100;
private static double lastPlatformPosition;
private final int platform_count = 3;
private boolean isCollided ;
private Plyr plyr;
@FXML
private static Pane easy_gamepane ; // This is linked to the Pane in your FXML file
public EasyPlatform(Pane easy_gamepane , Stick stick , Plyr plyr)
{// , ImageView plyr ) {
super(50); // Wider platform for easier game play
// easy_gamepane.getChildren().add(plyr);
this.stick = stick;
isCollided = stick.isCollided;
easy_gamepane.getChildren().add(stick); // Add the stick to the gameplayPane
this.plyr = plyr;
easy_gamepane.getChildren().add(plyr);
plyr.setLayoutX(0);
plyr.setLayoutY(320);
// plyr.setFitWidth(50);
// plyr.setFitHeight(50);
for (int i = 0; i < platform_count; i++) {
Rectangle platform = new Rectangle(width, 150); //150 is height
platform.setLayoutX(lastPlatformPosition);
platform.setLayoutY(350); // Assuming platforms are at the bottom
platform.setFill(Color.WHITE);
easy_gamepane.getChildren().add(platform);
lastPlatformPosition += distanceBetweenPlatforms;
}
}
public void start_animation() {
// Animation timer initialization
AnimationTimer animationTimer = new AnimationTimer() {
private long lastUpdate = 0;
@Override
public void handle(long now) {
if (now - lastUpdate >= 1_000_000_000) {
// Update player's position here
//plyr.move(25); // Assuming you have a move() method in the Plyr class
lastUpdate = now;
System.out.println(isCollided);
// Check for collision between Stick and EasyPlatform
if (stick.isCollided != isCollided) {
isCollided = stick.isCollided;
// Call the necessary methods or perform actions when collision occurs
if (isCollided){
double current_locn = plyr.getX();
// Collision occurred, handle accordingly
plyr.move(current_locn+distanceBetweenPlatforms);
}
}
}
}
};
// Rest of the method implementation
// Add any other necessary code here
animationTimer.start();
}
// Initialize the AnimationTimer
public void initializeAnimationTimer() {
String flagString = Boolean.toString(isCollided);
System.out.printf("boolean value of iscollided is : %s" , flagString);
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
// Check if it's time to move the player and generate a new platform
if (isCollided) {
plyr.move(150+distanceBetweenPlatforms);
isCollided = false; // Reset isCollided after handling the collision
}
}
};
// Start the AnimationTimer
timer.start();
}
// Public method to set the isCollided flag
public void setCollision(boolean isCollided) {
this.isCollided = isCollided;
}
private void movePlayerAndGeneratePlatform() {
// Move the player
//stick.setLayoutX(stick.getLayoutX() + distanceBetweenPlatforms);
// Check if it's time to generate a new platform
if (stick.getLayoutX() - lastPlatformPosition >= distanceBetweenPlatforms) {
generatePlatform();
lastPlatformPosition += distanceBetweenPlatforms;
}
}
public void playerMoved(double playerPositionX) {
// Check if it's time to generate a new platform
if (playerPositionX - lastPlatformPosition >= distanceBetweenPlatforms) {
generatePlatform();
lastPlatformPosition += distanceBetweenPlatforms;
}
}
private void generatePlatform()
{
Rectangle platform = new Rectangle(getWidth(), 150);
platform.setLayoutX(lastPlatformPosition);
platform.setLayoutY(350); // Assuming platforms are at the bottom
platform.setFill(Color.WHITE);
easy_gamepane.getChildren().add(platform);
lastPlatformPosition += distanceBetweenPlatforms;
}
}
class MediumPlatform extends Platform {
public Stick stick;
protected int width = 10;
protected Rectangle med_platform_rec;
private static int distanceBetweenPlatforms;
private static double lastPlatformPosition;
private final int platform_count = 3;
private boolean isCollided = false;
private Plyr plyr;
@FXML
private static Pane med_gamepane ; // This is linked to the Pane in your FXML file
public MediumPlatform(Pane med_gamepane , Stick stick , int distanceBetweenPlatforms , Plyr plyr)
{// , ImageView plyr ) {
super(50); // Wider platform for easier game play
// easy_gamepane.getChildren().add(plyr);
this.stick = stick;
med_gamepane.getChildren().add(stick); // Add the stick to the gameplayPane
this.plyr = plyr;
med_gamepane.getChildren().add(plyr);
plyr.setLayoutX(0);
plyr.setLayoutY(320);
// plyr.setFitWidth(50);
// plyr.setFitHeight(50);
for (int i = 0; i < platform_count; i++) {
Rectangle platform = new Rectangle(getWidth(), 150);
platform.setLayoutX(lastPlatformPosition);
platform.setLayoutY(350); // Assuming platforms are at the bottom
platform.setFill(Color.WHITE);
med_gamepane.getChildren().add(platform);
lastPlatformPosition += distanceBetweenPlatforms;
}
}
//Initialize the AnimationTimer
public void initializeAnimationTimer() {
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
// Check if it's time to move the player and generate a new platform
if (isCollided) {
movePlayerAndGeneratePlatform();
isCollided = false; // Reset isCollided after handling the collision
}
}
};
// Start the AnimationTimer
timer.start();
}
// Public method to set the isCollided flag
public void setCollision(boolean isCollided) {
this.isCollided = isCollided;
}
private void movePlayerAndGeneratePlatform() {
// Move the player
//stick.setLayoutX(stick.getLayoutX() + distanceBetweenPlatforms);
// Check if it's time to generate a new platform
if (stick.getLayoutX() - lastPlatformPosition >= distanceBetweenPlatforms) {
generatePlatform();
lastPlatformPosition += distanceBetweenPlatforms;
}
}
public void playerMoved(double playerPositionX) {
// Check if it's time to generate a new platform
if (playerPositionX - lastPlatformPosition >= distanceBetweenPlatforms) {
generatePlatform();
lastPlatformPosition += distanceBetweenPlatforms;
}
}
private void generatePlatform()
{
Rectangle platform = new Rectangle(getWidth(), 150);
platform.setLayoutX(lastPlatformPosition);
platform.setLayoutY(350); // Assuming platforms are at the bottom
platform.setFill(Color.WHITE);
med_gamepane.getChildren().add(platform);
lastPlatformPosition += distanceBetweenPlatforms;
}
}
class HardPlatform extends Platform {
public Stick stick;
protected int width = 10;
protected Rectangle hard_platform_rec;
private static int distanceBetweenPlatforms;
private static double lastPlatformPosition;
private final int platform_count = 3;
private boolean isCollided = false;
private Plyr plyr;
@FXML
private static Pane hard_gamepane ; // This is linked to the Pane in your FXML file
public HardPlatform(Pane hard_gamepane , Stick stick , int distanceBetweenPlatforms , Plyr plyr)
{// , ImageView plyr ) {
super(50); // Wider platform for easier game play
// easy_gamepane.getChildren().add(plyr);
this.stick = stick;
hard_gamepane.getChildren().add(stick); // Add the stick to the gameplayPane
this.plyr = plyr;
hard_gamepane.getChildren().add(plyr);
plyr.setLayoutX(0);
plyr.setLayoutY(320);
// plyr.setFitWidth(50);
// plyr.setFitHeight(50);
for (int i = 0; i < platform_count; i++) {
Rectangle platform = new Rectangle(getWidth(), 150);
platform.setLayoutX(lastPlatformPosition);
platform.setLayoutY(350); // Assuming platforms are at the bottom
platform.setFill(Color.WHITE);
hard_gamepane.getChildren().add(platform);
lastPlatformPosition += distanceBetweenPlatforms;
}
}
// Initialize the AnimationTimer
public void initializeAnimationTimer() {
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
// Check if it's time to move the player and generate a new platform
if (isCollided) {
movePlayerAndGeneratePlatform();
isCollided = false; // Reset isCollided after handling the collision
}
}
};
// Start the AnimationTimer
timer.start();
}
// Public method to set the isCollided flag
public void setCollision(boolean isCollided) {
this.isCollided = isCollided;
}
private void movePlayerAndGeneratePlatform() {
// Move the player
//stick.setLayoutX(stick.getLayoutX() + distanceBetweenPlatforms);
// Check if it's time to generate a new platform
if (stick.getLayoutX() - lastPlatformPosition >= distanceBetweenPlatforms) {
generatePlatform();
lastPlatformPosition += distanceBetweenPlatforms;
}
}
public void playerMoved(double playerPositionX) {
// Check if it's time to generate a new platform
if (playerPositionX - lastPlatformPosition >= distanceBetweenPlatforms) {
generatePlatform();
lastPlatformPosition += distanceBetweenPlatforms;
}
}
private void generatePlatform()
{
Rectangle platform = new Rectangle(getWidth(), 150);
platform.setLayoutX(lastPlatformPosition);
platform.setLayoutY(350); // Assuming platforms are at the bottom
platform.setFill(Color.WHITE);
hard_gamepane.getChildren().add(platform);
lastPlatformPosition += distanceBetweenPlatforms;
}
}