Example #1
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;
        }
    }
Example #2
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);
    }
Example #3
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);
            }
        }
    }
Example #4
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);
    }
Example #5
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);;
            }
        }
    }
Example #6
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;
    }