Esempio n. 1
0
 /**
  * Sets the data
  *
  * @param buffer Bytebuffer to put data into
  */
 public void get(ByteBuffer buffer) {
   buffer.putInt((int) world.x);
   buffer.putInt((int) world.y);
   buffer.putDouble(radians);
   buffer.put((byte) (moving ? 1 : 0));
   for (Bullet b : gun.getBullets()) {
     if (b != null) {
       buffer.putInt((int) b.getWorld().x);
       buffer.putInt((int) b.getWorld().y);
       buffer.putDouble(b.getAngle());
     } else {
       buffer.putInt(-1);
       buffer.putInt(-1);
       buffer.putDouble(-1);
     }
   }
 }
Esempio n. 2
0
    @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;
    }