Esempio n. 1
0
        /// <summary>
        /// Align with other vehicles.
        /// </summary>
        /// <param name="vehicles">Other vehicles</param>
        /// <returns>Steer force</returns>
        protected virtual Vector2 Align(List <SimpleVehicle> vehicles)
        {
            var sum   = Vector2.Zero;
            int count = 0;

            foreach (var vehicle in vehicles)
            {
                var d = GlobalPosition.DistanceSquaredTo(vehicle.GlobalPosition);
                if (d > 0 && d < DetectionAlignmentRadius * DetectionAlignmentRadius)
                {
                    sum += vehicle.Velocity;
                    count++;
                }
            }

            if (count > 0)
            {
                sum = (sum / count).Normalized() * MaxVelocity;
                return((sum - Velocity).Clamped(MaxForce));
            }
            else
            {
                return(Vector2.Zero);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Regroup with other vehicles.
        /// </summary>
        /// <param name="vehicles">Other vehicles</param>
        /// <returns>Steer force</returns>
        protected Vector2 Regroup(List <SimpleVehicle> vehicles)
        {
            float separationLimit = Radius * 2;
            var   sum             = Vector2.Zero;
            int   count           = 0;

            foreach (var vehicle in vehicles)
            {
                float d = GlobalPosition.DistanceSquaredTo(vehicle.GlobalPosition);
                if (d > 0 && d > separationLimit * separationLimit)
                {
                    sum += (GlobalPosition - vehicle.GlobalPosition).Normalized() / Mathf.Sqrt(d);
                    count++;
                }
            }

            if (count > 0)
            {
                sum = (sum / count).Normalized() * -MaxVelocity;
                return((sum - Velocity).Clamped(MaxForce));
            }
            else
            {
                return(Vector2.Zero);
            }
        }
 protected override void UpdateAcceleration()
 {
     if (GlobalPosition.DistanceSquaredTo(Target.GlobalPosition) < FleeDistance * FleeDistance)
     {
         ApplyForce(Flee(Target.GlobalPosition));
     }
 }
Esempio n. 4
0
        private void StateSettled()
        {
            if (_stateMachine.IsStateNew())
            {
                CollisionLayer = 0;
                CollisionMask  = 0;
                _velocity      = Vector2.Zero;
                GlobalPosition = GlobalPosition.Round();
                _animationPlayer.Play(ANIM_IDLE);
                _collisionShape2d.Disabled = true;
                _deathTimer.Start();
            }

            var playerPosition = GetTree().GetFirstNodeInGroup <Player>(Player.GROUP)?.GlobalPosition ?? Vector2.Zero;
            var near           = GlobalPosition.DistanceSquaredTo(playerPosition) < PLAYER_NEAR_DISTANCE * PLAYER_NEAR_DISTANCE;

            if (!_deathTimer.IsStopped() && near)
            {
                _deathTimer.Start();
            }
            else if (near && _blinkAnimationPlayer.IsPlaying())
            {
                ResetBlink();
                _deathTimer.Start();
            }
        }
Esempio n. 5
0
        private void StateSeparate()
        {
            var pushVec = Vector2.Zero;

            foreach (var node in GetTree().GetNodesInGroup(GROUP))
            {
                if (node is Node2D n)
                {
                    if (GlobalPosition.DistanceSquaredTo(n.GlobalPosition) < SEPARATION_DISTANCE * SEPARATION_DISTANCE)
                    {
                        pushVec = (GlobalPosition - n.GlobalPosition);
                        break;
                    }
                }
            }

            if (pushVec == Vector2.Zero)
            {
                _stateMachine.ChangeState(StateSettled);
            }
            else
            {
                pushVec   = pushVec.Normalized() * SEPARATION_SPEED;
                _velocity = MoveAndSlide(pushVec);
            }
        }
Esempio n. 6
0
        public override void _Process(float delta)
        {
            var playerPos = GetTree().GetFirstNodeInGroup <Player>(Player.GROUP)?.GlobalPosition ?? Vector2.Zero;
            var prevValid = _valid;

            _valid = !_requirePlayerNear || GlobalPosition.DistanceSquaredTo(playerPos) <= _playerNearDistance * _playerNearDistance;

            if (!prevValid == _valid)
            {
                ToggleHighlight(true);
            }
        }
Esempio n. 7
0
    void FollowTarget(float delta)
    {
        updateTargetDirectionTimer += delta;
        if (updateTargetDirectionTimer > 0.5f)
        {
            directionToFollow = (nTarget.GlobalPosition - GlobalPosition).Normalized();

            var dis = GlobalPosition.DistanceSquaredTo(nTarget.GlobalPosition);
            enemy.speed = dis > 50000 ? initialSpeed : 0;

            updateTargetDirectionTimer = 0f;
        }
    }