public override void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        if (h != 0f && v == 0f)
        {
            movement.Set(h, v);
            if (collisionManagement.CanMove(movement))
            {
                playerRef.Move(new Vector3(playerRef.speed * Time.deltaTime * h, 0f, 0f));
            }
        }
        else if (v != 0f && h == 0f)
        {
            movement.Set(h, v);
            if (collisionManagement.CanMove(movement))
            {
                playerRef.Move(new Vector3(0f, playerRef.speed * Time.deltaTime * v, 0f));
            }
        }
        if (Input.GetKey(KeyCode.Space))
        {
            GameObject collisionObject = collisionManagement.GetCollisionObject();
            if (collisionObject != null)
            {
                ObjectOfInterest interaction = collisionObject.GetComponent <ObjectOfInterest>();
                if (interaction != null)
                {
                    Debug.Log("Interaction = " + interaction);
                    interaction.Activate();
                }
            }
        }
    }
Example #2
0
        public MoveResult Move(IHitBox hitBox, Vector2 interpolation)
        {
            AxisAlignedBoundingBox aabb = hitBox.BoundingBox;

            var result = CollisionDetection.CanMove(aabb.Bounds, interpolation, DetectionType.Collision);

            if (result.HasFlag(MoveResult.LevelCompleted))
            {
                result = MoveResult.LevelCompleted;
            }
            else
            {
                result = Reposition(hitBox, result, interpolation);
            }

            Diagnostic.Write("aabb", aabb.Position);

            return(result);
        }
Example #3
0
        private MoveResult Reposition(IHitBox hitBox, MoveResult result, Vector2 interpolation)
        {
            AxisAlignedBoundingBox aabb = hitBox.BoundingBox;

            var moveX = result.HasFlag(MoveResult.X) &&
                        !result.HasFlag(MoveResult.BlockedOnNegativeX) &&
                        !result.HasFlag(MoveResult.BlockedOnPositiveX);

            if (moveX)
            {
                aabb.Position.X += interpolation.X;
            }

            bool surfaced = CanSetSurfaced(hitBox);
            var  moveY    = result.HasFlag(MoveResult.Y) &&
                            !result.HasFlag(MoveResult.BlockedOnNegativeY) &&
                            !result.HasFlag(MoveResult.BlockedOnPositiveY);

            if ((moveY || !surfaced) && !result.HasFlag(MoveResult.Died))
            {
                hitBox.State = HitBoxState.Airborne;
            }
            else
            {
                hitBox.State = HitBoxState.Surfaced;
            }

            if (moveY && !result.HasFlag(MoveResult.Died))
            {
                aabb.Position.Y += interpolation.Y;
            }

            var reverse = CollisionDetection.CanMove(aabb.Bounds, -interpolation, DetectionType.Retrace);

            if (moveX)             // fix issue when moving on X axis to the left.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeX) && interpolation.X > 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result          &= ~MoveResult.X;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveX) && interpolation.X < 0)
                {
                    aabb.Position.X -= interpolation.X;
                    result          &= ~MoveResult.X;
                }
            }

            if (moveY)             // fix issue when hitting an impassable on Y axis when jumping.
            {
                if (reverse.HasFlag(MoveResult.BlockedOnNegativeY) && interpolation.Y > 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result          &= ~MoveResult.Y;
                }
                else if (reverse.HasFlag(MoveResult.BlockedOnPositiveY) && interpolation.Y < 0)
                {
                    aabb.Position.Y -= interpolation.Y;
                    result          &= ~MoveResult.Y;
                }
            }

            return(result);
        }