Android Open Source - X3n0break Effect Manager






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  v  a 2s . co m*/
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.x3n0m0rph59.breakout.Effect.Type;

import com.badlogic.gdx.Gdx;

public final class EffectManager implements Serializable {
  /**
   * 
   */
  private static final long serialVersionUID = 8434448834555883310L;
  
  private static EffectManager instance = new EffectManager();  
  private List<Effect> effectList = new ArrayList<Effect>();
  
    
  public void addEffect(Effect.Type type) {    
    // perform basic sanity checking on new effects
    if (!isEffectAllowed(type)) {
      Logger.debug("Disallowed effect: " + type);
      
      return;
      
    } else {
      effectList.add(new Effect(type, Gdx.graphics.getFramesPerSecond()
          * Config.EFFECT_DURATION));

      switch (type) {
      case BOTTOM_WALL:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Bottom Wall!");
        break;

      case EXPAND_PADDLE:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Expand!");
        break;

      case FIREBALL:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Fireball!");
        break;

      case MULTIBALL:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Multiball");
        break;

      case PADDLE_GUN:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Guns!");
        break;

      case SHRINK_PADDLE:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Shrink!");
        break;

      case STICKY_BALL:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Sticky Ball!");
        break;

      case SPEED_UP:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Speed Up!");
        break;

      case SLOW_DOWN:
        ((App) Gdx.app.getApplicationListener()).getGameScreen()
            .addTextAnimation("Slow Down!");
        break;

      default:
        throw new RuntimeException("Unsupported type: " + type);
      }

      Logger.debug("New active effect: " + type);
    }
  }
  
  public void expireEffect(Effect e) {    
    switch (e.getType()) {
    case BOTTOM_WALL:
      // Test if we really are the last active effect of this type
      if (!isEffectActive(Effect.Type.BOTTOM_WALL))
        ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("No more Bottom Wall!");
      break;
      
    case EXPAND_PADDLE:
      ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("Shrink again!");
      break;
      
    case FIREBALL:
      // Test if we really are the last active effect of this type
      if (!isEffectActive(Effect.Type.FIREBALL))
        ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("Fireball vanished!");
      break;
      
    case MULTIBALL:      
      break;
      
    case PADDLE_GUN:
      // Test if we really are the last active effect of this type
      if (!isEffectActive(Effect.Type.PADDLE_GUN))
        ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("Guns jammed!");
      break;
      
    case SHRINK_PADDLE:
      ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("Grow back!");
      break;  
      
    case STICKY_BALL:
      // Test if we really are the last active effect of this type
      if (!isEffectActive(Effect.Type.STICKY_BALL))
        ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("No more Sticky Ball!");
      break;
      
    case SPEED_UP:
      ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("Slow down again!");
      break;  
      
    case SLOW_DOWN:
      ((App) Gdx.app.getApplicationListener()).getGameScreen().addTextAnimation("Speed up again!");
      break;
      
    default:
      throw new RuntimeException("Unsupported type: " + e.getType());    
    }
    
    Logger.debug("Effect expired: " + e.getType());
  }
  
  public boolean isEffectActive(Effect.Type effect) {
    for (final Effect e : effectList) {
      if (e.getType() == effect)
        return true;      
    }
    
    return false;
  }
  
  public boolean isEffectInGracePeriod(Effect.Type effect) {
    List<Effect> candidates = new ArrayList<Effect>();
    
    for (final Effect e : effectList) {
      if (e.getType() == effect) {
        candidates.add(e);
      }
    }
    
    Collections.sort(candidates, new Comparator<Effect>() {
              public int compare(Effect o1, Effect o2) { 
                return (int) (o2.getEffectDuration() - o1.getEffectDuration());
              };
            });
    
    if (candidates.size() > 0)
      if (candidates.get(0).getEffectDuration() <= Config.EFFECT_GRACE_PERIOD)
        return true;
    
    return false;
  }
  
  public boolean isEffectAllowed(Effect.Type type) {
    boolean allowed = true;
    
    switch (type) {
    case BOTTOM_WALL:
      break;
      
    case EXPAND_PADDLE:
      if (((App) Gdx.app.getApplicationListener()).getGameScreen().getPaddle().getWidth() >= 
          Config.PADDLE_MAX_WIDTH)
        allowed = false;
      break;
      
    case FIREBALL:
      break;
      
    case MULTIBALL:
      break;
      
    case PADDLE_GUN:
      break;
      
    case SHRINK_PADDLE:
      if (((App) Gdx.app.getApplicationListener()).getGameScreen().getPaddle().getWidth() <= 
        Config.PADDLE_MIN_WIDTH)
        allowed = false;
      break;
      
    case SLOW_DOWN:
      if (isEffectActive(Type.SLOW_DOWN))
        allowed = false;
      break;
      
    case SPEED_UP:
      if (isEffectActive(Type.SPEED_UP))
        allowed = false;
      break;
      
    case STICKY_BALL:
      break;
      
    default:
      break;    
    }
    
    return allowed;
  }
  
  public void step(float delta) {    
    final Iterator<Effect> i = effectList.iterator();        
    while (i.hasNext()) {
      Effect e = i.next();
      e.step(delta);
      
      if (e.isExpired()) {
        expireEffect(e);    
        i.remove();
      }
    }
  }
  
  public void clearEffects() {
    for (final Effect e : effectList)
      e.expire();
    
    effectList.clear();
  }
  
  public static EffectManager getInstance() {
    return instance;
  }

  static void setInstance(EffectManager e) {
    instance = e;
  }  
}




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