Android Open Source - X3n0break Paddle






From Project

Back to project page X3n0break.

License

The source code is released under:

GNU General Public License

If you think the Android project X3n0break listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.x3n0m0rph59.breakout;
//  ww  w  . j ava  2 s  .c  o  m
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;


public class Paddle extends GameObject {  
  /**
   * 
   */
  private static final long serialVersionUID = -2359831494748103391L;

  private float lastX = 0.0f, lastY = 0.0f;
  
  private float thrust = 0.0f;
  
  boolean drawFlash = false;
  
  public enum State {IDLE, CHASING};
  
  State state = State.IDLE;
  
  private Point chasePoint;
  
  private final ParticleSystem thruster = new ParticleSystem(new SpriteTuple[]{new SpriteTuple(ResourceMapper.getPath("fire.png", ResourceType.SPRITE), 
                                         198.0f, 197.0f, 198, 197)}, 
                        new Point(0.0f, 0.0f), -1.0f, 15.0f, 0.0f, 25.0f, 0.0f, 15.0f, 10.0f, 8.5f);

  private final GrapplingHook grapplingHook;
  
  public Paddle() {
    super(new SpriteObject(ResourceMapper.getPath("paddle.png", ResourceType.SPRITE), Config.PADDLE_DEFAULT_WIDTH, Config.PADDLE_HEIGHT, 600, 150), 
        new Point((Config.getInstance().getClientWidth() / 2) - (Config.PADDLE_DEFAULT_WIDTH / 2), 
              (Config.getInstance().getScreenHeight() - Config.PADDLE_BOTTOM_SPACING)), 
        Config.PADDLE_DEFAULT_WIDTH, Config.PADDLE_HEIGHT, 0.0f, 0.0f, 0.0f, 0.0f);
    
    grapplingHook = new GrapplingHook(new Point(0.0f, 0.0f));
    
    updateEnginePosition();
    updateGrapplingHookPosition();
  }
  
  @Override
  public void render(SpriteBatch batch) {
//    leftEngine.render(batch);
//    rightEngine.render(batch);
    
//    engine.render(batch);
    
    thruster.render(batch);
    
    getGrapplingHook().render(batch);
    
    final boolean inGracePeriod = EffectManager.getInstance().isEffectInGracePeriod(Effect.Type.EXPAND_PADDLE) || 
                    EffectManager.getInstance().isEffectInGracePeriod(Effect.Type.SHRINK_PADDLE);
    
    if (inGracePeriod && drawFlash)
      getSprite().setFlashed(true);
    else
      getSprite().setFlashed(false);
    
    super.render(batch);
  }
  
  @Override
  public void step(float delta) {
    // Do not move the paddle if the 
    // user tapped on "menu" or "fire space bomb"         
    final float iX = Gdx.input.getX();
    final float iY = Gdx.input.getY();
    
    final Vector3 unprojectediXY = ((App) Gdx.app.getApplicationListener()).getGameScreen().
                          getCamera().unproject(new Vector3(iX, iY, 0));
    
    final float mX = unprojectediXY.x;
    final float mY = unprojectediXY.y;
    
    final Rectangle hotRect = new Rectangle((int) (Config.getInstance().getScreenWidth() - 
                              Config.SCOREBOARD_WIDTH + 25),
                              (int) Config.WORLD_HEIGHT - (175 + 150), 350, (300 * 2));

    if (!hotRect.contains(new Vector2(mX, mY))) {      
      if (getState() == State.IDLE) {
        final float distanceX = Math.abs(getCenterPoint().getX() - mX);
        
        if (distanceX > getWidth()) {
          // the new point is far away
          // use a chasing algorithm to "fly" to it
          Logger.debug("Begin chasing to point: " + mX + ", " + mY);
          chaseTo(new Point(mX, mY));
          
        } else {
          // the new point is within immediate range
          // simply set our new position
          setCenteredPosition(new Point(mX, mY));
        }
      }
    }
    
    if (getState() == State.CHASING) {
      final float currentX = getCenterPosition().getX();
      final float currentY = getCenterPosition().getY();
      
      final float targetX = getChasePoint().getX();
//      final float targetY = getChasePoint().getY();
      
      if (Math.abs(currentX - targetX) <= Config.PADDLE_CHASING_SPEED + 1.0f /*&& 
        Math.abs(currentY - targetY) <= Config.PADDLE_CHASING_SPEED + 1.0f */) {
        
        // chase destination reached        
        setState(State.IDLE);
        Logger.debug("Target point reached");
        
      } else {      
        float newX = currentX;
        float newY = currentY;
        
        if (currentX - targetX < 0) {
          newX += Config.PADDLE_CHASING_SPEED;
        } else {
          newX -= Config.PADDLE_CHASING_SPEED;
        }
        
        setCenteredPosition(new Point(newX, newY));
        
        Logger.debug("Closing in...");        
      }
    }
    
    thrust /= 2.0f;
    updateEnginePosition();    
    
    thruster.step(delta);    
    
//    engine.step(delta);
    
//    leftEngine.step(delta);
//    rightEngine.step(delta);    
    
    grapplingHook.step(delta);
    
    if ((frameCounter % (Config.SYNC_FPS * Config.GRACE_PERIOD_BLINK_RATE)) == 0)
      drawFlash = !drawFlash;
    
    super.step(delta);
  }
  
  public void setCenteredPosition(Point position) {        
    if (position.getX() < 0 + (getWidth() / 2)) {
      setPosition(new Point(0.0f, Config.getInstance().getScreenHeight() - Config.PADDLE_BOTTOM_SPACING));
      
      // if we were chasing, cancel the chase
      if (getState() == State.CHASING)
        setState(State.IDLE);
      
    } else if (position.getX() > Config.getInstance().getClientWidth() - (getWidth() / 2)) { 
      setPosition(new Point(Config.getInstance().getClientWidth() - getWidth(), 
                  Config.getInstance().getScreenHeight() - Config.PADDLE_BOTTOM_SPACING));
      
      // if we were chasing, cancel the chase
      if (getState() == State.CHASING)
        setState(State.IDLE);
      
    } else {    
      setPosition(new Point(position.getX() - (getWidth() / 2), Config.getInstance().getScreenHeight() - Config.PADDLE_BOTTOM_SPACING));
    }
  }

  private void updateEnginePosition() {    
    final float dX = getX() - lastX;
    thrust += Math.abs(dX);
    
    if (thrust >= Config.THRUST_CAP)
      thrust = Config.THRUST_CAP;
    
//    Logger.debug("Thrust: " + thrust + "; deltaX: " + dX);
    
    if (dX == 0.0f) {      
      // do nothing
    } else if (dX > 0) {      
      thruster.setParticleDensity(thrust);
      thruster.setPositionAndAngle(new Point(getX(), getY() + getHeight() / 2), 180.0f);
      
    } else if (dX < 0) {      
      thruster.setParticleDensity(thrust);
      thruster.setPositionAndAngle(new Point(getX() + getWidth(), getY() + getHeight() / 2), -180.0f);
      
    }
    
    if (thrust < Config.THRUST_EPSILON) {
      thruster.setVisible(false);
    } else {
      thruster.setVisible(true);
    }
    
//    engine.setPositionAndAngle(new Point(getCenterPoint().getX(), 
//                         getY() + getHeight() - 5.0f), 45.0f);
        
//    leftEngine.setPositionAndAngle(new Point(getX() + Config.PADDLE_ENGINE_OFFSET, 
//                         getY() + getHeight() - 5.0f), 45.0f);
//    
//    rightEngine.setPositionAndAngle(new Point((getX() + getWidth()) - Config.PADDLE_ENGINE_OFFSET, 
//                           getY() + getHeight() - 5.0f), 45.0f);
  }
  
  private void updateGrapplingHookPosition() {
    grapplingHook.setPosition(new Point(getX() + getWidth() / 2, getY()));
  }
  
  public void expand() {
    this.width += Config.PADDLE_EXPANSION;
    
    sprite.setWidth(width);
    
    updateEnginePosition();
    updateGrapplingHookPosition();
  }
  
  public void shrink() {
    this.width -= Config.PADDLE_EXPANSION;
    
    sprite.setWidth(width);
    
    updateEnginePosition();
    updateGrapplingHookPosition();
  }

  @Override
  public void setPosition(Point position) {
    super.setPosition(position);
    
    updateEnginePosition();
    updateGrapplingHookPosition();
  }

  public float getdX() {
    final float result = getX() - lastX;    
    lastX = getX();
    
    return result;
  }
  
  public float getdY() {
    final float result = getY() - lastY;    
    lastY = getY();
    
    return result;
  }

  public Point getCenterPoint() {
    return new Point(getBoundingBox().getX() + getWidth() / 2, getBoundingBox().getY() + getHeight() / 2);
  }

  public GrapplingHook getGrapplingHook() {
    return grapplingHook;
  }
  
  public void setState(State state) {
    this.state = state;
  }
  
  public State getState() {
    return state;
  }
  
  public void chaseTo(Point position) {    
    setChasePoint(position);
    setState(State.CHASING);
  }
  
  public Point getChasePoint() {
    return chasePoint;
  }

  public void setChasePoint(Point chasePoint) {
    this.chasePoint = chasePoint;
  }

}




Java Source Code List

org.x3n0m0rph59.breakout.App.java
org.x3n0m0rph59.breakout.BackgroundFactory.java
org.x3n0m0rph59.breakout.Background.java
org.x3n0m0rph59.breakout.Ball.java
org.x3n0m0rph59.breakout.BottomWall.java
org.x3n0m0rph59.breakout.Brick.java
org.x3n0m0rph59.breakout.Config.java
org.x3n0m0rph59.breakout.Destroyable.java
org.x3n0m0rph59.breakout.EffectManager.java
org.x3n0m0rph59.breakout.Effect.java
org.x3n0m0rph59.breakout.FontLoader.java
org.x3n0m0rph59.breakout.FontTuple.java
org.x3n0m0rph59.breakout.ForceFeedback.java
org.x3n0m0rph59.breakout.GameInputProcessor.java
org.x3n0m0rph59.breakout.GameObject.java
org.x3n0m0rph59.breakout.GameScreen.java
org.x3n0m0rph59.breakout.GameState.java
org.x3n0m0rph59.breakout.GrapplingHookSegment.java
org.x3n0m0rph59.breakout.GrapplingHook.java
org.x3n0m0rph59.breakout.HelpInputProcessor.java
org.x3n0m0rph59.breakout.HelpScreen.java
org.x3n0m0rph59.breakout.HighScoreManager.java
org.x3n0m0rph59.breakout.HighScoreScreen.java
org.x3n0m0rph59.breakout.HighScore.java
org.x3n0m0rph59.breakout.HighscoreInputProcessor.java
org.x3n0m0rph59.breakout.IOSLauncher.java
org.x3n0m0rph59.breakout.LevelLoader.java
org.x3n0m0rph59.breakout.LevelSetSelectorInputProcessor.java
org.x3n0m0rph59.breakout.LevelSetSelectorScreen.java
org.x3n0m0rph59.breakout.Logger.java
org.x3n0m0rph59.breakout.MenuInputProcessor.java
org.x3n0m0rph59.breakout.MenuScreen.java
org.x3n0m0rph59.breakout.MusicLoader.java
org.x3n0m0rph59.breakout.MusicStream.java
org.x3n0m0rph59.breakout.ObjectPool.java
org.x3n0m0rph59.breakout.Paddle.java
org.x3n0m0rph59.breakout.ParticleSystem.java
org.x3n0m0rph59.breakout.Particle.java
org.x3n0m0rph59.breakout.Point.java
org.x3n0m0rph59.breakout.Poolable.java
org.x3n0m0rph59.breakout.Powerup.java
org.x3n0m0rph59.breakout.Projectile.java
org.x3n0m0rph59.breakout.Renderable.java
org.x3n0m0rph59.breakout.ResourceMapperDecisionCache.java
org.x3n0m0rph59.breakout.ResourceMapper.java
org.x3n0m0rph59.breakout.ScoreBoard.java
org.x3n0m0rph59.breakout.ScreenManager.java
org.x3n0m0rph59.breakout.ScreenType.java
org.x3n0m0rph59.breakout.SettingsInputProcessor.java
org.x3n0m0rph59.breakout.SettingsScreen.java
org.x3n0m0rph59.breakout.SoundLayer.java
org.x3n0m0rph59.breakout.SoundLoader.java
org.x3n0m0rph59.breakout.SoundSprite.java
org.x3n0m0rph59.breakout.SpaceBomb.java
org.x3n0m0rph59.breakout.SpriteLoader.java
org.x3n0m0rph59.breakout.SpriteObject.java
org.x3n0m0rph59.breakout.SpriteTuple.java
org.x3n0m0rph59.breakout.Star.java
org.x3n0m0rph59.breakout.Stepable.java
org.x3n0m0rph59.breakout.TextAnimationManager.java
org.x3n0m0rph59.breakout.TextAnimation.java
org.x3n0m0rph59.breakout.Util.java
org.x3n0m0rph59.breakout.Vector.java
org.x3n0m0rph59.breakout.android.AndroidLauncher.java
org.x3n0m0rph59.breakout.client.HtmlLauncher.java
org.x3n0m0rph59.breakout.desktop.DesktopLauncher.java