Android Open Source - X3n0break Sprite Object






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.j  a v  a 2  s  .  c  o m
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;


public class SpriteObject implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID = -7452691659892992793L;

  private transient Sprite sprite;
  
  private String filename;
  private int tw,th;
  
  private int frameCounter = 0;
  
  private float width, height, alpha = 1.0f, angleInDegrees = 0.0f;
  
  private Point centerOfRotation = new Point(0.0f, 0.0f);
  
  private boolean alphaBlending = false;

  private boolean flashed = false;
  
    
  public SpriteObject(String filename, float width, float height, int tw, int th) {
    this.width = width;
    this.height = height;
    
    this.filename = filename;
    this.tw = tw;
    this.th = th;
    
    sprite = SpriteLoader.getInstance().getSprite(filename, tw, th);
  }
  
  public void render(SpriteBatch batch, Point position, float width, float height) {    
    if (alphaBlending) {      
      batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
    } else {
      if (!flashed)
        batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
      else
        batch.setBlendFunction(GL20.GL_ONE_MINUS_SRC_COLOR, GL20.GL_ONE);
    }
    
      
    if (angleInDegrees != 0) {
      
      batch.draw(sprite, position.getX(), position.getY(), 
             centerOfRotation.getX(), centerOfRotation.getY(), 
             getWidth(), getHeight(), 1.0f, 1.0f, 
             angleInDegrees, true);
      
    } else {
      
      batch.draw(sprite, position.getX(), position.getY(), 
             getWidth(), getHeight());
      
    }
  }
  
  public void render(SpriteBatch batch, Point position) {
    this.render(batch, position, width, height);
  }
  
  public void render(SpriteBatch batch, Rectangle r) {
    this.render(batch, new Point(r.getX(), r.getY()), r.getWidth(), r.getHeight());
  }
  
  public void step(float delta) {
//    if (++frameCounter >= sprite.getHorizontalCount());
//      frameCounter = 0;
      
    frameCounter += Math.round(delta);
  }

  public int getFrameCounter() {
    return frameCounter;
  }

  public void setFrameCounter(int frameCounter) {
    this.frameCounter = frameCounter;
  }

  public float getWidth() {
    return width;
  }

  public void setWidth(float width) {
    this.width = width;
  }

  public float getHeight() {
    return height;
  }

  public void setHeight(float height) {
    this.height = height;
  }

  public float getAlpha() {
    return alpha;
  }

  public void setAlpha(float alpha) {
    this.alpha = alpha;
    
    sprite.setAlpha(alpha);
  }  
  
  public float getAngle() {
    return angleInDegrees;
  }

  public void setAngle(float angleInDegrees) {
    this.angleInDegrees = angleInDegrees;
  }
  
  public Point getCenterOfRotation() {  
    return centerOfRotation;
  }
  
  public void setCenterOfRotation(Point point) {  
    this.centerOfRotation = point;
  }
  
  public boolean getAlphaBlending() {
    return alphaBlending;
  }

  public void setAlphaBlending(boolean alphaBlending) {
    this.alphaBlending = alphaBlending;
  }

  public boolean isFlashed() {
    return flashed;
  }

  public void setFlashed(boolean flashed) {
    this.flashed = flashed;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Float.floatToIntBits(alpha);
    result = prime * result + Float.floatToIntBits(angleInDegrees);    
    result = prime * result + frameCounter;
    result = prime * result + Float.floatToIntBits(height);
    result = prime * result
        + ((sprite == null) ? 0 : sprite.hashCode());
    result = prime * result + Float.floatToIntBits(width);
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    SpriteObject other = (SpriteObject) obj;
    if (Float.floatToIntBits(alpha) != Float.floatToIntBits(other.alpha))
      return false;
    if (Float.floatToIntBits(angleInDegrees) != Float
        .floatToIntBits(other.angleInDegrees))
      return false;    
    if (frameCounter != other.frameCounter)
      return false;    
    if (Float.floatToIntBits(height) != Float.floatToIntBits(other.height))
      return false;
    if (sprite == null) {
      if (other.sprite != null)
        return false;
    } else if (!sprite.equals(other.sprite))
      return false;
    if (Float.floatToIntBits(width) != Float.floatToIntBits(other.width))
      return false;
    return true;
  }
  
  private synchronized void writeObject(ObjectOutputStream stream) 
      throws IOException {
    stream.defaultWriteObject();
    
    // work around non-serializability of sprites
    stream.writeObject(filename);
    stream.writeInt(tw);
    stream.writeInt(th);    
  }
  
  private synchronized void readObject(ObjectInputStream stream)
      throws IOException {
    
    try {
      stream.defaultReadObject();
      
      // work around non-serializability of sprites
      final String filename = (String) stream.readObject();
      final int tw = stream.readInt();
      final int th = stream.readInt();
      
      sprite = SpriteLoader.getInstance().getSprite(filename, tw, th);
      
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }    
  }
}




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