-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameScreen.java
More file actions
233 lines (193 loc) · 7.19 KB
/
Copy pathGameScreen.java
File metadata and controls
233 lines (193 loc) · 7.19 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
package puppy.code;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.ScreenUtils;
public class GameScreen implements Screen {
final GameLluviaMenu game;
private OrthographicCamera camera;
private SpriteBatch batch;
private BitmapFont font;
private Tarro tarro;
private Lluvia lluvia;
private Music musicIntro;
private Music musicLoop;
private boolean musicaActiva = false;
//Fondos (día/tarde/noche)
private Texture backgroundDay;
private Texture backgroundEvening;
private Texture backgroundNight;
private Texture[] backgrounds;
private int bgCurrent = 0;
private int bgNext = 1;
private float bgTimer = 0f;
private float bgAlpha = 0f;
private boolean bgTransition = false;
private static final float BG_CHANGE_EVERY = 20f;
private static final float BG_FADE_TIME = 3f;
// Nubes
private Texture nube1;
private Texture nube2;
private float nube1X, nube2X;
private float nube1Y, nube2Y;
// Debug
private boolean debugHitboxes = false;
private ShapeRenderer shape;
public GameScreen(final GameLluviaMenu game) {
this.game = game;
this.batch = game.getBatch();
this.font = game.getFont();
tarro = new Tarro();
// 🎵 música
musicIntro = Gdx.audio.newMusic(Gdx.files.internal("musicintro.ogg"));
musicLoop = Gdx.audio.newMusic(Gdx.files.internal("musicloop.ogg"));
musicLoop.setLooping(true);
// 🔉 establecer volumen
musicIntro.setVolume(0.3f);
musicLoop.setVolume(0.2f);
Music mLluvia = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
lluvia = new Lluvia(mLluvia);
// cámara y shape
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
shape = new ShapeRenderer();
// creación de objetos
tarro.crear();
lluvia.crear();
// fondos
backgroundDay = new Texture(Gdx.files.internal("background_day.png"));
backgroundEvening = new Texture(Gdx.files.internal("background_evening.png"));
backgroundNight = new Texture(Gdx.files.internal("background_night.png"));
backgrounds = new Texture[]{backgroundDay, backgroundEvening, backgroundNight};
// nubes
nube1 = new Texture(Gdx.files.internal("nube1.png"));
nube2 = new Texture(Gdx.files.internal("nube2.png"));
nube1X = 100;
nube1Y = 300;
nube2X = 520;
nube2Y = 270;
}
// MÚSICA DE INTRO + LOOP
@Override
public void show() {
if (!musicaActiva) {
musicaActiva = true;
musicIntro.play();
musicIntro.setOnCompletionListener(new Music.OnCompletionListener() {
@Override
public void onCompletion(Music music) {
if (!musicLoop.isPlaying()) {
musicLoop.play();
}
}
});
}
lluvia.continuar();
}
//LÓGICA PRINCIPAL
@Override
public void render(float delta) {
// Pausa con tecla ESC
if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)) {
pause();
return;
}
ScreenUtils.clear(0, 0, 0.2f, 1);
camera.update();
batch.setProjectionMatrix(camera.combined);
// Fondo con transición
bgTimer += delta;
if (!bgTransition && bgTimer >= BG_CHANGE_EVERY) {
bgTransition = true;
bgTimer = 0f;
bgAlpha = 0f;
bgNext = (bgCurrent + 1) % backgrounds.length;
}
if (bgTransition) {
bgAlpha = Math.min(1f, bgTimer / BG_FADE_TIME);
if (bgAlpha >= 1f) {
bgCurrent = bgNext;
bgTransition = false;
bgAlpha = 0f;
bgTimer = 0f;
}
}
batch.begin();
// Fondo actual
batch.draw(backgrounds[bgCurrent], 0, 0, 800, 480);
if (bgTransition) {
batch.setColor(1f, 1f, 1f, bgAlpha);
batch.draw(backgrounds[bgNext], 0, 0, 800, 480);
batch.setColor(1f, 1f, 1f, 1f);
}
// Nubes
nube1X -= 20 * delta;
nube2X -= 25 * delta;
if (nube1X < -200) nube1X = 800;
if (nube2X < -200) nube2X = 800;
batch.draw(nube1, nube1X, nube1Y, 200, 110);
batch.draw(nube2, nube2X, nube2Y, 150, 85);
// HUD
font.setColor(Color.WHITE);
font.getData().setScale(2f);
font.draw(batch, "Gotas: " + tarro.getPuntos(), 10, 470);
font.draw(batch, "Vidas: " + tarro.getVidas(), 670, 470);
font.draw(batch, "HighScore: " + game.getHigherScore(), 340, 450);
font.draw(batch, "Nivel: " + lluvia.getNivel(), 20, 40);
font.setColor(Color.WHITE);
// Lógica del juego
tarro.actualizarMovimiento();
boolean sigueJugando = lluvia.actualizarMovimiento(tarro);
if (!sigueJugando) {
if (game.getHigherScore() < tarro.getPuntos())
game.setHigherScore(tarro.getPuntos());
game.setScreen(new GameOverScreen(game));
dispose();
}
tarro.dibujar(batch);
lluvia.dibujar(batch);
batch.end();
// Debug
if (debugHitboxes) {
shape.setProjectionMatrix(camera.combined);
shape.begin(ShapeRenderer.ShapeType.Line);
shape.setColor(Color.RED);
Rectangle r = tarro.getArea();
shape.rect(r.x, r.y, r.width, r.height);
shape.setColor(Color.CYAN);
for (Rectangle drop : lluvia.getRaindrops())
shape.rect(drop.x, drop.y, drop.width, drop.height);
shape.end();
}
}
// Ciclo de vida
@Override public void resize(int width, int height) {}
@Override public void hide() {}
@Override public void resume() {}
@Override
public void pause() {
lluvia.pausar();
game.setScreen(new PausaScreen(game, this));
}
@Override
public void dispose() {
tarro.destruir();
lluvia.destruir();
if (nube1 != null) nube1.dispose();
if (nube2 != null) nube2.dispose();
if (backgroundDay != null) backgroundDay.dispose();
if (backgroundEvening != null) backgroundEvening.dispose();
if (backgroundNight != null) backgroundNight.dispose();
if (shape != null) shape.dispose();
if (musicIntro != null) musicIntro.dispose();
if (musicLoop != null) musicLoop.dispose();
}
}