Android Open Source - LucyTheMoocher Persistent Effect






From Project

Back to project page LucyTheMoocher.

License

The source code is released under:

MIT License

If you think the Android project LucyTheMoocher 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

/**
 * /*  w w w  . ja  va  2  s  .  c o m*/
 */
package com.lucythemoocher.graphics;

import java.util.LinkedList;

import com.lucythemoocher.Globals.Globals;

/**
 * Persistent effect which can be applied on any Drawable using pictures
 * Register a picture and its position with add(Image image, int x, int y)
 * and then it will stay at the screen with a decreasing alpha channel until 
 * it disappears (once add called, you have to use update and draw to make it work).
 * 
 * If the time between two calls to  add is longer than a value (timeBetweenAdd_ value), 
 * pictures will be added with an interpolation so that the pictures appear 
 * regularly and are frame rate independent.
 *
 */
public class PersistentEffect implements Drawable {

  private LinkedList<PersistentPic> list_;
  private int lastX_;
  private int lastY_;
  private float lastDrawTime_;
  private final static int timeBetweenAdd_ = 20;
  /**
   * Constructor
   */
  public PersistentEffect() {
    lastDrawTime_ = 0;
    list_ = new LinkedList<PersistentPic>();
  }
  
  /**
   * Add a picture and its position to the persistence
   * @param image 
   * @param x
   * @param y
   * @param persistenceTime Time until the picture disappears in ms
   */
  public void add(Image image, int x, int y, int persistenceTime) {
    float currentTime = Globals.getInstance().getGame().getTime();
    boolean hasDrawn = false;
    if (lastDrawTime_ == 0) { // first add
      list_.addLast(new PersistentPic(image, x, y, persistenceTime));
      hasDrawn = true;
    } else {
      for (float t = lastDrawTime_ + timeBetweenAdd_; t < currentTime; t += timeBetweenAdd_) {
        float a = (t - lastDrawTime_) / (currentTime - lastDrawTime_);
        int xx = (int)(a * (float)(lastX_ - x)) + x;
        int yy = (int)(a * (float)(lastY_ - y)) + y;
        list_.addLast(new PersistentPic(image, xx, yy, persistenceTime));
        hasDrawn = true;
      }
    }
    if (hasDrawn) {
      lastX_ = (int)x;
      lastY_ = (int)y;
      lastDrawTime_ = Globals.getInstance().getGame().getTime();
    }
  }
  
  /**
   * Draw all the pictures added with correct alpha
   */
  public void draw() {
    for (PersistentPic pic: list_) {
      pic.draw();
    }
  }
  
  /**
   * Update the list of pictures
   */
  public void update() {
    while (!list_.isEmpty() && list_.getFirst().isDead()) {
      list_.removeFirst();
    }  
  }
  
  /**
   * Clear the persistent list
   */
  public void reset() {
    list_.clear();
    lastDrawTime_ = 0;
  }

}




Java Source Code List

com.lucythemoocher.LucyTheMoocherActivity.java
com.lucythemoocher.FX.FXManager.java
com.lucythemoocher.FX.FX.java
com.lucythemoocher.Globals.Globals.java
com.lucythemoocher.actors.Actor.java
com.lucythemoocher.actors.ActorsManager.java
com.lucythemoocher.actors.Monster.java
com.lucythemoocher.actors.MonstersManager.java
com.lucythemoocher.actors.PlayerCharacter.java
com.lucythemoocher.actors.Projectile.java
com.lucythemoocher.actors.ProjectilesManager.java
com.lucythemoocher.actors.Tank.java
com.lucythemoocher.actors.TargetCharacter.java
com.lucythemoocher.actors.maincharacter.state.StateAttack.java
com.lucythemoocher.actors.maincharacter.state.StateFalling.java
com.lucythemoocher.actors.maincharacter.state.StateJumping.java
com.lucythemoocher.actors.maincharacter.state.StateNone.java
com.lucythemoocher.actors.maincharacter.state.StateRunning.java
com.lucythemoocher.actors.maincharacter.state.StateWallSliding.java
com.lucythemoocher.actors.maincharacter.state.StateWallWalking.java
com.lucythemoocher.actors.maincharacter.state.State.java
com.lucythemoocher.controls.AIController.java
com.lucythemoocher.controls.ActionController.java
com.lucythemoocher.controls.ButtonListener.java
com.lucythemoocher.controls.Controllable.java
com.lucythemoocher.controls.Controller.java
com.lucythemoocher.controls.GlobalController.java
com.lucythemoocher.controls.KeysListener.java
com.lucythemoocher.controls.TouchListener.java
com.lucythemoocher.events.EventNormal.java
com.lucythemoocher.events.EventSlow.java
com.lucythemoocher.events.Event.java
com.lucythemoocher.game.GameThread.java
com.lucythemoocher.game.Game.java
com.lucythemoocher.game.LevelLoader.java
com.lucythemoocher.graphics.ActorDrawer.java
com.lucythemoocher.graphics.Animation.java
com.lucythemoocher.graphics.Background.java
com.lucythemoocher.graphics.Camera.java
com.lucythemoocher.graphics.Drawable.java
com.lucythemoocher.graphics.Grid.java
com.lucythemoocher.graphics.HUD.java
com.lucythemoocher.graphics.Image.java
com.lucythemoocher.graphics.PersistentEffect.java
com.lucythemoocher.graphics.PersistentPic.java
com.lucythemoocher.graphics.PictureContainer.java
com.lucythemoocher.gui.MenuButtonListener.java
com.lucythemoocher.gui.MenuButtonTouchListener.java
com.lucythemoocher.gui.MenuButton.java
com.lucythemoocher.loops.CreditsLoop.java
com.lucythemoocher.loops.GameOverLoop.java
com.lucythemoocher.loops.InitMenuLoop.java
com.lucythemoocher.loops.LivesMenuLoop.java
com.lucythemoocher.loops.LoopGame.java
com.lucythemoocher.loops.LoopPause.java
com.lucythemoocher.loops.Loop.java
com.lucythemoocher.loops.MasterLoop.java
com.lucythemoocher.physics.Box.java
com.lucythemoocher.physics.Cinematic.java
com.lucythemoocher.physics.Map.java
com.lucythemoocher.sounds.SoundManager.java
com.lucythemoocher.sounds.SoundsState.java
com.lucythemoocher.sounds.StateLevel1.java
com.lucythemoocher.sounds.StateLevel2.java
com.lucythemoocher.sounds.StateLevel3.java
com.lucythemoocher.sounds.StateNormal.java
com.lucythemoocher.util.Direction.java
com.lucythemoocher.util.MathUtil.java
com.lucythemoocher.util.Resources.java
com.lucythemoocher.util.Timer.java