package com.pickle.PongWallPaper;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.primitive.Line;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.extension.ui.livewallpaper.BaseLiveWallpaperService;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import android.content.SharedPreferences;
public class PongWallPaper extends BaseLiveWallpaperService implements SharedPreferences.OnSharedPreferenceChangeListener
{
// ===========================================================
// Constants
// ===========================================================
public static final String SHARED_PREFS_NAME = "livewallpaperPongWallPapersettings";
// Camera Constants
private static int CAMERA_WIDTH = 480;
private static int CAMERA_HEIGHT = 720;
private static float BALL_VELOCITY = 120.0f;
private static float PADDLE_VELOCITY = 160.0f;
public static int GAME_WIDTH = 480;
public static int GAME_WIDTH_HALF = GAME_WIDTH / 2;
public static int GAME_HEIGHT = 720;
public static int GAME_HEIGHT_HALF = GAME_HEIGHT / 2;
// ===========================================================
// Fields
// ===========================================================
private Camera mCamera;
private Rectangle mBall;
// Shared Preferences
private SharedPreferences mSharedPreferences;
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public org.anddev.andengine.engine.Engine onLoadEngine()
{
int cameraWidth = 480;
int cameraHeight = 720;
CAMERA_WIDTH = cameraWidth;
CAMERA_HEIGHT = cameraHeight;
GAME_WIDTH = cameraWidth;
GAME_WIDTH_HALF = GAME_WIDTH / 2;
GAME_HEIGHT = cameraHeight;
GAME_HEIGHT_HALF = GAME_HEIGHT / 2;
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
this.mCamera.setCenter(0, 0);
return new org.anddev.andengine.engine.Engine(
new EngineOptions(true, ScreenOrientation.PORTRAIT,
new FillResolutionPolicy(), mCamera));
}
@Override
public void onLoadResources()
{
//Set the Base Texture Path
TextureRegionFactory.setAssetBasePath("gfx/");
}
@Override
public Scene onLoadScene()
{
this.mEngine.registerPostFrameHandler(new FPSLogger());
final Scene scene = new Scene(1);
scene.setBackground(new ColorBackground(0, 0, 0));
/* Walls */
scene.getBottomLayer().addEntity(
new Line(-GAME_WIDTH_HALF + 1, -GAME_HEIGHT_HALF,
-GAME_WIDTH_HALF + 1, GAME_HEIGHT_HALF)); // Left
scene.getBottomLayer().addEntity(
new Line(GAME_WIDTH_HALF, -GAME_HEIGHT_HALF, GAME_WIDTH_HALF,
GAME_HEIGHT_HALF)); // Right
scene.getBottomLayer().addEntity(
new Line(-GAME_WIDTH_HALF, -GAME_HEIGHT_HALF + 1,
GAME_WIDTH_HALF, -GAME_HEIGHT_HALF + 1)); // Top
scene.getBottomLayer().addEntity(
new Line(-GAME_WIDTH_HALF, GAME_HEIGHT_HALF, GAME_WIDTH_HALF,
GAME_HEIGHT_HALF)); // Bottom
scene.getBottomLayer().addEntity(
new Line(0, -GAME_HEIGHT_HALF, 0, GAME_HEIGHT_HALF)); // Middle
/* Ball */
final Rectangle ball = new Rectangle(0, 0, 20, 20) {
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
if (this.mX < -GAME_WIDTH_HALF) {
//this.setVelocityX(BALL_VELOCITY);
this.setPosition(0, 0);
} else if (this.mX + this.getWidth() > GAME_WIDTH_HALF) {
//this.setVelocityX(-BALL_VELOCITY);
this.setPosition(0, 0);
}
if (this.mY < -GAME_HEIGHT_HALF) {
this.setVelocityY(BALL_VELOCITY);
} else if (this.mY + this.getHeight() > GAME_HEIGHT_HALF) {
this.setVelocityY(-BALL_VELOCITY);
}
super.onManagedUpdate(pSecondsElapsed);
}
};
ball.setVelocityX(BALL_VELOCITY);
ball.setVelocityY(BALL_VELOCITY);
scene.getBottomLayer().addEntity(ball);
/* Paddles */
final Rectangle leftPaddle = new Rectangle(-GAME_WIDTH_HALF + 20, 0,
20, 90);
scene.getBottomLayer().addEntity(leftPaddle);
final Rectangle rightPaddle = new Rectangle(GAME_WIDTH_HALF - 40, 0,
20, 90);
scene.getBottomLayer().addEntity(rightPaddle);
scene.registerPreFrameHandler(new IUpdateHandler() {
@Override
public void reset() {
}
@Override
public void onUpdate(final float pSecondsElapsed) {
/* Update Left Paddle to track ball */
if ( ( leftPaddle.getY() + 40) > ball.getY()) {
leftPaddle.setVelocityY(-PADDLE_VELOCITY);
} else {
leftPaddle.setVelocityY(PADDLE_VELOCITY);
}
/* Update Right Paddle to track ball */
if ( ( rightPaddle.getY() + 40 ) > ball.getY()) {
rightPaddle.setVelocityY(-PADDLE_VELOCITY);
} else {
rightPaddle.setVelocityY(PADDLE_VELOCITY);
}
if (rightPaddle.collidesWith(ball)) {
ball.setVelocityX(-BALL_VELOCITY);
}
if (ball.getVelocityX() == BALL_VELOCITY) {
leftPaddle.setVelocityY(0f);
} else {
rightPaddle.setVelocityY(0f);
}
if (leftPaddle.collidesWith(ball)) {
ball.setVelocityX(BALL_VELOCITY);
}
}
});
return scene;
}
@Override
public void onLoadComplete()
{
}
@Override
protected void onTap(final int pX, final int pY)
{
}
@Override
public void onSharedPreferenceChanged(SharedPreferences pSharedPrefs, String pKey)
{
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}