@Override public boolean keyDown(int keycode) { for (int i = screenStack.size() - 1; i >= 0; i--) { GameScreen gs = screenStack.get(i); if (gs.processor() == null) continue; if (gs.processor().keyDown(keycode) || gs.blocksInput()) return true; } return false; }
@Override public boolean mouseMoved(int screenX, int screenY) { for (int i = screenStack.size() - 1; i >= 0; i--) { GameScreen gs = screenStack.get(i); if (gs.processor() == null) continue; if (gs.processor().mouseMoved(screenX, screenY) || gs.blocksInput()) return true; } return false; }
public void move(float x, float y) { position.x += x; position.y += y; Vector2 lPos = Utils.mapToScreen( new Vector2(position.x - 0.5F, position.y + 1), new Vector2(game.getMainCam().position.x, game.getMainCam().position.y)); label.setPosition(lPos.x, lPos.y); }
@Override public boolean touchDragged(int screenX, int screenY, int pointer) { for (int i = screenStack.size() - 1; i >= 0; i--) { GameScreen gs = screenStack.get(i); if (gs.processor() == null) continue; if (gs.processor().touchDragged(screenX, screenY, pointer) || gs.blocksInput()) return true; } return false; }
@Override public boolean scrolled(int amount) { for (int i = screenStack.size() - 1; i >= 0; i--) { GameScreen gs = screenStack.get(i); if (gs.processor() == null) continue; if (gs.processor().scrolled(amount) || gs.blocksInput()) return true; } return false; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); setContentView(R.layout.gamescreen); game = (GameScreen) findViewById(R.id.gameview); game.setBackgroundDrawable(Theme.getBackground(getAssets())); game.setParent(this); }
// switches to a new screen, discarding the old one public void switchScreen(Component s) { if (s == null) { return; } if (mainComponent == s) { // alreay on correct component! s.repaint(); return; } setVisible(false); removeAll(); add(s); if (s instanceof GameScreen) { ((GameScreen) s) .getMappanel() .setPosition(Game.hero().getMap(), Game.hero().x, Game.hero().y); } invalidate(); validate(); setVisible(true); /* * CBG This is needed to give the focus to the contained screen. * RequestFocusInWindow is preferable to requestFocus. */ s.requestFocus(); mainComponent = s; }
@Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.d("egg", "Paused"); game.pause(); this.finish(); }
private void performMove(Coordinates touchCoordinates) { end = !game.move(touchCoordinates); GameCanvas.this.invalidate(); Toast.makeText( parent, String.format(parent.getString(R.string.game_screen_turn), game.getTurn()), Toast.LENGTH_SHORT) .show(); }
public enemy() { entRect = new Rectangle(); entRect.x = MathUtils.random(0, 854 - 48); entRect.y = 480; entRect.width = 48; entRect.height = 34; lastEnemyTime = TimeUtils.nanoTime(); if (MathUtils.random(1, 5) == 2) { GameScreen.addBullet(getX(), getY()); GameScreen.enemyFire.play(); } }
public void draw( final Ship ship, final StarManager starManager, final EnemyManager enemyManager, final PowerUpManager powerupManager, final ShotManager shotManager, final GamePhase phase, final GameController controller) { if (phase == GamePhase.GAMING) { shotManager.drawShape(shapeRenderer, ppux, ppuy, border); powerupManager.drawShape(shapeRenderer, ppux, ppuy, border); shapeRenderer.begin(ShapeType.FilledRectangle); enemyManager.drawHealthBar(shapeRenderer, ppux, ppuy, border); shapeRenderer.end(); } shapeRenderer.setColor(Color.WHITE); starManager.drawShape(shapeRenderer, ppux, ppuy, border); ship.drawShape(shapeRenderer, ppux, ppuy, border); batch.begin(); ship.drawSprite(batch, ppux, ppuy, border); switch (phase) { case LEVEL_LOAD: // just waiting for refresh case GAMING: shotManager.drawSprite(batch, ppux, ppuy, border); powerupManager.drawSprite(batch, ppux, ppuy, border); enemyManager.drawSprite(batch, ppux, ppuy, border); break; case PAUSE: drawPauseTextBox(); break; case LEVEL_SCORE: // TODO make counter and stuff and at the end change phase // score = GameScreen.updatePhase(GamePhase.LEVEL_WAIT); case LEVEL_WAIT: drawLevelScore(controller, controller.getTimeBonus()); break; case DEAD: // TODO death animation drawGameOver(); break; case FINISHED: drawGameFinished(); break; case GAMESTART: break; default: break; } batch.end(); }
@Override protected void drawComponentAppearing(Painter painter) { switch (appearSelector) { case FIRST_APPEARING: TextureQuad tex1 = TexturesLoader.getQuad( String.format(GameRessources.SPEC_PATH_TERRAIN, 1) + GameRessources.TEX_SOL); float colorUnit = (float) ((double) getAppearCount() / timeToAppear() * 0.5); Color4f color = new Color4f(colorUnit, colorUnit, colorUnit); Color4f buttonColor = new Color4f(colorUnit * 2, colorUnit * 2, colorUnit * 2); painter.setColor(color); tex1.fillScreen2D(0.5, 0.5, true, painter); drawTitleImage(painter, buttonColor); newPart.setColor(buttonColor); loadPart.setColor(buttonColor); break; default: super.drawComponentAppearing(painter); } }
/** * Pushes screen on the stack, its {@code show()} method is called. * * @param screen screen to be added */ public static void pushScreen(GameScreen screen) { screenStack.push(screen); screen.show(); }
private void checkGameState() { if (end) { if (musicEnabled) { parent.stopService(new Intent(parent, BackgroundMusic.class)); } Leaderboard leaderboard = Leaderboard.load(parent); String name = parent.getIntent().getExtras().getString("name"); switch (difficulty) { case EASY: leaderboard.addEasyRecord(name, game.getTurn()); break; case NORMAL: leaderboard.addNormalRecord(name, game.getTurn()); break; case HARD: leaderboard.addHardRecord(name, game.getTurn()); } leaderboard.save(parent); new AlertDialog.Builder(parent) .setMessage(parent.getString(R.string.game_screen_game_over)) .setPositiveButton( parent.getString(R.string.game_screen_button_return), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(parent); boolean soundEnabled = settings.getBoolean("pref_enable_sound", true); if (soundEnabled) { MediaPlayer player = MediaPlayer.create(parent, R.raw.press); player.setLooping(false); player.start(); } parent.finish(); parent.overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right); } }) .setNegativeButton( parent.getString(R.string.game_screen_button_restart), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(parent); boolean soundEnabled = settings.getBoolean("pref_enable_sound", true); if (soundEnabled) { MediaPlayer player = MediaPlayer.create(parent, R.raw.press); player.setLooping(false); player.start(); } game = new Game(difficulty); if (musicEnabled) { parent.startService(new Intent(parent, BackgroundMusic.class)); } invalidate(); } }) .show(); } }
@Override protected void onResume() { super.onResume(); game.setBackgroundDrawable(Theme.getBackground(getAssets())); }
public class Robot { // Constants are Here final int JUMPSPEED = -15; final int MOVESPEED = 5; private int centerX = 100; private int centerY = 377; private boolean jumped = false; private boolean movingLeft = false; private boolean movingRight = false; private boolean ducked = false; private boolean readyToFire = true; private int speedX = 0; private int speedY = 0; public static Rect rect = new Rect(0, 0, 0, 0); public static Rect rect2 = new Rect(0, 0, 0, 0); public static Rect rect3 = new Rect(0, 0, 0, 0); public static Rect rect4 = new Rect(0, 0, 0, 0); public static Rect yellowRed = new Rect(0, 0, 0, 0); public static Rect footleft = new Rect(0, 0, 0, 0); public static Rect footright = new Rect(0, 0, 0, 0); private Background bg1 = GameScreen.getBg1(); private Background bg2 = GameScreen.getBg2(); private ArrayList<Projectile> projectiles = new ArrayList<Projectile>(); public void update() { // Moves Character or Scrolls Background accordingly. if (speedX < 0) { centerX += speedX; } if (speedX == 0 || speedX < 0) { bg1.setSpeedX(0); bg2.setSpeedX(0); } if (centerX <= 200 && speedX > 0) { centerX += speedX; } if (speedX > 0 && centerX > 200) { bg1.setSpeedX(-MOVESPEED / 5); bg2.setSpeedX(-MOVESPEED / 5); } // Updates Y Position centerY += speedY; // Handles Jumping speedY += 1; if (speedY > 3) { jumped = true; } // Prevents going beyond X coordinate of 0 if (centerX + speedX <= 60) { centerX = 61; } rect.set(centerX - 34, centerY - 63, centerX + 34, centerY); rect2.set(rect.left, rect.top + 63, rect.left + 68, rect.top + 128); rect3.set(rect.left - 26, rect.top + 32, rect.left, rect.top + 52); rect4.set(rect.left + 68, rect.top + 32, rect.left + 94, rect.top + 52); yellowRed.set(centerX - 110, centerY - 110, centerX + 70, centerY + 70); footleft.set(centerX - 50, centerY + 20, centerX, centerY + 35); footright.set(centerX, centerY + 20, centerX + 50, centerY + 35); } public void moveRight() { if (ducked == false) { speedX = MOVESPEED; } } public void moveLeft() { if (ducked == false) { speedX = -MOVESPEED; } } public void stopRight() { setMovingRight(false); stop(); } public void stopLeft() { setMovingLeft(false); stop(); } private void stop() { if (isMovingRight() == false && isMovingLeft() == false) { speedX = 0; } if (isMovingRight() == false && isMovingLeft() == true) { moveLeft(); } if (isMovingRight() == true && isMovingLeft() == false) { moveRight(); } } public void jump() { if (jumped == false) { speedY = JUMPSPEED; jumped = true; } } public void shoot() { if (readyToFire) { Projectile p = new Projectile(centerX + 50, centerY - 25); projectiles.add(p); } } public int getCenterX() { return centerX; } public int getCenterY() { return centerY; } public boolean isJumped() { return jumped; } public int getSpeedX() { return speedX; } public int getSpeedY() { return speedY; } public void setCenterX(int centerX) { this.centerX = centerX; } public void setCenterY(int centerY) { this.centerY = centerY; } public void setJumped(boolean jumped) { this.jumped = jumped; } public void setSpeedX(int speedX) { this.speedX = speedX; } public void setSpeedY(int speedY) { this.speedY = speedY; } public boolean isDucked() { return ducked; } public void setDucked(boolean ducked) { this.ducked = ducked; } public boolean isMovingRight() { return movingRight; } public void setMovingRight(boolean movingRight) { this.movingRight = movingRight; } public boolean isMovingLeft() { return movingLeft; } public void setMovingLeft(boolean movingLeft) { this.movingLeft = movingLeft; } public ArrayList getProjectiles() { return projectiles; } public boolean isReadyToFire() { return readyToFire; } public void setReadyToFire(boolean readyToFire) { this.readyToFire = readyToFire; } }
/** * Removes and returns the topmost screen on the stack, its {@code hide()} method is called. * * @return the removed screen */ public static GameScreen popScreen() { GameScreen gs = screenStack.pop(); gs.hide(); return gs; }
public class Enemy { private int power, speedX, speedY, centerX, centerY; private Background bg = GameScreen.getBg1(); public Rect r = new Rect(0, 0, 0, 0); public int health = 5; private int movementSpeedX, movementSpeedY; public void update() { follow(); centerX += speedX; centerY += speedY; speedX = bg.getSpeedX() * 5 + movementSpeedX; speedY = movementSpeedY; r.set(centerX - 25, centerY - 25, 50, 60); if (r.intersects(r, Robot.yellowRed)) { checkCollision(); } } private void follow() { if (centerX < -95 || centerX > 810) { movementSpeedX = 0; } else if (Math.abs(Robot.getCenterX() - centerX) < 5) { movementSpeedX = 0; } else { if (Robot.getCenterX() >= centerX) { movementSpeedX = 1; } else { movementSpeedX = -1; } } if (centerY < 0 || centerY > 480) { movementSpeedY = 0; } else if (Math.abs(Robot.getCenterY() - centerY) < 5) { movementSpeedY = 0; } else { if (Robot.getCenterY() >= centerY) { movementSpeedY = 1; } else { movementSpeedY = -1; } } } private void checkCollision() { if (r.intersects(r, Robot.bodyUpper) || r.intersects(r, Robot.bodyLower) || r.intersects(r, Robot.armStageLeft) || r.intersects(r, Robot.armStageRight)) { System.out.println("collision"); } } public void die() {} public void attack() {} public int getPower() { return power; } public int getSpeedX() { return speedX; } public int getCenterX() { return centerX; } public int getCenterY() { return centerY; } public Background getBg() { return bg; } public void setPower(int power) { this.power = power; } public void setSpeedX(int speedX) { this.speedX = speedX; } public void setCenterX(int centerX) { this.centerX = centerX; } public void setCenterY(int centerY) { this.centerY = centerY; } public void setBg(Background bg) { this.bg = bg; } }
public void provideInitialScreen(GameScreen screen) { screen.registerVMHost(this); GameRegistry.instance().setScreen(screen); }