public float radiusRun = 15f; //radio para huir void Start() { //INICIALIZAMOS LA DATA DEL EXTERIOR kinMeowth = meowth.kineticsAgent; steeringMeowth = meowth.steeringAgent; kineticsTarget = target.kineticsAgent; kineticsTrainer = trainer.kineticsAgent; kineticsRival = rival.kineticsAgent; // Centro de masa de glameow y meowth sera util pra cierta condicion Vector3 center = (kinMeowth.transform.position + kineticsTarget.transform.position) / 2; //COMENZAMOS A CONSTRUIR LA MAQUINA DE ESTADOS //1. ACCIONES: FollowTarget seekTarget = new FollowTarget(steeringMeowth, kinMeowth, kineticsTarget, maxSeekAccel); FollowTarget seekWorried = new FollowTarget(steeringMeowth, kinMeowth, kineticsTarget, 100f); Kinetics[] targets = new Kinetics[2]; targets[0] = kineticsTrainer; targets[1] = kineticsRival; RunFromTargets runFromTargets = new RunFromTargets(steeringMeowth, kinMeowth, targets, maxSeekAccel * 5); StopMoving stop = new StopMoving(kinMeowth, steeringMeowth); UpdateMaxSpeed moreMaxSpeed = new UpdateMaxSpeed(meowth, meowth.maxspeed * 5); // esto ayudara a aumentar la maxspeed UpdateMaxSpeed speedBackToNormal = new UpdateMaxSpeed(meowth, meowth.maxspeed); // esto guarda la maxspeed original para volverla a poner asi ShowIcon showHeart = new ShowIcon(this.gameObject, "Heart"); DisableIcon disableHeart = new DisableIcon(this.gameObject, "Heart"); ShowIcon showSweat = new ShowIcon(this.gameObject, "Sweat"); DisableIcon disableSweat = new DisableIcon(this.gameObject, "Sweat"); ShowIcon showExclamation = new ShowIcon(this.gameObject, "Exclamation"); DisableIcon disableExclamation = new DisableIcon(this.gameObject, "Exclamation"); //2. ESTADOS: List <Action> entryActions; //aqui iremos guardanndo todas las acciondes de entrada List <Action> exitActions; //aqui iremos guardanndo todas las acciones de salida List <Action> actions; //aqui guardaremos todas las acciones intermedias //2.a estado para perseguir enamorado (glameow) entryActions = new List <Action>() { showHeart }; //al entrar al estado ponemos un corazon actions = new List <Action>() { seekTarget }; //durante el estado perseguimos al enamorado exitActions = new List <Action>() { disableHeart }; //al salir quitamos el corazon State stalkTarget = new State(actions, entryActions, exitActions); //2.b estado para alertarse de entrenador cercano entryActions = new List <Action>() { showExclamation, stop }; //al entrar al estado debemos mostrar un signo de exclamacion actions = new List <Action>(); exitActions = new List <Action>() { disableExclamation }; //al salir quitamos el signo State alert = new State(actions, entryActions, exitActions); //2,c estado para perseguir sin corazon entryActions = new List <Action>();//al entrar al estado ponemos un corazon actions = new List <Action>() { seekTarget }; //durante el estado perseguimos al enamorado exitActions = new List <Action>(); //al salir quitamos el corazon State stalk = new State(actions, entryActions, exitActions); //2.d estado para huir del entrenador entryActions = new List <Action>() { moreMaxSpeed }; actions = new List <Action>() { runFromTargets }; exitActions = new List <Action>() { speedBackToNormal }; State runAway = new State(actions, entryActions, exitActions); //2.e estado para preocuparse por alguien y seguirlo preocupado entryActions = new List <Action>() { showSweat, disableHeart, disableExclamation }; actions = new List <Action>() { seekWorried }; exitActions = new List <Action>() { disableSweat }; State worry = new State(actions, entryActions, exitActions); //3. CONDICIONES: TooCloseToPoint closeCenterTrainer = new TooCloseToPoint(center, kineticsTrainer, radiusAlert); TooClose closeTrainer = new TooClose(kinMeowth, kineticsTrainer, radiusAlert); TooClose veryCloseTrainer = new TooClose(kinMeowth, kineticsTrainer, radiusRun); TooCloseToPoint closeCenterRival = new TooCloseToPoint(center, kineticsRival, radiusAlert); TooClose closeRival = new TooClose(kinMeowth, kineticsRival, radiusAlert); TooClose veryCloseRival = new TooClose(kinMeowth, kineticsRival, radiusRun); //Estas son las que de verdad necesitamos OrCondition anyTargetCloseCenter = new OrCondition(closeCenterRival, closeCenterTrainer); OrCondition anyTargetClose = new OrCondition(closeTrainer, closeRival); OrCondition anyTargetVeryClose = new OrCondition(veryCloseRival, veryCloseTrainer); NotCondition noOneClose = new NotCondition(anyTargetClose); NotCondition noOneVeryClose = new NotCondition(anyTargetVeryClose); WasCaught targetCaught = new WasCaught(kineticsTarget); List <Action> noActions = new List <Action>(); //4. TRANSICIONES: Transition anyHumanCloseCenter = new Transition(anyTargetCloseCenter, noActions, alert); Transition anyHumanClose = new Transition(anyTargetClose, noActions, alert); Transition noHumanClose = new Transition(noOneClose, noActions, stalk); Transition anyHumanVeryClose = new Transition(anyTargetVeryClose, noActions, runAway); Transition noHumanVeryClose = new Transition(noOneVeryClose, noActions, alert); Transition targetWasCaught = new Transition(targetCaught, noActions, worry); //4.1 AGREGAMOS TRANSICIONES A ESTADOS List <Transition> transitions = new List <Transition>() { anyHumanCloseCenter, targetWasCaught }; stalkTarget.transitions = transitions; transitions = new List <Transition>() { noHumanClose, anyHumanVeryClose, targetWasCaught }; alert.transitions = transitions; transitions = new List <Transition>() { anyHumanClose, targetWasCaught }; stalk.transitions = transitions; transitions = new List <Transition>() { noHumanVeryClose, targetWasCaught }; runAway.transitions = transitions; worry.transitions = new List <Transition>();//es un sumidero //5 MAQUINA DE ESTADOS State[] states = new State[] { stalkTarget, alert, stalk, runAway, worry }; meowthMachine = new StateMachine(states, stalkTarget); }
// Start is called before the first frame update void Start() { //DATOS EXTERNOS smellSensor = smellSensorScript.sensor; soundSensor = soundSensorScript.sensor; kinTrainer = trainerStaticData.kineticsAgent; steeringTrainer = trainerStaticData.steeringAgent; //COMENZAMOS A CONSTRUIR LA MAQUINA DE ESTADOS //1. ACCIONES: ShowIcon showPoisoned = new ShowIcon(this.gameObject, "Poisoned"); DisableIcon disablePoisoned = new DisableIcon(this.gameObject, "Poisoned"); ShowIcon showSleep = new ShowIcon(this.gameObject, "Sleeping"); DisableIcon disableSleep = new DisableIcon(this.gameObject, "Sleeping"); SetTimer setPoisonClock; SetTimer setSleepClock; ResetSensor resetSmellSensor = new ResetSensor(smellSensor); ResetSensor resetSoundSensor = new ResetSensor(soundSensor); List <ResetSensor> resets = new List <ResetSensor>() { resetSmellSensor, resetSoundSensor }; ResetSensorList resetAllSensor = new ResetSensorList(resets); UpdateMaxSpeed setOriginalSpeed = new UpdateMaxSpeed(trainerStaticData, trainerStaticData.maxspeed); UpdateMaxSpeed setSlowSpeed = new UpdateMaxSpeed(trainerStaticData, poisonedSpeed); UpdateMaxSpeed setZeroSpeed = new UpdateMaxSpeed(trainerStaticData, 0f); SetAngularSpeed setAngularToZero = new SetAngularSpeed(kinTrainer, 0f); SetAngularAccel setAngularAccelToZero = new SetAngularAccel(steeringTrainer, 0f); //accion para deshabilitar todos scripts de interes mientras dormimos List <DisableScript> disables = new List <DisableScript>(); foreach (var script in scriptsToDisable) { disables.Add(new DisableScript(script)); } DisableScriptList disableScripts = new DisableScriptList(disables); //accion para habilitar todos los script de interes si despertamos List <EnableScript> enables = new List <EnableScript>(); foreach (var script in scriptsToDisable) { enables.Add(new EnableScript(script)); } EnableScriptList enableScripts = new EnableScriptList(enables); //2. ESTADOS: List <Action> entryActions; //aqui iremos guardanndo todas las acciondes de entrada List <Action> exitActions; //aqui iremos guardanndo todas las acciones de salida List <Action> actions; //aqui guardaremos todas las acciones intermedias //2.a estado donde estamos sanos entryActions = new List <Action>(); //al entrar al estado ponemos un corazon actions = new List <Action>(); //durante el estado perseguimos al enamorado exitActions = new List <Action>(); //al salir quitamos el corazon State healthyState = new State(actions, entryActions, exitActions); //2.b estado donde estamos envenenados entryActions = new List <Action>() { showPoisoned, setSlowSpeed }; //al entrar al estado ponemos un corazon actions = new List <Action>(); //durante el estado perseguimos al enamorado exitActions = new List <Action>() { disablePoisoned, resetAllSensor, setOriginalSpeed }; //al salir quitamos el corazon State poisonedState = new State(actions, entryActions, exitActions); //2.c estado donde estamos dormidos entryActions = new List <Action>() { showSleep, setZeroSpeed, disableScripts, setAngularAccelToZero, setAngularToZero }; //al entrar al estado ponemos un corazon actions = new List <Action>(); //durante el estado perseguimos al enamorado //al salir debemos: // quitar el icono de dormir, resetear el sensor de sonido, reset el sensor de aroma porque puede haber un sweet // volver a habilitar lo que se deba exitActions = new List <Action>() { disableSleep, resetAllSensor, setOriginalSpeed, enableScripts }; //al salir quitamos el corazon State sleepState = new State(actions, entryActions, exitActions); //3. CONDICIONES: SmelledSomething smelledPoison = new SmelledSomething(smellSensor, "Poison"); SmelledSomething smelledSweet = new SmelledSomething(smellSensor, "Sweet"); HeardSleepSong heardSong = new HeardSleepSong(soundSensor); TimeOut poisonClock = new TimeOut(poisonTimer); setPoisonClock = new SetTimer(poisonClock); TimeOut sleepClock = new TimeOut(sleepTimer); setSleepClock = new SetTimer(sleepClock); //4. TRANSICIONES: List <Action> noActions = new List <Action>(); List <Action> transitionActions; transitionActions = new List <Action>() { setPoisonClock }; Transition gotPoisoned = new Transition(smelledPoison, transitionActions, poisonedState); Transition poisonTimeOut = new Transition(poisonClock, noActions, healthyState); Transition sweetScent = new Transition(smelledSweet, noActions, healthyState); transitionActions = new List <Action>() { setSleepClock }; Transition gotSlept = new Transition(heardSong, transitionActions, sleepState); Transition sleepTimeOut = new Transition(sleepClock, noActions, healthyState); //4.1 AGREGAMOS TRANSICIONES A ESTADOS List <Transition> transitions; transitions = new List <Transition>() { gotPoisoned, gotSlept };; healthyState.transitions = transitions; poisonedState.transitions = new List <Transition>() { poisonTimeOut, sweetScent }; sleepState.transitions = new List <Transition>() { sleepTimeOut, sweetScent }; //5 MAQUINA DE ESTADOS State[] states = new State[] { healthyState, poisonedState, sleepState }; trainerStatusMachine = new StateMachine(states, healthyState); }