diff --git a/core/src/com/me/pacman/state/MenuState.java b/core/src/com/me/pacman/state/MenuState.java index c4751ff..4be059d 100644 --- a/core/src/com/me/pacman/state/MenuState.java +++ b/core/src/com/me/pacman/state/MenuState.java @@ -1,11 +1,13 @@ package com.me.pacman.state; +import com.badlogic.gdx.Application; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.InputAdapter; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.math.Vector2; import com.me.pacman.PacDude; import com.me.pacman.Sound; @@ -19,6 +21,10 @@ public class MenuState extends LevelState { public static final int NEW_GAME = 0; public static final int HIGH_SCORES = 1; + private BoundingBox NEW_GAME_BOX = new BoundingBox(88, 66, 136, 84); + private BoundingBox HIGH_SCORE_BOX = new BoundingBox(88, 42, 136, 60); + + private static final int MENU_PADDING = 24; private static final int MENU_X = 84; private static final int MENU_Y = 85; @@ -42,8 +48,16 @@ public class MenuState extends LevelState { font = game.assets.getFont(); Gdx.input.setInputProcessor(this.new Controller()); - game.sound.play(Sound.Effect.BEGINNING); + + switch(Gdx.app.getType()) { + case Android: + selectedOption = -1; + break; + default: + selectedOption = 0; + break; + } } @Override @@ -88,6 +102,30 @@ public class MenuState extends LevelState { return super.keyDown(keycode); } + @Override + public boolean touchDown(int screenX, int screenY, int pointer, int button) { + Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY)); + if (NEW_GAME_BOX.contains(coords)) { + selectedOption = 0; + } else if (HIGH_SCORE_BOX.contains(coords)) { + selectedOption = 1; + } + return true; + } + + @Override + public boolean touchUp(int screenX, int screenY, int pointer, int button) { + Vector2 coords = game.viewport.unproject(new Vector2(screenX, screenY)); + if (selectedOption == 0 && NEW_GAME_BOX.contains(coords)) { + game.setScreen(new PlayState(game)); + } else if (selectedOption == 1 && HIGH_SCORE_BOX.contains(coords)) { + selectedOption = -1; + } else { + selectedOption = -1; + } + return true; + } + } @Override @@ -95,4 +133,25 @@ public class MenuState extends LevelState { Gdx.input.setInputProcessor(null); } + public static class BoundingBox { + private int minX, minY; + private int maxX, maxY; + + public BoundingBox(int minX, int minY, int maxX, int maxY) { + this.minX = minX; + this.minY = minY; + this.maxX = maxX; + this.maxY = maxY; + } + + public boolean contains(int x, int y) { + return minX < x && x < maxX && minY < y && y < maxY; + } + + public boolean contains(Vector2 coords) { + return contains((int) coords.x, (int) coords.y); + } + + } + } diff --git a/core/src/com/me/pacman/state/PlayState.java b/core/src/com/me/pacman/state/PlayState.java index fb2e343..d0b128a 100644 --- a/core/src/com/me/pacman/state/PlayState.java +++ b/core/src/com/me/pacman/state/PlayState.java @@ -499,6 +499,10 @@ public class PlayState extends LevelState { private final class Controller extends InputAdapter { + int downX; + int downY; + long downTime; + @Override public boolean keyDown(int keycode) { switch (keycode) { @@ -535,6 +539,47 @@ public class PlayState extends LevelState { return super.keyDown(keycode); } + @Override + public boolean touchDown(int screenX, int screenY, int pointer, int button) { + if (Math.abs(downX - screenX) < 50 && Math.abs(downY - screenY) < 50 + && System.currentTimeMillis() - downTime < 250) { + paused = !paused; + return true; + } + downX = screenX; + downY = screenY; + downTime = System.currentTimeMillis(); + return true; + } + + @Override + public boolean touchUp(int screenX, int screenY, int pointer, int button) { + int relaX = screenX - downX; + int relaY = screenY - downY; + + if (Math.abs(relaX) < 50 && Math.abs(relaY) < 50) { + // didn't move enough to consider a swipe + return true; + } + + if (Math.abs(relaX) > Math.abs(relaY)) { + // x > y, so moving left-right + if (relaX > 0) { + pacman.setNextDirection(Direction.RIGHT); + } else { + pacman.setNextDirection(Direction.LEFT); + } + } else { + // else, moving up/down + if (relaY > 0) { + pacman.setNextDirection(Direction.DOWN); + } else { + pacman.setNextDirection(Direction.UP); + } + } + return true; + } + } @Override