Android Open Source - X3n0break Grappling Hook






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;
/*  w w  w  .  java 2  s  .co  m*/
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;


public class GrapplingHook extends GameObject {
  /**
   * 
   */
  private static final long serialVersionUID = 1847648144682308278L;

  enum State {IDLE, EXTENDING, FULLY_EXTENDED, LOWERING}
  private State state = State.IDLE;
  
  private final GrapplingHookSegment hook    = new GrapplingHookSegment(GrapplingHookSegment.Type.HOOK, new Point(0.0f,0.0f));
  private final GrapplingHookSegment segment = new GrapplingHookSegment(GrapplingHookSegment.Type.SEGMENT, new Point(0.0f,0.0f));
  
  private float length = 0.0f;

  private boolean somethingAttached = false;
  
  public GrapplingHook(Point position) {
    super(null, position, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
    
    hook.setWidth(50.0f);
    hook.setHeight(40.0f);
    
    segment.setWidth(20.0f);
    segment.setHeight(20.0f);
  }
    
  public void toggleSwitch() {
    switch (state) {
    case IDLE:
    case LOWERING:
      setState(State.EXTENDING);
      break;
      
    case EXTENDING:
    case FULLY_EXTENDED:
      setState(State.LOWERING);
      break;
      
    default:
      throw new RuntimeException("Invalid state: " + state);
      
    }
  }
  
  public void resetState() {
    state = State.IDLE;
    length = 0.0f;
    somethingAttached = false;
    
    SoundLayer.stopLoop(Sounds.GRAPPLING_HOOK_LOOP);
  }
  
  public boolean collisionTest(Rectangle r) {
    return Util.collisionTest(getBoundingBox(), r);      
  }
  
  @Override
  public void render(SpriteBatch batch) {  
    if (state != State.IDLE) {
      hook.render(batch);
      
      for (int i = 0; i < (length / segment.getHeight()); i++) {
        final Point position = new Point(segment.getX(), hook.getY() + (hook.getHeight() + 
                                        i * segment.getHeight()));
        segment.setPosition(position);
        segment.render(batch);
      }
    }
  }
  
  @Override
  public void step(float delta) {
    switch (state) {
    case IDLE:
      // do nothing
      break;
      
    case EXTENDING:
      length += Config.GRAPPLING_HOOK_EXTEND_SPEED;
      
      if (length >= Config.GRAPPLING_HOOK_LENGTH)
        setState(State.FULLY_EXTENDED);
      break;
      
    case FULLY_EXTENDED:
      // do nothing
      break;    
      
    case LOWERING:
      length -= Config.GRAPPLING_HOOK_LOWER_SPEED;
      
      if (length <= 0)
        setState(State.IDLE);
      break;
      
    default:
      throw new RuntimeException("Invalid state: " + state);    
    }
  }
  
  @Override
  public void setPosition(Point position) {        
    final Point hookPos = new Point(position.getX() - (hook.getWidth() / 2) - 7.0f, 
                    position.getY() - (hook.getHeight() + length));
    final Point segmentPos = new Point(position.getX() - ((hook.getWidth() / 2) - (segment.getWidth() / 2)), 
                       position.getY());
    
    hook.setPosition(hookPos);
    segment.setPosition(segmentPos);
  }
  
  @Override
  public Rectangle getBoundingBox() {
    return hook.getBoundingBox();
  }
  
  @Override
  public boolean isExcemptFromSpeedFactorChange() {
    return true;
  }
  
  public Point getHookCenterPoint() {
    return hook.getCenterPosition();
  }

  public State getState() {
    return state;
  }

  public void setState(State state) {
    if (this.state == State.IDLE && state == State.EXTENDING)
      SoundLayer.loopSound(Sounds.GRAPPLING_HOOK_LOOP);
    
    if (this.state == State.FULLY_EXTENDED && state == State.LOWERING) {
      float pitch = 1.0f;
      
      if (isSomethingAttached())
        pitch = 0.85f;
      
      SoundLayer.loopSound(Sounds.GRAPPLING_HOOK_LOOP, pitch);
    }
    
    if (state == State.IDLE || state == State.FULLY_EXTENDED)
      SoundLayer.stopLoop(Sounds.GRAPPLING_HOOK_LOOP);
          
    this.state = state;
  }

  public boolean isSomethingAttached() {    
    return somethingAttached;
  }
  
  public void setSomethingAttached(boolean somethingAttached) {    
    this.somethingAttached = somethingAttached;
  }
}




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