@Override public void onUpdate(long elapsedTime) { mScreenRect.set(0, 0, mGameView.getWidth(), mGameView.getHeight()); mLoops++; mTimer += elapsedTime; if (mTimer > mMaxTimer) { gameOver(); } mRobot.update(elapsedTime); // Check if robot escaped if (!RectF.intersects(mScreenRect, mRobot.getRect())) { // change to random positions mRobot.randomized(mScreenRect); // penalty mScore -= 50; if (mScore < 0) { mScore = 0; } } if (mRobot.isDead()) { if (mScreenRect.width() > 0 && mScreenRect.height() > 0) { mRobot.randomized(mScreenRect); } } }
// Returns true if the line segment defined by the given two points intersects the given // rectangle. public static boolean lineSegmentIntersectsRect(PointF p1, PointF p2, RectF r2) { RectF line = new RectF( Math.min(p1.x, p2.x), Math.min(p1.y, p2.y), Math.max(p1.x, p2.x), Math.max(p1.y, p2.y)); // If the bounding box of the line segment intersects the rectangle or one contains the other, // the line segment intersects the rectangle. return RectF.intersects(line, r2) || line.contains(r2) || r2.contains(line); }
boolean intersects(RectF tRect, float tRot, RectF sRect, float sRot) { if (Math.abs(tRot) < Math.PI / 15 && Math.abs(sRot) < Math.PI / 15) { return RectF.intersects(tRect, sRect); } float dist = FloatMath.sqrt( sqr(tRect.centerX() - sRect.centerX()) + sqr(tRect.centerY() - sRect.centerY())); if (dist < 3) { return true; } // difference close to 90/270 degrees if (Math.abs(Math.cos(tRot - sRot)) < 0.3) { // rotate one rectangle to 90 degrees tRot += Math.PI / 2; float l = tRect.centerX() - tRect.height() / 2; float t = tRect.centerY() - tRect.width() / 2; tRect = new RectF(l, t, l + tRect.height(), t + tRect.width()); } // determine difference close to 180/0 degrees if (Math.abs(FloatMath.sin(tRot - sRot)) < 0.3) { // rotate t box // (calculate offset for t center suppose we rotate around s center) float diff = (float) (-Math.atan2(tRect.centerX() - sRect.centerX(), tRect.centerY() - sRect.centerY()) + Math.PI / 2); diff -= sRot; float left = sRect.centerX() + dist * FloatMath.cos(diff) - tRect.width() / 2; float top = sRect.centerY() - dist * FloatMath.sin(diff) - tRect.height() / 2; RectF nRect = new RectF(left, top, left + tRect.width(), top + tRect.height()); return RectF.intersects(nRect, sRect); } // TODO other cases not covered return RectF.intersects(tRect, sRect); }
/** * Draws a text label. * * @param canvas the canvas * @param labelText the label text * @param renderer the renderer * @param prevLabelsBounds the previous rendered label bounds * @param centerX the round chart center on X axis * @param centerY the round chart center on Y axis * @param shortRadius the short radius for the round chart * @param longRadius the long radius for the round chart * @param currentAngle the current angle * @param angle the label extra angle * @param left the left side * @param right the right side * @param color the label color * @param paint the paint * @param line if a line to the label should be drawn */ protected void drawLabel( Canvas canvas, String labelText, DefaultRenderer renderer, List<RectF> prevLabelsBounds, int centerX, int centerY, float shortRadius, float longRadius, float currentAngle, float angle, int left, int right, int color, Paint paint, boolean line) { if (renderer.isShowLabels()) { paint.setColor(color); double rAngle = Math.toRadians(90 - (currentAngle + angle / 2)); double sinValue = Math.sin(rAngle); double cosValue = Math.cos(rAngle); int x1 = Math.round(centerX + (float) (shortRadius * sinValue)); int y1 = Math.round(centerY + (float) (shortRadius * cosValue)); int x2 = Math.round(centerX + (float) (longRadius * sinValue)); int y2 = Math.round(centerY + (float) (longRadius * cosValue)); float size = renderer.getLabelsTextSize(); float extra = Math.max(size / 2, 10); paint.setTextAlign(Align.LEFT); if (x1 > x2) { extra = -extra; paint.setTextAlign(Align.RIGHT); } float xLabel = x2 + extra; float yLabel = y2; float width = right - xLabel; if (x1 > x2) { width = xLabel - left; } labelText = getFitText(labelText, width, paint); float widthLabel = paint.measureText(labelText); boolean okBounds = false; while (!okBounds && line) { boolean intersects = false; int length = prevLabelsBounds.size(); for (int j = 0; j < length && !intersects; j++) { RectF prevLabelBounds = prevLabelsBounds.get(j); if (prevLabelBounds.intersects(xLabel, yLabel, xLabel + widthLabel, yLabel + size)) { intersects = true; yLabel = Math.max(yLabel, prevLabelBounds.bottom); } } okBounds = !intersects; } if (line) { y2 = (int) (yLabel - size / 2); canvas.drawLine(x1, y1, x2, y2, paint); canvas.drawLine(x2, y2, x2 + extra, y2, paint); } else { paint.setTextAlign(Align.CENTER); } canvas.drawText(labelText, xLabel, yLabel, paint); if (line) { prevLabelsBounds.add(new RectF(xLabel, yLabel, xLabel + widthLabel, yLabel + size)); } } }
private boolean isVisible() { return RectF.intersects(documentView.getViewRect(), getTargetRectF()); }
private void update() { // Did an invader bump into the side of the screen boolean bumped = false; // Has the player lost boolean lost = false; // Move the player's ship playerShip.update(fps); // Update the invaders if visible // Update the players bullet if (bullet.getStatus()) { bullet.update(fps); } // Update all the invaders bullets if active for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getStatus()) { invadersBullets[i].update(fps); } } // Update all the invaders if visible for (int i = 0; i < numInvaders; i++) { if (invaders[i].getVisibility()) { // Move the next invader invaders[i].update(fps); // Does he want to take a shot? if (invaders[i].takeAim(playerShip.getX(), playerShip.getLength())) { // If so try and spawn a bullet if (invadersBullets[nextBullet].shoot( invaders[i].getX() + invaders[i].getLength() / 2, invaders[i].getY(), bullet.DOWN)) { // Shot fired // Prepare for the next shot nextBullet++; // Loop back to the first one if we have reached the last if (nextBullet == maxInvaderBullets) { // This stops the firing of another bullet until one completes its journey // Because if bullet 0 is still active shoot returns false. nextBullet = 0; } } } // If that move caused them to bump the screen change bumped to true if (invaders[i].getX() > screenX - invaders[i].getLength() || invaders[i].getX() < 0) { bumped = true; } } } // Did an invader bump into the edge of the screen if (bumped) { // Move all the invaders down and change direction for (int i = 0; i < numInvaders; i++) { invaders[i].dropDownAndReverse(); // Have the invaders landed if (invaders[i].getY() > screenY - screenY / 10) { lost = true; } } // Increase the menace level // By making the sounds more frequent menaceInterval = menaceInterval - 80; } if (lost) { prepareLevel(); } // Has the player's bullet hit the top of the screen if (bullet.getImpactPointY() < 0) { bullet.setInactive(); } // Has an invaders bullet hit the bottom of the screen for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getImpactPointY() > screenY) { invadersBullets[i].setInactive(); } } // Has the player's bullet hit an invader if (bullet.getStatus()) { for (int i = 0; i < numInvaders; i++) { if (invaders[i].getVisibility()) { if (RectF.intersects(bullet.getRect(), invaders[i].getRect())) { invaders[i].setInvisible(); soundPool.play(invaderExplodeID, 1, 1, 0, 0, 1); bullet.setInactive(); score = score + 10; // Has the player won if (score == numInvaders * 10) { paused = true; String mScore = score + ""; Log.d("Score: ", mScore); Context context = getContext(); Intent intent = new Intent(context, ScoreActivity.class); intent.putExtra("score", mScore); context.startActivity(intent); } } } } } // Has an alien bullet hit a shelter brick for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getStatus()) { for (int j = 0; j < numBricks; j++) { if (bricks[j].getVisibility()) { if (RectF.intersects(invadersBullets[i].getRect(), bricks[j].getRect())) { // A collision has occurred invadersBullets[i].setInactive(); bricks[j].setInvisible(); soundPool.play(damageShelterID, 1, 1, 0, 0, 1); } } } } } // Has a player bullet hit a shelter brick if (bullet.getStatus()) { for (int i = 0; i < numBricks; i++) { if (bricks[i].getVisibility()) { if (RectF.intersects(bullet.getRect(), bricks[i].getRect())) { // A collision has occurred bullet.setInactive(); bricks[i].setInvisible(); soundPool.play(damageShelterID, 1, 1, 0, 0, 1); } } } } // Has an invader bullet hit the player ship for (int i = 0; i < invadersBullets.length; i++) { if (invadersBullets[i].getStatus()) { if (RectF.intersects(playerShip.getRect(), invadersBullets[i].getRect())) { invadersBullets[i].setInactive(); lives--; soundPool.play(playerExplodeID, 1, 1, 0, 0, 1); // Is it game over? if (lives == 0) { paused = true; // lives = 3; // score = 0; String mScore = score + ""; Context context = getContext(); Intent intent = new Intent(context, ScoreActivity.class); intent.putExtra("score", mScore); context.startActivity(intent); // prepareLevel(); } } } } }