Ejemplo n.º 1
0
    public override void _IntegrateForces(Physics2DDirectBodyState bodyState)
    {
        Vector2 linearVelocity = bodyState.LinearVelocity;
        float   step           = bodyState.Step;

        PlayerInputInteraction playerInputInteraction = ListenToPlayerInput();

        linearVelocity.x -= this.floorHVelocity;
        floorHVelocity    = 0.0f;

        FloorContact floorContact = FindFloorContact(bodyState);

        ProcessSpawn(playerInputInteraction);
        ProcessShooting(playerInputInteraction, step);
        ProcessFloorContact(floorContact, step);
        linearVelocity = ProcessJump(playerInputInteraction, linearVelocity, step);
        linearVelocity = ProcessPlayerMovement(playerInputInteraction, linearVelocity, step);

        this.shooting = playerInputInteraction.Shoot;
        if (floorContact.FoundFloor)
        {
            floorHVelocity    = bodyState.GetContactColliderVelocityAtPosition(floorContact.FloorIndex).x;
            linearVelocity.x += floorHVelocity;
        }

        linearVelocity          += bodyState.TotalGravity * step;
        bodyState.LinearVelocity = linearVelocity;
    }
Ejemplo n.º 2
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        EmitSignal("_forces_integrated", new object[] { state });

        if (jelly == null)
        {
            return;
        }

        float   damping      = jelly.damping;
        float   stiffness    = jelly.stiffness;
        Vector2 totalImpulse = Vector2.Zero;

        foreach (Neighbour n in neighbours)
        {
            JellyAtom neighbourAtom       = n.jellyAtom;
            Vector2   posNeighbour        = neighbourAtom.GlobalPosition;
            Vector2   selfPos             = state.Transform.origin;
            float     restLength          = n.restLength;
            Vector2   diff                = posNeighbour - selfPos;
            float     distanceToNeighbour = diff.Length();
            Vector2   diffNormalized      = diff / distanceToNeighbour;
            Vector2   selfVelocity        = state.LinearVelocity;
            Vector2   neighbourVelocity   = neighbourAtom.LinearVelocity;
            float     force               = (distanceToNeighbour - restLength) * stiffness
                                            + (diffNormalized).Dot(neighbourVelocity - selfVelocity) * damping;

            totalImpulse += force * diff.Normalized() * state.Step;
        }

        ApplyCentralImpulse(totalImpulse);
    }
Ejemplo n.º 3
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        if (Mode == ModeEnum.Static)
        {
            GD.Print("Static");
            return;
        }

        integrateForcesCounter++;
        GD.Print(integrateForcesCounter);

        //GD.Print("IsApproxEqual?: " + _previousXForm.origin + " | " + state.Transform.origin);
        if (Utils.IsApproxEqual(_previousXForm.origin, state.Transform.origin))
        {
            GD.Print("_consecutiveEqualities");
            _consecutiveEqualities += 1;
        }

        if (_consecutiveEqualities > ConsecutiveMaxEqualities)
        {
            GD.Print("CallDeferred");
            SetDeferred("mode", ModeEnum.Static);
            CallDeferred("emit_signal", nameof(SleepingStateChanged));
        }

        _previousXForm = state.Transform;
    }
Ejemplo n.º 4
0
    private void UpdateLinearVelocity(Physics2DDirectBodyState bodyState)
    {
        Vector2 linearVelocity = LinearVelocity;

        linearVelocity.x         = this.direction * WALK_SPEED;
        bodyState.LinearVelocity = linearVelocity;
    }
Ejemplo n.º 5
0
    private void Walk(Physics2DDirectBodyState bodyState)
    {
        float wallSide = 0.0f;

        for (int i = 0; i < bodyState.GetContactCount(); i++)
        {
            Godot.Object contactColliderObject = bodyState.GetContactColliderObject(i);
            Vector2      contactLocalNormal    = bodyState.GetContactLocalNormal(i);

            if (contactColliderObject != null && contactColliderObject is Bullet)
            {
                Bullet contactCollidedBullet = contactColliderObject as Bullet;
                if (!contactCollidedBullet.Disabled)
                {
                    CallDeferred("BulletCollider", contactCollidedBullet, bodyState, contactLocalNormal);
                    break;
                }
            }

            wallSide = FindCorrectWallSide(contactLocalNormal, wallSide);
        }

        int correctDirection = FindCorrectDirection(wallSide);

        if (this.direction != correctDirection)
        {
            this.direction = correctDirection;

            Sprite  sprite = GetNode("Sprite") as Sprite;
            Vector2 scale  = sprite.Scale;
            scale.x      = -this.direction;
            sprite.Scale = scale;
        }
    }
Ejemplo n.º 6
0
    public override void _IntegrateForces(Physics2DDirectBodyState physics_state)
    {
        base._IntegrateForces(physics_state);
        Transform2D xform = physics_state.GetTransform();
        Vector2     origin;
        Transform2D wibble;

        if (xform.Origin.x > _screensize.x + _radius)
        {
            origin = new Vector2(0 - _radius, xform.Origin.y);
            wibble = new Transform2D(xform.x, xform.y, origin);
            physics_state.SetTransform(wibble);
        }
        if (xform.Origin.x < 0 - _radius)
        {
            origin = new Vector2(_screensize.x + _radius, xform.Origin.y);
            wibble = new Transform2D(xform.x, xform.y, origin);
            physics_state.SetTransform(wibble);
        }

        if (xform.Origin.y > _screensize.y + _radius)
        {
            origin = new Vector2(xform.Origin.x, 0 - _radius);
            wibble = new Transform2D(xform.x, xform.y, origin);
            physics_state.SetTransform(wibble);
        }

        if (xform.Origin.y < 0 - _radius)
        {
            origin = new Vector2(xform.Origin.x, _screensize.y + _radius);
            wibble = new Transform2D(xform.x, xform.y, origin);
            physics_state.SetTransform(wibble);
        }
    }
Ejemplo n.º 7
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        Vector2 velocity = GetLinearVelocity();

        velocity = updatedVelocity(velocity);
        updateAnimation(velocity);
        SetLinearVelocity(velocity);
        jump();
    }
Ejemplo n.º 8
0
        protected override void ApplyRotation(float delta, Physics2DDirectBodyState state)
        {
            if (Vel.Length() > 0f || RotateBy != 0)
            {
                Transform2D xform = state.Transform.Rotated(MathU.LerpAngle(state.Transform.Rotation, RotateBy == 0 ? LinearVelocity.Angle() + Mathf.Deg2Rad(90) : RotateBy,
                                                                            Type.RotationSpeed * delta * (Speed / Type.MaxSpeed) + Mathf.Abs(RotateBy / Mathf.Tau)) - state.Transform.Rotation);

                state.Transform = xform;
            }
        }
Ejemplo n.º 9
0
    public override void _IntegrateForces(Godot.Object st)
    {
        Physics2DDirectBodyState state = (Physics2DDirectBodyState)st;

        if (dragged)
        {
            Vector2 pos      = state.Transform.origin;
            Vector2 mousePos = body.GetGlobalMousePosition();
            state.ApplyCentralImpulse((mousePos - pos) * state.Step * 100);
        }
    }
Ejemplo n.º 10
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        if (shouldReset)
        {
            state.Transform      = new Transform2D(0f, Vector2.Zero);
            state.LinearVelocity = GetRandomVelocity();
            shouldReset          = false;
        }

        base._IntegrateForces(state);
    }
Ejemplo n.º 11
0
        public override void _IntegrateForces(Physics2DDirectBodyState state)
        {
            if (!_queuedNewLocation.HasValue)
            {
                return;
            }

            Position           = _queuedNewLocation.Value;
            _queuedNewLocation = null;
            Mode = ModeEnum.Rigid;
        }
Ejemplo n.º 12
0
    private void BulletCollider(Bullet contactCollidedBullet, Physics2DDirectBodyState bodyState, Vector2 contactLocalNormal)
    {
        Mode       = ModeEnum.Rigid;
        this.state = STATE_DYING;

        bodyState.AngularVelocity = Mathf.Sign(contactLocalNormal.x) * ANGULAR_VELOCITY_CONSTANT;
        Friction = FRICTION_CONSTANT;
        contactCollidedBullet.Disable();

        AudioStreamPlayer2D soundHit = GetNode("SoundHit") as AudioStreamPlayer2D;

        soundHit.Play();
    }
Ejemplo n.º 13
0
 public override void _IntegrateForces(Physics2DDirectBodyState state)
 {
     if (isDropped)
     {
         var bodies = GetCollidingBodies();
         foreach (Node2D node in bodies)
         {
             if (node is Player || Position.y > screenSize.y)
             {
                 GetNode <Node>("/root/Signals").EmitSignal("AddPowerUp", this);
                 QueueFree();
             }
         }
     }
 }
Ejemplo n.º 14
0
    public override void _IntegrateForces(Physics2DDirectBodyState bodyState)
    {
        String correctAnimation = FindCorrectAnimation();

        if (correctAnimation.Equals("walk"))
        {
            Walk(bodyState);
        }

        if (this.animation != correctAnimation)
        {
            UpdateAnimation(correctAnimation);
        }

        UpdateLinearVelocity(bodyState);
    }
Ejemplo n.º 15
0
    private FloorContact FindFloorContact(Physics2DDirectBodyState bodyState)
    {
        FloorContact floorContact = new FloorContact(false, -1);

        for (int i = 0; i < bodyState.GetContactCount(); i++)
        {
            Vector2 contactLocalNormal = bodyState.GetContactLocalNormal(i);

            if (contactLocalNormal.Dot(new Vector2(0, -1)) > 0.6f)
            {
                floorContact.FoundFloor = true;
                floorContact.FloorIndex = i;
            }
        }

        return(floorContact);
    }
Ejemplo n.º 16
0
        public override void _IntegrateForces(Physics2DDirectBodyState state)
        {
            if (!_ignoreTimeScale)
            {
                return;
            }

            var downscaledVel = state.LinearVelocity * _prevTimeScale;

            LinearVelocity = downscaledVel / Engine.TimeScale;

            var gravityDiff = (state.TotalGravity / Engine.TimeScale) - state.TotalGravity;

            LinearVelocity += gravityDiff * (state.Step / Engine.TimeScale);

            _prevTimeScale = Engine.TimeScale;
        }
Ejemplo n.º 17
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        AppliedForce  = state.TotalGravity;
        AppliedTorque = 0f;

        if (ThrusterControls.IsUFODriveEnabled)
        {
            var clampedVelocity = ThrusterControls.UFODriveVelocity.Normalized() * Mathf.Min(ThrusterControls.UFODriveVelocity.Length(), ufoDriveMaxSpeed);
            state.LinearVelocity = clampedVelocity;
            //state.LinearVelocity = ThrusterControls.UFODriveVelocity;
            state.AngularVelocity = ThrusterControls.UFODriveAngularVelocity;
        }
        else
        {
            AddForce(mainThruster.GlobalPosition - GlobalPosition, mainThruster.GetResultantThrustVector());
            AddForce(portRetroThruster.GlobalPosition - GlobalPosition, portRetroThruster.GetResultantThrustVector());
            AddForce(starboardRetroThruster.GlobalPosition - GlobalPosition, starboardRetroThruster.GetResultantThrustVector());
            AddForce(portForeThruster.GlobalPosition - GlobalPosition, portForeThruster.GetResultantThrustVector());
            AddForce(portAftThruster.GlobalPosition - GlobalPosition, portAftThruster.GetResultantThrustVector());
            AddForce(starboardForeThruster.GlobalPosition - GlobalPosition, starboardForeThruster.GetResultantThrustVector());
            AddForce(starboardAftThruster.GlobalPosition - GlobalPosition, starboardAftThruster.GetResultantThrustVector());
        }

        //Enable/Disable the UFO Drive visuals depending on chosen PropulsionMode
        //ufoDriveParticles.SetEmitting(PropulsionMode == ShipPropulsionMode.ManualUFODrive);
        ufoDriveParticles.SetEmitting(ThrusterControls.IsUFODriveEnabled);

        if (Input.IsActionJustPressed("MovementStop"))
        {
            state.LinearVelocity  = Vector2.Zero;
            state.AngularVelocity = 0f;
        }

        int contactCount = state.GetContactCount();

        if (contactCount > 0)
        {
            //GD.Print("Contact count: " + contactCount);
            for (int i = 0; i < contactCount; i++)
            {
                var vector = state.GetContactColliderVelocityAtPosition(i);
                //GD.Print("Vector: " + vector);
            }
        }
    }
Ejemplo n.º 18
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        if (_picked)
        {
            var axis = (_touchPos - GlobalPosition + _offset.Rotated(GlobalRotation)).Normalized();

            // state.LinearVelocity =  new Vector2(Mathf.Min(Mathf.Abs(diff.x), _VelocityLimit) * Mathf.Sign(diff.x), Mathf.Min(Mathf.Abs(diff.y), _VelocityLimit) * Mathf.Sign(diff.y));
            // state.LinearVelocity *= _DragScale;
            state.AngularVelocity = GlobalRotation * -1.5f;
        }

        if (state.GetContactCount() >= 1)
        {
            _localCollisionPos    = state.GetContactLocalPosition(0);
            _collisionVelocityLen = state.LinearVelocity.Length();
        }

        base._IntegrateForces(state);
    }
Ejemplo n.º 19
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        for (int i = 0; i < state.GetContactCount(); i++)
        {
            var contactLocalPosition = state.GetContactLocalPosition(i);
            var tilesInRadius        = GetTilesInRadius(GetTileMap, contactLocalPosition, Radius);

            if (tilesInRadius.Any())
            {
                RunBangAnimation();
            }

            foreach (var cell in tilesInRadius)
            {
                GetTileMap.SetCell((int)cell.x, (int)cell.y, -1);
                GetAnotherTileMap(GetAnotherNumber()).SetCell((int)cell.x, (int)cell.y, -1);;
            }
        }
    }
Ejemplo n.º 20
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        if (IsNetworkMaster())
        {
            if (Input.IsActionPressed("ui_right"))
            {
                rotationDir += 1;
            }
            if (Input.IsActionPressed("ui_left"))
            {
                rotationDir -= 1;
            }

            SetAppliedTorque(rotationDir * torque);
            rotationDir = 0;

            Rset("rotation", RotationDegrees);
        }
        else
        {
            RotationDegrees = rotation;
        }
    }
Ejemplo n.º 21
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        var lv      = state.LinearVelocity;
        var newAnim = Animation;

        if (IsDead)
        {
            newAnim = "dead";
        }
        else
        {
            newAnim = "walk";

            var wallSide = 0.0;

            for (int i = 0; i < state.GetContactCount(); ++i)
            {
                var contactObj         = state.GetContactColliderObject(i);
                var contactObjId       = state.GetContactColliderId(i);
                var contactLocalNormal = state.GetContactLocalNormal(i);

                if (contactObj != null)
                {
                    if (contactObj is Player)                     //Player is the object to get a collision with this mob.
                    {
                        Player player = (Player)contactObj;
                        Hit(player);
                    }
                }

                if (contactLocalNormal.x > 0.9)
                {
                    wallSide = 1;
                }
                else if (contactLocalNormal.x < -0.9)
                {
                    wallSide = -1;
                }
            }

            if (wallSide != 0 && wallSide != _direction)
            {
                _direction         = -_direction;
                MovingSprite.Scale = new Vector2(MovingSprite.Scale.x * -_direction, MovingSprite.Scale.y);
                newAnim            = "idle";
            }

            if (_direction < 0 && !_rcLeft.IsColliding() && _rcRight.IsColliding())
            {
                _direction         = -_direction;
                MovingSprite.Scale = new Vector2(MovingSprite.Scale.x * -_direction, MovingSprite.Scale.y);
                newAnim            = "idle";
            }
            else if (_direction > 0 && !_rcRight.IsColliding() && _rcLeft.IsColliding())
            {
                _direction         = -_direction;
                MovingSprite.Scale = new Vector2(MovingSprite.Scale.x * -_direction, MovingSprite.Scale.y);
                newAnim            = "idle";
            }

            var speed = (int)Constants.RandRand(MinSpeed, MaxSpeed);
            lv.x = _direction * speed;
        }

        if (IsInAttack)
        {
            newAnim = "hit";
        }

        if (Animation != newAnim)
        {
            Animation = newAnim;
            if (_direction > 0)
            {
                AnimatPlay.Play(Animation);
            }
            else
            {
                AnimatPlay.PlayBackwards(Animation);
            }
        }

        state.LinearVelocity = lv;
    }
Ejemplo n.º 22
0
 public override void _IntegrateForces(Physics2DDirectBodyState state)
 {
     EmitSignal("_forces_integrated", new object[] { state });
 }
Ejemplo n.º 23
0
    public override void _IntegrateForces(Physics2DDirectBodyState state)
    {
        if (Input.IsActionPressed("shield_up"))
        {
            if (!shieldUp)
            {
                GetNode <Sprite>("sS").GetNode <AnimationPlayer>("AnimationPlayer").Play("shield_up");
                shieldUp = true;
            }
        }

        if (Input.IsActionPressed("shield_down"))
        {
            if (shieldUp)
            {
                GetNode <Sprite>("sS").GetNode <AnimationPlayer>("AnimationPlayer").Play("shield_down");
                shieldUp = false;
            }
        }


        if (Input.IsActionPressed("ui_up") || Input.IsActionPressed("ui_down"))
        {
            if (Input.IsActionPressed("ui_up"))
            {
                SetAppliedForce(thrust.Rotated(Rotation));
            }

            if (Input.IsActionPressed("ui_down"))
            {
                SetAppliedForce(-thrust.Rotated(Rotation));
            }
        }
        else
        {
            SetAppliedForce(new Vector2());
        }

        if (Input.IsActionPressed("ui_right"))
        {
            rotationDir += 1;
        }
        if (Input.IsActionPressed("ui_left"))
        {
            rotationDir -= 1;
        }
        if (Input.IsActionPressed("shoot_1"))
        {
            shooting = true;
        }
        else
        {
            shooting = false;
        }
        if (Input.IsActionPressed("shoot_2"))
        {
            shooting2 = true;
            // GetNode<CPUParticles2D>("Weapon1").Emitting = true;
            Vector2 collisionPoint     = GetNode <RayCast2D>("RayCast2D").GetCollisionPoint();
            float   distanceFromObject = GetGlobalPosition().DistanceTo(collisionPoint);
            Log.p("distance from object: " + (distanceFromObject));
            Log.p("is colliding: " + GetNode <RayCast2D>("RayCast2D").IsColliding());

            if (GetNode <RayCast2D>("RayCast2D").IsColliding())
            {
                // GetNode<CPUParticles2D>("Weapon1").Lifetime = (distanceFromObject/1000);
            }
            else
            {
                // GetNode<CPUParticles2D>("Weapon1").Lifetime = 1f;
            }
        }
        else
        {
            shooting2 = false;
            // GetNode<CPUParticles2D>("Weapon1").Emitting = false;
        }

        SetAppliedTorque(rotationDir * torque);
        rotationDir = 0;
    }