Beispiel #1
0
    private void FixedUpdate()
    {
        // Get player
        var playerController = PlayerController.Instance;

        // Look for hostiles
        if (!_hasEnemy)
        {
            var   colliders        = Physics2D.OverlapCircleAll(transform.position, 5f);
            float smallestDistance = Single.MaxValue;
            foreach (var target in colliders)
            {
                if (target.CompareTag("Enemy"))
                {
                    var dist = (target.transform.position - transform.position).magnitude;
                    if (dist < smallestDistance)
                    {
                        smallestDistance = dist;
                        _currentEnemy    = target;
                        _hasEnemy        = true;
                        targetEnemy      = target.gameObject;
                    }
                }
            }
        }
        else if (_currentEnemy == null)
        {
            _hasEnemy   = false;
            targetEnemy = null;
        }

        if (_hasEnemy)
        {
            _patrol = false;

            // Line of sight
            var dir   = _currentEnemy.transform.position - transform.position;
            var check = Physics2D.Raycast(transform.position, dir.normalized, dir.magnitude, solidMask);

            if (check.collider == null)
            {
                WeaponUser.PointGun(_currentEnemy.transform.position, AnimationContoller(_currentEnemy.transform.position));

                if (dir.magnitude < 10f)
                {
                    // Check the player isnt in the way
                    var playerCheck = Physics2D.Raycast(transform.position, dir.normalized, dir.magnitude, playerMask);

                    if (playerCheck.collider == null && WeaponUser.FirePrimary(Rigidbody, transform.position, dir.normalized))
                    {
                        weaponRenderer.sprite = WeaponUser.primary.sprite;
                    }
                    Animator.SetBool("Walking", false);
                }
                else
                {
                    MoveTo(_currentEnemy.transform.position);
                    Animator.SetBool("Walking", true);
                }
            }
            else
            {
                _hasEnemy   = false;
                targetEnemy = null;
            }
        }

        if (!_hasEnemy)
        {
            var playerState = PlayerState.Instance;

            if (playerState.companionFollow)
            {
                _patrol = false;
                var dist = (playerController.transform.position - transform.position).magnitude;
                AnimationContoller(playerController.transform.position);
                WeaponUser.StopPointGun();
                if (dist > 1.5f)
                {
                    MoveTo(playerController.transform.position);
                    Animator.SetBool("Walking", true);
                }
                else
                {
                    Animator.SetBool("Walking", false);
                }
            }
            else if (_patrol)
            {
                _patrol = MoveTo(_patrolTarget);
                AnimationContoller(_patrolTarget);
                WeaponUser.StopPointGun();
                Animator.SetBool("Walking", true);
            }
            else
            {
                // Pick random destination
                _patrolTarget    = playerController.transform.position;
                _patrolTarget.x += UnityEngine.Random.Range(-3, 3);
                _patrolTarget.y += UnityEngine.Random.Range(-3, 3);
                _patrol          = MoveTo(_patrolTarget);
                AnimationContoller(_patrolTarget);
                WeaponUser.StopPointGun();
                Animator.SetBool("Walking", true);
            }

            // TP if far away
            if (!GameManager.Instance.bossBattle)
            {
                var d = (playerController.transform.position - transform.position).magnitude;
                if (d > 15)
                {
                    transform.position = playerController.transform.position;
                }
            }
        }
    }
Beispiel #2
0
    private void FixedUpdate()
    {
        // Flash red if damaged
        if (_hitTimer > 0)
        {
            _hitTimer -= Time.deltaTime;
        }
        else
        {
            GetComponentInChildren <Renderer>().material.SetInt("IsHit", 0);
        }

        // Stuck checker
        if (_hasTarget && transform.position == _lastPos)
        {
            _stuckTimer += Time.deltaTime;
        }
        else
        {
            _stuckTimer = 0;
        }
        _lastPos = transform.position;

        // ===========
        // NEW ENEMY AI
        // ===========

        if (_hasTarget && _stuckTimer > 4f)
        {
            _hasTarget = false;
        }

        // Get an enemy target if we haven't already got a target
        if (!_hasTarget || _isWandering || !_targetIsPlayer)
        {
            // Check if we can see a target. (be it player or companion)
            var targets = Physics2D.OverlapCircleAll(transform.position, 5); // TODO: Variable for range

            var smallestDist = Single.MaxValue;
            foreach (var target in targets)
            {
                var dir   = target.transform.position - transform.position;
                var check = Physics2D.Raycast(transform.position, dir, dir.magnitude, solidLayer);

                if (check.collider == null)
                {
                    if (target.CompareTag("Player"))
                    {
                        var dist = (target.transform.position - transform.position).magnitude;
                        if (dist < smallestDist)
                        {
                            smallestDist     = dist;
                            _currentTarget   = target.transform.position;
                            _targetIsHostile = true;
                            _hasTarget       = true;
                            _targetHostile   = target;
                            _isWandering     = false;
                            _targetIsPlayer  = true;
                        }
                    }
                    if (target.CompareTag("Companion"))
                    {
                        var dist        = (target.transform.position - transform.position).magnitude;
                        var companionAI = target.GetComponent <CompanionAI>();
                        if (dist < smallestDist && companionAI.hitpoints > 0)
                        {
                            smallestDist     = dist;
                            _currentTarget   = target.transform.position;
                            _targetIsHostile = true;
                            _hasTarget       = true;
                            _targetHostile   = target;
                            _isWandering     = false;
                            _targetIsPlayer  = false;
                        }

                        if (companionAI.targetEnemy == gameObject)
                        {
                            _currentTarget   = target.transform.position;
                            _targetIsHostile = true;
                            _hasTarget       = true;
                            _targetHostile   = target;
                            _isWandering     = false;
                            _targetIsPlayer  = false;
                            break;
                        }
                    }
                }
            }
        }
        else if (_targetHostile == null)
        {
            _hasTarget      = false;
            _targetIsPlayer = false;
        }

        // Get direction vector
        var targetDir = _currentTarget - transform.position;

        if (_hasTarget)
        {
            if (targetDir.magnitude > 10)
            {
                _hasTarget      = false;
                _targetIsPlayer = false;
            }
        }

        if (_hasTarget)
        {
            // Check if we have line of sight of the target (if we have one)
            if (_targetIsHostile)
            {
                // If we can't see the target, we forget him
                var check = Physics2D.Raycast(transform.position, targetDir, targetDir.magnitude, solidLayer);

                if (check.collider == null)
                {
                    WeaponUser.PointGun(_targetHostile.transform.position, AnimationContoller(_targetHostile.transform.position));

                    // Update target
                    _currentTarget = _targetHostile.transform.position;
                    targetDir      = _currentTarget - transform.position;

                    if (_hasWeapon && targetDir.magnitude < attackRange)
                    {
                        _weaponUser.FirePrimary(Rigidbody, transform.position, targetDir.normalized);
                        return;
                    }
                }
                else
                {
                    _hasTarget       = false;
                    _targetIsHostile = false;
                    _isWandering     = false;
                    _targetIsPlayer  = false;
                }

                Animator.SetBool("Walking", _hasTarget);
            }
            else if (targetDir.magnitude < 0.5f)
            {
                _hasTarget       = false;
                _targetIsHostile = false;
                _isWandering     = false;
                _targetIsPlayer  = false;
            }

            // Move towards target
            _hasTarget = MoveTo(_currentTarget);
            AnimationContoller(_currentTarget);
            Animator.SetBool("Walking", _hasTarget);
        }
        else
        {
            _wanderTimer += Time.deltaTime;
            var r = new System.Random();
            if (_wanderTimer >= 1 && r.Next(0, 100) <= wanderChance)
            {
                // wanderChance% chance to move
                // Select a random cell within 2 positions, we have 4 tries before failing
                for (var i = 0; i < 4; i++)
                {
                    var x = r.Next(-2, 2);
                    var y = r.Next(-2, 2);

                    _currentTarget = transform.position + new Vector3(x, y, 0);
                    _hasTarget     = MoveTo(_currentTarget);
                    _isWandering   = _hasTarget;
                    Animator.SetBool("Walking", _hasTarget);
                    AnimationContoller(_currentTarget);
                    if (_hasTarget)
                    {
                        break;
                    }
                }

                // Reset timer
                _wanderTimer = 0;
            }
            else
            {
                Animator.SetBool("Walking", false);
            }
        }
    }
Beispiel #3
0
    private void FixedUpdate()
    {
        rb.velocity = inputVector * moveSpeed;

        if (_invulnerabilityTimer >= 0)
        {
            _invulnerabilityTimer -= Time.deltaTime;
        }
        else if (_invulnerabilityTimer > -1f)
        {
            _invulnerabilityTimer  = -1;
            modelRenderer.material = _normalMaterial;
        }

        var interact = GetClosestInteractible(out var has);

        _ui.interactControl.SetActive(has);
        if (has)
        {
            if (interact != _lastOutlined && _lastOutlined != null)
            {
                _lastOutlined.StopOutline();
            }
            _lastOutlined = interact;
            interact.Outline();
            _ui.interactText.text = interact.GetInteractText();
        }
        else if (_lastOutlined != null)
        {
            _lastOutlined.StopOutline();
            _lastOutlined = null;
        }

        // Check for weapon changes, if changed, sync
        var state = PlayerState.Instance;

        if (state.currentState.primaryWeapon != _weaponUser.primary)
        {
            _weaponUser.SetPrimary(state.currentState.primaryWeapon);
            UpdateWeaponUI();
        }

        if (state.currentState.secondaryWeapon != _weaponUser.secondary)
        {
            _weaponUser.SetSecondary(state.currentState.secondaryWeapon);
            UpdateWeaponUI();
        }

        // Reload
        if (_primaryReload && _primaryReloadTimer >= 0)
        {
            _primaryReloadTimer -= Time.deltaTime;
        }
        else if (_primaryReload)
        {
            _primaryReload = false;
            ReloadPrimary();
        }
        if (_secondaryReload && _secondaryReloadTimer >= 0)
        {
            _secondaryReloadTimer -= Time.deltaTime;
        }
        else if (_secondaryReload)
        {
            _secondaryReload = false;
            ReloadSecondary();
        }

        // Weapons
        if (!GameManager.Instance.inDialogue)
        {
            try {
                var mouse    = Mouse.current;
                var keyboard = Keyboard.current;
                var mousePos = mouse.position.ReadValue();
                if (mouse.leftButton.isPressed || keyboard[Key.N].isPressed)
                {
                    if (state.currentState.primaryAmmo > 0)
                    {
                        var direction = new Vector3(mousePos.x, mousePos.y) -
                                        Camera.main.WorldToScreenPoint(transform.position);
                        direction.Normalize();

                        if (_weaponUser.FirePrimary(rb, transform.position, direction))
                        {
                            state.currentState.primaryAmmo--;
                            UpdateWeaponUI();
                        }
                    }
                    else if (state.currentState.primaryClip > 0 && !_primaryReload)
                    {
                        _primaryReload      = true;
                        _primaryReloadTimer = state.currentState.primaryWeapon.reloadTime * DifficultOwner.Instance.currentPreset.reloadScale;
                    }
                }
                else if (mouse.rightButton.isPressed || keyboard[Key.M].isPressed)
                {
                    if (state.currentState.secondaryAmmo > 0)
                    {
                        var direction = new Vector3(mousePos.x, mousePos.y) -
                                        Camera.main.WorldToScreenPoint(transform.position);
                        direction.Normalize();

                        if (_weaponUser.FireSecondary(rb, transform.position, direction))
                        {
                            state.currentState.secondaryAmmo--;
                            UpdateWeaponUI();
                        }
                    }
                    else if (state.currentState.secondaryClip > 0 && !_secondaryReload)
                    {
                        _secondaryReload      = true;
                        _secondaryReloadTimer = state.currentState.secondaryWeapon.reloadTime * DifficultOwner.Instance.currentPreset.reloadScale;
                    }
                }

                // Mouse flippage
                var mouseWorldPos = Camera.main.ScreenToWorldPoint(mousePos);

                var flipped = false;
                if (mouseWorldPos.x < transform.position.x)
                {
                    if (!_leftTriggered)
                    {
                        _animator.SetTrigger("FlipLeft");
                        _leftTriggered = true;
                        _animator.ResetTrigger("FlipRight");
                        _rightTriggered = false;
                    }

                    flipped = true;
                }
                else if (mouseWorldPos.x > transform.position.x)
                {
                    if (!_rightTriggered)
                    {
                        _animator.SetTrigger("FlipRight");
                        _rightTriggered = true;
                        _animator.ResetTrigger("FlipLeft");
                        _leftTriggered = false;
                    }
                }

                _weaponUser.PointGun(mouseWorldPos, flipped);
            } catch (Exception ex) {
            }
        }
    }