Repository navigation
Expand file tree
/
Copy pathPlayer.java
More file actions
461 lines (385 loc) · 13.8 KB
/
Copy pathPlayer.java
File metadata and controls
461 lines (385 loc) · 13.8 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import javafx.fxml.FXMLLoader;
import javafx.geometry.Point3D;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Circle;
import javafx.scene.shape.MeshView;
import javafx.scene.transform.Rotate;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.io.Serializable;
import java.util.LinkedList;
public class Player implements Serializable {
private String name;
private double posX,posY, posZ;
private int ground;
private int currentMaze;
private PlayerState state;
private PlayerState previousState;
private PlayerAnimation animation;
private Vector3D velocity,acceleration;
private float maxSpeed,accConstant,angularVelocity,gravity,friction,orientationSpeed,orientation,jumpVelocity,orientationX;
private float radius;
private boolean up,down,lookUp,lookDown,left,right,space,pickedUp;
private LinkedList<Bonus> bonus;
private LinkedList<Key> keys;
//Les états du joueur
public enum PlayerState implements Serializable{
BETWEEN,GROUND,STAIRSUP,STAIRSDOWN,DEAD,JUMPING
}
//Les animations du joueur
protected interface PlayerAnimation extends Serializable{
void animate(double elapsedSeconds);
boolean isOver();
}
protected class Falling implements PlayerAnimation{
public Falling(){
setGround(0);
state=null;
MazeInterface.sounds(3).play();
//add scream sound
}
public void animate(double elapsedSeconds){
System.out.println(posY);
applyGravity(elapsedSeconds);
if (posY <= ground) {
System.out.println("I'm dead !");
posY=ground;
velocity.setTo(0, 0, 0);
MazeInterface.sounds(3).stop();
MazeInterface.sounds(4).play();
changeState(PlayerState.DEAD);
//add crash sound
}
}
public boolean isOver(){
return state==PlayerState.DEAD;
}
}
protected class TurningTo implements PlayerAnimation{
protected int destination;
public TurningTo(int destination){
this.destination=destination;
orientationSpeed=1;
velocity.setTo(0,0,0);
// System.out.println("Destination: "+destination);
}
public void animate(double elapsedSeconds){
if(Math.abs(destination-orientation)<=180){
if(orientation<destination) turnLeft(elapsedSeconds);
else turnRight(elapsedSeconds);
}
else{
if(orientation<destination) turnRight(elapsedSeconds);
else turnLeft(elapsedSeconds);
}
if(isOver()) orientation=destination;
}
public boolean isOver(){
return (int)orientation==destination;
}
}
protected class LookingTo implements PlayerAnimation{
protected int destination;
public LookingTo(int destination){
this.destination=destination;
orientationSpeed=1;
velocity.setTo(0,0,0);
}
public void animate(double elapsedSeconds){
if(destination>orientationX && Math.abs(destination-orientationX)<=180 || Math.abs(destination-orientationX)>180 && destination<orientationX) lookUp(elapsedSeconds);
else lookDown(elapsedSeconds);
if(isOver()) orientationX=destination;
}
public boolean isOver(){
return (int)orientationX==destination;
}
}
public Player(String name){
this.name=name;
orientation=0;
posX=posZ=posY=0;
ground=0;
state=PlayerState.GROUND;
velocity=new Vector3D();
acceleration=new Vector3D();
maxSpeed=3f;
accConstant=3.5f;
gravity=1.5f;
friction=3;
orientationSpeed=2f;
bonus=new LinkedList<>();
keys=new LinkedList<>();
radius=0.15f;
currentMaze=0;
jumpVelocity=jumpVelocity(0.4f);
animation=null;
}
/******Les getters******/
public float radius(){
return radius;
}
public float orientationX(){
return orientationX;
}
public String getName(){ return name;}
public Point2D getPosition(){
return new Point2D.Double(posX,posZ);
}
public double getY(){
return posY;
}
//public Point2D getPosition(){ return position;}
public float orientation(){ return orientation;}
public LinkedList<Key> keys() {
return keys;
}
public LinkedList<Bonus> getBonus() {
return bonus;
}
public boolean hasPickedUp(){
return pickedUp;
}
public int getGround(){return ground;}
public int getMazeIndex(){return currentMaze;}
public PlayerState state() {
return state;
}
public PlayerState previousState() {
return previousState;
}
public PlayerAnimation animation() {
return animation;
}
/************************************************/
/******Les setters******/
public void setPosition(Point2D position, double y,float ori){
posX=position.getX();
posY=y;
posZ=position.getY();
this.orientation=ori;
}
public void setPosition(Point2D position, float ori){
setPosition(position,posY,ori);
}
public void setMaxSpeed(float s){
maxSpeed=s;
}
public void setMaze(int m){
currentMaze=m;
}
public void down(boolean b) {
down = b;
}
public void up(boolean b) {
up = b;
}
public void left(boolean b) {
left = b;
}
public void right(boolean b) {
right = b;
}
public void jump(boolean b){
space=b;
}
public void pick(boolean b){pickedUp=b;}
public void lookUp(boolean b){
lookUp=b;
}
public void lookDown(boolean b){
lookDown=b;
}
public void setAnimation(PlayerAnimation anim){
animation=anim;
}
public void changeState(PlayerState s){
if(state!=PlayerState.JUMPING) previousState=state;
state=s;
setAcceleration();
setLookUp();
}
public void reverseState(PlayerState s){
switch(s){
case STAIRSUP:changeState(PlayerState.STAIRSDOWN);
break;
case STAIRSDOWN:changeState(PlayerState.STAIRSUP);
break;
default:break;
}
}
public void setGround(int x){
ground=x;
}
public void setAcceleration(){
switch(state){
case STAIRSUP:acceleration.setTo((int)Math.cos(Math.toRadians(orientation)),1,(int)Math.sin((Math.toRadians(orientation))));
System.out.println(Math.cos(Math.toRadians(180))+" "+Math.sin(Math.toRadians(180)));
break;
case STAIRSDOWN:acceleration.setTo((int)Math.cos(Math.toRadians(orientation)),-1,(int)Math.sin(Math.toRadians(orientation)));
break;
case BETWEEN:acceleration.setTo(Math.cos(Math.toRadians(orientation)),0,Math.sin(Math.toRadians(orientation)));
break;
default:acceleration.setTo(Math.cos(Math.toRadians(orientation))*accConstant,0,Math.sin(Math.toRadians(orientation))*accConstant);
break;
}
}
public void setLookUp(){
switch(state){
case STAIRSUP:if((int)orientationX!=20) setAnimation(new LookingTo(20));
break;
case STAIRSDOWN:if((int)orientationX!=340) setAnimation(new LookingTo(340));
break;
case BETWEEN:
posY=ground;
if((int)orientationX!=340) setAnimation(new LookingTo(340));
break;
case GROUND:
posY=ground;
if(orientationX!=0) setAnimation(new LookingTo(0));
break;
}
}
/************************************************/
//Fonction de gestion des déplacements du joueur
public void updatePosition(double elapsedSeconds){
if(animation==null) {
if (state != PlayerState.JUMPING) {
if (left) {
turnLeft(elapsedSeconds);
if (up) moveForward(elapsedSeconds);
else if (down) moveBackward(elapsedSeconds);
} else if (right) {
turnRight(elapsedSeconds);
if (up) moveForward(elapsedSeconds);
else if (down) moveBackward(elapsedSeconds);
} else if (up) {
if (space) {
jump();
changeState(PlayerState.JUMPING);
} else if (left) turnLeft(elapsedSeconds);
else if (right) turnRight(elapsedSeconds);
moveForward(elapsedSeconds);
} else if (down) {
if (space) {
jump();
changeState(PlayerState.JUMPING);
}
else if (left) turnLeft(elapsedSeconds);
else if (right) turnRight(elapsedSeconds);
moveBackward(elapsedSeconds);
}
else if(lookUp){
lookUp(elapsedSeconds);
if (up) moveForward(elapsedSeconds);
else if (down) moveBackward(elapsedSeconds);
}
else if(lookDown){
lookDown(elapsedSeconds);
if (up) moveForward(elapsedSeconds);
else if (down) moveBackward(elapsedSeconds);
}
else if (space) {
jump();
changeState(PlayerState.JUMPING);
} else {
velocity.setTo(0, 0, 0);
angularVelocity = 0;
}
} else applyGravity(elapsedSeconds);
}
else{
if(!animation.isOver()) animation.animate(elapsedSeconds);
else {
System.out.println(velocity);
System.out.println(acceleration);
System.out.println("problem");
animation=null;
angularVelocity=0;
orientationSpeed=2;
velocity.setTo(0,0,0);
}
}
posX+=velocity.x()*elapsedSeconds;
posY+=velocity.y()*elapsedSeconds;
if (posY < ground && state!=PlayerState.STAIRSDOWN && state!=PlayerState.STAIRSUP ) {
posY=ground;
velocity.setTo(0, 0, 0);
if(state==PlayerState.JUMPING) changeState(previousState);
}
posZ+=velocity.z()*elapsedSeconds;
}
//Pour tourner à gauche
public void turnLeft(double elapsedSeconds){
angularVelocity+=(float)(orientationSpeed*elapsedSeconds);
if(angularVelocity>orientationSpeed)angularVelocity=(float)(orientationSpeed);
orientation=(orientation+angularVelocity)%360;
setAcceleration();
}
public void turnRight(double elapsedSeconds){
angularVelocity+=(float)(orientationSpeed*elapsedSeconds);
if(angularVelocity>orientationSpeed)angularVelocity=(float)(orientationSpeed);
orientation=(orientation+(360-angularVelocity))%360;
setAcceleration();
}
//Pour regarder vers le haut
public void lookUp(double elapsedSeconds){
angularVelocity+=(float)(orientationSpeed*elapsedSeconds);
if(angularVelocity>orientationSpeed)angularVelocity=(float)(orientationSpeed);
orientationX=(orientationX+angularVelocity)%360;
}
public void lookDown(double elapsedSeconds){
angularVelocity+=(float)(orientationSpeed*elapsedSeconds);
if(angularVelocity>orientationSpeed)angularVelocity=(float)(orientationSpeed);
orientationX=(orientationX+(360-angularVelocity))%360;
}
//Pour aller vers l'avant
public void moveForward(double elapsedSeconds){
velocity=velocity.add(acceleration.multiply(elapsedSeconds));
if(velocity.norm()>maxSpeed) velocity=velocity.posColinear(maxSpeed);
}
public void moveBackward(double elapsedSeconds){
velocity=velocity.subtract(acceleration.multiply(elapsedSeconds));
if(velocity.norm()>maxSpeed) velocity=velocity.posColinear(maxSpeed);
}
//Pour sauter
public void jump(){
velocity=velocity.add(new Vector3D(0,jumpVelocity,0));
System.out.println(velocity);
}
public float jumpVelocity(float jumpHeight){
return (float)Math.sqrt(2*gravity*jumpHeight);
}
public void applyGravity(double elapsedSeconds){
velocity = velocity.add(new Vector3D(0, -gravity, 0).multiply(elapsedSeconds));
}
public void pickUp(Key key){
keys.add(key);
pickedUp=true;
}
public void pickUp(Bonus bonuss){
bonus.add(bonuss);
pickedUp=true;
}
public void useBonus(){
bonus.remove();
}
public void useBonus(int i){
for(int a=0;a<i;a++) bonus.remove();
}
//La représentation graphique du joueur
public MeshView initPlayer() throws IOException{
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(this.getClass().getResource("fxml/User.fxml"));
PhongMaterial mat = new PhongMaterial();
mat.setSpecularColor(Color.SNOW);
mat.setDiffuseColor(Color.YELLOW);
MeshView player = fxmlLoader.<MeshView>load();
player.setMaterial(mat);
Rotate r=new Rotate();
r.setAxis(Rotate.X_AXIS);
r.setAngle(r.getAngle()-90);
player.getTransforms().add(r);
return player;
}
}