示例#1
0
    void ResolveCollisions(RaycastHit2D hit, CollisionType collisionType, ref Vector2 velocity, float moveDir, ref float rayLength)
    {
        collisions.collided = true;

        if (collisionType == CollisionType.HORIZONTAL && hit.distance == 0)
        {
            if (hit.transform.tag != "Hazard")
            {
                return;
            }
        }

        if (hit.transform.tag != "Gravity Switch")
        {
            if (!canFlipGravity)
            {
                canFlipGravity = true;
            }
        }

        switch (hit.transform.tag)
        {
        case "Hazard":
            if (!collisions.hitHazard)     //fix after making collisions end after 1 ray... doesn't really make sense to continue w/o slopes
            {
                if (!invincible)
                {
                    OnHazard();
                    collisions.hitHazard = true;
                }
            }
            break;

        case "Powerup":
            Powerup pw = hit.collider.GetComponent <Powerup>();
            powerups.AddLast(pw.GetPowerupType());
            if (powerups.Count > powerupSize && !greedy)
            {
                powerups.RemoveFirst();
            }
            UpdatePowerupUI();
            pw.Consume();
            return;

        case "Goal":
            EntityManager.instance.levelComplete = true;
            hit.collider.GetComponent <Goal>().GoalComplete();
            return;

        case "Through":
            if (collisionType == CollisionType.VERTICAL)
            {
                if (moveDir == 1 || hit.distance == 0 || userInput.y == -1)     //watch for bugs from removing fallingThroughPlat, invoke after .5 sec delay (seblague)
                {
                    return;
                }
                break;
            }
            else
            {
                return;
            }

        case "Yoku":
            if (collisionType == CollisionType.VERTICAL)
            {
                if (hit.distance == 0)
                {
                    return;
                }
            }
            break;

        case "Ice":
            if (collisionType == CollisionType.VERTICAL)
            {
                groundAcceleration = 0.75f;
            }
            break;

        case "Gravity Switch":
            if (canFlipGravity)
            {
                FlipGravity();
                canFlipGravity = false;
                return;
            }
            else
            {
                return;
            }

        case "Entity Switch":
            hit.collider.GetComponent <Switch>().Consume();
            return;

        case "Quicksand":
            inQuicksand = true;
            return;

        case "Water":
            if (!inWater)
            {
                inWater         = true;
                this.velocity.y = Mathf.Clamp(this.velocity.y, -2f, 10f);
            }
            collisions.collidedWater = true;
            return;

        case "Checkpoint":
            spawnPoint = hit.transform.position;
            hit.collider.GetComponent <Goal>().GoalComplete();
            return;

        default:
            break;
        }

        if (collisionType == CollisionType.VERTICAL)
        {
            velocity.y = (hit.distance - SKIN_WIDTH) * moveDir;
            rayLength  = hit.distance;

            if (!gravityFlipped)
            {
                collisions.below = moveDir == -1;
                collisions.above = moveDir == 1;
            }
            else
            {
                collisions.below = moveDir == 1;
                collisions.above = moveDir == -1;
            }
        }
        else
        {
            velocity.x = Mathf.Min(Mathf.Abs(velocity.x), (hit.distance - SKIN_WIDTH)) * moveDir;
            rayLength  = Mathf.Min(Mathf.Abs(velocity.x) + SKIN_WIDTH, hit.distance);

            collisions.left  = moveDir == -1;
            collisions.right = moveDir == 1;
        }
    }