Android Open Source - X3n0break Ball






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.ja va  2s.  c om*/
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;


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

  private static final float throwAngle = Config.BALL_THROW_ANGLE;  
  
  boolean drawFlash = false;
  
  public enum State {ROLLING, STUCK_TO_PADDLE}
  private State state = State.ROLLING;
  
  private boolean multiball = false;
    
  private final SpriteObject spriteNormalBall = new SpriteObject(ResourceMapper.getPath("ball.png", ResourceType.SPRITE), 
                                   Config.BALL_RADIUS * 2, Config.BALL_RADIUS * 2, 200, 200);
  private final SpriteObject spriteFireBall = new SpriteObject(ResourceMapper.getPath("fireball.png", ResourceType.SPRITE), 
                                 Config.BALL_RADIUS * 2, Config.BALL_RADIUS * 2, 200, 200);
  
  private final ParticleSystem trail = new ParticleSystem(new SpriteTuple[]{new SpriteTuple(ResourceMapper.getPath("star1.png", ResourceType.SPRITE), 255.0f, 255.0f, 255, 255), 
                                    new SpriteTuple(ResourceMapper.getPath("star2.png", ResourceType.SPRITE), 345.0f, 342.0f, 345, 342), 
                                    new SpriteTuple(ResourceMapper.getPath("star3.png", ResourceType.SPRITE), 270.0f, 261.0f, 270, 261), 
                                    new SpriteTuple(ResourceMapper.getPath("star4.png", ResourceType.SPRITE), 264.0f, 285.0f, 264, 285)}, 
                              new Point(0.0f, 0.0f), -1.0f, 5.0f, 0.0f, 45.0f, 0.0f, 15.0f, 15.0f, 5.0f);
  
  private final ParticleSystem fireBallTrail = 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, 25.0f, 10.0f, 45.0f, 0.0f, 15.0f, 150.f, 20.0f);

    
  public Ball(Point position) {
    this(position, false);
  }
  
  public Ball(Point position, boolean multiball) {
    super(null, position, Config.BALL_RADIUS * 2, Config.BALL_RADIUS * 2, 
        0.0f, 0.0f, 
        (float) Math.cos(Math.toRadians(throwAngle)) * +Config.BALL_SPEED, 
        (float) Math.sin(Math.toRadians(throwAngle)) * -Config.BALL_SPEED);
    
    this.multiball = multiball;
  }
  
  @Override
  public void render(SpriteBatch batch) {    
    if (EffectManager.getInstance().isEffectActive(Effect.Type.FIREBALL)) {
      
      fireBallTrail.render(batch);      
      setSprite(spriteFireBall);  
      
    } else {
      if (EffectManager.getInstance().isEffectActive(Effect.Type.STICKY_BALL))
        trail.render(batch);
      
      setSprite(spriteNormalBall);      
    }
    
    final boolean inGracePeriod = EffectManager.getInstance().isEffectInGracePeriod(Effect.Type.STICKY_BALL) || 
                      EffectManager.getInstance().isEffectInGracePeriod(Effect.Type.FIREBALL);

    if (inGracePeriod && drawFlash)
      getSprite().setFlashed(true);
    else
      getSprite().setFlashed(false);
    
    super.render(batch);
        
//    GL11.glBegin(GL11.GL_TRIANGLE_FAN);
//      if (EffectManager.getInstance().isEffectActive(EffectType.FIREBALL))
//        GL11.glColor4f(1.0f, 0.15f, 0.15f, 1.0f);
//      else
//        GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
//      
//      GL11.glVertex2f(x, y);
//      
//      int numSegments = 16;
//      float angle;
//      
//      for (int i = 0; i <= numSegments; i++) {
//        angle = (float) (i * 2.0f * Math.PI / numSegments);
//        GL11.glVertex2f((float) (Math.cos(angle) * radius) + x, 
//                (float) (Math.sin(angle) * radius) + y);
//      }
//    GL11.glEnd();
  }

  @Override
  public void step(float delta) {
    if (state != State.STUCK_TO_PADDLE) {            
      super.step(delta);  
    }
    
    spriteNormalBall.step(delta);
    spriteFireBall.step(delta);
    
    updateTrailPosition();

    trail.step(delta);
    fireBallTrail.step(delta);
    
    if ((frameCounter % (Config.SYNC_FPS * Config.GRACE_PERIOD_BLINK_RATE)) == 0)
      drawFlash = !drawFlash;
  }

  private void updateTrailPosition() {
    trail.setPositionAndAngle(this.getPosition(), getMovementAngleInDegrees());
    trail.setParticleSpeed(getSpeed());
    
    fireBallTrail.setPositionAndAngle(this.getPosition(), getMovementAngleInDegrees());
    fireBallTrail.setParticleSpeed(getSpeed());
  }

  @Override
  public Rectangle getBoundingBox() {
    return new Rectangle(position.getX(), position.getY(), Config.BALL_RADIUS * 2.0f, Config.BALL_RADIUS * 2.0f);
  }
    
  public void invertXVelocity() {
    setDeltaX(getDeltaX() * -1);
    
    updateTrailPosition();
  }

  public void invertYVelocity() {
    setDeltaY(getDeltaY() * -1);
    
    updateTrailPosition();
  }

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

  public float getSpeed() {
    return new Vector(getDeltaX(), getDeltaY(), getMovementAngleInDegrees()).magnitude();
  }

  public void setSpeed(float speed) {  
    setDeltaX((float) Math.sin(Math.toRadians(getMovementAngleInDegrees())) * speed);
    setDeltaY((float) Math.cos(Math.toRadians(getMovementAngleInDegrees())) * -speed);
    
    updateTrailPosition();
  }

  public State getState() {
    return state;
  }

  public void setState(State state) {
    this.state = state;
  }

  @Override
  public void changePosition(float deltaX, float deltaY) {
    super.changePosition(deltaX, deltaY);
    
    updateTrailPosition();
  }
  
  public void changeMovementAngle(float delta) {
    setMovementAngle(getMovementAngleInDegrees() + delta);    
    
    updateTrailPosition();
  }

  public boolean isMultiball() {
    return multiball;
  }
  
  
  public void setMovementAngle(float movementAngleInDegrees) {    
    setDeltaX((float) Math.sin(Math.toRadians(movementAngleInDegrees)) * getSpeed());
    setDeltaY((float) Math.cos(Math.toRadians(movementAngleInDegrees)) * -getSpeed());
    
    updateTrailPosition();
  }

  public void reflect() {
    float movementAngleInDegrees = getMovementAngleInDegrees();
    
    setDeltaX((float) Math.sin(Math.toRadians(movementAngleInDegrees)) * getSpeed());
    setDeltaY((float) Math.cos(Math.toRadians(movementAngleInDegrees)) * -getSpeed());
    
    updateTrailPosition();
  }
  
  public float getMovementAngleInDegrees() {
    return (float) Math.toDegrees(Math.atan2(getDeltaX(), -getDeltaY()));
  }
}




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