Exemple #1
0
    private void Update()
    {
        UpdateStates();
        remainingDistance = Mathf.RoundToInt(Mathf.Abs(agent.remainingDistance));
        IsMoving          = remainingDistance > 0;

        if (attackIntervalTimer <= 0)
        {
            attackIntervalTimer = attackInterval;
        }
        else
        {
            attackIntervalTimer -= Time.deltaTime;
        }

        if (self.Weapon == null)
        {
            scannedWeapons = Sensor.Scan <ILootable>(transform, collider, roamRange, lootLayerMask.value);
            if (!IsMoving) // no weapon and not moving
            {
                if (lootDetected && !gettingNearestObject && nearestLootable == null)
                {
                    GetNearestWeapon();
                }
                else if (nearestLootable != null && !lootInRange)
                {
                    try
                    {
                        MoveToNearestWeapon();
                    }
                    catch
                    {
                        nearestLootable = null;
                    }
                }
                else if (lootInRange)
                {
                    Stop();
                    try
                    {
                        nearestLootable.Pickup(ref self);
                    }
                    catch
                    {
                        nearestLootable = null;
                    }
                }
                else
                {
                    FindDirection(Move);
                }
            }
            else
            {
                if (lootDetected && !gettingNearestObject && nearestLootable == null)
                {
                    GetNearestWeapon();
                }
                else if (nearestLootable != null && !lootDetected && !lootInRange)
                {
                    nearestLootable = null;
                    Move();
                }
                else if (nearestLootable != null && lootDetected && !lootInRange)
                {
                    try
                    {
                        if (nearestLootable.Exists())
                        {
                            MoveToNearestWeapon();
                        }
                        else
                        {
                            nearestLootable = null;
                        }
                    }
                    catch
                    {
                        nearestLootable = null;
                    }
                }
                else if (nearestLootable != null && lootDetected && lootInRange)
                {
                    Stop();
                    try
                    {
                        nearestLootable.Pickup(ref self);
                    }
                    catch
                    {
                        nearestLootable = null;
                    }
                }
            }
        }
        else
        {
            nearestLootable = null;
            scannedEnemies  = Sensor.Scan <IPlayer>(transform, collider, roamRange, enemyLayerMask.value);
            if (!IsMoving) // has weapon and not moving
            {
                if (enemyDetected && !gettingNearestObject && nearestEnemy == null)
                {
                    GetNearestEnemy();
                }
                else if (enemyDetected && !gettingNearestObject && nearestEnemy != null && !enemyInRange)
                {
                    GetNearestEnemy();
                    MoveToNearestEnemy();
                }
                else if (enemyDetected && !gettingNearestObject && nearestEnemy != null && enemyInRange)
                {
                    GetNearestEnemy();
                    Stop();
                    Attack();
                }
                else
                {
                    FindDirection(Move);
                }
            }
            else // has weapon and moving
            {
                if (enemyDetected && !gettingNearestObject && nearestEnemy == null)
                {
                    GetNearestEnemy();
                }
                else if (nearestEnemy != null && !enemyInRange)
                {
                    MoveToNearestEnemy();
                }
                else if (enemyInRange)
                {
                    Stop();
                    Attack();
                }
            }
        }
    }