Example #1
0
void PrepareForKickOff::Enter(BallTeam* team)
{
    //reset key player pointers
    team->SetControllingPlayer(NULL);
    team->SetSupportingPlayer(NULL);
    team->SetReceiver(NULL);
    team->SetPlayerClosestToBall(NULL);

    int color = team->GetGame()->GetScored();
    if(color == BallTeam::blue)
        printf("blue get scored\n");
    else
        printf("red get scored\n");
    if(team->Color() == BallTeam::blue){
        if(team->GetGame()->GetScored() == BallTeam::blue){
            const int BlueRegions[TeamSize] = {1,6,8,3,5};
            ChangePlayerHomeRegions(team, BlueRegions);
        }
        else {//red team get score
            const int BlueRegions[TeamSize] = {16,20,23,21,13};
            ChangePlayerHomeRegions(team, BlueRegions);
        }
    }
    else //red team
    {
        if(team->GetGame()->GetScored() == BallTeam::red){
            const int RedRegions[TeamSize] = {16,20,23,21,13};
            ChangePlayerHomeRegions(team, RedRegions);
        }
        else{
            const int RedRegions[TeamSize] = {1,6,8,3,5};
            ChangePlayerHomeRegions(team, RedRegions);
        }
    }
    team->UpdateTargetsOfWaitingPlayers();
    if(team->GetGame()->GetScored() == BallTeam::blue) {
        double x = team->GetGame()->PlayingArea()->Left();
        double top = team->GetGame()->PlayingArea()->Top();
        double y = RandInRange(-top, top);
        team->GetGame()->GetBall()->PlaceAtPosition(Vector2D(x,y));
    }
    else if(team->GetGame()->GetScored() == BallTeam::red) 
    {
        double x = team->GetGame()->PlayingArea()->Right();
        double top = team->GetGame()->PlayingArea()->Top();
        double y = RandInRange(-top, top);
        team->GetGame()->GetBall()->PlaceAtPosition(Vector2D(x,y));
    }

    if(team->GetGame()->GetScored() != team->Color()){
        PlayerBase* it = team->GetPreparePlayer();
        FieldPlayer* plyr = static_cast<FieldPlayer*>(it);
        plyr->GetFSM()->ChangeState(ChaseBall::Instance());
        printf("I let player:%d to get ball\n",plyr->ID());
        //getchar();
    }

}
bool BEWD_GlobalPlayerState::OnMessage(FieldPlayer* player, const Telegram& telegram)
{
  switch(telegram.Msg)
  {
  case Msg_ReceiveBall:
    {
      //set the target
      player->Steering()->SetTarget(*(static_cast<Vector2D*>(telegram.ExtraInfo)));

      //change state 
      player->GetFSM()->ChangeState(BEWD_ReceiveBall::Instance());

      return true;
    }

    break;

  case Msg_SupportAttacker:
    {
      //if already supporting just return
      if (player->GetFSM()->isInState(*BEWD_SupportAttacker::Instance()))
      {
        return true;
      }
      
      //set the target to be the best supporting position
      player->Steering()->SetTarget(player->Team()->GetSupportSpot());

      //change the state
      player->GetFSM()->ChangeState(BEWD_SupportAttacker::Instance());

      return true;
    }

    break;

 case Msg_Wait:
    {
      //change the state
      player->GetFSM()->ChangeState(BEWD_Wait::Instance());

      return true;
    }

    break;

  case Msg_GoHome:
    {
      player->SetDefaultHomeRegion();
      
      player->GetFSM()->ChangeState(BEWD_ReturnToHomeRegion::Instance());

      return true;
    }

    break;

  case Msg_PassToMe:
    {  
      
      //get the position of the player requesting the pass 
      FieldPlayer* receiver = static_cast<FieldPlayer*>(telegram.ExtraInfo);

      #ifdef PLAYER_STATE_INFO_ON
      //debug_con << "Player " << player->ID() << " received request from " <<
                    receiver->ID() << " to make pass" << "";
      #endif

      //if the ball is not within kicking range or their is already a 
      //receiving player, this player cannot pass the ball to the player
      //making the request.
      if (player->Team()->Receiver() != NULL ||
         !player->BallWithinKickingRange() )
      {
        #ifdef PLAYER_STATE_INFO_ON
        //debug_con << "Player " << player->ID() << " cannot make requested pass <cannot kick ball>" << "";
        #endif

        return true;
      }
      
      //make the pass   
      player->Ball()->Kick(receiver->Pos() - player->Ball()->Pos(),
                           Prm.MaxPassingForce);

          
     #ifdef PLAYER_STATE_INFO_ON
     //debug_con << "Player " << player->ID() << " Passed ball to requesting player" << "";
     #endif
        
      //let the receiver know a pass is coming 
      Dispatcher->DispatchMsg(SEND_MSG_IMMEDIATELY,
                              player->ID(),
                              receiver->ID(),
                              Msg_ReceiveBall,
                              &receiver->Pos());

      //change state   
      player->GetFSM()->ChangeState(BEWD_Wait::Instance());

      player->FindSupport();

      return true;
    }

    break;

  }//end switch

  return false;
}