-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bike.java
281 lines (230 loc) · 7.11 KB
/
Bike.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
/*
* Author: Clément Petit
* Date: 15.10.2015
*/
package ch.epfl.cs107.play.game.actor.bike;
import java.awt.Color;
import ch.epfl.cs107.play.game.actor.Actor;
import ch.epfl.cs107.play.game.actor.ActorGame;
import ch.epfl.cs107.play.game.actor.GameEntity;
import ch.epfl.cs107.play.game.actor.ShapeGraphics;
import ch.epfl.cs107.play.game.actor.general.Wheel;
import ch.epfl.cs107.play.math.BasicContactListener;
import ch.epfl.cs107.play.math.Circle;
import ch.epfl.cs107.play.math.Contact;
import ch.epfl.cs107.play.math.ContactListener;
import ch.epfl.cs107.play.math.Entity;
import ch.epfl.cs107.play.math.PartBuilder;
import ch.epfl.cs107.play.math.Polygon;
import ch.epfl.cs107.play.math.Polyline;
import ch.epfl.cs107.play.math.Vector;
import ch.epfl.cs107.play.window.Canvas;
public class Bike extends GameEntity implements Actor{
private final float MAX_WHEEL_SPEED = 20.0f;
private boolean toRight = true; // Orientation du cycliste
private PartBuilder partBuilder;
private ShapeGraphics BikeGraphics;
private Polygon polygon;
private Wheel leftWheel;
private Wheel rightWheel;
private ShapeGraphics headGraphics;
private ShapeGraphics armGraphics;
private ShapeGraphics bottomGraphics;
private ShapeGraphics knee1Graphics;
private ShapeGraphics knee2Graphics;
private ShapeGraphics leg1Graphics;
private ShapeGraphics leg2Graphics;
private boolean hit;
private Vector handLocation;
private Vector knee1Location;
private Vector knee2Location;
private Vector foot1Location;
private Vector foot2Location;
public Bike(ActorGame game, boolean fixed, Vector position) {
super(game, fixed, position);
partBuilder = getEntity().createPartBuilder();
polygon = new Polygon(
0.0f, 0.5f,
0.5f, 1.0f,
0.0f, 2.0f,
-0.5f, 1.0f
);
partBuilder.setShape(polygon);
partBuilder.setGhost(false);
partBuilder.build();
leftWheel = new Wheel(getOwner(), false, position.add(-1.0f, 0.f), 0.5f);
rightWheel = new Wheel(getOwner(), false, position.add(1.0f, 0.f), 0.5f);
leftWheel.attach(getEntity(), new Vector(-1.0f, 0.0f), new Vector(-0.5f, -1.0f));
rightWheel.attach(getEntity(), new Vector(1.0f, 0.0f), new Vector(0.5f, -1.0f));
getOwner().addActor(this);
leftWheel.relax();
rightWheel.relax();
handLocation = new Vector(0.55f, 0.95f);
knee1Location = new Vector(0.0f,0.65f);
knee2Location = new Vector(0.0f,0.65f);
foot1Location = new Vector(-0.25f, 0.1f);
foot2Location = new Vector(0.25f, 0.1f);
ContactListener listener = new ContactListener() {
@Override
public void beginContact(Contact contact) {
if (contact.getOther().isGhost()) {
return ;
}
// si contact avec les roues
if (contact.getOther().getEntity().equals (leftWheel.getEntity())) {
return ;
}
hit = true ;
}
@Override
public void endContact(Contact contact) {}
};
this.getEntity().addContactListener(listener);
}
public Entity getBikeEntity() {
return getEntity();
}
public boolean getHit() {
return hit ;
}
public void destroy() {
getEntity().destroy();
rightWheel.destroy();
leftWheel.destroy(); //Ici, on aurait pu detach.
getOwner().removeActor(this);
}
public Wheel getLeftWheel() {
return leftWheel;
}
public Wheel getRightWheel() {
return rightWheel;
}
public void MoveLeft() {
if(rightWheel.getSpeed() < MAX_WHEEL_SPEED) {
rightWheel.power(MAX_WHEEL_SPEED);
}
}
public void MoveRight() {
if(leftWheel.getSpeed() > -MAX_WHEEL_SPEED) {
leftWheel.power(-MAX_WHEEL_SPEED);
}
}
// Voir la méthode bike game update (premier if) :
// cela permet de changer l'orientation du cycliste
// (en métant toRight en argument).
public void setOppositeDirection(boolean Dir) {
toRight = !(Dir);
}
public boolean getDirection() {
return toRight;
}
private Vector getHeadLocation() {
return new Vector(0.0f, 1.75f) ;
}
private Vector getShoulderLocation() {
if(toRight == true) {
return new Vector(-0.15f,1.5f);
}
else {
return new Vector(0.15f,1.5f);
}
}
private Vector getHandLocation() {
if(toRight == true) {
return handLocation;
}
else {
return new Vector(-handLocation.getX(), handLocation.getY());
}
}
public void setFinishedHandLocation() {
handLocation = new Vector(0.1f, 2.4f);
}
private Vector getBackLocation() {
if(toRight == true) {
return new Vector(-0.55f, 0.95f);
}
else {
return new Vector(0.55f, 0.95f);
}
}
private Vector getKnee1Location() {
if(toRight == true) {
return knee1Location;
}
else
{
return new Vector(-knee1Location.getX(),knee1Location.getY());
}
}
public void setKnee1Location(Vector location) {
knee1Location = location;
}
private Vector getKnee2Location() {
if(toRight == true) {
return knee2Location;
}
else
{
return new Vector(-knee2Location.getX(),knee2Location.getY());
}
}
public void setKnee2Location(Vector location) {
knee2Location = location;
}
private Vector getFoot1Location() {
if(toRight == true) {
return foot1Location;
}
else {
return new Vector(-foot1Location.getX(), foot1Location.getY());
}
}
public void setFoot1Location(Vector location) {
foot1Location = location;
}
private Vector getFoot2Location() {
if(toRight == true) {
return foot2Location;
}
else {
return new Vector(-foot2Location.getX(), foot2Location.getY());
}
}
public void setFoot2Location(Vector location) {
foot2Location = location;
}
@Override
public void draw(Canvas canvas) {
// Draw head
Circle head = new Circle(0.2f, getHeadLocation()) ;
headGraphics = new ShapeGraphics(head, Color.BLACK, Color.BLACK, 0.1f);
headGraphics.setParent(this);
// Draw arm
Polyline arm = new Polyline(getShoulderLocation(),getHandLocation()) ;
armGraphics = new ShapeGraphics(arm, Color.RED, Color.RED, 0.1f);
armGraphics.setParent(this);
Polyline bottom = new Polyline(getShoulderLocation(), getBackLocation());
bottomGraphics = new ShapeGraphics(bottom, Color.RED, Color.RED, 0.1f);
bottomGraphics.setParent(this);
Polyline knee1 = new Polyline(getBackLocation(), getKnee1Location());
knee1Graphics = new ShapeGraphics(knee1, Color.BLUE, Color.BLUE, 0.1f);
knee1Graphics.setParent(this);
Polyline knee2 = new Polyline(getBackLocation(), getKnee2Location());
knee2Graphics = new ShapeGraphics(knee2, Color.BLUE, Color.BLUE, 0.1f);
knee2Graphics.setParent(this);
Polyline leg1 = new Polyline(getKnee1Location(), getFoot1Location());
leg1Graphics = new ShapeGraphics(leg1, Color.BLACK, Color.BLACK, 0.1f);
leg1Graphics.setParent(this);
Polyline leg2 = new Polyline(getKnee2Location(), getFoot2Location());
leg2Graphics = new ShapeGraphics(leg2, Color.BLACK, Color.BLACK, 0.1f);
leg2Graphics.setParent(this);
headGraphics.draw(canvas);
armGraphics.draw(canvas);
bottomGraphics.draw(canvas);
knee1Graphics.draw(canvas);
knee2Graphics.draw(canvas);
leg1Graphics.draw(canvas);
leg2Graphics.draw(canvas);
}
}