Exemple #1
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;
    }