/** * Game logic to update the game-state. * * @param rect Drawable area. * @param delay Time delay since last update. */ public boolean update(Rect rect, int delay) { // Check that the drawing rectangle is valid if (rect.width() <= 0 || rect.width() <= 0) { return true; } if (!gameOver()) { for (Character o : obstacles) { o.update(rect, this); if (car.contains(o.s.x, o.s.y)) { if (o.type.equals(ObstacleType.BAD)) { gameOver = true; } else if (o.type.equals(ObstacleType.GOOD)) { score += 1; o.delete(this); } } } timeElapsed += delay; if (timeElapsed >= Constants.delayBetweenCars) { addRandomObstacle(); timeElapsed = 0; } updateCarLane(); return true; } else { return false; } }
public void update() { super.update(); if (currentState == STATE.ATTACKING) { attackEnemy(); } else { checkForEnemies(); } if (currentState == STATE.DEFENDING) {} }
public void update(long timePassed) { super.update(timePassed); tryKill(); }
public boolean Battle() throws IOException { player.update(); boolean alive = true; int health; flee = false; P_HEALTH = player.getHealth(); PC_HEALTH = player.getCHealth(); plevel = player.getLevel(); elevel = elgen.nextInt(2) + (plevel - 1); if (elevel != -1) { health = 70 + (elevel) * 30; } else { health = 70; elevel = 0; } E_HEALTH = health; EC_HEALTH = health; H.pln("You have encountered a level " + elevel + " enemy"); while (EC_HEALTH > 0 && PC_HEALTH > 0 && !flee) { engine = new Random(); H.pln("Your Health: " + PC_HEALTH + "/" + P_HEALTH); H.pln("Enemy's Health : " + EC_HEALTH + "/" + E_HEALTH); H.pln("What do you want to do?"); H.pln("(1)Attack, (2)Defend, (3)Flee"); choice = H.inputInt(); switch (choice) { case 1: if (engine.nextInt(99) + 1 <= 95) { damage = (int) ((engine.nextInt(P_MAX_DAMAGE[plevel])) * wD[player.getWeapon()]); if (damage <= EC_HEALTH) { H.pln("Enemy lost " + damage + " health!"); EC_HEALTH -= damage; } else { helper = EC_HEALTH; H.pln("Enemy lost " + helper + " health!"); EC_HEALTH -= damage; } } else { if (plevel <= 8) { damage = (int) (engine.nextInt(P_MAX_DAMAGE[plevel + 2]) * wD[player.getWeapon()]); H.pln("Critical Hit!"); if (damage <= EC_HEALTH) { H.pln("Enemy lost " + damage + " health!"); EC_HEALTH -= damage; } else { helper = EC_HEALTH; H.pln("Enemy lost " + helper + " health!"); EC_HEALTH = 0; } } else { if (plevel == 9) { damage = (int) (engine.nextInt(P_MAX_DAMAGE[plevel + 1]) * wD[player.getWeapon()]); H.pln("Critical Hit!"); if (damage <= EC_HEALTH) { H.pln("Enemy lost " + damage + " health!"); EC_HEALTH -= damage; } else { helper = EC_HEALTH; H.pln("Enemy lost " + helper + " health!"); EC_HEALTH -= damage; } } if (plevel == 10) { damage = EC_HEALTH; H.pln("You destroyed the Enemy with one blow!!!"); } } } damage = engine.nextInt(E_MAX_DAMAGE[elevel]) + 1; H.pln("You lost " + damage + " health"); PC_HEALTH -= damage; player.setCHealth(PC_HEALTH); if (player.getCHealth() <= 0) return false; break; case 2: H.pln("You have blocked the enemy attack!"); break; case 3: H.pln("You have fled"); flee = true; break; } } if (EC_HEALTH <= 0 && PC_HEALTH > 0) { player.addXP(EC_HEALTH * (-1)); H.pln("You gained " + String.valueOf(EC_HEALTH * (-1)) + " XP!"); } if (flee) { player.addXP((EC_HEALTH) * (-1)); H.pln("You lost " + EC_HEALTH + " XP!"); } player.saveAll(); return true; }
@Override public void update() { super.update(); if (this.getHp() <= 0) updateDeath(); }
@Override public synchronized void onDraw(Canvas canvas) { long nt = SystemClock.elapsedRealtime(); if (canvas == null) return; if (t < 0) t = nt; screenW = canvas.getWidth(); screenH = canvas.getHeight(); if (berzerk && nt - berzerkStart > berzerkLength) // Check if still in berzerk mode berzerk = false; if (mc == null) mc = new Character(screenW / 2, screenH - 30, screenW, screenH); if (!over) { // add Enemies if (firing && nt - g >= 150) { // Add bullets bullets.add(new Bullet(_x, _y, mc.getx(), mc.gety(), screenH, screenW)); g = nt; } // Adds a new enemy from the specified sets depending on the level reached if (nt - f >= 1000) { if (level < 2) { squares.add(new BoxParticle(r, screenH, screenW, mc.getx(), mc.gety())); } else if (level < 3) { parabolics.add(new ParabolicParticle(r, screenH, screenW, mc.getx(), mc.gety())); turns.add(new TurningParticle(r, screenH, screenW, mc.getx(), mc.gety())); } else { turns.add(new TurningParticle(r, screenH, screenW, mc.getx(), mc.gety())); squares.add(new BoxParticle(r, screenH, screenW, mc.getx(), mc.gety())); parabolics.add(new ParabolicParticle(r, screenH, screenW, mc.getx(), mc.gety())); homings.add(new HomingParticle(r, 20, 15, screenH, screenW, mc.getx(), mc.gety())); } f = nt; } // stop add Enemies rms.clear(); rms2.clear(); rms3.clear(); rms4.clear(); rms5.clear(); // Garbage Collection // Onscreen checking for (Bullet i : bullets) if (!i.onscreen()) rms.add(i); for (BoxParticle i : squares) if (!i.onscreen()) rms2.add(i); for (ParabolicParticle i : parabolics) if (!i.onscreen()) rms3.add(i); for (TurningParticle i : turns) if (!i.onscreen()) rms4.add(i); for (HomingParticle i : homings) if (!i.onscreen()) rms5.add(i); bullets.removeAll(rms); squares.removeAll(rms2); parabolics.removeAll(rms3); turns.removeAll(rms4); homings.removeAll(rms5); rms.clear(); rms2.clear(); rms3.clear(); rms4.clear(); rms5.clear(); // Garbage collection // end Onscreen checking // Enemy collision with bullets for (Bullet i : bullets) { for (BoxParticle j : squares) { if (sq(i.getx() - j.getx()) + sq(i.gety() - j.gety()) <= sq(i.getRadius() + j.getRadius())) { rms.add(i); // Add to garbage collection set rms2.add(j); if (r.nextInt(20) == 0) berzerkUps.add(new BerzerkUp(j.getx(), j.gety())); // Give chance of berzerk powerup score++; } } for (ParabolicParticle j : parabolics) { if (sq(i.getx() - j.getx()) + sq(i.gety() - j.gety()) <= sq(i.getRadius() + j.getRadius())) { rms.add(i); rms3.add(j); if (r.nextInt(20) == 0) berzerkUps.add(new BerzerkUp(j.getx(), j.gety())); score++; } } for (TurningParticle j : turns) { if (sq(i.getx() - j.getx()) + sq(i.gety() - j.gety()) <= sq(i.getRadius() + j.getRadius())) { rms.add(i); rms4.add(j); if (r.nextInt(20) == 0) berzerkUps.add(new BerzerkUp(j.getx(), j.gety())); score++; } } for (HomingParticle j : homings) { if (sq(i.getx() - j.getx()) + sq(i.gety() - j.gety()) <= sq(i.getRadius() + j.getRadius())) { rms.add(i); rms5.add(j); if (r.nextInt(20) == 0) berzerkUps.add(new BerzerkUp(j.getx(), j.gety())); score++; } } } squares.removeAll(rms2); parabolics.removeAll(rms3); turns.removeAll(rms4); homings.removeAll(rms5); bullets.removeAll(rms); rms.clear(); rms2.clear(); rms3.clear(); rms4.clear(); rms5.clear(); // Garbage collection // end Enemy collision with bullets // Enemy collision with character // one cannot die in berzerk mode for (BoxParticle j : squares) // Collision detection if (sq(mc.getx() - j.getx()) + sq(mc.gety() - j.gety()) <= sq(mc.getRadius() + j.getRadius())) { if (berzerk) { rms2.add(j); score++; } else over = true; // Game over if collision } for (ParabolicParticle j : parabolics) if (sq(mc.getx() - j.getx()) + sq(mc.gety() - j.gety()) <= sq(mc.getRadius() + j.getRadius())) { if (berzerk) { rms3.add(j); score++; } else over = true; } for (TurningParticle j : turns) if (sq(mc.getx() - j.getx()) + sq(mc.gety() - j.gety()) <= sq(mc.getRadius() + j.getRadius())) { if (berzerk) { rms4.add(j); score++; } else over = true; } for (HomingParticle j : homings) if (sq(mc.getx() - j.getx()) + sq(mc.gety() - j.gety()) <= sq(mc.getRadius() + j.getRadius())) { if (berzerk) { rms5.add(j); score++; } else over = true; } if (berzerk) { // Garbage collection squares.removeAll(rms2); parabolics.removeAll(rms3); turns.removeAll(rms4); homings.removeAll(rms5); over = false; } // end Enemy collision with character if (over) { berzerk = false; stop(); } // Berzerk powerup collision if (!berzerk) { for (BerzerkUp i : berzerkUps) if (sq(mc.getx() - i.getx()) + sq(mc.gety() - i.gety()) <= sq(mc.getRadius() + i.getRadius())) { berzerk = true; berzerkStart = SystemClock.elapsedRealtime(); rmsBU.add(i); } } berzerkUps.removeAll(rmsBU); rmsBU.clear(); // end Berzerk powerup collision } // Berzerk events if (berzerk && !over) { // Berzerk paints bitmapPaint = invert; textPaint = blackTextPaint; canvas.drawColor(Color.WHITE); } else { // Normal paints bitmapPaint = aA; textPaint = whiteTextPaint; canvas.drawColor(Color.BLACK); } // end Berzerk events if (!over) { // Rendering particles for (BoxParticle i : squares) { i.update(((double) (nt - t)) / 1000); // Translate to position with rotation matrix.setRotate(i.getAngle(), squareW / 2, squareH / 2); matrix.postTranslate(i.getx() - squareW / 2, i.gety() - squareH / 2); canvas.drawBitmap(squareBitmap, matrix, bitmapPaint); } for (ParabolicParticle i : parabolics) { i.update(((double) (nt - t)) / 1000); matrix.setRotate(i.getAngle(), parabolicW / 2, parabolicH / 2); matrix.postTranslate(i.getx() - parabolicW / 2, i.gety() - parabolicH / 2); canvas.drawBitmap(parabolicBitmap, matrix, bitmapPaint); } for (TurningParticle i : turns) { i.update(((double) (nt - t)) / 1000); matrix.setRotate(i.getAngle(), turningW / 2, turningH / 2); matrix.postTranslate(i.getx() - turningW / 2, i.gety() - turningH / 2); canvas.drawBitmap(turningBitmap, matrix, bitmapPaint); } for (HomingParticle i : homings) { i.update(((double) (nt - t)) / 1000); matrix.setRotate(i.getAngle(), homingW / 2, homingH / 2); matrix.postTranslate(i.getx() - homingW / 2, i.gety() - homingH / 2); canvas.drawBitmap(homingBitmap, matrix, bitmapPaint); } for (Bullet i : bullets) { i.update(((double) (nt - t)) / 1000); matrix.setRotate(i.getAngle(), bulletW / 2, bulletH / 2); matrix.postTranslate(i.getx() - bulletW / 2, i.gety() - bulletH / 2); canvas.drawBitmap(bulletBitmap, matrix, bitmapPaint); } for (BerzerkUp i : berzerkUps) { canvas.drawBitmap( berzerkBitmap, i.getx() - berzerkW / 2, i.gety() - berzerkH / 2, bitmapPaint); } mc.update(((double) (nt - t)) / 1000); for (HomingParticle i : homings) { i.updateMC(mc.getx(), mc.gety()); } matrix.setRotate(mc.getAngle(), mcW / 2, mcH / 2); matrix.postTranslate(mc.getx() - mcW / 2, mc.gety() - mcH / 2); canvas.drawBitmap(mcBitmap, matrix, bitmapPaint); // end Rendering particles // leveling if (score >= 50) level = 3; else if (score >= 25) level = 2; } // end leveling // Text rendering canvas.drawText("Score: " + score, 50, 25, textPaint); canvas.drawText("Level: " + level, screenW - 100, 25, textPaint); if (over) { canvas.drawText( OVER_STR, (screenW - largeTextPaint.measureText(OVER_STR)) / 2, screenH / 2 - 12, largeTextPaint); canvas.drawText( OVER_STR_2, (screenW - largeTextPaint.measureText(OVER_STR_2)) / 2, screenH / 2 + 16, largeTextPaint); } else if (berzerk) { canvas.drawText( BERZERK_STR, (screenW - redTextPaint.measureText(BERZERK_STR)) / 2, 25, redTextPaint); String timeStr = String.format("%.1f seconds left", ((double) berzerkLength - nt + berzerkStart) / 1000); if (nt - berzerkStart < 8000 || ((nt - berzerkStart) / 200) % 2 == 0) { canvas.drawText( timeStr, (screenW - redTextPaint.measureText(timeStr)) / 2, 40, redTextPaint); } } // end Text rendering t = nt; }