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; }); }
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; }); }
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; }
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; }