Java tutorial
/* ******************************************************************************** * Copyright (c) 2012 Samsung Electronics, Inc. * All rights reserved. * * This software is a confidential and proprietary information of Samsung * Electronics, Inc. ("Confidential Information"). You shall not disclose such * Confidential Information and shall use it only in accordance with the terms * of the license agreement you entered into with Samsung Electronics. ******************************************************************************** */ package com.thirdgame.shootkillninjabomb; import junit.framework.Assert; import org.andengine.engine.handler.IUpdateHandler; import org.andengine.entity.Entity; import org.andengine.entity.primitive.Rectangle; import org.andengine.entity.scene.IOnAreaTouchListener; import org.andengine.entity.scene.IOnSceneTouchListener; import org.andengine.entity.scene.ITouchArea; import org.andengine.entity.scene.Scene; import org.andengine.entity.sprite.AnimatedSprite; import org.andengine.entity.sprite.Sprite; import org.andengine.extension.physics.box2d.PhysicsConnector; import org.andengine.extension.physics.box2d.PhysicsFactory; import org.andengine.extension.physics.box2d.PhysicsWorld; import org.andengine.input.sensor.acceleration.AccelerationData; import org.andengine.input.sensor.acceleration.IAccelerationListener; import org.andengine.input.touch.TouchEvent; import org.andengine.opengl.font.BitmapFont; import org.andengine.opengl.texture.region.ITextureRegion; import org.andengine.opengl.vbo.VertexBufferObjectManager; import org.andengine.util.color.Color; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; import com.badlogic.gdx.physics.box2d.Contact; import com.badlogic.gdx.physics.box2d.ContactImpulse; import com.badlogic.gdx.physics.box2d.ContactListener; import com.badlogic.gdx.physics.box2d.Fixture; import com.badlogic.gdx.physics.box2d.FixtureDef; import com.badlogic.gdx.physics.box2d.Manifold; import com.thirdgame.shootkillninjabomb.GraphicsKeys.GameTextureKeys; public class GameLayer extends Entity implements IOnAreaTouchListener, IOnSceneTouchListener { private final GameScene mGameScene; private int touchCount = 0; private boolean isTouch = false; public Sprite mScoreMultiple; public Sprite mFreeze; private int mComboCount = 0; private int multiplier = 1; private int isWallsTouch = 0; public GameLayer(final GameScene mainScene) { mGameScene = mainScene; createGameWalls(); } /* @Override * Removed since not in use public void onUpdateLayer() { mHenLayer.onUpdateLayer(); mGunLayer.onUpdateLayer(); mHudLayer.onUpdateLayer(); mEffectsLayer.onUpdateLayer(); }*/ /* @Override public void runOnUpdateThread(final Runnable runnable) { mGameScene.runOnUpdateThread(runnable); }*/ public void registerUpdateHandlerOnScene(final IUpdateHandler pUpdateHandler) { mGameScene.registerUpdateHandler(pUpdateHandler); } public VertexBufferObjectManager getVertexBufferObjectManager() { return mGameScene.getVertexBufferObjectManager(); } public ITextureRegion getGameTextureRegion(final GameTextureKeys pKey) { return mGameScene.getGameTextureRegion(pKey); } /* public PhysicsWorld getPhysicsWorld() { return mGameScene.getPhysicsWorld(); }*/ /* @Override public boolean onAreaTouched(final TouchEvent sceneTouchEvent, final ITouchArea touchArea, final float touchAreaLocalX, final float touchAreaLocalY) { final AnimatedSpriteWithAreaToTouch spriteWithArea = (AnimatedSpriteWithAreaToTouch) touchArea; spriteWithArea.onAfterAreaTouched(sceneTouchEvent, touchArea, touchAreaLocalX, touchAreaLocalY); return false; }*/ /** Creates the wall surrounding the visible area of the game. */ private void createGameWalls() { // Assert.assertTrue(GameScene.mPhysicsWorld != null); /* final Rectangle floor = new Rectangle(0, mGameScene.getCameraHeight() - HudLayer.FLOR_WIDH, mGameScene.getCameraWidth(), LIMITATIONS_THICKNESS, mGameScene.getVertexBufferObjectManager()); final Rectangle roof = new Rectangle(0, HudLayer.CEILING_WIDTH, mGameScene.getCameraWidth(), LIMITATIONS_THICKNESS, mGameScene.getVertexBufferObjectManager()); final Rectangle left = new Rectangle(0, 0, HudLayer.WALL_WIDTH, mGameScene.getCameraHeight(), mGameScene.getVertexBufferObjectManager()); final Rectangle right = new Rectangle(mGameScene.getCameraWidth() - HudLayer.WALL_WIDTH, 0, LIMITATIONS_THICKNESS, mGameScene.getCameraHeight(), mGameScene.getVertexBufferObjectManager());*/ final Rectangle ground = new Rectangle(0, mGameScene.getCameraHeight() - 2, mGameScene.getCameraWidth(), 2, mGameScene.getVertexBufferObjectManager()); final Rectangle roof = new Rectangle(0, 0, mGameScene.getCameraWidth(), 2, mGameScene.getVertexBufferObjectManager()); final Rectangle left = new Rectangle(0, 0, 2, mGameScene.getCameraHeight(), mGameScene.getVertexBufferObjectManager()); final Rectangle right = new Rectangle(mGameScene.getCameraWidth() - 2, 0, 2, mGameScene.getCameraHeight(), mGameScene.getVertexBufferObjectManager()); ground.setColor(Color.TRANSPARENT); roof.setColor(Color.TRANSPARENT); left.setColor(Color.TRANSPARENT); right.setColor(Color.TRANSPARENT); final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.2f); final Body floorBody = PhysicsFactory.createBoxBody(GameScene.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef); final Body roofBody = PhysicsFactory.createBoxBody(GameScene.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef); final Body leftBody = PhysicsFactory.createBoxBody(GameScene.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef); final Body rightBody = PhysicsFactory.createBoxBody(GameScene.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef); floorBody.setUserData(new UserData("Floor", "", false)); roofBody.setUserData(new UserData("Wall", "", false)); leftBody.setUserData(new UserData("Wall", "", false)); rightBody.setUserData(new UserData("Wall", "", false)); GameScene.mPhysicsWorld.setContactListener(createContactListener()); mScoreMultiple = new Sprite( 400 - GameScene.activity.getGameTextureRegion(GameTextureKeys.TOP_SCOREMULTIPLE).getWidth(), 0, GameScene.activity.getGameTextureRegion(GameTextureKeys.TOP_SCOREMULTIPLE), getVertexBufferObjectManager()); mFreeze = new Sprite(400, 0, GameScene.activity.getGameTextureRegion(GameTextureKeys.TOP_FREEZE), getVertexBufferObjectManager()); mScoreMultiple.setVisible(false); mFreeze.setVisible(false); attachChild(ground); attachChild(roof); attachChild(left); attachChild(right); attachChild(mScoreMultiple); attachChild(mFreeze); } /** * Simple wrapper around GameScene.registerTouchArea() method. */ /* @Override public void registerTouchArea(final AnimatedSpriteWithAreaToTouch pTouchArea) { mGameScene.registerTouchArea(pTouchArea); } /** * Simple wrapper around GameScene.registerTouchArea() method. */ /* @Override public void registerTouchArea(final ButtonSpriteWithSound pTouchArea) { mGameScene.registerTouchArea(pTouchArea); }*/ /** * Simple wrapper around GameScene.registerTouchArea() method. */ /*@Override public void registerTouchArea(final ToggleButtonSprite pTouchArea) { mGameScene.registerTouchArea(pTouchArea); }*/ /** * Simple wrapper around GameScene.getCameraWidth() method. */ /*public float getCameraWidth() { return mGameScene.getCameraWidth(); }*/ /** * Simple wrapper around GameScene.getCameraHeight() method. */ /*public float getCameraHeight() { return mGameScene.getCameraHeight(); }*/ /** * Simple wrapper around GameScene.setCurrentScene(SceneType) method. */ public void setCurrentScene(final SceneType pSceneType) { mGameScene.setCurrentScene(pSceneType); } /** /** * Simple wrapper around EffectsLayer.prepareExplosion() method. */ /*public void prepareExplosion(final float x, final float y, final Color pColor) { mEffectsLayer.prepareExplosion(x, y, pColor); } */ /** * Simple wrapper around EffectsLayer.explode() method. */ /*public void explode() { mEffectsLayer.explode(); }*/ /** /** /** * Handles collisions of two game objects. * * @param pContact * - information about the objects involved in the collision. */ int i = 0; private ContactListener createContactListener() { ContactListener contactListener = new ContactListener() { @Override public void beginContact(final Contact pContact) { final Fixture fixtureA = pContact.getFixtureA(); final Fixture fixtureB = pContact.getFixtureB(); final Body bodyA = fixtureA.getBody(); final Body bodyB = fixtureB.getBody(); final UserData userDataA = (UserData) bodyA.getUserData(); final UserData userDataB = (UserData) bodyB.getUserData(); if (userDataA == null || userDataB == null) { return; } isTouch = false; isWallsTouch = 0; if ((userDataA.type == "Floor" && userDataB.type == "Shape") || (userDataB.type == "Floor" && userDataA.type == "Shape")) { i = 1; isWallsTouch = 1; if (userDataA.type == "Floor") { // bodyB.setActive(false); userDataB.type = "Remove"; if (GameScene.gameScene.countLives() > 1) GameScene.gameScene.setGameOver(); } else { // bodyA.setActive(false); userDataA.type = "Remove"; if (GameScene.gameScene.countLives() > 1) GameScene.gameScene.setGameOver(); } } if (((userDataA.type == "Floor" || userDataA.type == "Wall") && userDataB.type == "Bullet") || ((userDataB.type == "Floor" || userDataB.type == "Wall") && userDataA.type == "Bullet")) { if (userDataA.type == "Bullet") { userDataA.type = "Remove"; if (userDataA.count > 0) GameScene.gameScene.setBulletHitCount(); if (userDataA.count < 2) { isWallsTouch = 1; mComboCount = 0; } if (mComboCount > 1 && userDataA.count > 1) { touchCount += (mComboCount - 1) * 4; mGameScene.fireComboParticle(mComboCount - 1); } } else { if (userDataB.count < 2) { mComboCount = 0; isWallsTouch = 1; } if (userDataB.count > 0) GameScene.gameScene.setBulletHitCount(); if (mComboCount > 1 && userDataB.count > 1) { touchCount += (mComboCount - 1) * 4; mGameScene.fireComboParticle(mComboCount - 1); } userDataB.type = "Remove"; } } if ((userDataA.eggType == "redyellowegg" || userDataB.eggType == "redyellowegg") && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) { isTouch = true; // mGameScene.setScore(120); mGameScene.fireParticle(fixtureA); i = 1; isWallsTouch = 2; } if ((userDataA.eggType == "brownskyblueegg" || userDataB.eggType == "brownskyblueegg") && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) { isTouch = true; isWallsTouch = 2; // mGameScene.setScore(900); mGameScene.fireParticleOne(fixtureA); i = 1; } if ((userDataA.eggType == "orangeeyellowegg" || userDataB.eggType == "orangeeyellowegg") && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) { isTouch = true; isWallsTouch = 2; mGameScene.fireParticleTwo(fixtureA); i = 1; // mGameScene.setScore(550); } if ((userDataA.eggType == "blueeegreenegg" || userDataB.eggType == "blueeegreenegg") && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) { isTouch = true; isWallsTouch = 2; mGameScene.fireParticleThree(fixtureA); i = 1; } if ((userDataA.eggType == "newblueyellow" || userDataB.eggType == "newblueyellow") && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) { isTouch = true; isWallsTouch = 2; mGameScene.fireParticleFour(fixtureA); i = 1; getAudioPlayer().play(SoundKeys.MULTIPLIER); } if ((userDataA.eggType == "newblueredegg" || userDataB.eggType == "newblueredegg") && (userDataA.type == "Bullet" || userDataB.type == "Bullet")) { isTouch = true; isWallsTouch = 2; mGameScene.fireParticleFive(fixtureA); i = 1; getAudioPlayer().play(SoundKeys.FREEZE); } if (i == 1 && isWallsTouch == 2) { multiplier = 1; if (GameScene.gameScene.getMultiplier()) multiplier = 2; touchCount += multiplier; } if (isTouch) { if (userDataA.type == "Shape") { userDataB.count++; userDataA.type = "Remove"; if (userDataB.count == 2) { touchCount += 2 * multiplier; mComboCount++; mGameScene.fireBonusParticle(1, fixtureA); getAudioPlayer().play(SoundKeys.DOUBLE); } else if (userDataB.count == 3) { touchCount += 3 * multiplier; mGameScene.fireBonusParticle(2, fixtureA); getAudioPlayer().play(SoundKeys.DOUBLE); } else if (userDataA.isCritical) { touchCount += 5; mGameScene.fireBonusParticle(3, fixtureA); getAudioPlayer().play(SoundKeys.DOUBLE); } } else { userDataA.count++; userDataB.type = "Remove"; if (userDataA.count == 2) { touchCount += 2 * multiplier; mComboCount++; mGameScene.fireBonusParticle(1, fixtureA); getAudioPlayer().play(SoundKeys.DOUBLE); } else if (userDataA.count == 3) { touchCount += 3 * multiplier; mGameScene.fireBonusParticle(2, fixtureA); getAudioPlayer().play(SoundKeys.DOUBLE); } else if (userDataB.isCritical) { touchCount += 5; mGameScene.fireBonusParticle(3, fixtureA); getAudioPlayer().play(SoundKeys.DOUBLE); } } if (i == 1) { mGameScene.setScore(touchCount); getAudioPlayer().play(SoundKeys.EGG_BREAK); } } // mGameScene.cleanObjectList(); runOnUpdateThread(new Runnable() { @Override public void run() { switch (i) { case 1: mGameScene.cleanObjectList(); break; default: break; } } }); } @Override public void endContact(Contact contact) { // TODO Auto-generated method stub } @Override public void preSolve(Contact contact, Manifold oldManifold) { // TODO Auto-generated method stub final Fixture fixtureA = contact.getFixtureA(); final Fixture fixtureB = contact.getFixtureB(); UserData uA = (UserData) fixtureA.getBody().getUserData(); UserData uB = (UserData) fixtureB.getBody().getUserData(); if ((uB.type == "Bullet") || (uA.type == "Bullet")) contact.setEnabled(false); } @Override public void postSolve(Contact contact, ContactImpulse impulse) { // TODO Auto-generated method stub } }; return contactListener; /* Collision of a hen with the floor. */ /* if (CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(), Names.FlyingHenName, Names.FloorName) || CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(), Names.FlyingBombName, Names.FloorName) || CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(), Names.FlyingThunderboltName, Names.FloorName)) { // TODO: Check with bomb. getAudioPlayer().play(SoundKeys.BOUNCE); final IBullet bullet = userDataA.getObjectName().equals(Names.FlyingHenName) ? (IBullet) userDataA : (IBullet) userDataB; mGunLayer.onCollision(bullet); } else if (collisionWithRoofOrWalls(userDataA.getObjectName(), userDataB.getObjectName())) { getAudioPlayer().play(SoundKeys.BOUNCE); /* Collision of a hen with the hen top. */ /* } else if (CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(), Names.FlyingHenName, Names.HenName)) { getAudioPlayer().play(SoundKeys.CONNECTION); runOnUpdateThread(new Runnable() { @Override public void run() { mGunLayer.resetGunLayerAfterConnectedBulletOrTimeElapsed(true); } }); mHenLayer.onCollision(pContact); /* Collision of a bomb with the hen top. */ /* } else if (CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(), Names.FlyingBombName, Names.HenName) || CollisionListener.isExpectedContact(userDataA.getObjectName(), userDataB.getObjectName(), Names.FlyingThunderboltName, Names.HenName)) { final IBullet bullet; if (Names.FlyingBombName.equals(userDataA.getObjectName()) || Names.FlyingThunderboltName.equals(userDataA.getObjectName())) { bullet = (IBullet) userDataA; } else { bullet = (IBullet) userDataB; } bullet.setBodyType(BodyType.StaticBody); final AnimatedSpriteWithAreaToTouch animatedSpriteWithAreaToTouch = bullet .getAnimatedSpriteWithAreaToTouch(); final long[] tabOfAnimations = new long[] { 100, 100, 100 }; final float xPosition = animatedSpriteWithAreaToTouch.getX(); final float yPosition = animatedSpriteWithAreaToTouch.getY(); mEffectsLayer.prepareBombExplosion(xPosition, yPosition); animatedSpriteWithAreaToTouch.animate(tabOfAnimations, 3, 5, false, new OnAnimationFinishedListener( new IOnAnimationFinishedListener() { @Override public void onAnimationFinished(final AnimatedSprite pAnimatedSprite) { runOnUpdateThread(new Runnable() { @Override public void run() { if (bullet.getBulletType() == BulletType.THUNDERBOLT) { getAudioPlayer().play(SoundKeys.THUNDER); } else { getAudioPlayer().play(SoundKeys.BOMB_EXPLOSION); } if (bullet.getBulletType() == BulletType.BOMB) { mEffectsLayer.explodeBullet(); } mHenLayer.onCollisionWithBullet(pContact); animatedSpriteWithAreaToTouch.detachSelf(); final PhysicsWorld pw = getPhysicsWorld(); final PhysicsConnector mPhysicsConnector = pw.getPhysicsConnectorManager() .findPhysicsConnectorByShape(animatedSpriteWithAreaToTouch); if (mPhysicsConnector != null) { pw.unregisterPhysicsConnector(mPhysicsConnector); final Body b = mPhysicsConnector.getBody(); pw.destroyBody(b); } mGunLayer.resetGunLayerAfterConnectedBulletOrTimeElapsed(true); } }); } })); }*/ } /*private boolean collisionWithRoofOrWalls(final Names pObjectNameA, final Names pObjectNameB) { if (CollisionListener.isExpectedContact(pObjectNameA, pObjectNameB, Names.FlyingHenName, Names.RoofName)) { return true; } if (CollisionListener.isExpectedContact(pObjectNameA, pObjectNameB, Names.FlyingHenName, Names.RightWallName)) { return true; } if (CollisionListener.isExpectedContact(pObjectNameA, pObjectNameB, Names.FlyingHenName, Names.LeftWallName)) { return true; } return false; }*/ /* @Override public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) { return mGunLayer.handleSceneTouchEvent(pScene, pSceneTouchEvent); }*/ /** Simple wrapper around GameScene.setGameSpeed(float) method. */ public void setGameSpeed(final float pGameSpeed) { mGameScene.setGameSpeed(pGameSpeed); } /** Simple wrapper around GameScene.getAudioPlayer() method. */ /* public AudioPlayer getAudioPlayer() { return mGameScene.getAudioPlayer(); }*/ /** * Shows lightning starting at given position. * * @param xPosition * x coordinate of lightning beginning. * @param yPosition * y coordinate of lightning beginning. * @param type * axis of lightning. */ /* public void showLightning(final int xPosition, final int yPosition, final LightningType type) { mEffectsLayer.showLightning(xPosition, yPosition, type); }*/ /*public void showLevelUpText(final int pCurrentLevel) { mEffectsLayer.showLevelUpText(pCurrentLevel); }*/ public Activity getThirdGameActivity() { return mGameScene.getThirdGameActivity(); } @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { // TODO Auto-generated method stub if (pSceneTouchEvent.isActionDown()) { if (mGameScene.getSceneManager().getCurrentScene() == SceneType.GAME) { mGameScene.fireBullet(pSceneTouchEvent); getAudioPlayer().play(SoundKeys.BULLET); return true; } } return false; } @Override public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY) { // TODO Auto-generated method stub return false; } public void runOnUpdateThread(final Runnable runnable) { mGameScene.runOnUpdateThread(runnable); } public Sprite getScoreMultipleSprite() { return mScoreMultiple; } public Sprite getFreezeSprite() { return mFreeze; } public AudioPlayer getAudioPlayer() { return mGameScene.getAudioPlayer(); } }