Beispiel #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;
        }
    }
Beispiel #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);
    }
Beispiel #3
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;
    }