Android Open Source - GhostStories Combat Dice View






From Project

Back to project page GhostStories.

License

The source code is released under:

GNU General Public License

If you think the Android project GhostStories 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 games.ghoststories.views.combat;
/*from  w w w . ja v  a2s  .  c om*/
import games.ghoststories.data.DragData;
import games.ghoststories.data.GhostStoriesConstants;
import games.ghoststories.enums.EDiceSide;
import games.ghoststories.enums.EDragItem;

import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;

import com.interfaces.IDraggable;
import com.views.ToggledImageView;

/**
 * View that represents a single dice that is shown during the combat phase.
 * This dice is "rolled" when the player hits the ROLL button and supports
 * being dragged onto a ghost to attempt to "attack" it.
 */
public class CombatDiceView extends ToggledImageView implements IDraggable<DragData> {

   /**
    * Constructor
    * @param pContext
    */
   public CombatDiceView(Context pContext) {
      super(pContext);
      init();
   }

   /**
    * Constructor
    * @param pContext
    * @param pAttrs
    */
   public CombatDiceView(Context pContext, AttributeSet pAttrs) {
      super(pContext, pAttrs);
      init();
   }

   /**
    * Constructor
    * @param pContext
    * @param pAttrs
    * @param pDefStyle
    */
   public CombatDiceView(Context pContext, AttributeSet pAttrs, int pDefStyle) {
      super(pContext, pAttrs, pDefStyle);
      init();
   }
   
   /**
    * Cancels the dice rolling animation
    */
   public void cancelDiceAnimation() {
      mAnimating = false;            
      animate().cancel();
   }
   
   /*
    * (non-Javadoc)
    * @see com.interfaces.IDraggable#getDragData()
    */
   public DragData getDragData() {
      return new DragData(EDragItem.COMBAT_DICE, mDiceSide, this);
   }

   /**
    * Called to start the dice rolling animation for this dice
    * @param pRunnable The runnable to execute once the dice is done animating
    */
   public void startDiceAnimation(Runnable pRunnable) {      
      if(!mAnimating) {
         mAnimating = true;
         mAnimationCount = 0;
         animateDice();
         mHandler.sendMessageDelayed(Message.obtain(mHandler, 0, 0), 100);
         mRunnable = pRunnable;
      }       
   }
   
   /**
    * Called during the animation cycle to randomly get the next side
    * of the dice to show.
    * @return The drawable for the next side of the dice to show
    */
   protected int getNextDiceDrawable() {
      mDiceSide = sDiceSides.get(GhostStoriesConstants.sRandom.nextInt(
            sDiceSides.size()));
      return mDiceSide.getDiceDrawable();
   }

   /**
    * Performs one animation cycle on the dice. When this cycle is over will
    * call {@link #mEndAction}.
    */
   private void animateDice() {
      animate().setDuration(200).rotationBy(20*mFactor).withEndAction(mEndAction);        
   }
   
   /**
    * Initialize this view with random orientation and color
    */
   private void init() {      
      int rotation = GhostStoriesConstants.sRandom.nextInt(360);
      setRotation(rotation);
      
      //Pick a random dice color as the starting color          
      setImageResource(getNextDiceDrawable());
   }   
   
   /** List of possible dice sides **/
   protected static final List<EDiceSide> sDiceSides = 
         Arrays.asList(EDiceSide.values());
      
   /** The current face up dice side **/
   protected EDiceSide mDiceSide;
   
   /** Maximum number of animation cycles **/
   private static int sMaxAnimationCount = 10;
   
   /** Whether or not the dice is currently animating **/
   private boolean mAnimating = false;
   /** Number of animation cycles that have run in the current animation **/
   private int mAnimationCount = 0;
   /** Helper used to speed up/slow down the dice rolling rotation animation **/
   private int mFactor = 1;
   /** Handler used to send messages to trigger changing the dice sides **/
   private Handler mHandler = new DiceRollerHandler(this);      
   /** Runnable to trigger when animation completes **/
   private Runnable mRunnable;

   /**
    * Runnable that is triggered after every cycle of the dice rolling animation.
    * If there are still more cycles to run, then does it. If we have hit the
    * end of the animation then cancel the animation and notify the caller
    * via the {@link #mRunnable} runnable.
    */
   private Runnable mEndAction = new Runnable() {
      public void run() {
         mFactor *= -1;
         mAnimationCount++;
         if(mAnimationCount >= sMaxAnimationCount) {
            cancelDiceAnimation();
            if(mRunnable != null) {
               mRunnable.run();
            }
         } else {
            animateDice();   
         }         
      }
   };
   
   /**
    * Handler used to update the dice image during the animation
    */
   static class DiceRollerHandler extends Handler {
      private WeakReference<CombatDiceView> mRef;
      public DiceRollerHandler(CombatDiceView pView) {
         mRef = new WeakReference<CombatDiceView>(pView);
      }

      @Override
      public void handleMessage(Message msg) {  
         CombatDiceView view = mRef.get();
         if(view != null && view.mAnimating) {                   
            view.setImageResource(view.getNextDiceDrawable());                  
            sendMessageDelayed(Message.obtain(this, 0, 0), 100);
         }         
      }
   };
}




Java Source Code List

com.drawable.shapes.GradientRectangle.java
com.interfaces.IDraggable.java
com.utils.AndroidUtils.java
com.utils.AnimationUtils2.java
com.utils.ImageLoadingTask.java
com.utils.ImageRotationTask.java
com.utils.ImageViewUtils.java
com.views.NumberedImageView.java
com.views.ToggledImageView.java
com.views.layouts.ScaledLinearLayout.java
com.views.layouts.ScaledRelativeLayout.java
com.views.layouts.SquareGridLayout.java
com.views.layouts.SquareLinearLayout.java
com.views.layouts.SquareTableLayout.java
com.views.layouts.ZoomableRelativeLayout.java
com.views.listeners.DragTouchListener.java
games.ghoststories.activities.GameLoadingActivity.java
games.ghoststories.activities.GameScreenActivity.java
games.ghoststories.activities.TitleActivity.java
games.ghoststories.controllers.GhostDeckController.java
games.ghoststories.controllers.HaunterController.java
games.ghoststories.controllers.PlayerBoardCardController.java
games.ghoststories.controllers.VillageTileController.java
games.ghoststories.controllers.combat.CombatAreaController.java
games.ghoststories.controllers.combat.DiceDragListener.java
games.ghoststories.controllers.combat.GhostDragListener.java
games.ghoststories.controllers.combat.TaoTokenDragListener.java
games.ghoststories.data.DragData.java
games.ghoststories.data.GameBoardData.java
games.ghoststories.data.GhostData.java
games.ghoststories.data.GhostDeckData.java
games.ghoststories.data.GhostGraveyardData.java
games.ghoststories.data.GhostStoriesBitmaps.java
games.ghoststories.data.GhostStoriesConstants.java
games.ghoststories.data.GhostStoriesGameManager.java
games.ghoststories.data.PlayerData.java
games.ghoststories.data.TokenSupplyData.java
games.ghoststories.data.interfaces.IGameBoardListener.java
games.ghoststories.data.interfaces.IGamePhaseListener.java
games.ghoststories.data.interfaces.IGameTokenListener.java
games.ghoststories.data.interfaces.IGhostDeckListener.java
games.ghoststories.data.interfaces.IGhostListener.java
games.ghoststories.data.interfaces.ITokenListener.java
games.ghoststories.data.interfaces.IVillageTileListener.java
games.ghoststories.data.village.BuddhistTempleTileData.java
games.ghoststories.data.village.CircleOfPrayerTileData.java
games.ghoststories.data.village.VillageTileDataFactory.java
games.ghoststories.data.village.VillageTileData.java
games.ghoststories.enums.EBoardLocation.java
games.ghoststories.enums.ECardLocation.java
games.ghoststories.enums.EColor.java
games.ghoststories.enums.ECombatPhase.java
games.ghoststories.enums.EDiceSide.java
games.ghoststories.enums.EDice.java
games.ghoststories.enums.EDifficulty.java
games.ghoststories.enums.EDragItem.java
games.ghoststories.enums.EGamePhase.java
games.ghoststories.enums.EGhostAbility.java
games.ghoststories.enums.EHaunterLocation.java
games.ghoststories.enums.EPlayerAbility.java
games.ghoststories.enums.ETileLocation.java
games.ghoststories.enums.EVillageTile.java
games.ghoststories.fragments.AuxAreaFragment.java
games.ghoststories.fragments.GameboardFragment.java
games.ghoststories.utils.BitmapUtils.java
games.ghoststories.utils.GameUtils.java
games.ghoststories.utils.XmlUtils.java
games.ghoststories.views.GameScreen.java
games.ghoststories.views.aux_area.CardInfoView.java
games.ghoststories.views.aux_area.GamePhaseDetailsView.java
games.ghoststories.views.aux_area.GamePhaseView.java
games.ghoststories.views.aux_area.GhostDeckView.java
games.ghoststories.views.aux_area.GhostGraveyardCardView.java
games.ghoststories.views.aux_area.PlayerInfoView.java
games.ghoststories.views.combat.CombatDamageView.java
games.ghoststories.views.combat.CombatDiceAreaView.java
games.ghoststories.views.combat.CombatDiceView.java
games.ghoststories.views.combat.CombatGhostView.java
games.ghoststories.views.combat.CombatInstructionsView.java
games.ghoststories.views.combat.CombatRollView.java
games.ghoststories.views.combat.CombatView.java
games.ghoststories.views.combat.ExtraCombatDiceView.java
games.ghoststories.views.combat.GhostHealthView.java
games.ghoststories.views.common.AbstractNumberedTokenView.java
games.ghoststories.views.common.BuddhaTokenView.java
games.ghoststories.views.common.QiTokenView.java
games.ghoststories.views.common.TaoTokenView.java
games.ghoststories.views.common.YinYangTokenView.java
games.ghoststories.views.gameboard.PlayerAreaView.java
games.ghoststories.views.gameboard.PlayerBoardCardView.java
games.ghoststories.views.gameboard.PlayerBoardView.java
games.ghoststories.views.gameboard.PlayerTokenAreaView.java
games.ghoststories.views.gameboard.VillageTileView.java
games.ghoststories.views.graveyard.GraveyardScrollView.java
games.ghoststories.views.graveyard.GraveyardView.java
games.ghoststories.views.title.TitleButton.java
games.ghoststories.views.title.TitleScreen.java
games.ghoststories.views.village.CircleOfPrayerView.java