void InitStateMachine() { m_stateMachine.State = GoodManState.Hide; m_stateMachine.AddEnter(GoodManState.ShowUp, ShowUp); m_stateMachine.AddEnter(GoodManState.Lead, delegate() { agent.enabled = true; agent.destination = leadDes; MainCharacter.Instance.transform.SetParent(transform); }); m_stateMachine.BlindStateChangeEvent(LogicEvents.EndDisplayDialog, GoodManState.Lead, GoodManState.LeadAfterTalk); m_stateMachine.AddUpdate(GoodManState.LeadAfterTalk, delegate() { if ((transform.position - leadDes).magnitude < 0.2f) { m_stateMachine.State = GoodManState.GoodBye; } }); m_stateMachine.AddEnter(GoodManState.GoodBye, delegate() { DisplayDialog(goAwayPlot); }); m_stateMachine.BlindStateChangeEvent(LogicEvents.EndDisplayDialog, GoodManState.GoodBye, GoodManState.Leave); m_stateMachine.AddEnter(GoodManState.Leave, delegate() { agent.destination = goAwayDes; // M_Event.FireLogicEvent(LogicEvents.GoodManLeave,new LogicArg(this)); MainCharacter.Instance.transform.SetParent(null); }); }
void InitStateMachine() { m_stateMachine = new AStateMachine <State, LogicEvents> (State.Stand); m_stateMachine.AddEnter(State.Stand, delegate() { stateChangeTime = stateChangeInterval.Rand; stateMachineTimer = 0; m_animation.CrossFade(standClip, 0.2f); }); m_stateMachine.AddUpdate(State.Stand, delegate() { stateMachineTimer += Time.deltaTime; if (stateMachineTimer > stateChangeTime) { if (Random.Range(0, 1f) > 0.5f) { m_stateMachine.State = State.Idle; } else { m_stateMachine.State = State.Walk; } } }); m_stateMachine.AddEnter(State.Walk, delegate() { float turnTo = Random.Range(0, 360f); float turnTime = Mathf.Repeat(transform.rotation.eulerAngles.y - turnTo, 360f) / turnSpeed; transform.DORotate(new Vector3(0, turnTo, 0), turnTime).OnComplete(delegate() { m_stateMachine.State = State.WalkMove; }); m_animation.CrossFade(walkClip, 0.2f); }); m_stateMachine.AddEnter(State.WalkMove, delegate() { stateChangeTime = stateChangeInterval.Rand; stateMachineTimer = 0; }); m_stateMachine.AddUpdate(State.WalkMove, delegate() { transform.position += -transform.forward * walkSpeed * Time.deltaTime; stateMachineTimer += Time.deltaTime; if (stateMachineTimer > stateChangeTime) { m_stateMachine.State = State.Stand; } }); m_stateMachine.AddEnter(State.Idle, delegate() { stateChangeTime = stateChangeInterval.Rand; stateMachineTimer = 0; m_animation.CrossFade(idleClip, 0.2f); }); m_stateMachine.AddUpdate(State.Idle, delegate() { stateMachineTimer += Time.deltaTime; if (stateMachineTimer > stateChangeTime) { if (Random.Range(0, 1f) > 0.5f) { m_stateMachine.State = State.Stand; } else { m_stateMachine.State = State.Walk; } } }); m_stateMachine.AddEnter(State.Run, delegate() { Vector3 oriAngle = transform.eulerAngles; GameObject player = GameObject.FindWithTag("Player"); Vector3 toPlayer = player.transform.position - transform.position; transform.forward = toPlayer; Vector3 toAngle = transform.eulerAngles; transform.eulerAngles = oriAngle; float turnTo = toAngle.y; float turnTime = Mathf.Repeat(transform.rotation.eulerAngles.y - turnTo, 360f) / turnSpeed; transform.DORotate(new Vector3(0, turnTo, 0), turnTime).OnComplete(delegate() { m_stateMachine.State = State.RunMove; }); m_animation.CrossFade(runClip, 0.2f); }); m_stateMachine.AddEnter(State.RunMove, delegate() { stateChangeTime = stateChangeInterval.Rand; stateMachineTimer = 0; }); m_stateMachine.AddUpdate(State.RunMove, delegate() { transform.position += -transform.forward * runSpeed * Time.deltaTime; stateMachineTimer += Time.deltaTime; if (stateMachineTimer > stateChangeTime) { m_stateMachine.State = State.Stand; } }); }
void InitStateMachine() { if (IsStateMachineInited) { return; } IsStateMachineInited = true; m_stateMachine = new AStateMachine <HeroState, LogicEvents> (HeroState.None); m_stateMachine.BlindFromEveryState(LogicEvents.PlaceHeroPhase, HeroState.ReadyToPlace); m_stateMachine.AddEnter(HeroState.ReadyToPlace, delegate { transform.DOMove(oritinalPos, 1f); }); m_stateMachine.AddEnter(HeroState.MoveWithMouse, delegate { m_collider.size = new Vector3(0.1f, 0.1f, 1f); }); m_stateMachine.AddUpdate(HeroState.MoveWithMouse, delegate { transform.position = InputManager.FocusWorldPos; }); m_stateMachine.AddExit(HeroState.MoveWithMouse, delegate { m_collider.size = new Vector3(2.56f, 2.56f, 1f); }); m_stateMachine.AddEnter(HeroState.Prepare, delegate { if (TriggerBlock != null) { TriggerBlock.RegisterHero(this); TemBlock = TriggerBlock.BlockInfo; LogicManager.Instance.RegisterHero(this); transform.DOMove(TriggerBlock.GetCenterPosition(), 0.2f); // Disable Collider if (m_collider != null) { m_collider.enabled = false; } } }); m_stateMachine.BlindFromEveryState(LogicEvents.StrategyPhase, HeroState.Strategy); m_stateMachine.BlindFromEveryState(LogicEvents.AutoBattle, HeroState.Strategy); m_stateMachine.AddEnter(HeroState.Strategy, delegate { if (TemBlock == null) { m_stateMachine.State = HeroState.None; } else { targetLine.enabled = false; targetArrow.enabled = false; if (LogicManager.IsAutoPlay) { m_stateMachine.State = HeroState.StrategyAuto; } else { if (!(m_strategy is CustomStrategy)) { DestroyImmediate(m_strategy); m_strategy = gameObject.AddComponent <CustomStrategy>(); m_strategy.Init(this); } if (m_strategy is CustomStrategy) { ((CustomStrategy)m_strategy).target = TemSimpleBlock; ((CustomStrategy)m_strategy).isActive = false; } } } }); m_stateMachine.AddUpdate(HeroState.Strategy, delegate() { if (LogicManager.IsAutoPlay) { m_stateMachine.State = HeroState.StrategyAuto; } }); m_stateMachine.BlindStateEventHandler(HeroState.StrategyChoose, delegate(object obj) { LogicArg arg = (LogicArg)obj; if (arg.type == LogicEvents.ConfirmHero) { // Block block = (Block)arg.GetMessage(M_Event.BLOCK); // if ( TemBlock != null && !TemBlock.Equals(block)) // { m_stateMachine.State = HeroState.Strategy; // } } else if (arg.type == LogicEvents.SelectBlock) { Block block = (Block)arg.GetMessage(M_Event.BLOCK); if (m_move.IsInMoveRange(block.SimpleBlock)) { ((CustomStrategy)m_strategy).target = block.SimpleBlock; DrawToTarget(block); m_stateMachine.State = HeroState.StrategyDirection; } } }); m_stateMachine.BlindStateEventHandler(HeroState.StrategyDirection, delegate(object obj) { LogicArg arg = (LogicArg)obj; if (arg.type == LogicEvents.FingerUp) { m_stateMachine.State = HeroState.StrategyConfirm; } }); m_stateMachine.BlindStateEventHandler(HeroState.StrategyConfirm, delegate(object obj) { LogicArg arg = (LogicArg)obj; if (arg.type == LogicEvents.ConfirmHero) { Block block = (Block)arg.GetMessage(M_Event.BLOCK); if (TemBlock.Equals(block)) { m_stateMachine.State = HeroState.StrategyChoose; } } else if (arg.type == LogicEvents.ConfirmMove) { if (LogicManager.Instance.mode == LogicManager.Mode.SingleBattle) { if (arg.sender != this) { m_stateMachine.State = HeroState.Strategy; } } } }); m_stateMachine.AddEnter(HeroState.StrategyChoose, delegate { TemBlock.linkedBlock.visualType = BattleBlock.BlockVisualType.StrategyFocus; BattleField.ShowBlock(m_move.GetMoveRange(), BattleBlock.BlockVisualType.StrategyMoveRange); BattleField.ShowBlock(new SimBlock[] { TemBlock.SimpleBlock }, BattleBlock.BlockVisualType.StrategyChosenHero, false); }); m_stateMachine.AddEnter(HeroState.StrategyDirection, delegate() { targetArrow.enabled = true; Block block = BattleField.GetBlock(((CustomStrategy)m_strategy).target); BattleField.ShowBlock(new SimBlock[] { TemBlock.SimpleBlock }, BattleBlock.BlockVisualType.StrategyChosenHero, false); targetArrow.transform.position = block.linkedBlock.GetCenterPosition(); }); m_stateMachine.AddUpdate(HeroState.StrategyDirection, delegate { // set the target arrow to right angle Vector3 focusPos = InputManager.FocusWorldPos; Block block = BattleField.GetBlock(((CustomStrategy)m_strategy).target); Vector3 toward = focusPos - block.linkedBlock.GetCenterPosition(); float angle = Mathf.Atan2(toward.y, toward.x) * Mathf.Rad2Deg; angle = Mathf.Round((angle) / 90f) * 90f; targetArrow.transform.rotation = Quaternion.Euler(0, 0, angle); // set the angle of strategy ((CustomStrategy)m_strategy).angle = angle; // get the direction of the strategy Direction direction = ((CustomStrategy)m_strategy).GetDirection(); BattleField.ShowBlock(m_attack.GetAttackRange(((CustomStrategy)m_strategy).target, direction, GetHeroInfo().AttackRange), BattleBlock.BlockVisualType.StrategyAttackRange); }); m_stateMachine.AddExit(HeroState.StrategyDirection, delegate { ((CustomStrategy)m_strategy).isActive = true; LogicArg arg = new LogicArg(this); // Debug.Log("Fire Confirm Move "); M_Event.FireLogicEvent(LogicEvents.ConfirmMove, arg); }); m_stateMachine.AddEnter(HeroState.StrategyConfirm, delegate { BattleField.ResetVisualColor(true); TemBlock.linkedBlock.visualType = BattleBlock.BlockVisualType.StrategyConfirm; }); m_stateMachine.AddEnter(HeroState.StrategyAuto, delegate() { // set up the strategy if (!(m_strategy is AIStrategy)) { DestroyImmediate(m_strategy); m_strategy = gameObject.AddComponent <AIStrategy>(); m_strategy.Init(this); m_strategy.OnBeforeBattle(); } }); m_stateMachine.AddEnter(HeroState.BattleMove, delegate { targetLine.enabled = false; targetArrow.enabled = false; }); }
public void InitStateMachine() { // m_stateMachine.AddEnter (State.Create, delegate { // UIManager.Instance.ClearElement(); // UIManager.Instance.AddButton(ButtonType.CreateWolf , IconType.Create , 1, 1); // }); // // m_stateMachine.AddExit (State.Create, delegate { // AdvanceTurn (); // }); // // m_stateMachine.AddEnter (State.Feed, delegate { // UIManager.Instance.ClearElement(); // // UIManager.Instance.AddButtonAtRandomPosition(ButtonType.FeedSun , IconType.Sun ); // UIManager.Instance.AddButtonAtRandomPosition(ButtonType.FeedMoon , IconType.Moon ); // }); // // m_stateMachine.AddOnEvent (State.Feed, delegate(object arg) { // var lArg = (LogicArg)arg; // if ( lArg != null && lArg.eventType == LogicEvents.ButtonPress ) { // var bpArg = (ButtonArg)arg; // UIManager.Instance.ClearElement(); // UIManager.Instance.AddButtonAtRandomPosition(ButtonType.FeedSun , IconType.Sun ); // UIManager.Instance.AddButtonAtRandomPosition(ButtonType.FeedMoon , IconType.Moon ); // } // // }); // // m_stateMachine.AddExit (State.Feed, delegate { // AdvanceTurn(); // }); m_stateMachine.AddEnter(State.MainState, delegate { UIManager.Instance.AddButton(ButtonType.CreateButton, 1, 1); if (MCreatureManager.Instance.GetAnimalCount() > 0) { UIManager.Instance.AddButton(ButtonType.ActionButton, 2, 1); } if (DisasterManager.Instance.IsDisasterReady) { UIManager.Instance.AddButton(ButtonType.DisasterButton, 0, 1); } }); m_stateMachine.AddOnEvent(State.MainState, delegate(object arg) { var lArg = (LogicArg)arg; if (lArg.eventType == LogicEvents.ButtonPress) { var bArg = (ButtonArg)lArg; if (bArg.buttonType == ButtonType.CreateButton) { buttonCache = bArg.button; m_stateMachine.State = State.Create; } } }); ///// Create m_stateMachine.AddEnter(State.Create, delegate { // if ( Turn == 1 ) { // Basic Creature var list = GetCreatureCreateButtonList(); foreach (var btnType in list) { var btn = UIManager.Instance.AddButtonAtRandomPosition(btnType); if (buttonCache != null) { UIManager.Instance.MoveFrom(buttonCache, btn, 1f); } } if (buttonCache != null) { UIManager.Instance.RemoveElement(buttonCache); } } // m_miniGame.OnEnter(); }); m_stateMachine.AddOnEvent(State.Create, delegate(object arg) { var lArg = (LogicArg)arg; if (lArg.eventType == LogicEvents.ButtonPress) { var bArg = (ButtonArg)lArg; buttonTypeCache = bArg.buttonType; m_stateMachine.State = State.CreateMiniGame; } }); m_stateMachine.AddEnter(State.CreateMiniGame, delegate { m_miniGame = new FirstCreate(); m_miniGame.OnEnter(); }); m_stateMachine.AddUpdate(State.CreateMiniGame, delegate { if (m_miniGame.OnUpdate()) { m_stateMachine.State = State.Action; } }); m_stateMachine.AddOnEvent(State.CreateMiniGame, delegate(object arg) { var lArg = (LogicArg)arg; if (lArg.eventType == LogicEvents.ButtonPress) { var bArg = (ButtonArg)lArg; } if (m_miniGame != null) { m_miniGame.OnEvent((LogicArg)arg); } }); m_stateMachine.AddExit(State.CreateMiniGame, delegate { m_miniGame.OnExit(); m_miniGame = null; AdvanceTurn(); }); /////// Feed ///////// m_stateMachine.AddEnter(State.Action, delegate { // if ( Turn == 2 ) { m_miniGame = new FirstFeed(); } m_miniGame.OnEnter(); }); m_stateMachine.AddUpdate(State.Action, delegate { if (m_miniGame.OnUpdate()) { m_stateMachine.State = State.Create; } }); m_stateMachine.AddOnEvent(State.Action, delegate(object arg) { if (m_miniGame != null) { m_miniGame.OnEvent((LogicArg)arg); } }); m_stateMachine.AddExit(State.Action, delegate { m_miniGame.OnExit(); m_miniGame = null; AdvanceTurn(); }); /////// Play ///////// m_stateMachine.AddEnter(State.Play, delegate { // if ( Turn == 1 ) { m_miniGame = new NormalPlay(); } m_miniGame.OnEnter(); }); m_stateMachine.AddUpdate(State.Play, delegate { if (m_miniGame.OnUpdate()) { m_stateMachine.State = State.Action; } }); m_stateMachine.AddOnEvent(State.Play, delegate(object arg) { if (m_miniGame != null) { m_miniGame.OnEvent((LogicArg)arg); } }); m_stateMachine.AddExit(State.Play, delegate { m_miniGame.OnExit(); m_miniGame = null; AdvanceTurn(); }); m_stateMachine.State = State.MainState; }
void InitStateMachine() { m_stateMachine.AddEnter(GirlState.Init, delegate() { m_agent.speed = 0; }); m_stateMachine.AddEnter(GirlState.Walk, delegate() { m_agent.speed = MainCharacter.Instance.FollowSpeed; }); m_stateMachine.AddUpdate(GirlState.Walk, delegate() { if (isInRain) { m_stateMachine.State = GirlState.TakeOutUmbrella; } if (m_realTalking) { m_stateMachine.State = GirlState.TalkWithOutUmbrella; } }); m_stateMachine.AddEnter(GirlState.TakeOutUmbrella, delegate() { m_agent.speed = 0; m_animator.SetTrigger("TakeOut"); if (m_umbrellaAudioSource != null) { m_umbrellaAudioSource.clip = soundSetting.umbrellaTakeOut; m_umbrellaAudioSource.loop = false; m_umbrellaAudioSource.Play(); } }); m_stateMachine.AddUpdate(GirlState.TakeOutUmbrella, delegate { if (m_IsOpenUmbrella) { m_stateMachine.State = GirlState.WalkWithUmbrella; } }); m_stateMachine.AddEnter(GirlState.TakeOffUmbrella, delegate() { m_agent.speed = 0; m_animator.SetTrigger("TakeOff"); if (m_umbrellaAudioSource != null) { m_umbrellaAudioSource.clip = soundSetting.umbrellaTakeOff; m_umbrellaAudioSource.loop = false; m_umbrellaAudioSource.Play(); } }); m_stateMachine.AddUpdate(GirlState.TakeOffUmbrella, delegate { if (!m_IsOpenUmbrella) { m_stateMachine.State = GirlState.Walk; } }); m_stateMachine.AddEnter(GirlState.WalkWithUmbrella, delegate() { m_collider.radius = 1.5f; if (m_umbrellaAudioSource != null) { m_umbrellaAudioSource.clip = soundSetting.umbrellaWalk; m_umbrellaAudioSource.loop = true; m_umbrellaAudioSource.Play(); } }); m_stateMachine.AddUpdate(GirlState.WalkWithUmbrella, delegate() { m_agent.speed = MainCharacter.Instance.MoveSpeed * 0.96f; if (!m_IsPlayerIn && LogicManager.Instance.State < LogicManager.GameState.WalkOutStreetFour) { m_stateMachine.State = GirlState.WaitForPlayer; } // if ( IsTalking && !m_IsEndTalking ) if (m_realTalking) { m_stateMachine.State = GirlState.TalkWithPlayerInUmbrella; } if (!isInRain) { m_stateMachine.State = GirlState.TakeOffUmbrella; } }); m_stateMachine.AddEnter(GirlState.WaitForPlayer, delegate() { m_agent.speed = 0; m_collider.radius = 0.45f; }); m_stateMachine.AddUpdate(GirlState.WaitForPlayer, delegate() { if (m_IsPlayerIn) { m_stateMachine.State = GirlState.WalkWithUmbrella; } }); m_stateMachine.AddEnter(GirlState.TalkWithPlayerInUmbrella, delegate() { m_agent.speed = 0; }); m_stateMachine.AddUpdate(GirlState.TalkWithPlayerInUmbrella, delegate() { if (!m_realTalking) { m_stateMachine.State = GirlState.WalkWithUmbrella; } }); m_stateMachine.AddEnter(GirlState.TalkWithOutUmbrella, delegate() { m_agent.speed = 0; }); m_stateMachine.AddUpdate(GirlState.TalkWithOutUmbrella, delegate() { if (!m_realTalking) { m_stateMachine.State = GirlState.Walk; } }); m_stateMachine.BlindFromEveryState(LogicEvents.ForceGirlLeave, GirlState.LeavePlayer); m_stateMachine.AddEnter(GirlState.LeavePlayer, delegate() { stateTimer = 0; }); m_stateMachine.AddUpdate(GirlState.LeavePlayer, delegate() { Vector3 forward = Camera.main.transform.forward; forward.y = 0; m_agent.speed = 12f; m_agent.SetDestination(transform.position + forward * -5f); stateTimer += Time.deltaTime; if (stateTimer > 2f) { m_stateMachine.State = GirlState.WalkAway; } }); m_stateMachine.AddEnter(GirlState.WalkAway, delegate() { if (Model != null) { Model.SetActive(false); } Model.SetActive(false); M_Event.FireLogicEvent(LogicEvents.InvisibleFromPlayer, new LogicArg(this)); M_Event.FireLogicEvent(LogicEvents.SwitchDefaultBGM, new LogicArg(this)); }); m_stateMachine.State = GirlState.Init; }
void InitStateMachine() { m_stateMachine = new AStateMachine <HeroState, LogicEvents>(HeroState.None); m_stateMachine.BlindStateEventHandler(HeroState.Strategy, delegate(object obj) { LogicArg arg = (LogicArg)obj; Block block = (Block)arg.GetMessage(M_Event.BLOCK); Debug.Log("Get event" + arg.type); if (arg.type == LogicEvents.ConfirmHero) { Debug.Log("Get Confirm Hero" + block.SimpleBlock + " " + TemBlock.SimpleBlock); if (TemBlock != null && TemBlock.Equals(block)) { m_stateMachine.State = HeroState.StrategyMove; } else { m_stateMachine.State = HeroState.Strategy; } } else if (arg.type == LogicEvents.SelectBlock) { m_stateMachine.State = HeroState.Strategy; } }); m_stateMachine.BlindStateEventHandler(HeroState.StrategyMove, delegate(object obj) { LogicArg arg = (LogicArg)obj; Block block = (Block)arg.GetMessage(M_Event.BLOCK); if (arg.type == LogicEvents.ConfirmHero) { if (TemBlock != null && TemBlock.Equals(block)) { m_stateMachine.State = HeroState.StrategyAttack; } else { m_stateMachine.State = HeroState.Strategy; } } else if (arg.type == LogicEvents.SelectBlock) { m_stateMachine.State = HeroState.Strategy; } }); m_stateMachine.BlindStateEventHandler(HeroState.StrategyAttack, delegate(object obj) { LogicArg arg = (LogicArg)obj; Block block = (Block)arg.GetMessage(M_Event.BLOCK); if (arg.type == LogicEvents.ConfirmHero) { if (TemBlock != null && TemBlock.Equals(block)) { m_stateMachine.State = HeroState.StrategyMove; } else { m_stateMachine.State = HeroState.Strategy; } } else if (arg.type == LogicEvents.SelectBlock) { m_stateMachine.State = HeroState.Strategy; } }); m_stateMachine.AddEnter(HeroState.Strategy, delegate { if (m_collider == null) { m_collider = GetComponent <BoxCollider>(); } if (m_collider != null) { m_collider.size = new Vector3(2.56f, 2.56f, 1f); } if (m_collider != null) { m_collider.enabled = false; } }); m_stateMachine.AddEnter(HeroState.StrategyMove, delegate() { BattleField.ShowBlock(m_move.GetMoveRange(), BattleBlock.BlockVisualType.MoveRangeEnermy); }); m_stateMachine.AddEnter(HeroState.StrategyAttack, delegate() { BattleField.ShowBlock(m_attack.GetAttackRange(), BattleBlock.BlockVisualType.AttackRangeEnermy); }); m_stateMachine.BlindFromEveryState(LogicEvents.StrategyPhase, HeroState.Strategy); m_stateMachine.State = HeroState.Strategy; }
void InitStateMachine() { m_stateMachine = new AStateMachine <State, LogicEvents>(State.None); m_stateMachine.AddUpdate(State.MoveForward, OnMoveForwardUpdate); m_stateMachine.AddEnter(State.Wait, delegate { m_forwardSpeed = 0; temLocation = nextLocation; nextLocation = CalculateNext(); // arrive the tem Location if (temLocation != null) { temLocation.OnArrive(this); } m_stateMachine.State = State.WaitOnLocation; }); m_stateMachine.AddUpdate(State.WaitOnLocation, delegate { waittingTime += Time.deltaTime; OnWaitUpdate(); }); m_stateMachine.AddEnter(State.Pass, OnEnterPass); m_stateMachine.AddExit(State.Pass, delegate { transform.forward = GetForwardDirection(); transform.position = temRoad.GetStartPosition(); // leave the tem location and move to the next location temLocation.OnLeave(this); }); m_stateMachine.AddEnter(State.StopForFirstPriority, delegate { Debug.Log("Enter top "); m_sideSpeed = 0; }); m_stateMachine.AddUpdate(State.StopForFirstPriority, delegate { float SideDistanceMax = Width + 0.1f; float sideDistance = Mathf.Clamp(temRoad.GetDistanceToRoad(transform.position), 0.001f, SideDistanceMax); // Debug.Log("Side Distance " + sideDistance ); m_sideSpeed = Mathf.Sin(Mathf.Acos(Mathf.Clamp(1f - 2 * sideDistance / SideDistanceMax, -1f, 1f))) * MaxSpeed + 0.001f; // Update the forward Speed m_forwardSpeed = Mathf.Clamp(m_forwardSpeed - Acceleration / 2f * Time.deltaTime, SlowSpeed, MaxSpeed); // Update Direction forwardDirection = GetForwardDirection().normalized; transform.forward = Speed.normalized; transform.position += Speed * Time.deltaTime; // Test If The Policd Car Walk Passed if ((Vector3.Dot((firstPriorityCar.transform.position - transform.position), temRoad.GetDirection()) > firstPriorityCar.Length + Length) || temRoad != firstPriorityCar.temRoad) { m_stateMachine.State = State.BackToRoad; } }); m_stateMachine.AddExit(State.StopForFirstPriority, delegate { m_sideSpeed = 0; m_forwardSpeed = Mathf.Epsilon; }); m_stateMachine.AddUpdate(State.BackToRoad, delegate() { float SideDistanceMax = Width + 0.1f; float sideDistance = Mathf.Clamp(SideDistanceMax - temRoad.GetDistanceToRoad(transform.position), 0.001f, SideDistanceMax); // Debug.Log("Side Distance " + sideDistance ); m_sideSpeed = -(Mathf.Sin(Mathf.Acos(Mathf.Clamp(1f - 2 * sideDistance / SideDistanceMax, -1f, 1f))) * MaxSpeed); // Update the forward Speed m_forwardSpeed = Mathf.Clamp(m_forwardSpeed + Acceleration * Time.deltaTime, SlowSpeed, MaxSpeed); transform.forward = Speed.normalized; // Debug.Log("Back Speed " + Speed + " " + Speed.normalized + " side " + m_sideSpeed + " forward " + m_forwardSpeed + " direction " + forwardDirection ); transform.position += Speed * Time.deltaTime; if (Vector3.Dot((transform.position - temRoad.GetStartPosition()), SideDirection) < 0) { m_stateMachine.State = State.MoveForward; } }); m_stateMachine.AddExit(State.BackToRoad, delegate { if (temRoad != null) { transform.position = temRoad.GetNearestPoint(transform.position); } m_sideSpeed = 0; }); m_stateMachine.AddUpdate(State.StopForFirstPriorityWait, delegate { float SideDistanceMax = Width + 0.1f; float sideDistance = Mathf.Clamp(temRoad.GetDistanceToRoad(transform.position), 0.001f, SideDistanceMax); // Debug.Log("Side Distance " + sideDistance ); m_sideSpeed = Mathf.Sin(Mathf.Acos(Mathf.Clamp(1f - 2 * sideDistance / SideDistanceMax, -1f, 1f))) * MaxSpeed + 0.001f; // Update Direction forwardDirection = GetForwardDirection().normalized; transform.forward = Speed.normalized; transform.position += Speed * Time.deltaTime; // Test If The Policd Car Walk Passed if ((Vector3.Dot((firstPriorityCar.transform.position - transform.position), temRoad.GetDirection()) > firstPriorityCar.Length + Length) || temRoad != firstPriorityCar.temRoad) { m_stateMachine.State = State.BackToRoadWait; } }); m_stateMachine.AddExit(State.StopForFirstPriorityWait, delegate { m_sideSpeed = 0; m_forwardSpeed = Mathf.Epsilon; }); m_stateMachine.AddUpdate(State.BackToRoadWait, delegate() { float SideDistanceMax = Width + 0.1f; float sideDistance = Mathf.Clamp(SideDistanceMax - temRoad.GetDistanceToRoad(transform.position), 0.001f, SideDistanceMax); // Debug.Log("Side Distance " + sideDistance ); m_sideSpeed = -(Mathf.Sin(Mathf.Acos(Mathf.Clamp(1f - 2 * sideDistance / SideDistanceMax, -1f, 1f))) * MaxSpeed); // Update the forward Speed transform.forward = Speed.normalized; Debug.Log("Back Speed " + Speed + " " + Speed.normalized + " side " + m_sideSpeed + " forward " + m_forwardSpeed + " direction " + forwardDirection); transform.position += Speed * Time.deltaTime; if (Vector3.Dot((transform.position - temRoad.GetStartPosition()), SideDirection) < 0) { m_stateMachine.State = State.WaitOnLocation; } }); m_stateMachine.AddExit(State.BackToRoadWait, delegate { if (temRoad != null) { transform.position = temRoad.GetNearestPoint(transform.position); } m_sideSpeed = 0; }); }
void InitStateMachine() { m_stateMachine = new AStateMachine <GameState, LogicEvents>(GameState.None); // m_stateMachine.BlindTimeStateChange (GameState.Enter, GameState.TalkWithManInCafe, 1f); m_stateMachine.BlindStateChangeEvent(LogicEvents.EndTalkManInCafe, GameState.Enter, GameState.TalkWithManInCafe); m_stateMachine.BlindStateChangeEvent(LogicEvents.BeginDamage, GameState.TalkWithManInCafe, GameState.TryGoInRain); m_stateMachine.BlindStateChangeEvent(LogicEvents.SeeGirlStreetTwo, GameState.SeeTakePhoto, GameState.SeeGirlStreetTwo); m_stateMachine.BlindStateChangeEvent(LogicEvents.UnfocusCamera, GameState.SeeGirlStreetTwo, GameState.FindGirlStreetTwo); m_stateMachine.BlindStateChangeEvent(LogicEvents.BusStopEndTalkGirl, GameState.InBusStop, GameState.WalkWithGirl); m_stateMachine.BlindStateChangeEvent(LogicEvents.TrafficRedLight, GameState.WalkOutStreetFour, GameState.WalkIntoPeople); m_stateMachine.BlindTimeStateChange(GameState.WalkIntoPeople, GameState.WalkAcrossRoadWithGirl, 35f); m_stateMachine.BlindStateChangeEvent(LogicEvents.ForceGirlLeave, GameState.WalkAcrossRoadWithGirl, GameState.DepartFromGirl); // m_stateMachine.BlindTimeStateChange (GameState.WalkAcrossRoadWithGirl, GameState.DepartFromGirl, 10f); m_stateMachine.BlindStateChangeEvent(LogicEvents.InvisibleFromPlayer, GameState.DepartFromGirl, GameState.BeginShip); m_stateMachine.BlindStateChangeEvent(LogicEvents.EnterEnd, GameState.WalkInStreetColorful, GameState.PlayEndAnimation); m_stateMachine.BlindStateChangeEvent(LogicEvents.EndTalkWithFakeGirl, GameState.BackToApartment, GameState.TalkWithFakeGirl); m_stateMachine.BlindStateChangeEvent(LogicEvents.EndCredit, GameState.ShowCredit, GameState.End); m_stateMachine.BlindFromEveryState(LogicEvents.EndTalkWithGirl, GameState.TalkWithGirlInCafe); m_stateMachine.BlindFromEveryState(LogicEvents.EnterStreetOne, GameState.WalkInStreetOne); m_stateMachine.BlindFromEveryState(LogicEvents.EnterRotateBuilding, GameState.SeeBuilding); m_stateMachine.BlindFromEveryState(LogicEvents.EnterBorrowUmbrella, GameState.BorrowUmbrella); m_stateMachine.BlindFromEveryState(LogicEvents.EnterStreetTwo, GameState.WalkInStreetTwo); m_stateMachine.BlindFromEveryState(LogicEvents.EnterTakePhoto, GameState.SeeTakePhoto); m_stateMachine.BlindFromEveryState(LogicEvents.EnterStreetThree, GameState.WalkInStreetThree); m_stateMachine.BlindFromEveryState(LogicEvents.EnterBusStop, GameState.InBusStop); m_stateMachine.BlindFromEveryState(LogicEvents.EnterStreetThreeEnd, GameState.WalkOutStreetThree); m_stateMachine.BlindFromEveryState(LogicEvents.EnterStreetFour, GameState.WalkInStreetFour); m_stateMachine.BlindFromEveryState(LogicEvents.GirlSayPlayMusic, GameState.ListenToMusic); m_stateMachine.BlindFromEveryState(LogicEvents.EnterStreetFourEnd, GameState.WalkOutStreetFour); m_stateMachine.BlindFromEveryState(LogicEvents.PickUpMusicPlayer, GameState.PickUpMusicPlayer); m_stateMachine.BlindFromEveryState(LogicEvents.EnterStreetColorful, GameState.WalkInStreetColorful); m_stateMachine.BlindFromEveryState(LogicEvents.EnterApartment, GameState.BackToApartment); m_stateMachine.BlindFromEveryState(LogicEvents.WalkInApartment, GameState.PlayEndAnimation); m_stateMachine.BlindFromEveryState(LogicEvents.EndHitThree, GameState.ShowCredit); m_stateMachine.AddEnter(GameState.WalkAcrossRoadWithGirl, delegate() { M_Event.FireLogicEvent(LogicEvents.TrafficGreenLight, new LogicArg(this)); }); m_stateMachine.AddEnter(GameState.BeginShip, delegate() { LogicArg arg = new LogicArg(this); arg.AddMessage(M_Event.EVENT_BGM_FADE_TIME, 0); M_Event.FireLogicEvent(LogicEvents.SwitchDefaultBGM, arg); }); m_stateMachine.AddEnter(GameState.TalkWithFakeGirl, delegate() { LogicArg arg = new LogicArg(this); arg.AddMessage(M_Event.EVENT_BGM_FADE_TIME, 5f); M_Event.FireLogicEvent(LogicEvents.SwitchDefaultBGM, arg); }); m_stateMachine.AddEnter(GameState.End, delegate() { SceneManager.LoadScene("Title"); }); m_stateMachine.AddEnter(GameState.PlayEndAnimation, delegate() { m_MainCharacter.transform.DOMove(m_MainCharacter.transform.forward * m_MainCharacter.MoveSpeed * 2f, 2f).SetRelative(true); }); m_stateMachine.State = startState; }
public void InitStateMachine() { m_stateMachine.AddEnter(State.Enter, delegate { ResetTimer(); Animal.ActionEmotion(-1); Animal.Action = true; }); m_stateMachine.AddUpdate(State.Enter, delegate { if (timer < 0) { m_stateMachine.State = State.Walk; } }); m_stateMachine.AddExit(State.Enter, delegate { Animal.Action = false; }); m_stateMachine.AddEnter(State.Walk, delegate { SetRandomTarget(); ResetTimer(); M_Event.FireLogicEvent(LogicEvents.AnimalAction, new AnimalActionArg(this, AnimalAction.Walk, transform)); if (m_health.IsHungry) { m_stateMachine.State = State.FindFood; } }); m_stateMachine.AddUpdate(State.Walk, delegate { Agent.nextPosition = transform.position; if (Agent.remainingDistance > Agent.stoppingDistance) { if (timer < -5f) { SetRandomTarget(); ResetTimer(); } Vector3 walkDir = Agent.desiredVelocity.normalized; walkDir.y = 0; if (walkDir.magnitude < 0.01f) { walkDir = transform.forward; walkDir.y = 0; } walkDir = walkDir.normalized; Debug.DrawLine(transform.position, transform.position + walkDir * 5f, Color.red); Animal.Move(walkDir, true); } else { if (Random.Range(0, 1f) < 0.2f) { m_stateMachine.State = State.Lie; } else { m_stateMachine.State = State.Rest; } } }); m_stateMachine.AddExit(State.Walk, delegate { }); m_stateMachine.AddEnter(State.Rest, delegate { Animal.Move(Vector3.zero, false); }); m_stateMachine.AddUpdate(State.Rest, delegate { if (IsTimerReady()) { m_stateMachine.State = State.Walk; } }); m_stateMachine.AddExit(State.Rest, delegate { }); m_stateMachine.AddEnter(State.Lie, delegate { Animal.ActionEmotion((int)AnimalAction.Lie); Animal.Action = true; ResetTimer(); Animal.Move(Vector3.zero, false); M_Event.FireLogicEvent(LogicEvents.AnimalAction, new AnimalActionArg(this, AnimalAction.Lie, transform)); }); m_stateMachine.AddUpdate(State.Lie, delegate { Animal.Action = false; if (IsTimerReady() && Animal.Stand) { m_stateMachine.State = State.Walk; } }); m_stateMachine.AddExit(State.Lie, delegate { Animal.ActionEmotion(-1); Animal.Action = false; Animal.Move(new Vector3(0.001f, 0, 0.001f), false); }); m_stateMachine.AddEnter(State.FindFood, delegate { Debug.Log("Find Food "); if (targetFood == null) { foreach (var aviableFood in aviableFoodList) { targetFood = MFood.FindNearestFood(aviableFood, transform.position); if (targetFood != null) { break; } } } if (targetFood == null) { ResetTimer(); m_stateMachine.State = State.Rest; } else { m_stateMachine.State = State.WalkToFood; } }); m_stateMachine.AddEnter(State.WalkToFood, delegate { Animal.ActionEmotion(-1); Debug.Log("Walk To Food "); if (targetFood == null) { m_state = State.FindFood; } else { Agent.SetDestination(targetFood.transform.position); var lookAt = gameObject.GetComponent <MalbersAnimations.Utilities.LookAt>(); if (lookAt != null) { lookAt.Target = targetFood.transform; } } }); m_stateMachine.AddUpdate(State.WalkToFood, delegate { Agent.nextPosition = transform.position; if (targetFood == null || !targetFood.IsAviable()) { m_stateMachine.State = State.FindFood; } else { Vector3 toward = targetFood.transform.position - transform.position; toward.y = 0; float towardAngle = Vector3.Angle(transform.forward, toward); // Debug.Log("Re D " + Agent.remainingDistance + " stop " + Agent.stoppingDistance ); if (Agent.remainingDistance > Agent.stoppingDistance) { Vector3 walkDir = Agent.desiredVelocity.normalized; walkDir.y = 0; if (walkDir.magnitude < 0.01f) { walkDir = transform.forward; walkDir.y = 0; } walkDir = walkDir.normalized; Debug.DrawLine(transform.position, transform.position + walkDir * 5f, Color.red); Animal.Move(walkDir, true); if (toward.magnitude > 5f) { Animal.Shift = true; } else { Animal.Shift = false; } } else { m_stateMachine.State = State.Eat; } } }); m_stateMachine.AddExit(State.WalkToFood, delegate { Animal.Shift = false; var lookAt = gameObject.GetComponent <MalbersAnimations.Utilities.LookAt>(); if (lookAt != null) { lookAt.Target = null; } }); m_stateMachine.AddEnter(State.Eat, delegate { Animal.ActionEmotion((int)AnimalAction.Eat); Animal.Action = true; Animal.Move(Vector3.zero, true); var lookAt = gameObject.GetComponent <MalbersAnimations.Utilities.LookAt>(); if (lookAt != null && targetFood != null) { lookAt.Target = targetFood.transform; } }); m_stateMachine.AddUpdate(State.Eat, delegate { Animal.Action = false; if (targetFood == null || !targetFood.IsAviable()) { m_stateMachine.State = State.FindFood; } if (m_health.IsFull) { m_stateMachine.State = State.Walk; } else { m_health.EatFood(targetFood); } }); m_stateMachine.AddExit(State.Eat, delegate { Animal.ActionEmotion(-1); targetFood = null; var lookAt = gameObject.GetComponent <MalbersAnimations.Utilities.LookAt>(); if (lookAt != null) { lookAt.Target = null; } if (!m_health.IsHungry) { M_Event.FireLogicEvent(LogicEvents.AnimalAction, new AnimalActionArg(this, AnimalAction.Eat, transform)); } }); m_stateMachine.State = State.Walk; }
void InitStateMachine() { m_stateMachine = new AStateMachine <State, LogicEvents> (State.None); m_stateMachine.AddEnter(State.PlaceHero, delegate { InitGame(); M_Event.FireLogicEvent(LogicEvents.PlaceHeroPhase, new LogicArg(this)); }); m_stateMachine.AddEnter(State.WaitPlaceHero, delegate { if (isOnline) { List <RawHeroInfo> heros = new List <RawHeroInfo>(); foreach (Hero h in heroList) { if (h.GetHeroInfo().TeamColor == TeamColor.Blue) { heros.Add(h.GetHeroInfo().GetRawHeroInfo()); } } NetworkManager.Instance.SendPlaceHero(heros.ToArray()); } else { m_stateMachine.State = State.Strategy; } }); m_stateMachine.AddEnter(State.Strategy, delegate { M_Event.FireLogicEvent(LogicEvents.StrategyPhase, new LogicArg(this)); OnBeforeBattle(heroList.ToArray()); RecordTime(); }); m_stateMachine.AddUpdate(State.Strategy, delegate() { if (IsAutoPlay) { m_stateMachine.State = State.WaitStrategy; } }); m_stateMachine.AddEnter(State.WaitStrategy, delegate { if (isOnline) { List <HeroMoveInfo> heros = new List <HeroMoveInfo>(); foreach (Hero h in heroList) { if (h.GetHeroInfo().TeamColor == TeamColor.Blue && h.GetHeroInfo().IsAlive) { heros.Add(h.GetMoveInfo()); } } Debug.Log("Send Move Hero"); NetworkManager.Instance.SendMoveHero(heros.ToArray()); } else if (isAI) { } else { m_stateMachine.State = State.Battle; } }); m_stateMachine.AddUpdate(State.WaitStrategy, delegate { if (isAI) { if (IsAllReady(TeamColor.Blue) && IsAllReady(TeamColor.Red)) { m_stateMachine.State = State.Battle; } } }); m_stateMachine.AddEnter(State.Battle, OnBattle); m_stateMachine.AddEnter(State.Count, delegate { m_stateMachine.State = State.Strategy; }); }