An instantiable/inheritable state machine reusable for complex components. Only acts as a data structure By: NeilDG
Пример #1
0
//	[SerializeField] LogicManager.GameState startFollowState;
//	[SerializeField] LogicManager.GameState endFollowState;

    protected override void MAwake()
    {
        base.MAwake();
        m_stateMachine = new AStateMachine <GirlState, LogicEvents> (GirlState.None);
        m_agent        = GetComponent <UnityEngine.AI.NavMeshAgent> ();
//		NextTarget ();
        m_animator = GetComponentInChildren <Animator> ();

        InitStateMachine();


        {
            m_umbrellaAudioSource              = gameObject.AddComponent <AudioSource> ();
            m_umbrellaAudioSource.volume       = 0.7f;
            m_umbrellaAudioSource.spatialBlend = 1f;
            m_umbrellaAudioSource.playOnAwake  = false;
            m_umbrellaAudioSource.minDistance  = 1f;
            m_umbrellaAudioSource.maxDistance  = 10f;
            //			AnimationCurve curve = new AnimationCurve ();
            //			curve.AddKey (new Keyframe (0, 1f));
            //			curve.AddKey (new Keyframe (1f, 0));
            //			m_umbrellaAudioSource.SetCustomCurve (AudioSourceCurveType.CustomRolloff , soundSetting.curve);
            m_umbrellaAudioSource.Stop();
        }
    }
Пример #2
0
    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;
        });
    }
Пример #3
0
 public AState(string stateLabel, AStateMachine stateMachine)
 {
     this.stateLabel   = stateLabel;
     this.stateMachine = stateMachine;
 }
Пример #4
0
 public void AddOnChange(AStateMachine <State, LogicEvents> .StateChangeHandler handler)
 {
     m_stateMachine.AddOnChange(handler);
 }
 public DragWorldState(string stateLabel, AStateMachine stateMachine)
     : base(stateLabel, stateMachine)
 {
 }
 public MouseDownState(string stateLabel, AStateMachine stateMachine)
     : base(stateLabel, stateMachine)
 {
 }
Пример #7
0
    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;
        });
    }
Пример #8
0
 public MouseDownState(string stateLabel, AStateMachine stateMachine) : base(stateLabel, stateMachine)
 {
 }
Пример #9
0
 public DragWorldState(string stateLabel, AStateMachine stateMachine) : base(stateLabel, stateMachine)
 {
 }
 public NoInputState(string stateLabel, AStateMachine stateMachine)
     : base(stateLabel, stateMachine)
 {
 }
Пример #11
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;
    }
Пример #12
0
 public void RegisterStateChange(AStateMachine <GameState, LogicEvents> .StateChangeHandler func)
 {
     m_stateMachine.AddOnChange(func);
 }
Пример #13
0
 public NoInputState(string stateLabel, AStateMachine stateMachine) : base(stateLabel, stateMachine)
 {
 }
Пример #14
0
 public AState(string stateLabel, AStateMachine stateMachine)
 {
     this.stateLabel = stateLabel;
     this.stateMachine = stateMachine;
 }
Пример #15
0
    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;
    }
Пример #16
0
    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;
            }
        });
    }
Пример #17
0
    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;
        });
    }