Sie sind auf Seite 1von 7

public class Main extends BaseGameActivity implements IScrollDetectorListener, IOnSceneTouchListener, ICatapultDetectorListener{ // =========================================================== // Constants // =========================================================== private static final

int CAMERA_WIDTH = 480; private static final int CAMERA_HEIGHT = 320; public static final float MAX_CHARGE_DISTANCE = 80; private static final String TAG = "AndEngineTest"; private private private private private private private private ZoomCamera mCamera; Texture mTexture; TiledTextureRegion mBallTextureRegion; TiledTextureRegion mPlayerTextureRegion; SurfaceScrollDetector mScrollDetector; TMXTiledMap mTMXTiledMap; Player mActivePlayer; CatapultDetector mCatapultDetector; //Nueva variable Creada

// =========================================================== // Constructors // =========================================================== // =========================================================== // Getter & Setter // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override public void onLoadComplete() { // TODO Auto-generated method stub createPlayer(); } @Override public Engine onLoadEngine() { this.mCamera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT) { @Override public void onApplyMatrix(GL10 pGL) { GLHelper.setProjectionIdentityMatrix(pGL); GLU.gluOrtho2D(pGL, (int) this.getMinX(), (int) this.getMaxX(), (int) this.getMaxY(), (int) this.getMinY()); } }; final int alturaTotal = CAMERA_HEIGHT * 3; this.mCamera.setBounds(0, CAMERA_WIDTH, 0, alturaTotal); this.mCamera.setBoundsEnabled(true); return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); }

@Override public void onLoadResources() { this.mTexture = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.mBallTextureRegion = TextureRegionFactory.createTiledFromAsset( this.mTexture, this, "gfx/ui_ball.png", 0, 0, 2, 4); this.mPlayerTextureRegion = TextureRegionFactory.createTiledFromAsset( this.mTexture, this, "gfx/ui_player.png", 128, 0, 2, 4); this.mEngine.getTextureManager().loadTexture(this.mTexture); } @Override public Scene onLoadScene() { this.mScrollDetector = new SurfaceScrollDetector(this); this.mScrollDetector.setEnabled(false); //Creamos el detector de catapulta this.mCatapultDetector = new CatapultDetector(MAX_CHARGE_DISTANCE,this); this.mCatapultDetector.setEnabled(true);

this.mEngine.registerUpdateHandler(new FPSLogger()); final Scene scene = new Scene(1); try { final TMXLoader tmxLoader = new TMXLoader(this, this.mEngine .getTextureManager(), // TextureOptions.BILINEAR_PREMULTIPLYALPHA, TextureOptions.NEAREST, new ITMXTilePropertiesListener() { @Override public void onTMXTileWithPropertiesCreated(TMXTiledMap TMXLayer pTMXLayer, TMXTile pTMXTile, TMXProperties<TMXTileProperty> pTMXTileProperties) { } }); this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/tiled.tmx"); } catch (final TMXLoadException tmxle) { Debug.e(tmxle); } final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0); scene.getFirstChild().attachChild(tmxLayer); // TODO Auto-generated method stub

pTMXTiledMap,

scene.setOnAreaTouchTraversalFrontToBack(); this.mScrollDetector = new SurfaceScrollDetector(this); this.mScrollDetector.setEnabled(false); //Hemos puesto esto a false

/* final int centerX = (CAMERA_WIDTH this.mFaceTextureRegion.getWidth()) / 2; final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion .getHeight()) / 2; final Sprite ball = new Sprite(centerX, centerY, this.mFaceTextureRegion); scene.getLastChild().attachChild(ball); */ scene.setOnSceneTouchListener(this); scene.setTouchAreaBindingEnabled(true); return scene; } private void createPlayer() { final Scene scene = this.mEngine.getScene(); final Player sprite = new Player(200, 100, this.mPlayerTextureRegion); // scene.registerTouchArea(sprite); scene.getLastChild().attachChild(sprite); this.mActivePlayer = sprite; } @Override public void onScroll(ScrollDetector pScollDetector, TouchEvent pTouchEvent, float pDistanceX, float pDistanceY) { this.mCamera.offsetCenter(-pDistanceX, -pDistanceY); } @Override public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { if (this.mActivePlayer != null) { this.mCatapultDetector.onTouchEvent(pSceneTouchEvent); } return true; } @Override public void onCharge(CatapultDetector pCatapultDetector, TouchEvent pTouchEvent, float pDistance, float pAngle) { this.mActivePlayer.setCharge(pAngle,pDistance); } @Override public void onShoot(CatapultDetector pCatapultDetector, TouchEvent pTouchEvent, float pDistance, float pAngle) { Log.d(TAG, "Disparo... {Distancia:" + pDistance + ", angulo: " + pAngle + "}"); }

public class Splash extends BaseSplashActivity{ private final int SPLASH_DURATION=1; private final float SPLASH_SCALE_FROM=0.5f; @Override protected ScreenOrientation getScreenOrientation() { // TODO Auto-generated method stub return ScreenOrientation.LANDSCAPE; } @Override protected ITextureSource onGetSplashTextureSource() { // TODO Auto-generated method stub return new AssetTextureSource(this, "gfx/splash.png"); } @Override protected float getSplashDuration() { // TODO Auto-generated method stub return SPLASH_DURATION; } @Override protected Class<? extends Activity> getFollowUpActivity() { // TODO Auto-generated method stub return Main.class; } protected float getSplashScaleFrom() { return SPLASH_SCALE_FROM; } }

public class Player extends AnimatedSprite

private static final String TAG = "Player"; private static final int PLAYER_CHARGE_ANIMATIONS = 5; public Player(float pX, float pY, TiledTextureRegion pTiledTextureRegion) { super(pX, pY, pTiledTextureRegion); } public void setCharge(final float angle, final float distance){ final int step = Math.round(distance * PLAYER_CHARGE_ANIMATIONS / Main.MAX_CHARGE_DISTANCE); this.stopAnimation(step); this.setRotation(angle); } }

public class CatapultDetector extends BaseDetector { // =========================================================== // Constants // =========================================================== private static final float ANGLE_CONSTANT = 90; private static final float DEFAULT_MAX_DISTANCE = 80; // =========================================================== // Fields // =========================================================== // Listener for the Detector private final ICatapultDetectorListener mCatapultDetectorListener; private float mMaxDistance; // First Touch private float mFirstX; private float mFirstY; // =========================================================== // Constructors // =========================================================== public CatapultDetector( final ICatapultDetectorListener pCatapultDetectorListener) { this(DEFAULT_MAX_DISTANCE, pCatapultDetectorListener); } public CatapultDetector(final float pMaxDistance, final ICatapultDetectorListener pCatapultDetectorListener) { this.setMaxDistance(pMaxDistance); this.mCatapultDetectorListener = pCatapultDetectorListener; } // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== @Override protected boolean onManagedTouchEvent(TouchEvent pSceneTouchEvent) { final float touchX = this.getX(pSceneTouchEvent); final float touchY = this.getY(pSceneTouchEvent); final int action = pSceneTouchEvent.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: this.mFirstX = touchX; this.mFirstY = touchY; return true; case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: // case MotionEvent.ACTION_CANCEL: final float distanceX = Math.abs(touchX - this.mFirstX); final float distanceY = Math.abs(touchY - this.mFirstY); final float distance = Math.min((float) Math.hypot( (double) distanceX, (double) distanceY), mMaxDistance);

final double angleX = touchX - this.mFirstX; final double angleY = touchY - this.mFirstY; final float angle = (float) Math.toDegrees(Math.atan2(angleY, angleX)) + ANGLE_CONSTANT; if (action == MotionEvent.ACTION_MOVE) { this.mCatapultDetectorListener.onCharge(this, pSceneTouchEvent, distance, angle); } else { this.mCatapultDetectorListener.onShoot(this, pSceneTouchEvent, distance, angle); } return true; default: return false; } } // =========================================================== // Getter & Setter // =========================================================== public void setMaxDistance(float mMaxDistance) { this.mMaxDistance = mMaxDistance; } public float getMaxDistance() { return mMaxDistance; } // =========================================================== // Methods // =========================================================== protected float getX(final TouchEvent pTouchEvent) { return pTouchEvent.getX(); } protected float getY(final TouchEvent pTouchEvent) { return pTouchEvent.getY(); } // =========================================================== // Inner and Anonymous Classes // =========================================================== public static interface ICatapultDetectorListener { // =========================================================== // Constants // =========================================================== // =========================================================== // Methods // =========================================================== public void onCharge(final CatapultDetector pCatapultDetector, final TouchEvent pTouchEvent, final float pDistance, final float pAngle); public void onShoot(final CatapultDetector pCatapultDetector, final TouchEvent pTouchEvent, final float pDistance, final float pAngle);

} }

Das könnte Ihnen auch gefallen