Skip to content

Commit 66266dc

Browse files
committed
Added a pause menu & resmoothed the controls again.
1 parent 682cf3a commit 66266dc

File tree

2 files changed

+99
-43
lines changed

2 files changed

+99
-43
lines changed

core/src/ga/gussio/ld38/earthinvaders/screen/GameScreen.java

Lines changed: 94 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ga.gussio.ld38.earthinvaders.screen;
22

33
import com.badlogic.gdx.Gdx;
4+
import com.badlogic.gdx.Input;
45
import com.badlogic.gdx.audio.Music;
56
import com.badlogic.gdx.audio.Sound;
7+
import com.badlogic.gdx.files.FileHandle;
68
import com.badlogic.gdx.graphics.Color;
79
import com.badlogic.gdx.graphics.OrthographicCamera;
810
import com.badlogic.gdx.graphics.Texture;
@@ -15,6 +17,7 @@
1517
import com.badlogic.gdx.math.Vector2;
1618
import com.badlogic.gdx.math.Vector3;
1719
import com.badlogic.gdx.utils.viewport.FitViewport;
20+
import com.badlogic.gdx.Input.Keys;
1821

1922
import java.util.HashMap;
2023
import java.util.Random;
@@ -44,7 +47,7 @@ public class GameScreen extends Screen implements InputListener {
4447
private BitmapFont scoreFont;
4548
private Particle[] background;
4649

47-
private Button leftButton, rightButton, exit, retry;
50+
private Button leftButton, rightButton, exit, pauseExit, retry, resume;
4851
private Player player;
4952

5053
private long spawnTimer;
@@ -58,6 +61,8 @@ public class GameScreen extends Screen implements InputListener {
5861
private Music soundtrack;
5962
private Sound meteorDestroySound;
6063

64+
private boolean paused = false;
65+
6166
public GameScreen() {
6267
entities.clear();
6368
camera = new OrthographicCamera();
@@ -88,7 +93,9 @@ public GameScreen() {
8893
rightButton = new Button(150, 10, "buttons/control_button.png");
8994

9095
exit = new Button(1230, 450, "buttons/exit.png");
96+
pauseExit = new Button(1230, 450, "buttons/exit.png");
9197
retry = new Button(500, 450, "buttons/retry.png");
98+
resume = new Button(500, 450, "buttons/resume.png");
9299

93100
soundtrack = Gdx.audio.newMusic(Gdx.files.internal("sound/soundtrack.wav"));
94101
meteorDestroySound = Gdx.audio.newSound(Gdx.files.internal("sound/explosion.wav"));
@@ -172,55 +179,93 @@ public void render(SpriteBatch sb, ShapeRenderer sr) {
172179
scoreFont.draw(sb, "Highscore: "+Game.getHighscore(), highscoreX, Game.HEIGHT*7/10-60);
173180
sb.end();
174181
}
182+
183+
if(paused){
184+
sr.begin();
185+
sr.set(ShapeRenderer.ShapeType.Filled);
186+
sr.setColor(Color.GRAY);
187+
sr.rect(Game.WIDTH/4, Game.HEIGHT*4/10, Game.WIDTH/2, Game.HEIGHT*4/10);
188+
sr.end();
189+
190+
GlyphLayout textLayout = new GlyphLayout(scoreFont, "Game paused.");
191+
float textX = Game.WIDTH/4 + (Game.WIDTH/2 - textLayout.width) / 2;
192+
sb.begin();
193+
scoreFont.draw(sb, "Game paused.", textX, Game.HEIGHT*7/10);
194+
resume.renderSB(sb);
195+
pauseExit.renderSB(sb);
196+
sb.end();
197+
}
175198
}
176199

177200
@Override
178201
public void tick() {
179-
if(health > 0) {
180-
if (leftButton.clicked)
181-
player.setDirection(-1);
182-
else if (rightButton.clicked)
183-
player.setDirection(1);
184-
else
185-
player.setDirection(0);
186-
for (Entity e : entities) {
187-
e.tick();
188-
}
189-
if (dmgAnimation > 0) {
190-
health--;
191-
dmgAnimation--;
192-
}
202+
if(!paused) {
203+
if (health > 0) {
204+
if (leftButton.clicked)
205+
player.setDirection(-1);
206+
else if (rightButton.clicked)
207+
player.setDirection(1);
208+
else
209+
player.setDirection(0);
210+
for (Entity e : entities) {
211+
e.tick();
212+
}
213+
if (dmgAnimation > 0) {
214+
health--;
215+
dmgAnimation--;
216+
}
193217

194-
if (System.currentTimeMillis() > spawnTimer) {
195-
entities.add(new Meteorite(meteoriteSprites, warningSprites, meteorDestroySound));
196-
long dtime = System.currentTimeMillis() - startTime;
197-
if (dtime > 10000) {
198-
startTime = System.currentTimeMillis();
218+
if (System.currentTimeMillis() > spawnTimer) {
219+
entities.add(new Meteorite(meteoriteSprites, warningSprites, meteorDestroySound));
220+
long dtime = System.currentTimeMillis() - startTime;
221+
if (dtime > 10000) {
222+
startTime = System.currentTimeMillis();
223+
}
224+
spawnTimer = System.currentTimeMillis() + spawnFactor;
199225
}
200-
spawnTimer = System.currentTimeMillis() + spawnFactor;
201-
}
202226

203-
scoreTimer++;
204-
if (scoreTimer > 60) {
205-
score++;
206-
scoreTimer = 0;
227+
scoreTimer++;
228+
if (scoreTimer > 60) {
229+
score++;
230+
scoreTimer = 0;
231+
}
232+
} else {
233+
if (!appliedScore) {
234+
Game.checkHighscore(score);
235+
appliedScore = true;
236+
}
237+
retry.tick();
238+
exit.tick();
239+
240+
if (retry.clicked) {
241+
retry.clicked = false;
242+
Game.setCurrentScreen(new GameScreen());
243+
}
244+
245+
if (exit.clicked) {
246+
exit.clicked = false;
247+
Game.setCurrentScreen(new MenuScreen());
248+
}
207249
}
208250
}else{
209-
if(!appliedScore) {
210-
Game.checkHighscore(score);
211-
appliedScore = true;
251+
if(resume.clicked){
252+
resume.clicked = false;
253+
paused = false;
254+
soundtrack.play();
212255
}
213-
retry.tick();
214-
exit.tick();
215-
216-
if(retry.clicked){
217-
retry.clicked = false;
218-
Game.setCurrentScreen(new GameScreen());
256+
if(pauseExit.clicked){
257+
pauseExit.clicked = false;
258+
Game.setCurrentScreen(new MenuScreen());
219259
}
260+
}
220261

221-
if(exit.clicked){
222-
exit.clicked = false;
262+
if(Gdx.input.isKeyJustPressed(Keys.BACK)){
263+
if(paused){
223264
Game.setCurrentScreen(new MenuScreen());
265+
paused = false;
266+
}else {
267+
paused = true;
268+
soundtrack.pause();
224269
}
225270
}
226271
}
@@ -255,15 +300,19 @@ public void touchDown(int screenX, int screenY, int pointer, int button) {
255300
if(!left &! right){//didnt hit a button
256301
player.shoot();
257302
}
303+
System.out.println(pointer);
258304
if(left)
259305
pointers.put(pointer, 1);
260306
else if(right)
261307
pointers.put(pointer, 2);
262308
else
263309
pointers.put(pointer, 0);
264310

311+
265312
retry.click(new Vector2(coords.x, coords.y));
266313
exit.click(new Vector2(coords.x, coords.y));
314+
pauseExit.click(new Vector2(coords.x, coords.y));
315+
resume.click(new Vector2(coords.x, coords.y));
267316
}
268317

269318
@Override
@@ -282,22 +331,21 @@ public void touchUp(int screenX, int screenY, int pointer, int button) {
282331
}
283332
pointers.remove(pointer);
284333
}
334+
285335
retry.release(new Vector2(coords.x, coords.y));
286336
exit.release(new Vector2(coords.x, coords.y));
337+
pauseExit.release(new Vector2(coords.x, coords.y));
338+
resume.release(new Vector2(coords.x, coords.y));
287339
}
288340

289341
@Override
290342
public void touchDragged(int screenX, int screenY, int pointer) {
291343
Vector3 coords = camera.unproject(new Vector3(screenX, screenY, 0));
292344
boolean left = leftButton.drag(new Vector2(coords.x, coords.y));
293345
boolean right = rightButton.drag(new Vector2(coords.x, coords.y));
294-
int currentValue;
295-
if(pointers.containsKey(pointers)) {
296-
currentValue = pointers.get(pointer);
346+
if(pointers.containsKey(pointer)) {
347+
int currentValue = pointers.get(pointer);
297348
pointers.remove(pointer);
298-
}else{
299-
currentValue = 0;
300-
}
301349
if (left) {
302350
pointers.put(pointer, 1);
303351
leftButton.clicked = true;
@@ -313,7 +361,10 @@ public void touchDragged(int screenX, int screenY, int pointer) {
313361
else if (currentValue == 2)
314362
rightButton.clicked = false;
315363
}
364+
}
316365
retry.drag(new Vector2(coords.x, coords.y));
317366
exit.drag(new Vector2(coords.x, coords.y));
367+
pauseExit.click(new Vector2(coords.x, coords.y));
368+
resume.click(new Vector2(coords.x, coords.y));
318369
}
319370
}

core/src/ga/gussio/ld38/earthinvaders/screen/MenuScreen.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ga.gussio.ld38.earthinvaders.screen;
22

33
import com.badlogic.gdx.Gdx;
4+
import com.badlogic.gdx.Input;
45
import com.badlogic.gdx.graphics.Color;
56
import com.badlogic.gdx.graphics.OrthographicCamera;
67
import com.badlogic.gdx.graphics.Texture;
@@ -94,6 +95,10 @@ public void tick() {
9495
}
9596
play.tick();
9697
music.tick();
98+
99+
if(Gdx.input.isKeyJustPressed(Input.Keys.BACK)){
100+
Gdx.app.exit();
101+
}
97102
}
98103

99104
@Override

0 commit comments

Comments
 (0)