protected void makeHTransition(HGenericStateFSM _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 HGenericStateFSM(string _stateName, int _stateId, GameObject _gameo, int _hLevel, bool _finalHLevel, HGenericStateFSM _fatherState, AIAgent1 _scriptAIAgent) { stateName = _stateName; stateId = _stateId; gameObject = _gameo; myHLevel = _hLevel; finalHLevel = _finalHLevel; if (_fatherState == null) { fatherState = _fatherState; } agentScript = _scriptAIAgent; myHInitialize += initializeHState; myHUpdate += updateHState; myHFinalize += finalizeHState; myHTransition += checkHierarchyTransitions; myHHandleCollisionEnter += handleHEnCollision; myHHandleTriggerEnter += handleHEnTrigger; }
public HFiglio1FSM(string _stateName, GameObject _gameo, int _hLevel, HGenericStateFSM _fatherState, AIAgent1 _scriptAIAgent) : base(_stateName, _gameo, _hLevel, _scriptAIAgent) { finalHLevel = true; fatherState = _fatherState; myInitialize += initFiglio; myUpdate += updateFiglio; }
//USEFUL METHODS------------------------------------- #region USEFULMETHODS public void addTransition(myStateTransition _method, HGenericStateFSM _state) { allocateMyTransitions(indexTransitions + 1); myTransitions [indexTransitions] += _method; targetStateTransitions [indexTransitions] = _state; indexTransitions++; }
//TRANSITIONS //----------------------------------------------------------------------------------------------------------------------------------------------- protected virtual bool checkHierarchyTransitions(ref HGenericStateFSM _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 (HGenericStateFSM st in states) { if (st == _nextState) { //se appartiene a me, makeHTransition(_nextState); result = false; break; } } } return(result); }
public int getSubStateIndex(HGenericStateFSM state) { for (int i = 0; i < states.Length; i++) { if (states[i] == state) { return(i); } } return(-1); }
public void addState(HGenericStateFSM state) { if (index >= mappedStates.Length) { reallocateMappedStates(); } //Debug.Log ("aggiungo lo stato " + state.StateName + " all'indice " + index); mappedStates [index] = state; //Debug.Log ("ho aggiunto lo stato " + mappedStates [index].StateName + " all'indice " + index); index++; }
void reallocateHStates() { HGenericStateFSM [] tempStates = new HGenericStateFSM[hstatesIndex + 1]; int i = 0; foreach (HGenericStateFSM hst in hstates) { tempStates[i] = hst; i++; } hstates = tempStates; }
public bool setActiveState(HGenericStateFSM _state) { for (int i = 0; i < hstates.Length; i++) { if (hstates[i] == _state) { activeState = hstates[i]; return(true); } } return(false); }
protected virtual void Update() { /* * if (!AIVisible) { * _rigidbody.velocity = new Vector2(0.0f, 0.0f); * return; * } */ //Debug.Log ("stato attivo : " + activeState.StateName); if (!PlayStatusTracker.inPlay) { return; } HGenericStateFSM _nextState = activeState; if (activeState.MyHTransition != null) { bool result = false; result = activeState.MyHTransition(ref _nextState); if (debugPlay) { Debug.Log(" > result " + result + " ref id " + _nextState); } if (result != false) { bool found = false; foreach (HGenericStateFSM st in hstates) { if (st == _nextState) { found = true; break; } } makeTransition(_nextState); } } activeState.MyHUpdate(); }
public bool setActiveState(string _stateName) { for (int i = 0; i < hstates.Length; i++) { if (hstates[i].StateName == _stateName) { activeState = hstates[i]; //activeHStateIndex = i; return(true); } } return(false); }
/* * protected virtual void makeTransition1(int _targetStateID) { * * HGenericStateFSM targetState = statesMap.getStateByID (_targetStateID); * * * * object ob = null; * * int [] hierarchy; * int nextStateInd = -1; * if(activeState.myFinalize != null) { * ob = activeState.myFinalize(); * } * * hierarchy = getHierarchyIndexes (targetState); * nextStateInd = hierarchy [hierarchy.Length - 1]; * * * * if (nextStateInd != -1) { * * setHStatesActiveIndexes(hierarchy); * * activeState = hstates[nextStateInd]; * * if (targetState.myInitialize != null) { * * targetState.myInitialize (ref ob); * * } * * * } * else { * * Debug.Log ("ATTENZIONE - GRAVE ERRORE NELL'INDICIZZAZIONE DEGLI HSTATE FATHER"); * * } * * * * } */ public void addState(HGenericStateFSM _hstate) { if (hstatesIndex == 0) { hstates = new HGenericStateFSM[1]; } else { reallocateHStates(); } hstates [hstatesIndex] = _hstate; if (activeState == null) { #if _DEBUG Debug.Log("Dentro AIAgent setto come active state " + _hstate.StateName); #endif activeState = _hstate; } hstatesIndex++; }
/* * protected virtual void Update2 () { * * if (!PlayStatusTracker.inPlay) * return; * * int nextState = activeState.StateID; * * if (activeState.MyHTransition != null) { * * int result = -1; * * nextState = activeState.MyHTransition(ref result); * * if(debugPlay) * Debug.Log(" > result " + result + " ref id " + nextState); * * if(nextState != -1 ) { * * if(nextState != activeState.StateID) { * * * if(debugPlay) * Debug.Log ("TRANSITION - transizione a ref id " + nextState); * * makeTransition(nextState); * * * } * else { * * Debug.Log ("ATTENZIONE - lo stato destinazione (indice " + nextState + ") è uguale a quello attuale" + activeState.StateName); * * } * } * * } * * activeState.myUpdate (); * * } */ /* * protected virtual void Update1 () { * * if (!PlayStatusTracker.inPlay) * return; * * int nextState = activeState.StateID; * * if (hstates [activeHStateIndex].MyHTransition != null) { * * int result = -1; * * nextState = hstates [activeHStateIndex].MyHTransition(ref result); * * if(debugPlay) * Debug.Log(" > result " + result + " ref id " + nextState); * * if(nextState != -1 ) { * * if(nextState != activeState.StateID) { * * * if(debugPlay) * Debug.Log ("TRANSITION - transizione a ref id " + nextState); * * makeTransition(nextState); * * * } * else { * * Debug.Log ("ATTENZIONE - lo stato destinazione (indice " + nextState + ") è uguale a quello attuale" + activeState.StateName); * * } * } * * } * * hstates[activeHStateIndex].myUpdate (); * * } */ protected virtual void makeTransition(HGenericStateFSM _targetState) { object ob = null; if (activeState == _targetState) { Debug.Log("ATTENZIONE - stato destinazione uguale a stato attuale"); return; } if (activeState.MyHFinalize != null) { //ob = activeState.MyHFinalize(); activeState.MyHFinalize(); } activeState = _targetState; if (activeState.MyHInitialize != null) { activeState.MyHInitialize(); } }
public void setActiveState(HGenericStateFSM _state) { bool found = false; foreach (HGenericStateFSM 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 HEnemyStateFSM(string _stateName, GameObject _gameo, bool _finalHLevel, HGenericStateFSM _fatherState, AIAgent1 _scriptAIAgent) : base(_stateName, _gameo, _finalHLevel, _fatherState, _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; }
void allocateMyTransitions(int len) { if (len == 1) { //first allocation myTransitions = new myStateTransition[len]; targetStateTransitions = new HGenericStateFSM[len]; } else { //reallocate myStateTransition [] tempTrans = new myStateTransition[len]; HGenericStateFSM [] tempStates = new HGenericStateFSM[len]; //reallocate transition int i = 0; foreach (myStateTransition tr in myTransitions) { tempTrans[i] = tr; i++; } i = 0; foreach (HGenericStateFSM tr in targetStateTransitions) { tempStates[i] = tr; i++; } myTransitions = tempTrans; targetStateTransitions = tempStates; //targetStateNameTransitions = tempString; } }
protected virtual bool checkMyTransitions(ref HGenericStateFSM _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); /* * bool isMyTransition = false; * * foreach(HGenericStateFSM targetS in targetStateTransitions) { * * if(targetS==_nextState) { * isMyTransition = true; * break; * } * * * } * * if(isMyTransition) { * * makeHTransition( targetStateTransitions[tn] ); * return -1; * * } * else { * * _nextState = targetStateTransitions[tn]; * return -2; * * } * * */ } } 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); }