package com.example.animationsprite;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;
import android.hardware.SensorManager;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
public class MainActivity extends SimpleBaseGameActivity implements
IAccelerationListener {
private static final int CAMERA_WIDTH = 360;
private static final int CAMERA_HEIGHT = 240;
private BitmapTextureAtlas mBitmapTextureAtlas,
backgroundBitmapTextureAtlas;
private TiledTextureRegion mBoxFaceTextureRegion;
ITextureRegion backgroundTiledTextureRegion;
private int mFaceCount = 0;
private PhysicsWorld mPhysicsWorld;
float mGravityX;
float mGravityY;
private Scene mScene;
AnimatedSprite faceAnimatedSprite;
Body bodyFace;
FixtureDef faceFixtureDef = PhysicsFactory.createFixtureDef(100, 0.1f, 0.0f),
wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.0f, 0.0f);
Camera camera;
BuildableBitmapTextureAtlas mBuildableBitmapTextureAtlas;
Sprite backGroundSprite;
@Override
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED,
new FillResolutionPolicy(), camera);
}
@Override
public void onCreateResources()
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
// this.mBuildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(
// this.getTextureManager(), 1024, 512, TextureOptions.DEFAULT);
this.mBuildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(
this.getTextureManager(), 1024, 512, TextureOptions.DEFAULT);
setBackgroundImage();
this.mBitmapTextureAtlas = new BitmapTextureAtlas(
this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(this.mBitmapTextureAtlas, this,
"face_box_tiled.png", 0, 0, 2, 1); // 64x32
this.mBitmapTextureAtlas.load();
try {
this.mBuildableBitmapTextureAtlas
.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
0, 0, 1));
this.mBuildableBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e) {
Debug.e(e);
}
}
void setBackgroundImage() {
this.backgroundBitmapTextureAtlas = new BitmapTextureAtlas(
this.getTextureManager(), 512, 1024, TextureOptions.NEAREST_PREMULTIPLYALPHA);
backgroundTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(backgroundBitmapTextureAtlas, this,
"background_sky.png", 0, 0);
this.backgroundBitmapTextureAtlas.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,
SensorManager.GRAVITY_EARTH), false);
this.mScene = new Scene();
// this.mScene.setBackground(new Background(0, 0, 0));
backGroundSprite = new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT,
this.backgroundTiledTextureRegion,
this.getVertexBufferObjectManager());
// scene.setBackground(new Background(Color.WHITE));
mScene.setBackgroundEnabled(false);
this.mScene.attachChild(backGroundSprite);
create_4_Wall();
create_face_sprite();
mScene.setOnSceneTouchListener(new IOnSceneTouchListener() {
@Override
public boolean onSceneTouchEvent(Scene pScene,
TouchEvent pSceneTouchEvent) {
if (pSceneTouchEvent.isActionDown()) {
// body.setLinearVelocity(new
// Vector2(body.getLinearVelocity().x,-8f));
jumpFace(bodyFace);
}
return false;
}
});
return this.mScene;
}
private void create_face_sprite() {
// for sprite
faceAnimatedSprite = new AnimatedSprite(200, 200,
this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
bodyFace = PhysicsFactory.createBoxBody(this.mPhysicsWorld,
faceAnimatedSprite, BodyType.DynamicBody, faceFixtureDef);
faceAnimatedSprite.animate(new long[] { 200, 200 }, 0, 1, true);
faceAnimatedSprite.setUserData(bodyFace);
// this.mScene.registerTouchArea(face);
// physicsHandler = new PhysicsHandler(face);
// face.registerUpdateHandler(physicsHandler);
this.mScene.attachChild(faceAnimatedSprite);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
faceAnimatedSprite, bodyFace, true, true));
// end for sprite
}
private void create_4_Wall() {
final VertexBufferObjectManager vertexBufferObjectManager = this
.getVertexBufferObjectManager();
final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2,
CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2,
vertexBufferObjectManager);
final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT,
vertexBufferObjectManager);
final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2,
CAMERA_HEIGHT, vertexBufferObjectManager);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left,
BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right,
BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(ground);
this.mScene.attachChild(roof);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
}
@Override
public void onAccelerationAccuracyChanged(
final AccelerationData pAccelerationData) {
}
@Override
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
this.mGravityX = pAccelerationData.getX();
this.mGravityY = pAccelerationData.getY();
final Vector2 gravity = Vector2Pool.obtain(this.mGravityX,
this.mGravityY);
// this.mPhysicsWorld.setVelocityIterations(10);
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
@Override
public void onResumeGame() {
super.onResumeGame();
this.enableAccelerationSensor(this);
}
@Override
public void onPauseGame() {
super.onPauseGame();
this.disableAccelerationSensor();
}
void jumpFace(Body _body) {
Body body = _body;
Vector2 velocity = Vector2Pool.obtain(0, -5);
// Vector2 velocity = Vector2Pool.obtain(10, 10);
body.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
}
}