コード例 #1
0
ファイル: Racer.cs プロジェクト: mubeenfahmed/desert-racer
    // Direction can be one of "up" or "down". This begins the process of changing the lane.
    // Later, the GameObject should call `racer.CompleteChangeLanes()` to finish the transition.
    // TODO: Check if the lane that they're transitioning to is occupied by a thing? Should that
    // be handled at the data layer? Probably.
    public bool ChangeLanes(string direction)
    {
        // If a lane change is already underway, then we simply return `false`.
        if (this.isChangingLanes)
        {
            return(false);
        }

        // Determine a possible `nextLane`.
        int nextLane = direction == "up" ? Lanes.IncreaseLane(this.lane) : Lanes.DecreaseLane(this.lane);

        // Check if a car is already in that lane
        bool carIsThere = this.carExistsInLane(nextLane, this.position);

        if (carIsThere)
        {
            return(false);
        }

        // If our current lane is the `nextLane`, then we must be at a boundary. We can't move in
        // this direction, so we return false.
        if (this.lane == nextLane)
        {
            return(false);
        }

        // Otherwise, we set the car to `isChangingLanes`, update our `nextLane`, and return `true`.
        this.isChangingLanes = true;
        this.nextLane        = nextLane;
        return(true);
    }