Exemplo n.º 1
0
 public virtual void ResetState()
 {
     transform.position = initState.pos;
     platformMask       = initState.platformMask;
     triggerMask        = initState.triggerMask;
     interactFlag       = initState.interactFlag;
     isSleep            = initState.isSleep;
     velocity           = Vector3.zero;
     targetVelocity     = Vector3.zero;
     gameObject.layer   = initState.currentLayer;
     GetComponent <SpriteRenderer>().maskInteraction = initState.maskInteraction;
     collisionState.Reset();
 }
Exemplo n.º 2
0
    /// <summary>
    /// attempts to move the character to position + deltaMovement. Any colliders in the way will cause the movement to
    /// stop when run into.
    /// </summary>
    /// <param name="deltaMovement">Delta movement.</param>
    public void Move(Vector3 deltaMovement)
    {
        // save off our current grounded state which we will use for becameGroundedThisFrame
        var oldCollisionState = new CollisionState2D(collision);

        // clear our state
        collision.Reset();
        _raycastHitsThisFrame.Clear();
        _isGoingUpSlope = false;

        var desiredPosition = entity.Position + deltaMovement;

        primeRaycastOrigins(desiredPosition, deltaMovement);

        // first, we check for a slope below us before moving
        // only check slopes if we are going down and grounded
        if (deltaMovement.y < 0 && oldCollisionState.Below)
        {
            handleVerticalSlope(ref deltaMovement);
        }

        // now we check movement in the horizontal dir
        if (Mathf.Abs(deltaMovement.x) > 0)
        {
            moveHorizontally(ref deltaMovement, oldCollisionState);
        }

        // next, check movement in the vertical dir
        if (Mathf.Abs(deltaMovement.y) > 0)
        {
            moveVertically(ref deltaMovement, oldCollisionState);
        }

        // move then update our state
        if (data.usePhysicsForMovement)
        {
            rigidBody.MovePosition(entity.Position + deltaMovement);
            velocity = rigidBody.velocity;
        }
        else
        {
            //transform.Translate( deltaMovement, Space.World );
            var newPosition = entity.Position + deltaMovement;

            entity.SetPosition(newPosition);

            // only calculate velocity if we have a non-zero deltaTime
            if (FrameCounter.Instance.deltaTime > 0)
            {
                velocity = deltaMovement / FrameCounter.Instance.deltaTime;
            }
        }

        // After translation, update proximity check so collision state is fresh.
        // Without this, collision state doesn't get updated if there's no delta movement.
        collision = CheckProximity(data.skinWidth * 2, Direction2D.ALL);

        // Add to the collision buffer.
        collisionBuffer.AddFirst(new CollisionState2D(collision));

        if (collisionBuffer.Count > data.collisionBufferMax)
        {
            collisionBuffer.RemoveLast();
        }

        // set our becameGrounded state based on the previous and current collision state
        if (!oldCollisionState.Below && collision.Below)
        {
            collision.becameGroundedThisFrame = true;
        }

        // if we are going up a slope we artificially set a y velocity so we need to zero it out here
        if (_isGoingUpSlope)
        {
            velocity.y = 0;
        }

        // send off the collision events if we have a listener
        if (OnControllerCollidedEvent != null)
        {
            for (var i = 0; i < _raycastHitsThisFrame.Count; i++)
            {
                OnControllerCollidedEvent(_raycastHitsThisFrame[i]);
            }
        }
    }