-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlataforma.pde
More file actions
351 lines (300 loc) · 10.4 KB
/
Copy pathPlataforma.pde
File metadata and controls
351 lines (300 loc) · 10.4 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
class Plataforma extends FBox {
public float[] position = new float [2];
public CheckPoint checkPoint;
public FBox areaInteractiva;
public FBox areaDeVuelo;
public int index, nivel;
public FBox[] areaDeCaida = new FBox[2];
private FBox ancla;
private FRevoluteJoint union;
private float tiempoAnimacion;
private boolean isAnimacionTerminada;
private String estado;
private PImage textura;
public float birdDireccion = 1;
//--------------------------------------------
Plataforma(float _posX, float _posY, float _sizeX, float _sizeY) {
super(_sizeX, _sizeY);
ancla = new FBox(_sizeX*0.2, _sizeY*0.2);
checkPoint = new CheckPoint(this);
textura = loadImage("plataformaTexture.png");
textura.resize(int (getWidth()), int(getHeight()));
attachImage(textura);
estado = "noControlable";
Posicionar(_posX, _posY);
StartPlataforma();
StartAncla();
StartUnion();
StartAreaInteractiva();
this.setGroupIndex(-1);//!IMPORTANTE: Este valor al estar negativo, no colisiona con los objetos que tengan este mismo valor en negativo.
}
//--------------------------------------------
void StartPlataforma() {
this.setName("plataforma");
this.setStatic(false);
this.setSensor(false);
this.setDensity(CalcularDensidad());
mundo.add(this);
OcultarEnModoNoDesarrollador(this);
}
void StartAncla() {
ancla.setPosition( position[x], position[y] );
ancla.setDensity(this.getDensity() * 1000);
mundo.add(ancla);
OcultarEnModoNoDesarrollador(ancla);
}
void StartUnion() {
union = new FRevoluteJoint(this, ancla);
union.setFill(0);
union.setEnableMotor(true);
union.setEnableLimit(true);
union.setCollideConnected(false);
mundo.add(union);
if (desarrollador == false) {
union.setNoFill();
union.setNoStroke();
}
}
void StartAreaInteractiva() {
areaInteractiva = new FBox(this.getWidth()-20, 10);
areaInteractiva.setPosition( position[x], position[y] - (this.getHeight()/2) );
areaInteractiva.setName("areaNoElegida");
areaInteractiva.setSensor(true);
areaInteractiva.setGroupIndex(-1);
mundo.add(areaInteractiva);
OcultarEnModoNoDesarrollador(areaInteractiva);
}
void StartAreaDeVuelo() {//Se inicia desde el mapa...
areaDeVuelo = new FBox(this.getWidth(), 30);
float ajustarAltura = this.getHeight() - (this.getHeight()/2) + 110;
areaDeVuelo.setPosition(position[x], position[y] - ajustarAltura);
areaDeVuelo.setName("areaDeVuelo");
areaDeVuelo.setGroupIndex(-1);
areaDeVuelo.setSensor(true);
areaDeVuelo.setNoFill();
OcultarEnModoNoDesarrollador(areaDeVuelo);
//... porque necesita el nivel de la plataforma
if (this.nivel != 3) mundo.add(areaDeVuelo);
}
void StartAreaDeCaida(String borde) {//Se inicia desde el mapa...
for (int i=0; i < areaDeCaida.length; i++) {
if (borde.equals("RIGHT") && i == 0)//Se detiene el primer ciclo
continue;
else if (borde.equals("LEFT") && i == 1)//Se detiene el segundo ciclo
break;
else if (borde.equals("AMBOS"));//No se deteniene
areaDeCaida[i] = new FBox (5, this.getHeight()*0.3);
areaDeCaida[i].setName("areaDeCaida");
areaDeCaida[i].setGroupIndex(-1);
areaDeCaida[i].setSensor(true);
areaDeCaida[i].setDensity(0.1);
areaDeCaida[i].setFill(255, 40, 80);
areaDeCaida[i].setNoStroke();
if (i == 0) areaDeCaida[i].setPosition(position[x]-(this.getWidth()/2), position[y]);
if (i == 1) areaDeCaida[i].setPosition(position[x]+(this.getWidth()/2), position[y]);
mundo.add(areaDeCaida[i]);
OcultarEnModoNoDesarrollador(areaDeCaida[i]);
FRevoluteJoint unionAreaDeCaida = new FRevoluteJoint(this, areaDeCaida[i]);
unionAreaDeCaida.setFill(0);
unionAreaDeCaida.setEnableMotor(true);
unionAreaDeCaida.setEnableLimit(true);
mundo.add(unionAreaDeCaida);
if (!desarrollador) {
unionAreaDeCaida.setNoFill();
unionAreaDeCaida.setNoStroke();
}
}//end for
}
//--------------------------------------------
void Update() {
checkPoint.Update();
//Cuando la pajaro toca una nueva plataforma
if (areaInteractiva.isTouchingBody(bird) && areaInteractiva.getName() == "areaNoElegida") {
//Se desactivan todas y luego se activa la que esta bajo contacto
DesactivarPlataformas();
rotacionIgualadaConTuio = false;
areaInteractiva.setName("areaElegida");
estado = "play-animation";
}
//Para reproducir la animacion de seleccion
if (estado == "play-animation") {
AnimacionDeSeleccion("controlable");
}
//Plataforma controlable
if (estado == "controlable" && areaInteractiva.getName() == "areaElegida") {
bird.GetPlataformaControlable(this);
Girar();
LimitarGiro();
//println("velocidad angular " + this.getAngularVelocity());
}
//Regresa la plataforma a su lugar una vez se elije otra
if (areaInteractiva.getName() == "areaNoElegida" && estado == "controlable") {
ReiniciarPlataforma();
LimitarGiro();
}
if (areaInteractiva.getName() == "areaNoElegida" && estado == "noControlable") {
ReiniciarPlataforma();//Es un doble reinicio para evitar bugs
}
}
//--------------------------------------------
void Girar() {
float speed = radians(35);
//float factor = 4000000;
//float factor = 800000;
float factor = 4400000;
//float factor = 1800000;---modo1
//Manejo mediante el teclado
if (keyPressed) {
if (key == CODED) {
if (keyCode == RIGHT) ancla.adjustAngularVelocity(speed);
if (keyCode == LEFT) ancla.adjustAngularVelocity(-speed);
}
}
}
//---------------------
void LimitarGiro() {
float limite = radians(45);
if (getRotation() >= limite) {
setRotation(limite);
setAngularVelocity(0);
adjustAngularVelocity(radians(-1));
} else {
if (getRotation() <= -limite) {
setRotation(-limite);
setAngularVelocity(0);
adjustAngularVelocity(radians(1));
}
}
}
//--------------------
float CalcularDensidad() {
//float densidadAbsoluta = 100;
float densidadAbsoluta = 105;
float areaCorrelativa = (6*unidad) * (6*unidad);
float areaDePlataforma = getWidth() * getHeight();
float factorDensidad = areaCorrelativa/areaDePlataforma;
float densidadRelativa = densidadAbsoluta * factorDensidad;
return densidadRelativa;
}
//--------------------------------------------
void DesactivarPlataformas() {
//Busca a las areas de interaccion de cada plataforma
ArrayList <FBody> todosLosObjetos = mundo.getBodies();
for (FBody objeto : todosLosObjetos) {
if (objeto.getName() == "areaElegida")
objeto.setName("areaNoElegida");
}
}
//--------------------------------------------
void AnimacionDeSeleccion(String _estado) {
if (isAnimacionTerminada == false) {
if (tiempoAnimacion == 0) {
this.addImpulse(0, 7000000);
}
if (tiempoAnimacion == 20) {
this.addImpulse(0, -9000000);
}
if (tiempoAnimacion > 30) {
this.setVelocity(0, 0);
this.resetForces();
estado = _estado;
tiempoAnimacion = 0;
isAnimacionTerminada = true;
}
tiempoAnimacion++;
}
//println(this.getVelocityY());
//println(ancla.getForceY());
}
//----------------------------------------------------------------------------------------
void ReiniciarPlataforma() {
setAngularVelocity(0);
ReiniciarRotacion();
ReiniciarPosicion();
ReiniciarEstado();
ReiniciarAreaInteractiva();
isAnimacionTerminada = false;
}
//------------------------
void ReiniciarRotacion() {
if (degrees(getRotation()) >= 1) {
float _rotacion = degrees(getRotation())-1.5;
setRotation(radians(_rotacion));
}
if (degrees(getRotation()) <= -1) {
float _rotacion = degrees(getRotation())+1.5;
setRotation(radians(_rotacion));
}
}
//------------------------
void ReiniciarPosicion() {
if (this.getX() <= position[x] || this.getY() <= position[y]) {
float diferenciaDePosicionX = this.getX() - this.position[x];
float diferenciaDePosicionY = this.getY() - this.position[y];
float movimientoX = diferenciaDePosicionX/1.5;
float movimientoY = diferenciaDePosicionY/1.5;
setPosition( position[x]-movimientoX, getY()-movimientoY );
}
}
//------------------------
void ReiniciarEstado() {
if (degrees(getRotation()) > -0.5 && degrees(getRotation()) < 0.5)
if (this.getY() > position[y]-0.5 && this.getY() < position[y]+0.5)
estado = "noControlable";
}
//------------------------
void ReiniciarAreaInteractiva() {
areaInteractiva.resetForces();
areaInteractiva.setVelocity(0, 0);
areaInteractiva.setPosition( position[x], position[y] - (this.getHeight()/2) );
}
//----------------------------------------------------------------------------------------
//--------------------------------------------
void Posicionar(float _posX, float _posY) {
position[x] = _posX + RectModeCorner(x);
position[y] = _posY + RectModeCorner(y);
setPosition( position[x], position[y] );
}
//--------------------
float RectModeCorner(int eje) {
if (eje == x) return this.getWidth()/2;
else return this.getHeight()/2;
}
//--------------------------------------------
void OcultarEnModoNoDesarrollador(FBody _objeto) {
if (!desarrollador) {
_objeto.setNoFill();
_objeto.setNoStroke();
}
}
//--------------------------------------------
void DarColor() {
if (nivel == 1) setFill(87, 225, 59);//verde
else if (nivel == 2) setFill(59, 196, 225);//azul
else if (nivel == 3) setFill(125, 59, 225);//morado
else if (nivel == 4) setFill(225, 59, 167);//rosa
else if (nivel == 5) setFill(225, 99, 59);//naranja
else setFill(255);
}
//--------------------------------------------
void Enumerar() {
if (desarrollador)
{
for (int i=0; i < this.index; i++) {
FBox contador = new FBox(10, 10);
contador.setPosition(position[x]+(15*i), position[y]-(this.getHeight()/2)+25);
contador.setSensor(true);
contador.setGroupIndex(-1);
mundo.add(contador);
}
}
}
//-------------------------
void GetIndex(int _index) {
index = _index;
}
//-------------------------
void GetNivel(int _nivel) {
nivel = _nivel;
}
}