bool UnitTest3b::States( State_Machine_Event event, MSG_Object * msg, int state, int substate ) { BeginStateMachine //Global message responses /////////////////////////////////////////////////////////////// DeclareState( STATE_Initialize ) OnEnter ChangeState( STATE_Chain1 ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain1 ) OnMsg( MSG_UnitTestPing ) SendMsgDelayed( 1.0f, MSG_UnitTestAck, msg->GetSender() ); OnMsg( MSG_UnitTestAck ) SendMsg( MSG_UnitTestDone, msg->GetSender() ); OnCCMsg( MSG_UnitTestDone ) ChangeStateDelayed( 1.5f, STATE_Success ); OnCCMsg( MSG_UnitTestMessage ) ChangeState( STATE_Broken ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Success ) OnEnter OutputDebugString( L"UnitTest3b Success\n" ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Broken ) OnEnter OutputDebugString( L"UnitTest3b Broken\n" ); EndStateMachine }
bool UnitTest3a::States( State_Machine_Event event, MSG_Object * msg, int state, int substate ) { BeginStateMachine //Global message responses /////////////////////////////////////////////////////////////// DeclareState( STATE_Initialize ) OnEnter ChangeStateDelayed( 1.0f, STATE_Chain1 ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain1 ) OnEnter SendMsgBroadcastNow( MSG_UnitTestPing, OBJECT_Ignore_Type ); OnMsg( MSG_UnitTestAck ) BroadcastAddToList( msg->GetSender() ); SetCCReceiver( msg->GetSender() ); SendMsgBroadcastToList( MSG_UnitTestAck ); OnMsg( MSG_UnitTestDone ) ClearCCReceiver(); SendMsgDelayedToState( 1.0f, MSG_UnitTestMessage ); OnMsg( MSG_UnitTestMessage ) ChangeState( STATE_Success ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Success ) OnEnter OutputDebugString( L"UnitTest3a Success\n" ); EndStateMachine }
bool UnitTest2c::States( State_Machine_Event event, MSG_Object * msg, int state, int substate ) { BeginStateMachine //Global message responses /////////////////////////////////////////////////////////////// DeclareState( STATE_Initialize ) OnEnter ChangeStateDelayed( 1.0f, STATE_Success ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Success ) OnEnter m_owner->GetBody().SetHealth( 100 ); PopStateMachine(); OutputDebugString( L"UnitTest2c Success\n" ); EndStateMachine }
/** * State machine */ bool SMRandomPath::States( State_Machine_Event event, MSG_Object* msg, int state, int substate ) { BeginStateMachine // global message responses OnMsg(MSG_Damaged) // update health and seek player m_owner->SetHealth( m_owner->GetHealth() - msg->GetIntData() ); PushStateMachine( *new SMCombat( m_owner, g_database.Find(m_idPlayer)->GetID(), true) ); OnMsg(MSG_Reset) // reset machine ChangeState(STATE_PickPath); /*-------------------------------------------------------------------------*/ DeclareState( STATE_PickPath ) OnEnter // start path computation request g_world.AddPathRequest(m_owner->GetGridPosition(), g_world.GetRandomMapLocation(), m_owner->GetID()); OnMsg(MSG_PathComputed) // follow path ChangeStateDelayed(1.0f, STATE_FollowPath); /*-------------------------------------------------------------------------*/ DeclareState( STATE_FollowPath ) OnEnter // set velocity m_owner->SetVelocity(5.0f); m_owner->SetAcceleration(0.5f); OnUpdate PathWaypointList* waypointList = g_world.GetWaypointList(m_owner->GetID()); // check if player nearby D3DXVECTOR3 vPlayerDist = m_owner->GetPosition() - g_database.Find(m_idPlayer)->GetPosition(); if( D3DXVec3Length( &vPlayerDist ) < 4.0f && g_database.Find(m_idPlayer)->GetHealth() > 0 ) { ChangeState( STATE_SwitchToCombat ); } // if out of waypoints, pick new path else if(waypointList->empty()) { ChangeState( STATE_PickPath ); } // move towards waypoint else { // determine direction (ignore height) D3DXVECTOR2 vDirection = (*waypointList->begin()) - m_owner->GetGridPosition(); // determine if the object has arrived if( D3DXVec2Length( &vDirection ) < 0.1f ) { // pop off waypoint waypointList->pop_front(); } else { // set object direction towards position D3DXVec2Normalize(&vDirection, &vDirection); m_owner->SetGridDirection(vDirection); } } OnExit // remove any waypoints g_world.ClearWaypointList(m_owner->GetID()); // stop object m_owner->ResetMovement(); /*-------------------------------------------------------------------------*/ DeclareState( STATE_SwitchToCombat ) OnEnter // push combat state machine PushStateMachine( *new SMCombat( m_owner, g_database.Find(m_idPlayer)->GetID(), false) ); /*-------------------------------------------------------------------------*/ EndStateMachine }
bool UnitTest4::States( State_Machine_Event event, MSG_Object * msg, int state, int substate ) { BeginStateMachine //Global message responses OnMsg( MSG_UnitTestMessage ) ChangeState( STATE_Broken ); OnMsg( MSG_UnitTestBroken ) ChangeState( STATE_Broken ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Initialize ) OnEnter ChangeState( STATE_Chain1 ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain1 ) OnEnter SendMsgDelayedToSubstate( 1.0f, MSG_UnitTestMessage ); OnMsg( MSG_UnitTestMessage ) ChangeSubstate( SUBSTATE_Inside1 ); OnExit SendMsgDelayedToStateMachine( 2.0f, MSG_UnitTestMessage2 ); //Should get caught in STATE_Chain3 DeclareSubstate( SUBSTATE_Inside1 ) OnEnter SendMsgDelayedToSubstate( 1.0f, MSG_UnitTestMessage ); OnMsg( MSG_UnitTestMessage ) SendMsgDelayedToSubstate( 1.0f, MSG_UnitTestBroken ); //Should never be delivered due to scoping ChangeSubstateDelayed( 0.5f, SUBSTATE_Inside2 ); DeclareSubstate( SUBSTATE_Inside2 ) OnEnter SendMsgDelayedToSubstate( 1.0f, MSG_UnitTestMessage2 ); OnMsg( MSG_UnitTestMessage2 ) ChangeSubstate( SUBSTATE_Inside3 ); OnExit SendMsgDelayedToState( 1.0f, MSG_UnitTestMessage3 ); DeclareSubstate( SUBSTATE_Inside3 ) OnMsg( MSG_UnitTestMessage3 ) ChangeState( STATE_Chain2 ); //Should trigger OnExit in both substate and state OnExit SendMsgDelayedToStateMachine( 1.0f, MSG_UnitTestMessage ); //Should get caught in STATE_Chain2 /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain2 ) OnMsg( MSG_UnitTestMessage ) ChangeState( STATE_Chain3 ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain3 ) OnMsg( MSG_UnitTestMessage2 ) ChangeState( STATE_Chain4 ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain4 ) OnEnter SendMsgDelayedToState( 1.0f, MSG_UnitTestMessage ); OnMsg( MSG_UnitTestMessage ) ChangeState( STATE_Chain5 ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain5 ) OnEnter ChangeSubstate( SUBSTATE_Inside1 ); DeclareSubstate( SUBSTATE_Inside1 ) OnEnter ChangeSubstate( SUBSTATE_Inside2 ); OnExit SendMsgDelayedToState( 1.0f, MSG_UnitTestMessage ); DeclareSubstate( SUBSTATE_Inside2 ) OnEnter ChangeStateDelayed( 2.0f, STATE_Broken ); OnMsg( MSG_UnitTestMessage ) ChangeState( STATE_Chain6 ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Chain6 ) OnEnter SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage, MSG_Data(1) ); OnMsg( MSG_UnitTestMessage ) if( msg->IsIntData() && msg->GetIntData() == 1 ) { SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage2, MSG_Data(1.0f) ); } else { ChangeState( STATE_Broken ); } OnMsg( MSG_UnitTestMessage2 ) if( msg->IsFloatData() && msg->GetFloatData() == 1.0f ) { SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage3, MSG_Data(true) ); } else { ChangeState( STATE_Broken ); } OnMsg( MSG_UnitTestMessage3 ) if( msg->IsBoolData() && msg->GetBoolData() == true ) { SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage4, MSG_Data(m_owner->GetID()) ); } else { ChangeState( STATE_Broken ); } OnMsg( MSG_UnitTestMessage4 ) if( msg->IsObjectIDData() && msg->GetObjectIDData() == m_owner->GetID() ) { SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage5, MSG_Data(m_owner) ); } else { ChangeState( STATE_Broken ); } OnMsg( MSG_UnitTestMessage5 ) if( msg->IsPointerData() && msg->GetPointerData() == m_owner ) { D3DXVECTOR2 v = D3DXVECTOR2( 10.0f, 5.0f ); SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage6, MSG_Data( v ) ); } else { ChangeState( STATE_Broken ); } OnMsg( MSG_UnitTestMessage6 ) if( msg->IsVector2Data() && msg->GetVector2Data().x == 10.0f && msg->GetVector2Data().y == 5.0f ) { SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage7, MSG_Data( D3DXVECTOR3( 2.0f, 4.0f, 6.0f ) ) ); } else { ChangeState( STATE_Broken ); } OnMsg( MSG_UnitTestMessage7 ) if( msg->IsVector3Data() && msg->GetVector3Data().x == 2.0f && msg->GetVector3Data().y == 4.0f && msg->GetVector3Data().z == 6.0f ) { SendMsgDelayedToState( 0.2f, MSG_UnitTestMessage8 ); } else { ChangeState( STATE_Broken ); } OnMsg( MSG_UnitTestMessage8 ) if( msg->IsValidData() ) { ChangeState( STATE_Broken ); } else { ChangeState( STATE_Success ); } /////////////////////////////////////////////////////////////// DeclareState( STATE_Success ) OnEnter OutputDebugString( L"UnitTest4 Success\n" ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Broken ) OnEnter OutputDebugString( L"UnitTest4 Broken\n" ); EndStateMachine }
bool Example::States( State_Machine_Event event, MSG_Object * msg, int state, int substate ) { BeginStateMachine //Global message response section OnMsg( MSG_Reset ) ResetStateMachine(); OnMsg( MSG_MouseClick ) m_owner->GetMovement().SetTarget( msg->GetVector3Data() ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Initialize ) OnEnter m_owner->GetMovement().SetIdleSpeed(); ChangeStateDelayed( 1.0f, STATE_Idle ); //m_owner->GetTiny().SetDiffuse(1.0f, 0.0f, 0.0f); //Color Tiny (Example) /////////////////////////////////////////////////////////////// DeclareState( STATE_PickPlayerToChase ) OnEnter m_owner->GetMovement().SetWalkSpeed(); m_curTarget = GetFarthestAgent(); if( m_curTarget == 0 ) { ChangeState( STATE_MoveToRandomTarget ); } SendMsgToStateMachineNow( MSG_SetTargetPosition ); OnMsg( MSG_SetTargetPosition ) GameObject* go = g_database.Find( m_curTarget ); if( go ) { D3DXVECTOR3 target = go->GetBody().GetPos(); m_owner->GetMovement().SetTarget( target ); } SendMsgToState( MSG_SetTargetPosition ); OnMsg( MSG_Arrived ) ChangeState( STATE_Idle ); /////////////////////////////////////////////////////////////// DeclareState( STATE_Idle ) OnEnter m_owner->GetMovement().SetIdleSpeed(); if( rand()%2 == 0 ) { ChangeStateDelayed( RandDelay( 1.0f, 2.0f ), STATE_MoveToRandomTarget ); } else { ChangeStateDelayed( RandDelay( 1.0f, 2.0f ), STATE_PickPlayerToChase ); } /////////////////////////////////////////////////////////////// DeclareState( STATE_MoveToRandomTarget ) OnEnter m_owner->GetMovement().SetJogSpeed(); D3DXVECTOR3 target( 0, 0, 0 ); target.x = (float) ( rand() % 256 ) / 256.f; target.z = (float) ( rand() % 256 ) / 256.f; m_owner->GetMovement().SetTarget( target ); OnMsg( MSG_Arrived ) ChangeState( STATE_Idle ); EndStateMachine }