VActorStateTable.cpp
Engine/source/Verve/VActor/VActorStateTable.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Verve 4// Copyright (C) 2014 - Violent Tulip 5// 6// Permission is hereby granted, free of charge, to any person obtaining a copy 7// of this software and associated documentation files (the "Software"), to 8// deal in the Software without restriction, including without limitation the 9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10// sell copies of the Software, and to permit persons to whom the Software is 11// furnished to do so, subject to the following conditions: 12// 13// The above copyright notice and this permission notice shall be included in 14// all copies or substantial portions of the Software. 15// 16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22// IN THE SOFTWARE. 23//----------------------------------------------------------------------------- 24#include "VActorStateTable.h" 25#include "VActor.h" 26 27//----------------------------------------------------------------------------- 28 29bool VActorStateTable::isRegisteredState( VActorState *pState ) 30{ 31 for ( tStateConstIterator itr = mStateVector.begin(); itr != mStateVector.end(); itr++ ) 32 { 33 // Target State? 34 if ( ( *itr ).State == pState ) 35 { 36 // Yes. 37 return true; 38 } 39 } 40 41 // No. 42 return false; 43} 44 45void VActorStateTable::clear( void ) 46{ 47 // Clear the States. 48 mLastState = NULL; 49 mCurrentState = NULL; 50 51 // Clear the State Vector. 52 mStateVector.clear(); 53}; 54 55void VActorStateTable::sort( void ) 56{ 57 mStateVector.sort( &_onSortCallback ); 58} 59 60void VActorStateTable::registerState( VActorState *pState, const F32 &pPriority ) 61{ 62 // Already a State? 63 if ( isRegisteredState( pState ) ) 64 { 65 // Exit Now. 66 return; 67 } 68 69 // Create the Reference. 70 sStateRef entry; 71 entry.State = pState; 72 entry.Priority = pPriority; 73 74 // Push to Back. 75 mStateVector.push_back( entry ); 76 77 // Set Current? 78 if ( mStateVector.size() == 1 ) 79 { 80 // Set State. 81 setState( pState ); 82 } 83}; 84 85void VActorStateTable::setState( VActorState *pState ) 86{ 87 if ( !mObject || !pState || pState == mCurrentState ) 88 { 89 // Invalid. 90 return; 91 } 92 93 if ( mCurrentState ) 94 { 95 // Exit. 96 exit(); 97 98 // Exit the Old State. 99 mCurrentState->exit( mObject ); 100 } 101 102 // Update States. 103 mLastState = mCurrentState; 104 mCurrentState = pState; 105 106 // Enter. 107 enter(); 108 109 // Enter the New State. 110 pState->enter( mObject ); 111}; 112 113VActorState *VActorStateTable::execute( void ) 114{ 115 if ( !mObject || !mCurrentState ) 116 { 117 // Invalid. 118 return NULL; 119 } 120 121 for ( tStateConstIterator itr = mStateVector.begin(); itr != mStateVector.end(); itr++ ) 122 { 123 // Fetch State Reference. 124 const sStateRef &stateRef = ( *itr ); 125 126 // Enter State? 127 if ( stateRef.State->execute( mObject ) ) 128 { 129 // Set the State. 130 setState( stateRef.State ); 131 132 // Return. 133 return stateRef.State; 134 } 135 } 136 137 // No Valid Entries, Ouch! 138 Con::warnf( "VActorStateTable::execute() - No Valid Entries." ); 139 140 // Return Current State. 141 return mCurrentState; 142} 143 144S32 QSORT_CALLBACK VActorStateTable::_onSortCallback( const VActorStateTable::sStateRef *pA, const VActorStateTable::sStateRef *pB ) 145{ 146 if ( pB->Priority > pA->Priority ) 147 { 148 return 1; 149 } 150 else if ( pB->Priority < pA->Priority ) 151 { 152 return -1; 153 } 154 155 return 0; 156} 157