Android Open Source - GhostStories Player Board Card Controller






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.controllers;
// w  ww .  ja  v  a  2  s  . c om
import games.ghoststories.R;
import games.ghoststories.data.DragData;
import games.ghoststories.data.GameBoardData;
import games.ghoststories.data.GhostData;
import games.ghoststories.data.GhostStoriesGameManager;
import games.ghoststories.data.PlayerData;
import games.ghoststories.data.village.CircleOfPrayerTileData;
import games.ghoststories.enums.ECardLocation;
import games.ghoststories.enums.EDragItem;
import games.ghoststories.enums.EGamePhase;
import games.ghoststories.enums.EGhostAbility;
import games.ghoststories.enums.EHaunterLocation;
import games.ghoststories.enums.EVillageTile;
import games.ghoststories.utils.GameUtils;
import games.ghoststories.views.combat.CombatView;
import games.ghoststories.views.gameboard.PlayerBoardCardView;

import java.util.List;

import android.app.Activity;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.ViewGroup;

/**
 * Controller class that handles user interactions with the player game boards.
 * This includes dragging and dropping of ghosts onto valid game boards and
 * initiating combat by clicking on the ghosts. Each space on a player game
 * board is represented by a single {@link PlayerBoardCardController}.
 */
public class PlayerBoardCardController implements OnDragListener, OnClickListener {
   
   /**
    * Constructor
    * @param pGameBoardData The data for game board
    * @param pView The view for the game board
    * @param pCardLocation The location of the game board
    */
   public PlayerBoardCardController(GameBoardData pGameBoardData, 
         PlayerBoardCardView pView, ECardLocation pCardLocation) {
      mGameBoardData = pGameBoardData;
      mView = pView;
      mCardLocation = pCardLocation;
      mView.setOnDragListener(this);
      mView.setOnClickListener(this);
   }

   /*
    * (non-Javadoc)
    * @see android.view.View.OnClickListener#onClick(android.view.View)
    */
   public void onClick(View pView) {   
      //Only allow clicking on the ghost during the attack phase if there
      //is a ghost on this space and it is attackable
      GhostStoriesGameManager gm = GhostStoriesGameManager.getInstance();   
      PlayerData primaryPlayer = gm.getCurrentPlayerData();
      GhostData ghostData = mGameBoardData.getGhostData(mCardLocation);
      if(gm.getCurrentGamePhase() == EGamePhase.YangPhase2 && 
            ghostData != null &&
            GameUtils.isGhostAttackable(mGameBoardData.getLocation(), 
                  mCardLocation, primaryPlayer.getLocation())) {
         //Inflate the combat view
         Activity activity = (Activity)mView.getContext();
         LayoutInflater li = activity.getLayoutInflater();         
  
         CombatView view = (CombatView)li.inflate(R.layout.combat, null);

         //Find the other players who are on this tile
         List<PlayerData> secondaryPlayers = 
               gm.getPlayersOnTile(primaryPlayer.getLocation());
         secondaryPlayers.remove(primaryPlayer);

         //Find the adjacent ghost if there is one
         List<GhostData> ghosts = 
               GameUtils.getAdjacentGhosts(primaryPlayer.getLocation());
         ghosts.remove(ghostData);
         GhostData ghost2 = ghosts.size() > 0 ? ghosts.get(0) : null;

         //Set the combat data on the view
         view.setCombatData(primaryPlayer, secondaryPlayers, ghostData, ghost2);         

         activity.addContentView(view, new ViewGroup.LayoutParams(
               ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));         
      }
   }

   /*
    * (non-Javadoc)
    * @see android.view.View.OnDragListener#onDrag(android.view.View, android.view.DragEvent)
    */
   public boolean onDrag(View pView, DragEvent pEvent) {
      boolean handle = false;
      Object data = pEvent.getLocalState();
      if(data instanceof DragData) {
         DragData dragData = (DragData)data;
         if(dragData.getDragItem() == EDragItem.GHOST_CARD) {
            switch(pEvent.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
               handle = true;
               break;
            case DragEvent.ACTION_DROP:
               GhostData ghostData = (GhostData)dragData.getData();

               //Only allow dropping on this space if valid
               if(GameUtils.isSpaceValid(ghostData, mGameBoardData, mCardLocation)) {
                  mGameBoardData.addGhost(ghostData, mCardLocation);
                  handle = true;
                  handleEnterAbility(ghostData);               
               }                               
               break;
            }
         }
      }
      return handle;
   }

   /**
    * Perform the ghosts enter ability when dropped on the game board
    * @param pData The ghost to perform the enter ability for
    */
   private void handleEnterAbility(GhostData pData) {
      GhostStoriesGameManager gm = GhostStoriesGameManager.getInstance();
      boolean advanceGamePhase = true;
      for(EGhostAbility ability : pData.getEnterAbilities()) {
         switch(ability) {
         case ALL_LOSE_ABILITY: //TODO Handle this
            break;
         case ALL_LOSE_TAO: //TODO Handle this
            break;
         case CLEAR_CIRCLE_OF_PRAYER:
            CircleOfPrayerTileData tileData = 
            (CircleOfPrayerTileData)gm.getVillageTile(EVillageTile.CIRCLE_OF_PRAYER);
            tileData.setTokenColor(null);
            break;
         case HAUNT_BOARD:
            pData.setHaunterLocation(EHaunterLocation.BOARD, null);
            break;
         case HAUNT_TILE:
            GameUtils.hauntVillageTile(mGameBoardData.getLocation(), 
                  mCardLocation);
            break;
         case LOSE_ABILITY:
            gm.getPlayerData(mGameBoardData.getColor()).setAbilityActive(false);
            break;
         case LOSE_DIE:
            gm.removeDice();
            break;
         case SUMMON_GHOST:
            //TODO Probably need to make it obvious you need to redraw a ghost
            advanceGamePhase = false;
            break;
         default:
            break;
         }
      }
      if(advanceGamePhase) {
         gm.advanceGamePhase();
      }         
   }

   /** The location of the space this controller handles **/
   private final ECardLocation mCardLocation;
   /** The data for the game board **/
   private final GameBoardData mGameBoardData;
   /** The view for the game board **/
   private final PlayerBoardCardView mView;   
}




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