Android Open Source - GhostStories Combat Ghost 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;
// w  w w .j av a2s . c o  m
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import games.ghoststories.R;
import games.ghoststories.data.GhostData;
import games.ghoststories.data.interfaces.IGhostListener;
import games.ghoststories.enums.EColor;
import games.ghoststories.enums.EDragItem;
import games.ghoststories.enums.EHaunterLocation;
import games.ghoststories.utils.GameUtils;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * View for a single ghost during the combat phase of the game. This view contains
 * the image of the ghost, a layer for showing damage to the ghost that sits 
 * on top of the ghost image, and the current health of the ghost below the
 * ghost image. 
 */
public class CombatGhostView extends RelativeLayout implements IGhostListener {

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

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

   /**
    * Constructor
    * @param pContext
    * @param pAttrs
    * @param pDefStyle
    */
   public CombatGhostView(Context pContext, AttributeSet pAttrs, int pDefStyle) {
      super(pContext, pAttrs, pDefStyle);
   }      
   
   /**
    * Adds a damage token to the ghost.
    * @param pTokenId The drawable resource id of the token to add
    * @param pOwner The owner view
    * @param pType The type of the owner view
    * @param pColor The color of the damage token
    */
   public void addDamageToken(int pTokenId, View pOwner, EDragItem pType,
         EColor pColor) {
     for(CombatDamageView cdv : mDamageViews) {
        if(cdv.getVisibility() == INVISIBLE) {
           cdv.setImageResource(pTokenId);
           cdv.setVisibility(VISIBLE);
           cdv.setData(pOwner, this, pType, pColor, pTokenId);
           mDamageViewMap.put(pTokenId, cdv);
           break;
        }
     }
   }
   
   /**
    * Removes a damage token on the ghost
    * @param pTokenId The id of the token to remove
    */
   public void removeDamageToken(int pTokenId) {
      CombatDamageView cdv = mDamageViewMap.get(pTokenId);
      cdv.setVisibility(INVISIBLE);
   }
   
   /**
    * @return The list of {@link CombatDamageView}s for this ghost 
    */
   public List<CombatDamageView> getDamageViews() {
      return Collections.unmodifiableList(mDamageViews);
   }
   
   /*
    * (non-Javadoc)
    * @see games.ghoststories.data.interfaces.IGhostListener#ghostKilled()
    */
   public void ghostKilled() {
   }
   
   /*
    * (non-Javadoc)
    * @see games.ghoststories.data.interfaces.IGhostListener#haunterUpdated(games.ghoststories.enums.EHaunterLocation, games.ghoststories.enums.EHaunterLocation, java.lang.Runnable)
    */
   public void haunterUpdated(EHaunterLocation pOldLocation,
         EHaunterLocation pNewLocation, Runnable pRunnable) {
   }

   /*
    * (non-Javadoc)
    * @see games.ghoststories.data.interfaces.IGhostListener#resistanceUpdated()
    */
   public void resistanceUpdated() {
      //Iterate over the resistance map and update the health views.
      Map<EColor, Integer> resistanceMap = mGhostData.getResistance();
      
      for(Entry<EColor, Integer> entry : resistanceMap.entrySet()) {         
         int ghostHealth = entry.getValue();
         List<GhostHealthView> activeHealths = mActiveHealthViews.get(entry.getKey());
         int activeHealthCount = activeHealths.size();
         int numHealthToDeactivate = activeHealthCount - ghostHealth;
         for(int i = 0; i < numHealthToDeactivate; ++i) {
            activeHealths.get(i).setActive(false);
         }
         for(int i = numHealthToDeactivate; i < activeHealthCount; ++i) {
            activeHealths.get(i).setActive(true);
         }
      }
   }
   
   /**
    * Sets the data model to use for this ghost view. Populates the ghost image,
    * and ghost health using the model
    * @param pGhostData The ghost data model
    */
   public void setGhostData(GhostData pGhostData) {
      mGhostData = pGhostData;
      mNumActiveHealthViews = 0;
      ImageView ghostImage = (ImageView)findViewById(R.id.ghost);
      ghostImage.setImageResource(pGhostData.getImageId());
      
      for(Entry<EColor, Integer> entry : pGhostData.getResistance().entrySet()) {
         EColor color = entry.getKey();
         Integer numHealth = entry.getValue();
         for(int i = 0; i < numHealth; ++i) {
            GhostHealthView ghv = getNextView();
            ghv.setColor(color);
            ghv.setActive(true);
            ghv.setVisibility(VISIBLE);     
            
            if(!mActiveHealthViews.containsKey(color)) {
               mActiveHealthViews.put(color, new ArrayList<GhostHealthView>());
            }
            mActiveHealthViews.get(color).add(ghv);
            mNumActiveHealthViews++;
         }
      }
      
      View topDamage = findViewById(R.id.top_damage);
      mDamageViews.add((CombatDamageView)topDamage.findViewById(R.id.combat_damage_1));
      mDamageViews.add((CombatDamageView)topDamage.findViewById(R.id.combat_damage_2));
      mDamageViews.add((CombatDamageView)topDamage.findViewById(R.id.combat_damage_3));
      mDamageViews.add((CombatDamageView)topDamage.findViewById(R.id.combat_damage_4));
      View bottomDamage = findViewById(R.id.bottom_damage);
      mDamageViews.add((CombatDamageView)bottomDamage.findViewById(R.id.combat_damage_1));
      mDamageViews.add((CombatDamageView)bottomDamage.findViewById(R.id.combat_damage_2));
      mDamageViews.add((CombatDamageView)bottomDamage.findViewById(R.id.combat_damage_3));
      mDamageViews.add((CombatDamageView)bottomDamage.findViewById(R.id.combat_damage_4));
      
      mGhostData.addGhostListener(this);
      GameUtils.invalidateView(this);
   }
   
   /**
    * Helper method for building the health area under the ghost. Returns the 
    * next unused health view.
    * @return The next unused health view
    */
   private GhostHealthView getNextView() {
      GhostHealthView view = null;
      View healthRow = null;
      switch(mNumActiveHealthViews) {
      case 0:         
         healthRow = findViewById(R.id.top_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_1);         
         break;
      case 1:
         healthRow = findViewById(R.id.top_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_2);         
         break;
      case 2:
         healthRow = findViewById(R.id.top_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_3);         
         break;
      case 3:
         healthRow = findViewById(R.id.top_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_4);         
         break;
      case 4:
         healthRow = findViewById(R.id.bottom_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_1);         
         break;
      case 5:
         healthRow = findViewById(R.id.bottom_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_2);         
         break;
      case 6: 
         healthRow = findViewById(R.id.bottom_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_3);         
         break;
      case 7:
         healthRow = findViewById(R.id.bottom_health);
         view = (GhostHealthView)healthRow.findViewById(R.id.combat_health_4);         
         break;
      }
      
      return view;
   }
   
   /**
    *  The health views that are active for this ghost -- The layout contains
    *  the maximum number of possible health views but we only activate a certain
    *  number based on the ghost health.
    **/
   private Map<EColor, List<GhostHealthView>> mActiveHealthViews =          
         new HashMap<EColor, List<GhostHealthView>>();
   /** Counter for the current total number of active health views **/
   private int mNumActiveHealthViews = 0;
   
   /** 
    * List of damage views that lie on the ghost. These are updated as damage
    * is dealt to the ghost
    **/
   private List<CombatDamageView> mDamageViews = new ArrayList<CombatDamageView>();
   
   /** Mapping to keep track of the view associated with each damage view **/
   private Map<Integer, CombatDamageView> mDamageViewMap = 
         new ConcurrentHashMap<Integer, CombatDamageView>();
   
   /** The ghost data model **/
   private GhostData mGhostData = null;            
}




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