-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
326 lines (273 loc) · 8.22 KB
/
Game.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
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;
import java.lang.*;
import org.jbox2d.dynamics.Body;
import javafx.scene.canvas.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.geometry.Insets;
import javafx.scene.text.TextAlignment;
import javafx.scene.paint.Color;
import java.util.EventListener;
import javafx.scene.input.MouseEvent;
import javafx.event.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import org.jbox2d.dynamics.BodyType;
import javafx.scene.shape.*;
import static java.lang.Math.random;
import java.util.ArrayList;
import javafx.scene.input.KeyEvent;
import javafx.stage.*;
import javafx.scene.Scene;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.Media;
import java.io.File;
import java.io.FileInputStream;
public class Game extends Stage
{
private int _state = -1;
public ArrayList<Item> bodiesToRemove;
public World world;
public float SCALE;
public float WIDTH;
public float HEIGHT;
static float wall_size = 0.3f;
private GameLoop gameLoop;
private ArrayList<String> input = new ArrayList<String>();
private Pane layout;
private float total = 0;
private float ball_acumulator = 0;
private float ball_tick = 0.05f;
public Target target;
public Player p1;
public Player p2;
public Wall left;
public Wall right;
public Wall top;
public Wall bot;
Rectangle pausefilter;
private Boolean setPause = false;
private static String[] texts = {"Reprendre", "Retour au menu principal", "Quitter"};
private Menu myMenu;
TerrainArea ta_left;
TerrainArea ta_right;
private Boolean reset_target = false;
private Boolean finished = false;
public void addShape(Shape shape)
{
layout.getChildren().add(shape);
}
public void removeItem(Item i)
{
world.destroyBody(i.getBody());
layout.getChildren().remove(i.getShape());
}
public Game(float sc, float w, float h, PlayerParams param_p1, PlayerParams param_p2)
{
setTitle("Shoot'n Goal ! (en jeu)");
//Media sound = new Media("/sounds/hit.wav");//(new File("/sounds/hit.wav").toURI().toString());
//MediaPlayer mp = new MediaPlayer(sound);
SCALE = sc;
WIDTH = w;
HEIGHT = h;
setOnCloseRequest(new EventHandler<WindowEvent>() {
public void handle(WindowEvent we)
{
we.consume();
}
});
pausefilter = new Rectangle(SCALE * WIDTH, SCALE * HEIGHT);
pausefilter.setFill(Color.rgb(0, 0, 0, 0.5));
myMenu = new Menu(30, 30, 300, texts);
StackPane root = new StackPane();
ta_left = new TerrainArea(SCALE * ((WIDTH / 2) - 0.5f), SCALE * HEIGHT, "#000064", param_p1._name, "5");
ta_right = new TerrainArea(SCALE * ((WIDTH / 2) - 0.5f), SCALE * HEIGHT, "#640000", param_p2._name, "5");
//ta_left.setPrefWidth(SCALE * ((WIDTH / 2) - 0.5));
HBox hArea = new HBox(SCALE * 1);
hArea.setMaxHeight(SCALE * HEIGHT);
hArea.getChildren().addAll(ta_left, ta_right);
root.getChildren().add(hArea);
BorderPane bp = new BorderPane();
bp.setMargin(myMenu, new Insets(200, ((WIDTH * sc) / 2) - 150, 200, ((WIDTH * sc) / 2) - 150));
bp.setCenter(myMenu);
//root.setAlignment(myMenu,Pos.BASELINE_CENTER);
Scene scene = new Scene(root);
initStyle(StageStyle.UNDECORATED);
setResizable(false);
setScene(scene);
sizeToScene();
scene.setOnKeyPressed(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String key = e.getCode().toString();
if(key == "ESCAPE")
{
if(setPause == true)
return;
layout.getChildren().add(pausefilter);
root.getChildren().add(bp);
setPause = true;
gameLoop.stop();
setTitle("Shoot'n Goal ! (en pause)");
}
if(setPause)
{
if(key == "UP")
{
myMenu.up();
}
else if(key == "DOWN")
{
myMenu.down();
}
else if(key == "ENTER")
{
_state = myMenu.select();
if(_state != 0)
{
close();
return;
}
else
{
setTitle("Shoot'n Goal ! (en jeu)");
layout.getChildren().remove(pausefilter);
root.getChildren().remove(bp);
setPause = false;
if(!finished)
gameLoop.start();
}
}
}
if(!input.contains(key))
input.add(key);
}
});
scene.setOnKeyReleased(
new EventHandler<KeyEvent>()
{
public void handle(KeyEvent e)
{
String key = e.getCode().toString();
input.remove(key);
}
});
world = new World(new Vec2(0f, 0f));
bodiesToRemove = new ArrayList<Item>();
gameLoop = new GameLoop(this);
layout = new Pane();
layout.setPrefWidth(sc * w);
layout.setPrefHeight(sc * h);
root.getChildren().add(layout);
String[] keys = {"Z", "S", "Q", "D"};
String[] keys2 = {"UP", "DOWN", "LEFT", "RIGHT"};
left = new Wall(this, wall_size / 2, HEIGHT / 2, wall_size, HEIGHT, 0.06f, Color.rgb(0, 0, 0, 0.25f), Color.rgb(0, 0, 0), 0);
right = new Wall(this, WIDTH - wall_size / 2, HEIGHT / 2, wall_size, HEIGHT, 0.06f, Color.rgb(0, 0, 0, 0.25f), Color.rgb(0, 0, 0), 0);
top = new Wall(this, WIDTH / 2, HEIGHT - wall_size / 2, WIDTH - (2 * wall_size), wall_size, 0.06f, Color.rgb(0, 0, 0, 0.25f), Color.rgb(0, 0, 0), 0);
bot = new Wall(this, WIDTH / 2, wall_size / 2, WIDTH - (2 * wall_size), wall_size, 0.06f, Color.rgb(0, 0, 0, 0.25f), Color.rgb(0, 0, 0), 0);
target = new Target(this, WIDTH / 2, HEIGHT / 2, 2f, 0.75f, 0.1f, Color.rgb(0, 255, 0, 1f), Color.rgb(0, 0, 0));
target.body.setLinearVelocity(new Vec2(0, 5));
target.body.setAngularVelocity(3);
p1 = new Player(this, 1, 5.0f, 0.95f, param_p1);
p2 = new Player(this, -1, 5.0f, 0.95f, param_p2);
Separator sep = new Separator(this, WIDTH / 2, HEIGHT / 2, 1, WIDTH);
MyCollider myCollider = new MyCollider(this);
world.setContactListener(myCollider);
gameLoop.start();
}
public void proceed(float secs)
{
if(reset_target)
{
target.resetPosition();
reset_target = false;
}
total += secs;
removeBodies();
p1.move();
p2.move();
Vec2 vel = target.getBody().getLinearVelocity();
target.getBody().setLinearVelocity(new Vec2(vel.x * 0.99f, vel.y * 0.99f));
ball_acumulator += secs;
if(ball_acumulator > ball_tick)
{
ball_acumulator -= ball_tick;
Vec2 playerPos = p1._ship.body.getPosition();
Ball ball = new Ball(this, 0.2f, playerPos.x, playerPos.y, 0.02f, Color.rgb(0, 0, 255), Color.rgb(255, 255, 255));
ball.getBody().applyLinearImpulse(new Vec2(15, 0), ball.body.getPosition(), true);
playerPos = p2._ship.body.getPosition();
Ball ball2 = new Ball(this, 0.2f, playerPos.x, playerPos.y, 0.02f, Color.rgb(255, 0, 0), Color.rgb(255, 255, 255));
ball2.getBody().applyLinearImpulse(new Vec2(-15, 0), ball2.body.getPosition(), true);
}
world.step(secs, 8, 3);
}
public void removeBodies()
{
for (int i = bodiesToRemove.size() - 1 ; i >= 0 ; i--)
{
removeItem(bodiesToRemove.get(i));
bodiesToRemove.remove(i);
}
//bodiesToRemove.clear();
}
public void render()
{
for(Body b = world.getBodyList(); b != null; b = b.getNext())
{
Object o = b.getUserData();
if(o instanceof Item)
{
((Item) o).updatePosition(this);
}
}
}
public void scored(int player)
{
if(player == 1)
{
//p1 marque
p2._score--;
if(p2._score == 0)
{
ta_right.updateScore("Lose !");
ta_right.updateScore("Win !");
}
else
{
ta_right.updateScore(Integer.toString(p2._score));
}
}
else
{
// p2 marque
p1._score--;
if(p1._score == 0)
{
ta_left.updateScore("Lose !");
ta_right.updateScore("Win !");
}
else
{
ta_left.updateScore(Integer.toString(p1._score));
}
}
reset_target = true;
if(p2._score == 0 || p1._score == 0)
{
finished = true;
gameLoop.stop();
}
}
public Boolean pressed(String key)
{
if(input.contains(key))
return true;
return false;
}
public int getState(){return _state;}
public float getScale(){return SCALE;}
}