Ejemplo n.º 1
0
    public void FindLurchTarget()
    {
        var playerList = GetTree().GetNodesInGroup("Players");

        findTargetSfx.Stop();
        if (playerList.Count > 0)
        {
            Player player    = playerList[0] as Player;
            var    lurchTime = 1f;
            if (Position.DistanceTo(player.GlobalPosition) > lurchTime * MaxVelocity)
            {
                lurchDirection = Position.DirectionTo(player.GlobalPosition);
                lurchTarget    = Position + lurchDirection * MaxVelocity;
            }
            else
            {
                lurchDirection = Position.DirectionTo(player.GlobalPosition);
                lurchTarget    = player.GlobalPosition;
                lurchTime     *= GlobalPosition.DistanceTo(lurchTarget) / MaxVelocity;
            }
            sprite.FlipH = lurchDirection.x < 0;
            tween.Remove(this, "global_position");
            tween.InterpolateProperty(this, "global_position", Position, lurchTarget, lurchTime, easeType: Tween.EaseType.Out);
            tween.Start();
        }
    }
Ejemplo n.º 2
0
 private void StopTimerIfPointReached()
 {
     if (GlobalPosition.DistanceTo(_wanderController.TargetPosition) <= WanderCheckRange)
     {
         _wanderController.StopWanderTimer();
     }
 }
Ejemplo n.º 3
0
    public override void _PhysicsProcess(float delta)
    {
        if (_ai != null)
        {
            _ai.Evaluate();
        }

        if (GlobalPosition.DistanceTo(GetGlobalMousePosition()) > 20f && _shouldMove)
        {
            _movement_axis = (GetGlobalMousePosition() - GlobalPosition).Normalized();
        }
        else
        {
            _movement_axis = Vector2.Zero;
        }

        if (_movement_axis == Vector2.Zero)
        {
            ApplyFriction(Acceleration * delta);
        }
        else
        {
            ApplyMovement(_movement_axis * Acceleration * delta, Speed);
        }
        _motion = MoveAndSlide(_motion);
    }
Ejemplo n.º 4
0
 private void onDestroyCheckTimerTimeout()
 {
     if (GlobalPosition.DistanceTo(Global.Player.GlobalPosition) > destroyDistance)
     {
         destroy();
     }
 }
Ejemplo n.º 5
0
    public override void _PhysicsProcess(float delta)
    {
        //Outline.Visible = PlayerColliding;
        if (IsJustDropped && PlayerBody != null)
        {
            if (GlobalPosition.DistanceTo(PlayerBody.GlobalPosition) > 28)
            {
                IsJustDropped = false;
            }
        }
        PlayerColliding = false;

        if (CurrentItem == null)
        {
            try
            {
                CurrentItem = Database <Item> .Get(ItemID);
            }
            catch
            {
                ItemSprite.Texture = null;
                throw new Exception($"Incorrect item-ID set for {nameof(ItemEntity)}");
            }
        }

        ItemSprite.Texture = CurrentItem.Icon;
    }
    public override void _PhysicsProcess(float delta)
    {
        if (startedMoving)
        {
            EmitSignal(nameof(StartedMoving));
            startedMoving = false;
        }

        if (Input.IsActionPressed("click"))
        {
            _targetGlobalPosition = GetGlobalMousePosition();
            startedMoving         = true;
        }
        if (GlobalPosition.DistanceTo(_targetGlobalPosition) < DISTANCE_TRESHOLD)
        {
            return;
        }
        _velocity = Steering.ArriveTo(_velocity,
                                      GlobalPosition,
                                      _targetGlobalPosition,
                                      maxSpeed: MaxSpeed,
                                      SlowdownRadius);
        _velocity         = MoveAndSlide(_velocity);
        Triangle.Rotation = _velocity.Angle();
    }
Ejemplo n.º 7
0
 protected virtual void SnapSpriteToGrid()
 {
     Sprite.GlobalPosition = GlobalPosition.DistanceTo(Sprite.GlobalPosition) > 0.7
                         ? GlobalPosition.Round()
                         : Sprite.GlobalPosition.Round();
     DeathSprite.GlobalPosition = GlobalPosition.Round();
 }
Ejemplo n.º 8
0
    public override void _PhysicsProcess(float delta)
    {
        if (path == null)
        {
            path = (GetNode("/root/World") as Gameplay).FindPathToPlayer(GlobalPosition);
        }
        else
        {
            if (path.Length < 2)
            {
                path = null;
                return;
            }

            var dir = (path[1] - GlobalPosition).Normalized();
            MoveAndSlide(dir * movementSpeed, new Vector2(0, -1));
            if (GlobalPosition.DistanceTo(path[1]) <= 10)
            {
                path = null;
            }
        }

        if (attackTimer <= 0)
        {
            Attack();
            attackTimer = 1.5f;
        }

        attackTimer -= delta;
    }
Ejemplo n.º 9
0
            protected override void UpdateAcceleration()
            {
                MaxVelocity = Mathf.Max(GlobalPosition.DistanceTo(Target.GlobalPosition) / 10, 4f);
                MaxForce    = Mathf.Max(GlobalPosition.DistanceTo(Target.GlobalPosition) / 100, 0.1f);

                base.UpdateAcceleration();
            }
Ejemplo n.º 10
0
    public async void RecalculatePath()
    {
        Array targetBuildings = GetTree().GetNodesInGroup("EnemyTargets");

        if (targetBuildings.Count == 0)
        {
            return;
        }

        var   closest         = (Building)targetBuildings[0];
        float closestDistance = GlobalPosition.DistanceTo(closest.GlobalPosition);

        foreach (Building building in targetBuildings)
        {
            if (!IsInstanceValid(building))
            {
                continue;
            }
            float dist = GlobalPosition.DistanceTo(building.GlobalPosition);

            if (dist < closestDistance && !building.Deleting)
            {
                closest         = building;
                closestDistance = dist;
            }
            await ToSignal(GetTree().CreateTimer(0.01f), "timeout");
        }

        target = closest;
    }
Ejemplo n.º 11
0
    public override void _PhysicsProcess(float delta)
    {
        if (completed)
        {
            return;
        }

        if (GlobalPosition.DistanceTo(player.GlobalPosition) <= 600 && !running)
        {
            enemyCount = 1;
            running    = true;
        }

        if (running)
        {
            spawnCounter -= delta;

            if (spawnCounter <= 0)
            {
                if (GetNode("Enemies").GetChildren().Length <= 50)
                {
                    SpawnEnemy();
                    while (random.Next(0, 5) < 2)
                    {
                        SpawnEnemy();
                    }

                    spawnCounter = 2;
                }
            }
        }
    }
Ejemplo n.º 12
0
 public override void _PhysicsProcess(float delta)
 {
     Velocity = GlobalPosition.DirectionTo(Target) * Speed;
     if (GlobalPosition.DistanceTo(Target) > 5)
     {
         Velocity = MoveAndSlide(Velocity);
     }
 }
Ejemplo n.º 13
0
    public override void _PhysicsProcess(float delta)
    {
        knockback = knockback.MoveToward(Vector2.Zero, 200 * delta);
        knockback = MoveAndSlide(knockback);

        Vector2 dir = Vector2.Zero;

        switch (state)
        {
        case AIState.IDLE:
            velocity = velocity.MoveToward(Vector2.Zero, 200 * delta);
            SeekPlayer();
            RestartWander();
            break;

        case AIState.WANDER:
            SeekPlayer();
            RestartWander();
            dir             = GlobalPosition.DirectionTo(wanderController.TargetPosition);
            velocity        = velocity.MoveToward(dir * maxSpeed, acceleration * delta);
            batSprite.FlipH = velocity.x < 0;
            if (GlobalPosition.DistanceTo(wanderController.TargetPosition) <= maxSpeed * delta)
            {
                state = PickRandomState(stateArray);
                wanderController.StartWanderTimer(rng.RandfRange(1, 3));
            }
            break;

        case AIState.CHASE:
            var player = playerZone.Player;
            if (player != null)
            {
                dir      = GlobalPosition.DirectionTo(player.GlobalPosition);
                velocity = velocity.MoveToward(dir * maxSpeed, acceleration * delta);
            }
            else
            {
                state = AIState.IDLE;
            }
            batSprite.FlipH = velocity.x < 0;
            break;

        default:
            break;
        }
        if (softCollision.isColliding())
        {
            velocity += softCollision.GetPushVector() * delta * 300;
        }
        velocity = MoveAndSlide(velocity);
    }
Ejemplo n.º 14
0
    public override void _Process(float delta)
    {
        base._Process(delta);

        TargetArea.Visible = Globals.HoveringBuilding == this || Globals.SelectedBuilding == this;

        if (TargetArea.GetOverlappingBodies().Count == 0)
        {
            ShootTimer.Stop();

            RotationTimer = RotationTimerStart;
        }
        else
        {
            if (ShootTimer.IsStopped())
            {
                ShootTimer.Start();
            }

            PhysicsBody2D closestTarget   = (PhysicsBody2D)TargetArea.GetOverlappingBodies()[0];
            float         closestDistance = GlobalPosition.DistanceTo(closestTarget.GlobalPosition);

            foreach (PhysicsBody2D body in TargetArea.GetOverlappingBodies())
            {
                float dist = GlobalPosition.DistanceTo(body.GlobalPosition);
                if (closestDistance > dist)
                {
                    closestTarget   = body;
                    closestDistance = dist;
                }
            }

            float target = closestTarget.GlobalPosition.AngleToPoint(RotateGroup.GlobalPosition);

            if (target != RotationEnd)
            {
                RotationStart = RotateGroup.GlobalRotation;
                RotationEnd   = target;
                RotationTimer = RotationTimerStart;
            }

            if (RotateGroup.GlobalRotation != RotationEnd)
            {
                RotateGroup.GlobalRotation = Mathf.LerpAngle(RotateGroup.GlobalRotation, RotationEnd, RotationTimer);

                RotationTimer += delta * RotationSpeed;

                RotationTimer = Mathf.Min(RotationTimer, 1f);
            }
        }
    }
Ejemplo n.º 15
0
    public void wanderState(float delta)
    {
        seekPlayer();

        if (wanderController.getTimeLeft() == 0)
        {
            doWanderAndIdleLogic();
        }
        moveToward(wanderController.targetPosition, delta);
        if (GlobalPosition.DistanceTo(wanderController.targetPosition) < 4)
        {
            doWanderAndIdleLogic();
        }
    }
Ejemplo n.º 16
0
    private void Start()
    {
        switch (GetState())
        {
        case State.Jump:
            velocity.y -= JumpForce;
            break;

        case State.Zap:
            Sound.Play("zap.wav", this, 20);
            protonLine.Width   = 128;
            protonLine.Texture = lightningTexture;
            if (Mode != Particle.Proton)
            {
                swapAnim.Play("Proton");
                Mode = Particle.Proton;
            }
            break;

        case State.Hook:
            Sound.Play("grapple.wav", this, 15);
            protonLine.Width   = 32;
            protonLine.Texture = lightningTexture;
            if (Mode != Particle.Electron)
            {
                swapAnim.Play("Electron");
                Mode = Particle.Electron;
            }
            RotateTween(0);
            hookDistance = Mathf.Max(GlobalPosition.DistanceTo(closest.GlobalPosition), MinimumHookLength);
            prevProton   = closest;
            break;

        case State.WallSlide:
            RotateTween(0);
            break;

        case State.WallJump:
            velocity.x = -WallJumpKick *Mathf.Sign(wallCurr.CastTo.x);

            velocity.y = -WallJumpForce;
            break;
        }

        string name = GetState().ToString();

        anim.Play(anim.HasAnimation(name) ? name : "Idle");
    }
Ejemplo n.º 17
0
    public override void _PhysicsProcess(float delta)
    {
        if (KnockBackTimer.TimeLeft == 0)
        {
            var      g = GetTree().GetNodesInGroup("Player");
            Node2D[] p = new Node2D[g.Count];
            g.CopyTo(p, 0);
            p = p.Where(n => PatrolArea.GetOverlappingBodies().Contains(n) && (n as Knight).Health > 0).ToArray();

            if (p.Any())
            {
                if (p.FirstOrDefault().GlobalPosition.DistanceTo(GlobalPosition) > 10 && (p.FirstOrDefault() as Knight).Health != 0)
                {
                    Move(p.FirstOrDefault().GlobalPosition < GlobalPosition ? -1 : 1);
                }
                else
                {
                    Move(0);
                }
            }
            else
            {
                if (GlobalPosition.DistanceTo(SpawnPos) > 2)
                {
                    Move(SpawnPos.x > GlobalPosition.x ? 1 : -1);
                }
                else
                {
                    Move(0);
                }
            }

            p = p.Where(n => n.GlobalPosition.DistanceTo(GlobalPosition) < 10).ToArray();

            if (p.Any() && IsOnFloor() && CanAttack && !Attacking && Health > 0)
            {
                Attack();
            }
        }
        else
        {
            Move(FlyR ? 1 : -1);
        }

        ProcessGravity(delta);
    }
Ejemplo n.º 18
0
    public override void _PhysicsProcess(float delta)
    {
        if (InfluencedBodies != null)
        {
            foreach (Node node in InfluencedBodies)
            {
                if (node is SpacePhysicsObject body)
                {
                    float distanceFromBody = GlobalPosition.DistanceTo(body.GlobalPosition);

                    body.AddForce(body.GetAngleTo(GlobalPosition) + body.Rotation, Gravity / (distanceFromBody / (Gravity * 100)) * delta);
                }
            }
            if (OrbitalParent != null)
            {
            }
        }
    }
Ejemplo n.º 19
0
    private void UpdateZap()
    {
        const int signCost = 50;

        int sign = Mathf.Sign(velocity.x);

        if (sign == 0)
        {
            sign = lastMoveSign;
        }
        else
        {
            lastMoveSign = sign;
        }

        protonLine.ClearPoints();
        closest = null;

        // A proton is only reachable if it's in range of the circle's area + any number
        float closestDist = Mathf.Pi * ZapDistance * ZapDistance + 0.69420f;

        foreach (Proton body in protonField.GetOverlappingAreas())
        {
            float dist = GlobalPosition.DistanceTo(body.GlobalPosition);

            // prioritize protons in the movement direction
            Vector2 dir = GlobalPosition.DirectionTo(body.GlobalPosition);
            if (dir.x * sign > 0)
            {
                dist -= signCost;
            }

            if (Mathf.Abs(dist) < closestDist && body != prevProton)
            {
                protonRay.CastTo = ToLocal(body.GlobalPosition);
                if (protonRay.IsColliding())
                {
                    continue;
                }
                closestDist = dist;
                closest     = body;
            }
        }
    }
Ejemplo n.º 20
0
    protected void GetMovementInput(float delta)
    {
        var inputVector = Vector2.Zero;

        inputVector = player.GlobalPosition - GlobalPosition;
        inputVector = inputVector.Normalized();
        if (GlobalPosition.DistanceTo(player.GlobalPosition) > stoppingDistance)
        {
            velocity = velocity.MoveToward(inputVector * speed, accel * delta);
        }
        else if (GlobalPosition.DistanceTo(player.GlobalPosition) < retreatDistance)
        {
            velocity = velocity.MoveToward(-inputVector * speed, accel * delta);
        }
        else
        {
            velocity = velocity.MoveToward(Vector2.Zero, accel * delta);
        }
    }
Ejemplo n.º 21
0
    public async void RecalculatePath()
    {
        Array targetBuildings = GetTree().GetNodesInGroup("EnemyTargets");

        if (targetBuildings.Count == 0)
        {
            return;
        }

        var   closest         = (Building)targetBuildings[0];
        float closestDistance = GlobalPosition.DistanceTo(closest.GlobalPosition);

        foreach (Building building in targetBuildings)
        {
            if (!IsInstanceValid(building))
            {
                continue;
            }

            float dist = GlobalPosition.DistanceTo(building.GlobalPosition);

            if (dist < closestDistance && !building.Deleting)
            {
                closest         = building;
                closestDistance = dist;
            }
            await ToSignal(GetTree().CreateTimer(0.01f), "timeout");
        }

        if (!IsInstanceValid(closest))
        {
            CallDeferred("RecalculatePath");
            return;
        }

        target = closest;

        NavigationManager.QueueAgent(this, target.GlobalPosition, (Array <Vector2> newPath) =>
        {
            path = newPath;
        });
    }
Ejemplo n.º 22
0
        public override void _PhysicsProcess(float delta)
        {
            var distance = GlobalPosition.DistanceTo(target.GlobalPosition);

            if (distance > activeDistance + 100)
            {
                return;
            }

            var direction = GlobalPosition.DirectionTo(target.GlobalPosition);

            if (distance > 300)
            {
                parent.MoveAndSlide(direction * speed);
            }
            else if (distance < 250)
            {
                parent.MoveAndSlide(-direction * speed);
            }
        }
Ejemplo n.º 23
0
        public override void _PhysicsProcess(float delta)
        {
            var distance = GlobalPosition.DistanceTo(target.GlobalPosition);

            if (distance < 300)
            {
                var direction = GlobalPosition.DirectionTo(target.GlobalPosition);
                directionCallback(direction);

                if (framesUntilAttack == 0)
                {
                    framesUntilAttack = (int)GD.RandRange(0, 60) + attackFrameFrequency;
                    Shoot(direction);
                }
                else
                {
                    framesUntilAttack = Math.Max(framesUntilAttack - 1, 0);
                }
            }
        }
Ejemplo n.º 24
0
    private void wanderState()
    {
        seekPlayer();
        NewTargetPosition = wanderController.NewTargetPosition();
        if (wanderController.GetTimeLeft() == 0)
        {
            state = pickRandomState(new Global.BehaviorState[] { Global.BehaviorState.STATE_IDLE, Global.BehaviorState.STATE_WANDER });
            Random r = new Random();
            wanderController.StartWanderTimer(r.Next(1, 4));
            Direction = GlobalPosition.DirectionTo(NewTargetPosition);
        }

        Velocity = Velocity.MoveToward(Direction * MaxSpeed, Acceleration);
        if (GlobalPosition.DistanceTo(NewTargetPosition) <= 4)
        {
            state = pickRandomState(new Global.BehaviorState[] { Global.BehaviorState.STATE_IDLE, Global.BehaviorState.STATE_WANDER });
            Random r = new Random();
            wanderController.StartWanderTimer(r.Next(1, 4));
        }
    }
Ejemplo n.º 25
0
    public override void _Process(float delta)
    {
        var mouse = GetGlobalMousePosition();

        MouseOver = GlobalPosition.DistanceTo(mouse) < 50;

        if (Input.IsActionJustPressed("click") && MouseOver)
        {
            FollowMouse = true;
        }
        if (Input.IsActionJustReleased("click"))
        {
            FollowMouse = false;
        }

        if (FollowMouse)
        {
            GetParent <Node2D>().GlobalPosition = mouse;
        }
    }
Ejemplo n.º 26
0
    public override void _PhysicsProcess(float delta)
    {
        if (influencedBodies != null)
        {
            foreach (Node node in influencedBodies)
            {
                if (node is SpacePhysicsObject body)
                {
                    float distanceFromBody = GlobalPosition.DistanceTo(body.GlobalPosition);

                    body.AddForce(body.GetAngleTo(GlobalPosition) + body.Rotation, Gravity / (distanceFromBody / (Gravity * 100)) * delta);
                }
            }
        }
        if (OrbitalParent != null)
        {
            Vector2 vectorToParent   = GlobalPosition - OrbitalParent.GlobalPosition;
            float   distanceToParent = GlobalPosition.DistanceTo(OrbitalParent.GlobalPosition);
            GlobalPosition = OrbitalParent.GlobalPosition + (GlobalPosition - OrbitalParent.GlobalPosition).Rotated(OrbitalRotationSpeed * delta);
        }
    }
Ejemplo n.º 27
0
        public override void _InputEvent(Object viewport, InputEvent @event, int shapeIdx)
        {
            if (dialogActive)
            {
                return;
            }

            if (@event.IsActionPressed("left_click"))
            {
                GetTree().SetInputAsHandled();
                if (GlobalPosition.DistanceTo(player.GlobalPosition) < 16)
                {
                    EmitSignal(nameof(SendTextToDialog), text);
                }
                else
                {
                    player.Set("targetX", new Vector2(GetGlobalMousePosition().x, 0));
                    player.Set("targetY", new Vector2(0, GetGlobalMousePosition().y));
                }
            }
        }
Ejemplo n.º 28
0
    public override void _Process(float delta)
    {
        if (Target == null)
        {
            return;
        }
        if (GlobalPosition.DistanceTo(Target.GlobalPosition) < 5 && KillTimer.TimeLeft == 0)
        {
            KillTimer.Start(0.3f);
            Particle.Emitting = false;
            if (!Used && Target is Knight k)
            {
                Used = true;
                k.SoulCount++;
            }
        }

        Velocity += new Vector2(Target.GlobalPosition.x > GlobalPosition.x ? MoveSpeed * delta : -MoveSpeed * delta,
                                Target.GlobalPosition.y > GlobalPosition.y ? MoveSpeed * delta : -MoveSpeed * delta);

        MoveAndSlide(Velocity, new Vector2(0, 1));
    }
Ejemplo n.º 29
0
    public Interactable getInteractable()
    {
        if (interactableAreas.Length == 0)
        {
            return(null);
        }
        float  closestDistance = -1f;
        Area2D closestArea     = null;

        foreach (Area2D area in interactableAreas)
        {
            Node2D parent = area.GetParent <Node2D>();
            if (!(parent is Interactable) || !(parent as Interactable).canInteract())
            {
                continue;
            }
            // If we are above the interactable we need to be facing front.
            if (GlobalPosition.y < parent.GlobalPosition.y && side_state == SideStates.back)
            {
                continue;
            }
            // If we are below the interactable we need to be facing back.
            if (parent.GlobalPosition.y < GlobalPosition.y && side_state == SideStates.front)
            {
                continue;
            }
            float distance = GlobalPosition.DistanceTo(area.GlobalPosition);
            if (closestDistance < 0 || distance < closestDistance)
            {
                closestDistance = distance;
                closestArea     = area;
            }
        }
        if (closestArea == null)
        {
            return(null);
        }
        return(closestArea.GetParent <Interactable>());
    }
    public override void _PhysicsProcess(float delta)
    {
        _targetGlobalPosition = _target.GlobalPosition;
        float toTarget = GlobalPosition.DistanceTo(_targetGlobalPosition);

        if (startedMoving)
        {
            // Emit signal one frame after agent starts moving. This is because
            // Target uses GetOverlappingBodies(), which is updated only once per frame,
            // to detect overlapping bodies.
            EmitSignal(nameof(StartedMoving));
            startedMoving = false;
        }

        if (toTarget < DISTANCE_TRESHOLD)
        {
            return;
        }

        // OffsetTargetPosition is a position slightly behind the object that the agent follows.
        // We get it by calculating the direction to the target (subtract agent's position from target's
        // position and normalize the result), multiplying the direction with the follow offset,
        // and subtracting that from the target's global position.
        // We only use it if the distance to target is greater than the follow offset. Otherwise,
        // the offsetTargetPosition is set to the current position.
        Vector2 offsetTargetPosition = toTarget > FollowOffset
                                        ? _targetGlobalPosition - (_targetGlobalPosition - GlobalPosition).Normalized() * FollowOffset
                                        : GlobalPosition;

        _velocity = Steering.ArriveTo(_velocity,
                                      GlobalPosition,
                                      offsetTargetPosition,
                                      MaxSpeed,
                                      ArriveRadius,
                                      Mass);
        _velocity         = MoveAndSlide(_velocity);
        Triangle.Rotation = _velocity.Angle();
    }