@Override public Scene onCreateScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); final Scene scene = new Scene(); scene.setBackground(new Background(0, 0, 0)); for (int i = 0; i < COUNT; i++) { final float tension = MathUtils.random(-0.5f, 0.5f); this.addRectangleWithTension(scene, tension, MathUtils.random(0, DURATION * 2f)); } return scene; }
@Override protected void onUpdateControlKnob(final float pRelativeX, final float pRelativeY) { if (pRelativeX == 0 && pRelativeY == 0) { super.onUpdateControlKnob(0, 0); return; } if (this.mAllowDiagonal) { final float angle = MathUtils.radToDeg(MathUtils.atan2(pRelativeY, pRelativeX)) + 180; if (DigitalOnScreenControl.testDiagonalAngle(0, angle) || DigitalOnScreenControl.testDiagonalAngle(360, angle)) { super.onUpdateControlKnob(-EXTENT_SIDE, 0); } else if (DigitalOnScreenControl.testDiagonalAngle(45, angle)) { super.onUpdateControlKnob(-EXTENT_DIAGONAL, -EXTENT_DIAGONAL); } else if (DigitalOnScreenControl.testDiagonalAngle(90, angle)) { super.onUpdateControlKnob(0, -EXTENT_SIDE); } else if (DigitalOnScreenControl.testDiagonalAngle(135, angle)) { super.onUpdateControlKnob(EXTENT_DIAGONAL, -EXTENT_DIAGONAL); } else if (DigitalOnScreenControl.testDiagonalAngle(180, angle)) { super.onUpdateControlKnob(EXTENT_SIDE, 0); } else if (DigitalOnScreenControl.testDiagonalAngle(225, angle)) { super.onUpdateControlKnob(EXTENT_DIAGONAL, EXTENT_DIAGONAL); } else if (DigitalOnScreenControl.testDiagonalAngle(270, angle)) { super.onUpdateControlKnob(0, EXTENT_SIDE); } else if (DigitalOnScreenControl.testDiagonalAngle(315, angle)) { super.onUpdateControlKnob(-EXTENT_DIAGONAL, EXTENT_DIAGONAL); } else { super.onUpdateControlKnob(0, 0); } } else { if (Math.abs(pRelativeX) > Math.abs(pRelativeY)) { if (pRelativeX > 0) { super.onUpdateControlKnob(EXTENT_SIDE, 0); } else if (pRelativeX < 0) { super.onUpdateControlKnob(-EXTENT_SIDE, 0); } else if (pRelativeX == 0) { super.onUpdateControlKnob(0, 0); } } else { if (pRelativeY > 0) { super.onUpdateControlKnob(0, EXTENT_SIDE); } else if (pRelativeY < 0) { super.onUpdateControlKnob(0, -EXTENT_SIDE); } else if (pRelativeY == 0) { super.onUpdateControlKnob(0, 0); } } } }
/** Each tower fires at the first enemy in their respective queue Runs every .025 seconds */ private void collisionDetect() { for (ITower t : towers) { Enemy enemy = t.getLockedOn(); if (enemy == null) { t.onIdleInWave(); continue; } if (enemy.isDead()) { t.removeEnemyFromQueue(enemy); continue; } if (t.inSights(enemy.getXReal(), enemy.getYReal())) { t.onImpact(enemy); t.shoot(enemy); /** TODO: Make a "canRotate" variable for each class so this isn't such a hack */ if (t.getClass() == IceTower.class || t.getClass() == SpikeTower.class) continue; dx = t.getEntity().getX() - (enemy.getX() - enemy.getWidthScaled() / 3.5f); dy = t.getEntity().getY() - (enemy.getY() - enemy.getHeightScaled() / 3.5f); angle = MathUtils.atan2(dy, dx); realAngle = (float) (angle * (180.0f / Math.PI)); t.getEntity().setRotation(realAngle); } else { t.removeEnemyFromQueue(enemy); t.onEnemyOutOfRange(enemy); } } }
@Override protected void draw(final GLState pGLState, final Camera pCamera) { final int tileColumns = this.mTileColumns; final int tileRows = this.mTileRows; final int tileWidth = this.mTMXTiledMap.getTileWidth(); final int tileHeight = this.mTMXTiledMap.getTileHeight(); final float scaledTileWidth = tileWidth * this.mScaleX; final float scaledTileHeight = tileHeight * this.mScaleY; final float[] cullingVertices = this.mCullingVertices; RectangularShapeCollisionChecker.fillVertices( 0, 0, this.mWidth, this.mHeight, this.getLocalToSceneTransformation(), cullingVertices); final float layerMinX = cullingVertices[SpriteBatch.VERTEX_INDEX_X]; final float layerMinY = cullingVertices[SpriteBatch.VERTEX_INDEX_Y]; final float cameraMinX = pCamera.getXMin(); final float cameraMinY = pCamera.getYMin(); final float cameraWidth = pCamera.getWidth(); final float cameraHeight = pCamera.getHeight(); /* Determine the area that is visible in the camera. */ final float firstColumnRaw = (cameraMinX - layerMinX) / scaledTileWidth; final int firstColumn = MathUtils.bringToBounds(0, tileColumns - 1, (int) Math.floor(firstColumnRaw)); final int lastColumn = MathUtils.bringToBounds( 0, tileColumns - 1, (int) Math.ceil(firstColumnRaw + cameraWidth / scaledTileWidth)); final float firstRowRaw = (cameraMinY - layerMinY) / scaledTileHeight; final int firstRow = MathUtils.bringToBounds(0, tileRows - 1, (int) Math.floor(firstRowRaw)); final int lastRow = MathUtils.bringToBounds( 0, tileRows - 1, (int) Math.floor(firstRowRaw + cameraHeight / scaledTileHeight)); for (int row = firstRow; row <= lastRow; row++) { for (int column = firstColumn; column <= lastColumn; column++) { this.mSpriteBatchVertexBufferObject.draw( GLES20.GL_TRIANGLE_STRIP, this.getSpriteBatchIndex(column, row) * SpriteBatch.VERTICES_PER_SPRITE, SpriteBatch.VERTICES_PER_SPRITE); } } }
@Override public void tact(long now, long period) { final float nextStep = MathUtils.distance(mPoint.x, mPoint.y, mNextPoint.x, mNextPoint.y); if (nextStep > 10) { float distance = (float) period / 1000 * mSpeed; float m = nextStep - distance; float x = (m * mPoint.x + distance * mNextPoint.x) / nextStep; float y = (m * mPoint.y + distance * mNextPoint.y) / nextStep; mPoint.x = x; mPoint.y = y; } mSprite.setPosition(mPoint.x - mPointOffset.x, mPoint.y - mPointOffset.y); mSpriteShadow.setPosition(mSprite.getX() + mPointShadow.x, mSprite.getY() + mPointShadow.y); }
/** * @param pGLState * @throws IOException */ @Override protected void writeTextureToHardware(final GLState pGLState) throws IOException { final IPVRTexturePixelBufferStrategyBufferManager pvrTextureLoadStrategyManager = this.mPVRTexturePixelBufferStrategy.newPVRTexturePixelBufferStrategyManager(this); int width = this.getWidth(); int height = this.getHeight(); final int dataLength = this.mPVRTextureHeader.getDataLength(); final int bytesPerPixel = this.mPVRTextureHeader.getBitsPerPixel() / DataConstants.BITS_PER_BYTE; final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(width) && MathUtils.isPowerOfTwo(height) && this.mPVRTextureHeader.mPVRTextureFormat == PVRTextureFormat.RGBA_8888; if (!useDefaultAlignment) { /* Adjust unpack alignment. */ GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1); } int currentLevel = 0; int currentPixelDataOffset = 0; while (currentPixelDataOffset < dataLength) { if (currentLevel > 0 && (width != height || MathUtils.nextPowerOfTwo(width) != width)) { Debug.w( "Mipmap level '" + currentLevel + "' is not squared. Width: '" + width + "', height: '" + height + "'. Texture won't render correctly."); } final int currentPixelDataSize = height * width * bytesPerPixel; /* Load the current level. */ this.mPVRTexturePixelBufferStrategy.loadPVRTextureData( pvrTextureLoadStrategyManager, width, height, bytesPerPixel, this.mPixelFormat, currentLevel, currentPixelDataOffset, currentPixelDataSize); currentPixelDataOffset += currentPixelDataSize; /* Prepare next mipmap level. */ width = Math.max(width / 2, 1); height = Math.max(height / 2, 1); currentLevel++; } if (!useDefaultAlignment) { /* Restore default unpack alignment. */ GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT); } }
/** Calculate the euclidian distance between the first two fingers. */ private static float calculatePointerDistance(final MotionEvent pMotionEvent) { return MathUtils.distance( pMotionEvent.getX(0), pMotionEvent.getY(0), pMotionEvent.getX(1), pMotionEvent.getY(1)); }
@Override public void getPositionOffset(final float[] pOffset) { pOffset[VERTEX_INDEX_X] = this.mCenterX + MathUtils.randomSign() * this.mWidthHalf; pOffset[VERTEX_INDEX_Y] = this.mCenterY + MathUtils.randomSign() * this.mHeightHalf; }
@Override protected void onManagedDraw(final GLState pGLState, final Camera pCamera) { if (this.mClippingEnabled) { /* Enable scissor test, while remembering previous state. */ final boolean wasScissorTestEnabled = pGLState.enableScissorTest(); final int surfaceHeight = pCamera.getSurfaceHeight(); /* In order to apply clipping, we need to determine the the axis aligned bounds in OpenGL coordinates. */ /* Determine clipping coordinates of each corner in surface coordinates. */ final float[] lowerLeftSurfaceCoordinates = pCamera.getSurfaceCoordinatesFromSceneCoordinates( this.convertLocalCoordinatesToSceneCoordinates(0, 0)); final int lowerLeftX = (int) Math.round(lowerLeftSurfaceCoordinates[Constants.VERTEX_INDEX_X]); final int lowerLeftY = surfaceHeight - (int) Math.round(lowerLeftSurfaceCoordinates[Constants.VERTEX_INDEX_Y]); final float[] upperLeftSurfaceCoordinates = pCamera.getSurfaceCoordinatesFromSceneCoordinates( this.convertLocalCoordinatesToSceneCoordinates(0, this.mHeight)); final int upperLeftX = (int) Math.round(upperLeftSurfaceCoordinates[Constants.VERTEX_INDEX_X]); final int upperLeftY = surfaceHeight - (int) Math.round(upperLeftSurfaceCoordinates[Constants.VERTEX_INDEX_Y]); final float[] upperRightSurfaceCoordinates = pCamera.getSurfaceCoordinatesFromSceneCoordinates( this.convertLocalCoordinatesToSceneCoordinates(this.mWidth, this.mHeight)); final int upperRightX = (int) Math.round(upperRightSurfaceCoordinates[Constants.VERTEX_INDEX_X]); final int upperRightY = surfaceHeight - (int) Math.round(upperRightSurfaceCoordinates[Constants.VERTEX_INDEX_Y]); final float[] lowerRightSurfaceCoordinates = pCamera.getSurfaceCoordinatesFromSceneCoordinates( this.convertLocalCoordinatesToSceneCoordinates(this.mWidth, 0)); final int lowerRightX = (int) Math.round(lowerRightSurfaceCoordinates[Constants.VERTEX_INDEX_X]); final int lowerRightY = surfaceHeight - (int) Math.round(lowerRightSurfaceCoordinates[Constants.VERTEX_INDEX_Y]); /* Determine minimum and maximum x clipping coordinates. */ final int minClippingX = MathUtils.min(lowerLeftX, upperLeftX, upperRightX, lowerRightX); final int maxClippingX = MathUtils.max(lowerLeftX, upperLeftX, upperRightX, lowerRightX); /* Determine minimum and maximum y clipping coordinates. */ final int minClippingY = MathUtils.min(lowerLeftY, upperLeftY, upperRightY, lowerRightY); final int maxClippingY = MathUtils.max(lowerLeftY, upperLeftY, upperRightY, lowerRightY); /* Determine clipping width and height. */ final int clippingWidth = maxClippingX - minClippingX; final int clippingHeight = maxClippingY - minClippingY; /* Finally apply the clipping. */ pGLState.glPushScissor(minClippingX, minClippingY, clippingWidth, clippingHeight); /* Draw children, etc... */ super.onManagedDraw(pGLState, pCamera); /* Revert scissor test to previous state. */ pGLState.glPopScissor(); pGLState.setScissorTestEnabled(wasScissorTestEnabled); } else { super.onManagedDraw(pGLState, pCamera); } }
public static final <T> T random(final List<T> pList) { return pList.get(MathUtils.random(0, pList.size() - 1)); }
@Override protected Bird onAllocatePoolItem() { Log.e("GAME", "NewSmallLeft"); return new SimpleBirdL( background.getWidth(), MathUtils.random(0, 200), BirdType.SIMPLE_SMALL_LEFT); }
@Override protected Bird onAllocatePoolItem() { Log.e("GAME", "NewSmallRight"); return new SimpleBirdR( -BirdsSizes.smallBirdWidth, MathUtils.random(0, 200), BirdType.SIMPLE_SMALL_RIGHT); }
@Override protected void onManagedUpdate(float pSecondsElapsed) { if (score > record) record = score; FPScounter++; if (FPScounter == FPS) { FPScounter = 0; timeLeft--; timeText.setText(String.valueOf(timeLeft)); FPScounter = 0; } if (timeLeft == 0) this.onBackKeyPressed(); int bigBird = MathUtils.random(0, 800); int smallBird = MathUtils.random(0, 1000); for (int i = 0; i < plumageArray.size(); i++) { plumageArray.get(i).lifetime -= pSecondsElapsed; if (plumageArray.get(i).lifetime <= 0) { plumageArray.get(i).detachSelf(); plumageArray.get(i).dispose(); plumageArray.remove(plumageArray.get(i)); } } if (birdArray.size() < maxBirdCount) { if (bigBird == 1 || bigBird == 2) { Log.w("GAME", "addBigRight"); birdArray.add( GetNewtBird( bigRightBirdPool, -BirdsSizes.bigBirdWidth, MathUtils.random(0, 200), BirdsSizes.bigBirdDuration)); getChildByIndex(0).attachChild(birdArray.getLast()); } else if (bigBird == 3 || bigBird == 4) { Log.w("GAME", "addBigLeft"); birdArray.add( GetNewtBird( bigLeftBirdPool, background.getWidth(), MathUtils.random(0, 200), BirdsSizes.bigBirdDuration)); getChildByIndex(0).attachChild(birdArray.getLast()); } if (smallBird == 1 || smallBird == 2) { Log.w("GAME", "addSmallRight"); birdArray.add( GetNewtBird( smallRightBirdPool, -BirdsSizes.smallBirdWidth, MathUtils.random(0, 200), BirdsSizes.smallBirdDuration)); getChildByIndex(0).attachChild(birdArray.getLast()); } else if (smallBird == 3 || smallBird == 4) { Log.w("GAME", "addSmallLeft"); birdArray.add( GetNewtBird( smallLeftBirdPool, background.getWidth(), MathUtils.random(0, 200), BirdsSizes.smallBirdDuration)); getChildByIndex(0).attachChild(birdArray.getLast()); } } for (int i = 0; i < birdArray.size(); i++) { if ((birdArray.get(i).getX() >= background.getWidth() && birdArray.get(i).currentDirection == DIRECTIONS.RIGHT) || (birdArray.get(i).getX() <= -BirdsSizes.bigBirdWidth && birdArray.get(i).currentType == BirdType.SIMPLE_BIG_LEFT)) { AddToPool(i); } else if ((birdArray.get(i).getX() <= -BirdsSizes.smallBirdWidth && birdArray.get(i).currentType == BirdType.SIMPLE_SMALL_LEFT)) { AddToPool(i); } } super.onManagedUpdate(pSecondsElapsed); }
@Override protected void writeTextureToHardware(final GLState pGLState) { final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat(); final int glInternalFormat = pixelFormat.getGLInternalFormat(); final int glFormat = pixelFormat.getGLFormat(); final int glType = pixelFormat.getGLType(); GLES20.glTexImage2D( GLES20.GL_TEXTURE_2D, 0, glInternalFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null); final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha; /* Non alpha premultiplied bitmaps are loaded with ARGB_8888 and converted down manually. */ final Config bitmapConfig = (preMultipyAlpha) ? this.mBitmapTextureFormat.getBitmapConfig() : Config.ARGB_8888; final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources; final int textureSourceCount = textureSources.size(); final ITextureAtlasStateListener<IBitmapTextureAtlasSource> textureStateListener = this.getTextureAtlasStateListener(); for (int i = 0; i < textureSourceCount; i++) { final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(i); try { final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig); if (bitmap == null) { throw new NullBitmapException( "Caused by: " + bitmapTextureAtlasSource.getClass().toString() + " --> " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap."); } final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && pixelFormat == PixelFormat.RGBA_8888; if (!useDefaultAlignment) { /* Adjust unpack alignment. */ GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1); } if (preMultipyAlpha) { GLUtils.texSubImage2D( GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, glFormat, glType); } else { pGLState.glTexSubImage2D( GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, this.mPixelFormat); } if (!useDefaultAlignment) { /* Restore default unpack alignment. */ GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT); } bitmap.recycle(); if (textureStateListener != null) { textureStateListener.onTextureAtlasSourceLoaded(this, bitmapTextureAtlasSource); } } catch (final NullBitmapException e) { if (textureStateListener != null) { textureStateListener.onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, e); } else { throw e; } } } }
@Override public void onLoadScene() { // Load the menu resources ResourceManager.loadMenuResources(); // Create the background BackgroundSprite = new Sprite( ResourceManager.getInstance().cameraWidth / 2f, ResourceManager.getInstance().cameraHeight / 2f, ResourceManager.menuBackgroundTextureRegion, ResourceManager.getInstance().engine.getVertexBufferObjectManager()); BackgroundSprite.setScaleX(ResourceManager.getInstance().cameraWidth); BackgroundSprite.setScaleY(ResourceManager.getInstance().cameraHeight / 480f); BackgroundSprite.setZIndex(-5000); this.attachChild(BackgroundSprite); // Create clouds that move from one side of the screen to the other, and repeat. CloudSprites = new Sprite[20]; for (Sprite curCloudSprite : CloudSprites) { curCloudSprite = new Sprite( MathUtils.random( -(this.getWidth() * this.getScaleX()) / 2, ResourceManager.getInstance().cameraWidth + (this.getWidth() * this.getScaleX()) / 2), MathUtils.random( -(this.getHeight() * this.getScaleY()) / 2, ResourceManager.getInstance().cameraHeight + (this.getHeight() * this.getScaleY()) / 2), ResourceManager.cloudTextureRegion, ResourceManager.getInstance().engine.getVertexBufferObjectManager()) { private float XSpeed = MathUtils.random(0.2f, 2f); private boolean initialized = false; @Override protected void onManagedUpdate(final float pSecondsElapsed) { super.onManagedUpdate(pSecondsElapsed); if (!initialized) { initialized = true; this.setScale(XSpeed / 2); this.setZIndex(-4000 + Math.round(XSpeed * 1000f)); MainMenu.getInstance().sortChildren(); } if (this.getX() < -(this.getWidth() * this.getScaleX()) / 2) { XSpeed = MathUtils.random(0.2f, 2f); this.setScale(XSpeed / 2); this.setPosition( ResourceManager.getInstance().cameraWidth + (this.getWidth() * this.getScaleX()) / 2, MathUtils.random( -(this.getHeight() * this.getScaleY()) / 2, ResourceManager.getInstance().cameraHeight + (this.getHeight() * this.getScaleY()) / 2)); this.setZIndex(-4000 + Math.round(XSpeed * 1000f)); MainMenu.getInstance().sortChildren(); } this.setPosition(this.getX() - (XSpeed * (pSecondsElapsed / 0.016666f)), this.getY()); } }; this.attachChild(curCloudSprite); } // Create a Play button. Notice that the Game scenes, unlike menus, are not referred to in a // static way. PlayButton = new ButtonSprite( (ResourceManager.getInstance().cameraWidth - ResourceManager.buttonTiledTextureRegion.getTextureRegion(0).getWidth()) / 2f, (ResourceManager.getInstance().cameraHeight - ResourceManager.buttonTiledTextureRegion.getTextureRegion(0).getHeight()) * (1f / 3f), ResourceManager.buttonTiledTextureRegion.getTextureRegion(0), ResourceManager.buttonTiledTextureRegion.getTextureRegion(1), ResourceManager.getInstance().engine.getVertexBufferObjectManager()); PlayButtonText = new Text( 0, 0, ResourceManager.fontDefault32Bold, "PLAY", ResourceManager.getInstance().engine.getVertexBufferObjectManager()); PlayButtonText.setPosition((PlayButton.getWidth()) / 2, (PlayButton.getHeight()) / 2); PlayButton.attachChild(PlayButtonText); this.attachChild(PlayButton); PlayButton.setOnClickListener( new OnClickListener() { @Override public void onClick( ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) { // Create a new GameLevel and show it using the SceneManager. And play a click. SceneManager.getInstance().showScene(new GameLevel()); ResourceManager.clickSound.play(); } }); this.registerTouchArea(PlayButton); // Create an Option button. Notice that the SceneManager is being told to not pause the scene // while the OptionsLayer is open. OptionsButton = new ButtonSprite( PlayButton.getX() + PlayButton.getWidth(), PlayButton.getY(), ResourceManager.buttonTiledTextureRegion.getTextureRegion(0), ResourceManager.buttonTiledTextureRegion.getTextureRegion(1), ResourceManager.getInstance().engine.getVertexBufferObjectManager()); OptionsButtonText = new Text( 0, 0, ResourceManager.fontDefault32Bold, "OPTIONS", ResourceManager.getInstance().engine.getVertexBufferObjectManager()); OptionsButtonText.setPosition((OptionsButton.getWidth()) / 2, (OptionsButton.getHeight()) / 2); OptionsButton.attachChild(OptionsButtonText); this.attachChild(OptionsButton); OptionsButton.setOnClickListener( new OnClickListener() { @Override public void onClick( ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) { // Show the OptionsLayer and play a click. SceneManager.getInstance().showOptionsLayer(false); ResourceManager.clickSound.play(); } }); this.registerTouchArea(OptionsButton); // Create a title TitleText = new Text( 0, 0, ResourceManager.fontDefault72Bold, "HAPPY BIRDS", ResourceManager.getInstance().engine.getVertexBufferObjectManager()); TitleText.setPosition( (ResourceManager.getInstance().cameraWidth) / 2, (ResourceManager.getInstance().cameraHeight * 2) / 3f); TitleText.setColor(0.153f, 0.290f, 0.455f); this.attachChild(TitleText); }