Exemplo n.º 1
0
        // Update is called once per frame
        void Update()
        {
            Vector3 pos = transform.position;

            if (m_IsKO)
            {
                m_KOTimer += Time.deltaTime;
                if (m_KOTimer > 3.0f)
                {
                    GoToRespawn();
                }

                return;
            }

            //The update need to run, so we can check the health here.
            //Another method would be to add a callback in the CharacterData that get called
            //when health reach 0, and this class register to the callback in Start
            //(see CharacterData.OnDamage for an example)
            if (m_CharacterData.Stats.CurrentHealth == 0)
            {
                m_Animator.SetTrigger(m_FaintParamID);

                m_Agent.isStopped = true;
                m_Agent.ResetPath();
                m_IsKO    = true;
                m_KOTimer = 0.0f;

                Data.Death();

                m_CharacterAudio.Death(pos);

                return;
            }


            Ray screenRay = CameraController.Instance.GameplayCamera.ScreenPointToRay(Input.mousePosition);



            if (m_TargetInteractable != null)
            {
                CheckInteractableRange();
            }

            if (m_CurrentTargetCharacterData != null)
            {
                if (m_CurrentTargetCharacterData.Stats.CurrentHealth == 0)
                {
                    m_CurrentTargetCharacterData = null;
                }
                else
                {
                    CheckAttack();
                }
            }

            float mouseWheel = Input.GetAxis("Mouse ScrollWheel");

            if (!Mathf.Approximately(mouseWheel, 0.0f))
            {
                Vector3 view = m_MainCamera.ScreenToViewportPoint(Input.mousePosition);
                if (view.x > 0f && view.x < 1f && view.y > 0f && view.y < 1f)
                {
                    CameraController.Instance.Zoom(-mouseWheel * Time.deltaTime * 20.0f);
                }
            }

            if (Input.GetMouseButtonDown(0))
            { //if we click the mouse button, we clear any previously et targets
                if (m_CurrentState != State.ATTACKING)
                {
                    m_CurrentTargetCharacterData = null;
                    m_TargetInteractable         = null;
                }
                else
                {
                    m_ClearPostAttack = true;
                }
            }


            if (!EventSystem.current.IsPointerOverGameObject() && m_CurrentState != State.ATTACKING)
            {
                //Raycast to find object currently under the mouse cursor
                ObjectsRaycasts(screenRay);

                if (Input.GetMouseButton(0))
                {
                    if (m_TargetInteractable == null && m_CurrentTargetCharacterData == null)
                    {
                        InteractableObject obj = m_Highlighted as InteractableObject;
                        if (obj)
                        {
                            InteractWith(obj);
                        }
                        else
                        {
                            CharacterData data = m_Highlighted as CharacterData;
                            if (data != null)
                            {
                                m_CurrentTargetCharacterData = data;
                            }
                            else
                            {
                                MoveCheck(screenRay);
                            }
                        }
                    }
                }
            }

            m_Animator.SetFloat(m_SpeedParamID, m_Agent.velocity.magnitude / m_Agent.speed);

            //Keyboard shortcuts
            if (!inCinematic && Input.GetKeyUp(KeyCode.I))
            {
                UISystem.Instance.ToggleInventory();
            }
        }
Exemplo n.º 2
0
        // Update is called once per frame
        void Update()
        {
            //See the Update function of CharacterControl.cs for a comment on how we could replace
            //this (polling health) to a callback method.
            if (m_CharacterData.Stats.CurrentHealth == 0)
            {
                m_Animator.SetTrigger(m_DeathAnimHash);

                m_CharacterAudio.Death(transform.position);
                m_CharacterData.Death();

                if (m_LootSpawner != null)
                {
                    m_LootSpawner.SpawnLoot();
                }

                Destroy(m_Agent);
                Destroy(GetComponent <Collider>());
                Destroy(this);
                return;
            }

            Vector3       playerPosition = CharacterControl.Instance.transform.position;
            CharacterData playerData     = CharacterControl.Instance.Data;

            switch (m_State)
            {
            case State.IDLE:
            {
                if (Vector3.SqrMagnitude(playerPosition - transform.position) < detectionRadius * detectionRadius)
                {
                    if (SpottedAudioClip.Length != 0)
                    {
                        SFXManager.PlaySound(SFXManager.Use.Enemies, new SFXManager.PlayData()
                            {
                                Clip     = SpottedAudioClip[Random.Range(0, SpottedAudioClip.Length)],
                                Position = transform.position
                            });
                    }

                    m_PursuitTimer    = 4.0f;
                    m_State           = State.PURSUING;
                    m_Agent.isStopped = false;
                }
            }
            break;

            case State.PURSUING:
            {
                float distToPlayer = Vector3.SqrMagnitude(playerPosition - transform.position);
                if (distToPlayer < detectionRadius * detectionRadius)
                {
                    m_PursuitTimer = 4.0f;

                    if (m_CharacterData.CanAttackTarget(playerData))
                    {
                        m_CharacterData.AttackTriggered();
                        m_Animator.SetTrigger(m_AttackAnimHash);
                        m_State = State.ATTACKING;
                        m_Agent.ResetPath();
                        m_Agent.velocity  = Vector3.zero;
                        m_Agent.isStopped = true;
                    }
                }
                else
                {
                    if (m_PursuitTimer > 0.0f)
                    {
                        m_PursuitTimer -= Time.deltaTime;

                        if (m_PursuitTimer <= 0.0f)
                        {
                            m_Agent.SetDestination(m_StartingAnchor);
                            m_State = State.IDLE;
                        }
                    }
                }

                if (m_PursuitTimer > 0)
                {
                    m_Agent.SetDestination(playerPosition);
                }
            }
            break;

            case State.ATTACKING:
            {
                if (!m_CharacterData.CanAttackReach(playerData))
                {
                    m_State           = State.PURSUING;
                    m_Agent.isStopped = false;
                }
                else
                {
                    if (m_CharacterData.CanAttackTarget(playerData))
                    {
                        m_CharacterData.AttackTriggered();
                        m_Animator.SetTrigger(m_AttackAnimHash);
                    }
                }
            }
            break;
            }
            m_Animator.SetFloat(m_SpeedAnimHash, m_Agent.velocity.magnitude / Speed);
        }