コード例 #1
0
        private IEnumerator Start()
        {
            currentChanse = Random.Range(minChanse, maxChanse);

            IMover        _mover     = new Mover(this, moveSpeed);
            IMyAnimations _animation = new CharacterAnimation(GetComponent <Animator>());
            IFlip         _flip      = new FlipWithTranform(this);
            IHealth       _health    = GetComponent <IHealth>();
            IAttacker     _attacker  = GetComponent <IAttacker>();

            Idle        idle        = new Idle(_mover, _animation);
            Walk        walk        = new Walk(this, _mover, _animation, _flip, patrols);
            ChasePlayer chasePlayer = new ChasePlayer(_mover, _flip, _animation, IsPlayerRightSide);
            Attack      attack      = new Attack(_player.transform.GetComponent <IHealth>(), _flip, _animation, _attacker, maxAttackTime, IsPlayerRightSide);
            TakeHit     takeHit     = new TakeHit(_health, _animation);
            Dead        dead        = new Dead(this, _animation, () =>
            {
                if (currentChanse > Random.Range(0, 100))
                {
                    Instantiate(scorePrefab, transform.position, Quaternion.identity);
                }
            });



            _stateMachine.AddTransision(idle, walk, () => idle.IsIdle == false);                               //Idle'dan Walk'a IsIdle == false ise geç.
            _stateMachine.AddTransision(idle, chasePlayer, () => DistanceFromMeToPlayer() < chaseDistance);    //Idle'dan ChasePlayer'a methoddan dönen değer chaseDistance'dan küçük ise geç.
            _stateMachine.AddTransision(walk, chasePlayer, () => DistanceFromMeToPlayer() < chaseDistance);    //Walk'dan ChasePlayer'a methoddan dönen değer chaseDistance'dan küçük ise geç.
            _stateMachine.AddTransision(chasePlayer, attack, () => DistanceFromMeToPlayer() < attackDistance); //ChasePlayer'dan Attack'a methoddan dönen değer attackDistance'dan küçük ise geç.
            //Ters İşlemler
            _stateMachine.AddTransision(walk, idle, () => !walk.IsWalking);
            _stateMachine.AddTransision(chasePlayer, idle, () => DistanceFromMeToPlayer() > chaseDistance);
            _stateMachine.AddTransision(attack, chasePlayer, () => DistanceFromMeToPlayer() > attackDistance);

            _stateMachine.AddAnyState(dead, () => _health.IsDead);       //Kalan can 1'den küçük ise dead state çalıştır.
            _stateMachine.AddAnyState(takeHit, () => takeHit.IsTakeHit); //isTakeHit true dönerse takeHit çalıştır.

            _stateMachine.AddTransision(takeHit, chasePlayer, () => takeHit.IsTakeHit == false);
            _stateMachine.SetState(idle);

            yield return(null);
        }
コード例 #2
0
        private IEnumerator Start()
        {
            currentchance = Random.Range(minChance, maxChance);
            IMover       mover     = new MoverWithTranslate(this, moveSpeed);
            IMyAnimation animation = new CharacterAnimation(GetComponent <Animator>());
            IFlip        flip      = new FlipWithTransform(this);
            IHealth      health    = GetComponent <IHealth>();
            IAttacker    attacker  = GetComponent <IAttacker>();
            IStopEdge    stopEdge  = GetComponent <IStopEdge>();

            Idle        idle        = new Idle(mover, animation);
            Walk        walk        = new Walk(this, mover, animation, flip, patrols);
            ChasePlayer chasePlayer = new ChasePlayer(mover, flip, animation, stopEdge, IsPlayerRightSide);
            Attack      attack      = new Attack(_player.transform.GetComponent <IHealth>(), flip, animation, attacker, maxAttackTime, IsPlayerRightSide);
            TakeHit     takeHit     = new TakeHit(health, animation);
            Dead        dead        = new Dead(this, animation, () =>
            {
                if (currentchance > Random.Range(0, 100))
                {
                    Instantiate(scorePrefab, transform.position, Quaternion.identity);
                }
            });

            _stateMachine.AddTransition(idle, walk, () => idle.IsIdle == false);
            _stateMachine.AddTransition(idle, chasePlayer, () => DistanceFromMeToPlayer() < chaseDistance);
            _stateMachine.AddTransition(walk, chasePlayer, () => DistanceFromMeToPlayer() < chaseDistance);
            _stateMachine.AddTransition(chasePlayer, attack, () => DistanceFromMeToPlayer() < attackDistance);

            _stateMachine.AddTransition(walk, idle, () => !walk.IsWalking);
            _stateMachine.AddTransition(chasePlayer, idle, () => DistanceFromMeToPlayer() > chaseDistance);
            _stateMachine.AddTransition(attack, chasePlayer, () => DistanceFromMeToPlayer() > attackDistance);

            _stateMachine.AddAnyState(dead, () => health.IsDead);
            _stateMachine.AddAnyState(takeHit, () => takeHit.IsTakeHit);

            _stateMachine.AddTransition(takeHit, chasePlayer, () => takeHit.IsTakeHit == false);

            _stateMachine.SetState(idle);

            yield return(null);
        }