protected void makeHTransition(HStateFSM _nextState) { #if _DEBUG Debug.Log("TRANSIZIONE INTERNA - sono " + stateName + " e passo da " + activeState.StateName + " a " + _nextState.StateName + "++++++++++++++++++++++++++"); #endif if (activeState == _nextState) { #if _WARNING_DEBUG Debug.Log("ATTENZIONE - stato destinazione uguale a stato attuale"); #endif return; } if (activeState.myFinalize != null) { activeState.myFinalize(); } activeState = _nextState; #if _DEBUG Debug.Log("sto per inizializzare " + activeState.stateName); #endif if (activeState.myInitialize != null) { activeState.myInitialize(); } }
public HStateFSM(string _stateName, int _stateId, GameObject _gameo, int _hLevel, bool _finalHLevel, HStateFSM _fatherState, AIAgent1 _scriptAIAgent) { //GameObject gameo = this.gameObject; stateName = _stateName; stateId = _stateId; gameObject = _gameo; myHLevel = _hLevel; finalHLevel = _finalHLevel; if (_fatherState == null) { fatherState = _fatherState; } //TODO : ? //agentScript = gameObject.GetComponent<AIAgent1> (); agentScript = _scriptAIAgent; playerScript = gameObject.GetComponent <PlayerMovements> (); if (playerScript == null) { #if _WARNING_DEBUG Debug.Log("ATTENZIONE - script PlayerMovements non trovato"); #endif } par = gameObject.GetComponent <AIParameters> (); if (par == null) { #if _WARNING_DEBUG Debug.Log("ATTENZIONE - script AIParameters non trovato"); #endif } statusPar = par.statusParameters; myHInitialize += initializeHState; myHUpdate += updateHState; myHFinalize += finalizeHState; myHTransition += checkHierarchyTransitions; myHHandleCollisionEnter += handleHEnCollision; myHHandleTriggerEnter += handleHEnTrigger; //agentScript.statesMap.addState (this); }
/* * private int getIndexState (string _stateName) { * * int ind = agentScript.statesMap.getStateIDByName (_stateName); * if(_DEBUG_PLAY) * Debug.Log ("stato " + _stateName + " preso"); * * return ind; * * } * * protected int getState (string _stateName) { * * int output = getIndexState (_stateName); * * if (output == -1) { * * Debug.Log ("ATTENZIONE - stato " + _stateName + " rimasto a -1"); * * } * * return output; * * } */ public void addTransition(myStateTransition _method, HStateFSM _state) { allocateMyTransitions(indexTransitions + 1); myTransitions [indexTransitions] += _method; targetStateTransitions [indexTransitions] = _state; indexTransitions++; }
//TRANSITIONS //----------------------------------------------------------------------------------------------------------------------------------------------- protected virtual bool checkHierarchyTransitions(ref HStateFSM _nextState) { bool result = false; result = checkMyTransitions(ref _nextState); //TODO : e qui? //if(result != false) //Debug.Log("-> result " + result + ", nextstate : " + _nextState + " post transition of " + StateName); if (!finalHLevel && result == false) { //se al mio livello non è stata rilevata nessuna transizione //dobbiamo scavare ancora e scoprire se nei livelli sottostanti serve fare una transizione if (activeState.myHTransition != null) { result = activeState.myHTransition(ref _nextState); } else { #if _WARNING_DEBUG Debug.Log("ATTENZIONE - HFSM - finalize del sotto stato " + activeState.StateName + " è nulla "); #endif } } //se ho rilevato una transizione, devo capire se devo occuparmene io o meno //in caso positivo, cioè lo stato finale è sotto uno dei miei, faccio la transizione e rimetto result a -1, //così che sopra di me non si accorgano di nulla //altrimenti, se non è di mia competenza, lascio result != -1 e se ne occuperà qualcuno sopra di me if (/*HLevel == 0 &&*/ !finalHLevel && result != false) { //TODO: come fare per cambiare macro stato //Debug.Log ("devo faare qualcosa? sono " + StateName + " e devo passare a " + _nextState.StateName); foreach (HStateFSM st in states) { if (st == _nextState) { //se appartiene a me, makeHTransition(_nextState); result = false; break; } } } return(result); }
public int getSubStateIndex(HStateFSM state) { for (int i = 0; i < states.Length; i++) { if (states[i] == state) { return(i); } } return(-1); }
void reallocateHStates() { HStateFSM [] tempStates = new HStateFSM[statesIndex + 1]; int i = 0; foreach (HStateFSM hst in states) { tempStates[i] = hst; i++; } states = tempStates; }
protected virtual bool checkMyTransitions(ref HStateFSM _nextState) { bool transitionDone = false; bool result = false; if (myTransitions == null) { #if _DEBUG Debug.Log("ATTENZIONE - livello gerarchia " + myHLevel + ", NESSUNA TRANSIZIONE presente nello stato " + stateName); #endif return(result); } for (int tn = 0; tn < myTransitions.Length; tn++) { if (myTransitions[tn] != null) { result = myTransitions[tn](); #if _DEBUG Debug.Log("-> -> result è " + result + " e ref id " + _nextState); #endif if (result != false) { _nextState = targetStateTransitions[tn]; return(true); } } else { #if _WARNING_DEBUG Debug.Log("ATTENZIONE - livello gerarchia " + myHLevel + ", la func transition numero " + tn + " dello stato " + StateName + " è null"); #endif } } //dovrebbe essere -1, cioè nessuna transizione return(result); }
public void setActiveState(HStateFSM _state) { bool found = false; foreach (HStateFSM st in states) { if (_state == st) { found = true; } } if (found) { activeState = _state; } else { #if _WARNING_DEBUG Debug.Log("ATTENZIONE - tentativo di settare come stato attivo uno stato non figlio"); #endif } }
public void addState(HStateFSM _hstate) { if (statesIndex == 0) { states = new HStateFSM[1]; } else { reallocateHStates(); } states [statesIndex] = _hstate; if (activeState == null) { #if _DEBUG Debug.Log("Dentro " + stateName + " setto come active state " + _hstate.StateName); #endif activeState = _hstate; } statesIndex++; }
/* * public void addTransition(myStateTransition _method, string _stateName) { * * * allocateMyTransitions (indexTransitions + 1); * * myTransitions [indexTransitions] += _method; * * targetStateTransitions [indexTransitions] = _stateName; * * targetStateIndexTransitions [indexTransitions] = -1; * * indexTransitions++; * * } */ void allocateMyTransitions(int len) { if (len == 1) { //first allocation myTransitions = new myStateTransition[len]; targetStateTransitions = new HStateFSM[len]; } else { //reallocate myStateTransition [] tempTrans = new myStateTransition[len]; HStateFSM [] tempStates = new HStateFSM[len]; //reallocate transition int i = 0; foreach (myStateTransition tr in myTransitions) { tempTrans[i] = tr; i++; } i = 0; foreach (HStateFSM tr in targetStateTransitions) { tempStates[i] = tr; i++; } myTransitions = tempTrans; targetStateTransitions = tempStates; //targetStateNameTransitions = tempString; } }
public HFinalChildFSM(string _stateName, GameObject _gameo, int _hLevel, HStateFSM _fatherState, AIAgent1 _scriptAIAgent) : base(_stateName, _gameo, _hLevel, _scriptAIAgent) { }