Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        private void UpdateInternal()
        {
            if (_currentTarget != null && CanFireTarget(_currentTarget))
            {
                FireTarget(_currentTarget);
                return;
            }

            ShooterBehaviour closestTarget = null;
            var closestDistance            = float.MaxValue;

            var targetsToRemove = new List <ShooterBehaviour>();

            foreach (var potentialTarget in _potentialTargets)
            {
                var     vec1        = transform.position;
                var     vec2        = potentialTarget.transform.position;
                Vector2 difference  = vec2 - vec1;
                float   sign        = (vec2.y < vec1.y) ? -1.0f : 1.0f;
                var     diffRadians = Mathf.Deg2Rad * Vector2.Angle(Vector2.right, difference) * sign;

                var raycastHit = Physics2D.Raycast(_muzzle.position, new Vector2(Mathf.Cos(diffRadians), Mathf.Sin(diffRadians)), _circleCollider.radius, LayerMask.GetMask(SolidLayerName));

                if (raycastHit.collider == null)
                {
                    continue;
                }

                var shooter = raycastHit.collider.GetComponent <ShooterBehaviour>();

                if (shooter == potentialTarget)
                {
                    if (raycastHit.distance < closestDistance)
                    {
                        closestDistance = raycastHit.distance;
                        closestTarget   = shooter;
                    }
                }
                else
                {
                    targetsToRemove.Add(potentialTarget);
                }
            }

            foreach (var targetToRemove in targetsToRemove)
            {
                _potentialTargets.Remove(targetToRemove);
            }

            if (closestTarget)
            {
                _currentTarget = closestTarget;
                FireTarget(closestTarget);
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        private bool CanFireTarget(ShooterBehaviour target)
        {
            var     vec1        = transform.position;
            var     vec2        = target.transform.position;
            Vector2 difference  = vec2 - vec1;
            float   sign        = (vec2.y < vec1.y) ? -1.0f : 1.0f;
            var     diffRadians = Mathf.Deg2Rad * Vector2.Angle(Vector2.right, difference) * sign;

            var raycastHit = Physics2D.Raycast(_muzzle.position, new Vector2(Mathf.Cos(diffRadians), Mathf.Sin(diffRadians)), _circleCollider.radius, LayerMask.GetMask(SolidLayerName));
            var character  = raycastHit.collider.GetComponent <Character>();

            return(character == target);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="target"></param>
        private void FireTarget(ShooterBehaviour target)
        {
            var     vec1        = transform.position;
            var     vec2        = target.transform.position;
            Vector2 difference  = vec2 - vec1;
            float   sign        = (vec2.y < vec1.y) ? -1.0f : 1.0f;
            var     diffRadians = Mathf.Deg2Rad * Vector2.Angle(Vector2.right, difference) * sign;

            targetOrientation = new Vector2(Mathf.Cos(diffRadians), Mathf.Sin(diffRadians));

            _weapon.transform.localRotation = Quaternion.Euler(targetOrientation);
            //_weapon.SetTargetOrientation(targetOrientation);
            _weapon.Trigger();
        }
        /// <summary>
        ///
        /// </summary>
        private void Update()
        {
            if (_elapsedSinceStart < StartTime)
            {
                _elapsedSinceStart += GameplayStatics.gameDeltaTime;
                return;
            }

            _elapsedSinceLastFire    += GameplayStatics.gameDeltaTime;
            _elapsedSinceLastDash    += GameplayStatics.gameDeltaTime;
            _elapsedSinceLastRaycast += GameplayStatics.gameDeltaTime;

            if (_elapsedSinceLastFire < _intervalBetweenFires && _elapsedSinceLastDash < _intervalBetweenDashes)
            {
                return;
            }

            if (_currentTarget != null)
            {
                var          shouldShoot = _elapsedSinceLastRaycast < IntervalBetweenRaycasts;
                RaycastHit2D raycastHit  = default;

                if (!shouldShoot)
                {
                    var difference = (_currentTarget.transform.position - _character.transform.position).normalized;
                    raycastHit  = Physics2D.Raycast(_character.transform.position + difference * GameConstants.HalfTileSize, difference, _circleCollider.radius, LayerMask.GetMask("Character", "Solid"));
                    shouldShoot = raycastHit.collider != null;
                }

                if (shouldShoot)
                {
                    targetOrientation = (_currentTarget.transform.position - _character.transform.position).normalized;

                    var canShoot = true;
                    var weapon   = _character.currentEquippable as HeatBasedWeaponEquippable;
                    if (weapon != null && weapon.isOverheating)
                    {
                        canShoot = false;
                    }

                    if (canShoot)
                    {
                        var shootRandGo = _shootOdds;
                        if (_elapsedSinceLastFire > _intervalBetweenFires && Random.Range(0.0f, 1.0f) <= shootRandGo)
                        {
                            _character.Action(CharacterAction.Fire);
                            _elapsedSinceLastFire = 0.0f;
                        }
                    }
                    else if (raycastHit.distance <= DashDistance && _elapsedSinceLastDash > _intervalBetweenDashes)
                    {
                        var dashRandGo = _characterDashOdds;
                        if (_currentTarget.GetComponent <BreakableConstruct>() != null)
                        {
                            dashRandGo = ConstructDashOdds;
                        }

                        if (Random.Range(0.0f, 1.0f) <= dashRandGo)
                        {
                            _character.Action(CharacterAction.Dash);
                            _elapsedSinceLastDash = 0.0f;
                        }
                    }
                    return;
                }
            }

            if (_elapsedSinceLastRaycast < IntervalBetweenRaycasts)
            {
                return;
            }

            ShooterBehaviour closestTarget = null;
            var closestDistance            = float.MaxValue;

            foreach (var potentialTarget in _potentialTargets)
            {
                if (potentialTarget == null)
                {
                    continue;
                }

                var difference = (potentialTarget.transform.position - _character.transform.position).normalized;

                var raycastHit = Physics2D.Raycast(_character.transform.position + difference * GameConstants.HalfTileSize, difference, _circleCollider.radius, LayerMask.GetMask("Character", "Solid"));
                if (raycastHit.collider != null && raycastHit.collider.GetComponent <ShooterBehaviour>() == potentialTarget)
                {
                    var shooterCharacter = potentialTarget as Character;
                    if (shooterCharacter != null)
                    {
                        if (shooterCharacter.gameplayState == CharacterGameplayState.Dead ||
                            shooterCharacter.isInvincible)
                        {
                            continue;
                        }
                    }

                    if (raycastHit.distance < closestDistance)
                    {
                        closestDistance = raycastHit.distance;
                        closestTarget   = potentialTarget;
                    }
                }
            }

            _currentTarget = closestTarget;
            if (_currentTarget != null)
            {
                targetOrientation = (closestTarget.transform.position - _character.transform.position).normalized;
            }

            _elapsedSinceLastRaycast = 0.0f;
        }