Пример #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            var trackLength = 2;

            trec          = new RacingTrack(trackLength);
            textBox1.Text = trackLength.ToString();
        }
Пример #2
0
    public RacingTrack GenerateTrackRand()
    {
        RacingTrack racingTrack = CreateTrack(UnityEngine.Random.Range(1, 100000), length, difficulty);

        generateCheckPointsAndStart(racingTrack);
        return(racingTrack);
    }
Пример #3
0
    public RacingTrack GenerateTrack()
    {
        RacingTrack racingTrack = CreateTrack(seed, length, difficulty);

        generateCheckPointsAndStart(racingTrack);
        return(racingTrack);
    }
Пример #4
0
 public void StartGame(Scene old, Scene current)
 {
     if (current.name == "Main" && !inGame)
     {
         inGame = true;
         if (trackController == null)
         {
             trackController = FindObjectOfType <TrackController>();
         }
         trackController.difficulty = difficulty;
         trackController.seed       = seed != 0 ? seed : UnityEngine.Random.Range(1, 100000);
         currentTrack   = trackController.GenerateTrack();
         lastCheckpoint = trackController.lastCheckpoint;
         Instantiate(Car);
         playing = true;
         InGamePanel.SetActive(true);
         SetCheckpoint(lastCheckpoint + 1);
         SetScore();
     }
 }
Пример #5
0
    private void generateCheckPointsAndStart(RacingTrack racingTrack)
    {
        int checkpointIndex = 0;

        for (int i = 1; i < racingTrack.trackChunks.Count - 1 / checkpointFrequency; i++)
        {
            TrackChunk chunk = racingTrack.trackChunks[i];
            if ((i * checkpointFrequency) % 1 <= 0.01)
            {
                Checkpoint clone = Instantiate(checkpoint, chunk.transform) as Checkpoint;
                setFlagPosition(chunk, clone.transform);
                clone.checkpointIndex = checkpointIndex;
                checkpointIndex++;
            }
        }
        lastCheckpoint = checkpointIndex - 1;
        TrackChunk finishChunk = racingTrack.trackChunks[0];
        Finish     finishFlag  = Instantiate(finish, finishChunk.transform) as Finish;

        setFlagPosition(finishChunk, finishFlag.transform);
    }
Пример #6
0
    public void GenerateTrack()
    {
        difficulty = Mathf.Clamp01(difficulty);
        length     = Mathf.Clamp(length, MIN_LENGTH, MAX_LENGTH);

        HashSet <TrackNode> nodes = new HashSet <TrackNode>();

        // Start one block forward
        IntVector3 cursor = new IntVector3();

        cursor.z = 1;

        float heightChangeProb = HEIGHT_CHANGE_PROB * difficulty;
        int   remainingChunks  = length;
        int   currentRotation  = 0;
        bool  done             = false;

        int firstStretchLen = 0;
        int countdown       = INITIAL_GENERATION_TIMEOUT * length;

        while (!done)
        {
            if (countdown-- < 0)
            {
                throw new InvalidTrackException("Initial track generation timed out.");
            }

            int heightDiff = 0;
            TrackChunk.ChunkType direction = TrackChunk.ChunkType.STRAIGHT;

            if (firstStretchLen > FIRST_STRETCH_LEN)
            {
                if (rand.NextDouble() < heightChangeProb)
                {
                    heightDiff = (rand.NextDouble() < 0.5 ? 1 : -1);
                }

                // Decide Direction
                if (heightDiff == 0 && rand.NextDouble() < DIRECTION_TURN_PROB * difficulty)
                {
                    if (rand.NextDouble() < 0.5)
                    {
                        direction = TrackChunk.ChunkType.CURVE_LEFT;
                    }
                    else
                    {
                        direction = TrackChunk.ChunkType.CURVE_RIGHT;
                    }
                }
            }

            firstStretchLen++;

            int nextRotation = currentRotation;

            // Update rotation
            if (direction == TrackChunk.ChunkType.CURVE_RIGHT)
            {
                nextRotation += 90;
            }
            else if (direction == TrackChunk.ChunkType.CURVE_LEFT)
            {
                nextRotation -= 90;
            }

            nextRotation = ClampRotation(nextRotation);

            IntVector3 nextPos = GetNextPoint(cursor, nextRotation, 0);

            if (HasNodeAt(nodes, nextPos))
            {
                continue;
            }

            if (heightDiff == 0)
            {
                IntVector3 nextPosUp   = GetNextPoint(cursor, nextRotation, 1);
                IntVector3 nextPosDown = GetNextPoint(cursor, nextRotation, -1);

                if (HasNodeAt(nodes, nextPosUp) || HasNodeAt(nodes, nextPosDown))
                {
                    continue;
                }
            }
            else
            {
                IntVector3 nextPosHDiff = GetNextPoint(cursor, nextRotation, heightDiff);
                if (HasNodeAt(nodes, nextPosHDiff))
                {
                    continue;
                }
            }

            nextPos.y += heightDiff;

            TrackNode node = new TrackNode(cursor, currentRotation);
            node.chunkType  = direction;
            node.heightDiff = heightDiff;
            nodes.Add(node);
            trackOrderedChunks.Add(node);

            currentRotation = nextRotation;
            cursor          = nextPos;

            if (firstStretchLen > FIRST_STRETCH_LEN)
            {
                remainingChunks--;
            }

            if (remainingChunks == 0)
            {
                done = true;
            }
        }

        // Now, close the circuit

        bool ok = CloseCircuit(nodes, cursor, currentRotation);

        // Instance prefabs
        List <TrackChunk> instancedChunks = InstanceChunkClones(trackOrderedChunks);

        if (!ok)
        {
            throw new InvalidTrackException("A* pathfinding timed out.");
        }
        else
        {
            RacingTrack rt = trackBase.AddComponent <RacingTrack>();
            rt.trackChunks = instancedChunks;
            rt.startChunk  = instancedChunks[0];
            rt.endChunk    = instancedChunks[instancedChunks.Count - 1];
        }
    }