Inheritance: MonoBehaviour
Exemplo n.º 1
0
    void AddPiece(TrackPiece p)
    {
        TrackPiece selected = InstantiateTrackPiece(p);

        selected.transform.SetParent(transform);
        selected.track_position.AttachTo(GetLastTrackPiece().track_position, track_scale);
        future_track_pieces.Add(selected);

        if (other_possible_pieces.ContainsKey(selected.track_position.end_position))
        {
            other_possible_pieces.Remove(selected.track_position.end_position);
        }

        other_possible_pieces.Add(selected.track_position.end_position, new TrackRarityList(track_objects));

        foreach (TrackPosition tp in selected.track_position.TilesOccupied())
        {
            map.Add(tp, p);
        }

        GenerateObstacles(selected);

        selected.SetColor(gen_material);

        selected.gameObject.SetActive(false);
    }
Exemplo n.º 2
0
    public override void CheckVisit(TrackPiece piece, Player triggering_player)
    {
        int index = track.IndexOf(piece);

        if (triggering_player.is_active)
        {
            if (index >= 0)
            {
                int distance_from_end = track.Count - index - 1;
                while (distance_from_end < leading_track_count)
                {
                    AddNextPiece();
                    distance_from_end++;
                }
                GameManager.instance.RespawnPlayers(piece);
            }
            else
            {
                while (track[track.Count - 1] != piece)
                {
                    AddNextPiece();
                }
                CheckVisit(piece, triggering_player);
                return;
            }
        }

        last_visited_piece[triggering_player] = piece;
        if (GeneralUIController.instance != null)
        {
            GeneralUIController.instance.player_positions.SetPosition(triggering_player.player_number, track.IndexOf(last_visited_piece[triggering_player]) / (float)track_count);
        }
    }
Exemplo n.º 3
0
 public void RemoveFromMap(TrackPiece p)
 {
     foreach (TrackPosition tp in p.track_position.TilesOccupied())
     {
         map.Remove(tp);
     }
 }
Exemplo n.º 4
0
    private TrackPiece GetTrackPieceAfter(TrackPiece tp)
    {
        List <TrackPiece> trackPieces = trackBuilder.GetTrackPieces();
        int i = trackPieces.IndexOf(tp);

        return((i + 1 < trackPieces.Count) ? trackPieces[i + 1] : null);
    }
Exemplo n.º 5
0
    private TrackPiece GetTrackPieceBefore(TrackPiece tp)
    {
        List <TrackPiece> trackPieces = trackBuilder.GetTrackPieces();
        int i = trackPieces.IndexOf(tp);

        return((i - 1 >= 0) ? trackPieces[i - 1] : null);
    }
Exemplo n.º 6
0
    void Update()
    {
        if (manager != null)
        {
            TrackPiece piece = manager.LastPiece(player);

            if (piece != null)
            {
                Vector2 end_position = piece.track_position.end_position.position * piece.transform.localScale.x;

                Vector3 point_at = new Vector3(end_position.x, 0, end_position.y);

                Quaternion rotation = Quaternion.LookRotation(point_at - transform.position);

                rotation = Quaternion.Euler(new Vector3(transform.rotation.x, rotation.eulerAngles.y, transform.rotation.z));

                transform.rotation = rotation;

                if (Mathf.DeltaAngle(rotation.eulerAngles.y, transform.root.rotation.eulerAngles.y) > 90f)
                {
                    timer += Time.deltaTime;
                    if (timer > 2f)
                    {
                        pointer.SetActive(true);
                    }
                }
                else
                {
                    timer = 0;
                    pointer.SetActive(false);
                }
            }
        }
    }
Exemplo n.º 7
0
    public override void AddPiece(TrackPiece p)
    {
        if (track.Count < track_count)
        {
            p.gameObject.SetActive(true);
            track.Add(p);
            track_ids.Add(p, track_ids.Count);
            if (track.Count == 1)
            {
                foreach (Player player in GameManager.instance.GetPlayers())
                {
                    last_visited_piece[player] = p;
                }
            }
            if (track.Count == track_count)
            {
                GameObject new_finish_line = Instantiate(finish_line);
                Vector2    end_position    = (p.track_position.end_position.position + p.track_position.NextStartingPoint().position) / 2 * p.transform.localScale.x;
                new_finish_line.transform.position = new Vector3(end_position.x, 0, end_position.y);

                new_finish_line.transform.SetParent(p.transform, true);

                new_finish_line.transform.rotation    = Quaternion.Euler(new Vector3(0, (int)p.track_position.end_direction * 90, 0));
                new_finish_line.transform.localScale *= p.transform.localScale.x;

                p.SetColor(final_piece_material);
            }
            else
            {
                p.SetColor(track_material);
            }
        }
    }
Exemplo n.º 8
0
    public TrackPiece GetNextPiece()
    {
        TrackPiece to_return = future_track_pieces[0];

        other_possible_pieces.Remove(to_return.track_position.end_position);
        future_track_pieces.RemoveAt(0);

        return(to_return);
    }
Exemplo n.º 9
0
    void Awake()
    {
        foreach (MeshRenderer mr in GetComponentsInChildren <MeshRenderer>())
        {
            mr.enabled = false;
        }

        parentTrackPiece = GetComponentInParent <TrackPiece>();
    }
Exemplo n.º 10
0
    public void ConcatenateWithPreviousTrackPiece(TrackPiece previousTrackPiece)
    {
        Waypoint firstWaypoint    = GetFirstWaypoint();
        Waypoint prevLastWaypoint = previousTrackPiece.GetLastWaypoint();

        transform.rotation = Quaternion.FromToRotation(firstWaypoint.transform.forward,
                                                       prevLastWaypoint.transform.forward);
        transform.rotation = Quaternion.LookRotation(transform.forward, Vector3.up); // Always looking to up
        transform.position = prevLastWaypoint.transform.position + (transform.position - firstWaypoint.transform.position);
    }
Exemplo n.º 11
0
 public void RespawnPlayers(TrackPiece piece)
 {
     if (respawn_on == piece)
     {
         foreach (Player player in to_respawn)
         {
             player.Active(true);
         }
         to_respawn.Clear();
         respawn_on = null;
     }
 }
Exemplo n.º 12
0
    TrackPiece RemoveLastPiece()
    {
        TrackPiece to_return = future_track_pieces[future_track_pieces.Count - 1];

        RemoveFromMap(to_return);

        other_possible_pieces.Remove(to_return.track_position.end_position);
        Destroy(to_return.gameObject);
        future_track_pieces.RemoveAt(future_track_pieces.Count - 1);

        return(to_return);
    }
Exemplo n.º 13
0
    // Start is called before the first frame update
    void Start()
    {
        //m_pool = new LeanGameObjectPool();
        TrackPiece.setSpawner(this);

        m_curPivot = new GameObject();
        m_curPivot.transform.position = Vector3.zero;

        for (int i = 0; i < m_minTrackSize; i++)
        {
            spawnTrack(0);
        }
    }
Exemplo n.º 14
0
    public IEnumerator GenerateTrackPieces()
    {
        while (true)
        {
            yield return(null);

            if (future_track_pieces.Count < lead)
            {
                TrackPosition gen_at = GetNextGenPosition();
                if (other_possible_pieces.ContainsKey(gen_at))
                {
                    List <TrackRarity> possible_pieces = other_possible_pieces[gen_at].pieces;
                    TrackPiece         selected        = null;
                    if (possible_pieces.Count > 0)
                    {
                        // If possible pieces remain try one
                        float rand  = Random.Range(0, other_possible_pieces[gen_at].rarity);
                        int   count = -1;

                        float rarity_to_remove = 0;
                        while (rand > 0)
                        {
                            count++;
                            selected         = possible_pieces[count].piece;
                            rand            -= possible_pieces[count].rarity;
                            rarity_to_remove = possible_pieces[count].rarity;
                        }
                        possible_pieces.RemoveAt(count);
                        other_possible_pieces[gen_at].rarity -= rarity_to_remove;

                        if (PieceCanFit(selected))
                        {
                            // If can fit add it to future pieces
                            AddPiece(selected);
                        }
                    }
                    else
                    {
                        // If no possible pieces remain backtrack
                        RemoveLastPiece();
                    }
                }
                else
                {
                    // if gen at does not exist add it to the gen with all track_objects as possible;
                    other_possible_pieces.Add(gen_at, new TrackRarityList(track_objects));
                }
            }
        }
    }
Exemplo n.º 15
0
    void SetFirstPiece(TrackPiece selected)
    {
        starting_piece.track_position.Init(new TrackPosition(Vector2.zero, TrackPosition.Height.middle), TrackPiecePosition.Direction.north, track_scale);

        selected.transform.SetParent(transform);
        future_track_pieces.Add(selected);

        foreach (TrackPosition tp in selected.track_position.TilesOccupied())
        {
            map.Add(tp, selected);
        }

        selected.SetColor(gen_material);
    }
Exemplo n.º 16
0
    public bool PieceCanFit(TrackPiece p)
    {
        if (future_track_pieces.Count > 0)
        {
            foreach (TrackPosition pos in p.track_position.TilesOccupied(future_track_pieces[future_track_pieces.Count - 1].track_position))
            {
                if (map.ContainsKey(pos))
                {
                    return(false);
                }
            }
        }

        return(true);
    }
Exemplo n.º 17
0
    void Update()
    {
        //Checks whether another block must be generated
        this.block_creation_timer += Time.deltaTime;
        if (this.block_creation_timer >= this.block_creation_frequency)
        {
            this.block_creation_timer = 0;

            //Performs spatial displacement for the Perlin Noise module
            this.point_z += this.point_shift_step * Time.timeScale;

            //Checks if this block is the first to be generated
            if (this.track_piece != null)
            {
                //Creates a new block
                TrackPiece new_track_piece = new TrackPiece();
                //Sets the new block as child of the previous block so that the asset perform the fitting
                new_track_piece.SetParentNode(this.track_piece);
                //Stores the reference to the new block
                this.track_piece = new_track_piece;
            }
            //If this is the first block
            else
            {
                this.track_piece = new TrackPiece();
            }

            //Adds self destruction script
            if (self_destruct_blocks)
            {
                SelfDestruction self_destruction = this.track_piece.GetGameObject().AddComponent <SelfDestruction>();
                //Sets the life span
                self_destruction.life_span = this.blocks_life_span;
            }

            //Applies the settings in the new block
            TrackPieceEditor track_piece_editor = track_piece.game_object.GetComponent <TrackPieceEditor>();
            track_piece_editor.seed    = this.seed;
            track_piece_editor.point.z = this.point_z;
            //Calls internal configuration process of the block
            track_piece_editor.Setup();
            //Performs block generation based on configuration parameters
            (new TrackPieceGenerator()).Generate(track_piece);
            //Eliminate previous block reference from the current block, as it is no longer necessary to keep connection between the two
            track_piece.SetParentNode(null);
        }
    }
Exemplo n.º 18
0
    void AddRandomPiece()
    {
        int        maximumTries = 10; // Just in case to avoid infinite loops
        bool       foundCompatibleTrackPiece = false;
        GameObject newTrackPieceGO           = null;

        while (--maximumTries != 0)
        {
            if (trackPieces.Count <= numPieces - 1)
            {
                newTrackPieceGO = GameObject.Instantiate(GetRandomTrackPiecePrefab()) as GameObject;
            }
            else
            {
                newTrackPieceGO = GameObject.Instantiate(lastTrackPiecePrefab.gameObject) as GameObject;
            }
            newTrackPieceGO.transform.localScale = Vector3.one * trackScale;

            TrackPiece newTrackPiece = newTrackPieceGO.GetComponent <TrackPiece>();
            // Align with the previous track piece
            TrackPiece prevTrackPiece = trackPieces[trackPieces.Count - 1];
            newTrackPiece.ConcatenateWithPreviousTrackPiece(prevTrackPiece);

            if (CollidesWithTrack(newTrackPieceGO))
            {
                if (maximumTries > 1) // Dont destroy in the last try
                {
                    Destroy(newTrackPieceGO);
                }
            }
            else // We have found a good candidate for track piece!
            {
                foundCompatibleTrackPiece      = true;
                newTrackPiece.transform.parent = transform;
                break;
            }
        }

        trackPieces.Add(newTrackPieceGO.GetComponent <TrackPiece>());

        if (!foundCompatibleTrackPiece)
        {
            //Debug.LogWarning("HAVENT FOUND COMPATIBLE TRACK PIECE, backtracking");
            Backtrack();
        }
    }
Exemplo n.º 19
0
    public List <Waypoint> GetNPointsBeforeOrAfter(Vector3 position, int n, bool before)
    {
        // Find the two consecutive points such that the ship is between them.
        // Amongst these, take the closest ones (there can be many, take only into
        // those that belong to the track pieces around the player (before, over and after))

        TrackPiece trackPieceBelow  = GetTrackPieceBelow(position);
        TrackPiece trackPieceBefore = GetTrackPieceBefore(trackPieceBelow);
        TrackPiece trackPieceAfter  = GetTrackPieceAfter(trackPieceBelow);

        List <Waypoint> waypoints  = trackBuilder.GetWaypointsList();
        List <Waypoint> resultList = new List <Waypoint>();

        Waypoint wpBefore = waypoints[waypoints.Count - 1], wpAfter = wpBefore;

        for (int i = 0; i < waypoints.Count - 1; ++i)
        {
            Waypoint wp1 = waypoints[i], wp2 = waypoints[i + 1];
            bool     wp1Before = Vector3.Dot(wp1.GetForward(), (position - wp1.transform.position)) > 0;
            bool     wp2After  = Vector3.Dot(wp2.GetForward(), (position - wp2.transform.position)) < 0;
            if (wp1Before && wp2After)
            {
                if (wp1.GetTrackPiece() == trackPieceBefore ||
                    wp1.GetTrackPiece() == trackPieceBelow ||
                    wp1.GetTrackPiece() == trackPieceAfter)
                {
                    if (before)
                    {
                        for (int j = i; j >= Mathf.Max(i - n + 1, 0); --j)
                        {
                            resultList.Add(waypoints[j]);
                        }
                    }
                    else
                    {
                        for (int j = i + 1; j < Mathf.Min(i + 1 + n, waypoints.Count); ++j)
                        {
                            resultList.Add(waypoints[j]);
                        }
                    }
                    return(resultList);
                }
            }
        }
        return(resultList);
    }
Exemplo n.º 20
0
    public virtual void AddPiece(TrackPiece p)
    {
        p.gameObject.SetActive(true);

        track.Add(p);
        track_ids.Add(p, pieces_removed + track_ids.Count);
        if (track.Count == 1)
        {
            foreach (Player player in GameManager.instance.GetPlayers())
            {
                last_visited_piece[player] = p;
            }
        }

        if (track.Count > track_count)
        {
            RemoveOldestPiece();
        }

        p.SetColor(track_material);
    }
    private IEnumerator SpawnEnemy(int amount, TrackPiece trackPiece, float spawnDelay = 1f)
    {
        //Debug.Log("Spawn Enemy");
        enemySpawnQueueCount += amount;
        for (int i = 0; i < amount; i++)
        {
            yield return(new WaitForEndOfFrame());

            if (i > trackPiece.enemySpawnPoints.Count)
            {
                break;
            }

            Transform        spawnPoint = trackPiece.enemySpawnPoints[i];
            WheelDriveCustom newEnemy   = LeanPool.Spawn(gameData.enemyPrefab, trackPiece.enemySpawnPoints[i].position,
                                                         trackPiece.enemySpawnPoints[i].rotation);
            enemies.Add(newEnemy);
            trackPiece.QueueEnemy(spawnPoint, newEnemy);
            enemySpawnQueueCount--;
        }
    }
Exemplo n.º 22
0
    public GameObject AddTrackPiece(Vector3 totalAngle, Vector3 modifiedPosition, Vector3 eulerAngles, Vector3 startAngle, Vector3 offset, float percentageOfTrack, int secondCurveStart)
    {
        GameObject newTrackPiece;

        if (unusedTrackPieces.Count > 0)
        {
            newTrackPiece = unusedTrackPieces[0];
            unusedTrackPieces.RemoveAt(0);

            newTrackPiece.SetActive(true);
        }
        else
        {
            newTrackPiece = Instantiate(trackPrefab, transform);

            TrackPiece trackPieceClass = newTrackPiece.GetComponent <TrackPiece>();

            trackPieceClass.Start();
            trackPieceClass.rollerCoaster = this;
        }

        //reset position and angle before adjusting the track
        newTrackPiece.transform.position         = Vector3.zero;
        newTrackPiece.transform.localEulerAngles = Vector3.zero;

        TrackPiece newTrackPieceClass = newTrackPiece.GetComponent <TrackPiece>();

        newTrackPieceClass.totalAngle = totalAngle;
        trackPieces.Add(newTrackPiece);

        //adjust the track
        newTrackPieceClass.AdjustTrack(totalAngle, startAngle, percentageOfTrack, secondCurveStart);

        //set track rotation (after adjustment to make sure the adjustment process goes well)
        newTrackPiece.transform.eulerAngles = eulerAngles;
        //subtract offset
        newTrackPiece.transform.position = modifiedPosition - offset;

        return(newTrackPiece);
    }
    public void TrackSpawned(TrackPiece trackPiece)
    {
        //Debug.Log("Track Spawned");
        totalTracksSpawned += 1;

        if (totalTracksSpawned <= 1)
        {
            return;
        }

        if (enemies.Count + enemySpawnQueueCount < minEnemies)
        {
            StartCoroutine(SpawnEnemy(maxEnemies - enemies.Count, trackPiece));
            return;
        }

        if (enemies.Count + enemySpawnQueueCount < maxEnemies)
        {
            //if (UnityEngine.Random.Range(0, 1) == 1)
            StartCoroutine(SpawnEnemy(1, trackPiece));
        }
    }
Exemplo n.º 24
0
    void Start()
    {
        //Generates track
        TrackPiece track_piece = null;

        for (int block = 0; block < this.blocks_count; ++block)
        {
            //Performs spatial displacement for the Perlin Noise module
            this.point_z += this.point_shift_step * Time.timeScale;

            //Checks if this block is the first to be generated
            if (track_piece != null)
            {
                //Creates a new block
                TrackPiece new_track_piece = new TrackPiece();
                //Sets the new block as child of the previous block so that the asset perform the fitting
                new_track_piece.SetParentNode(track_piece);
                //Stores the reference to the new block
                track_piece = new_track_piece;
            }
            //If this is the first block
            else
            {
                track_piece = new TrackPiece();
            }

            //Applies the settings in the new block
            TrackPieceEditor track_piece_editor = track_piece.game_object.GetComponent <TrackPieceEditor>();
            track_piece_editor.seed    = this.seed;
            track_piece_editor.point.z = this.point_z;
            //Calls internal configuration process of the block
            track_piece_editor.Setup();
            //Performs block generation based on configuration parameters
            (new TrackPieceGenerator()).Generate(track_piece);
            //Eliminate previous block reference from the current block, as it is no longer necessary to keep connection between the two
            track_piece.SetParentNode(null);
        }
    }
Exemplo n.º 25
0
    //normal: should it use the normal start track rotation or use a converted version using MathHelper.ConvertQuant2Euler()
    public Vector3 getCurrentAngle(GameObject startTrack, bool normal)
    {
        Vector3 currentAngle = Vector3.zero;

        TrackPiece trackPiece = startTrack.GetComponent <TrackPiece>();

        if (!trackPiece.modified)
        {
            currentAngle = trackPiece.totalAngle;
        }
        currentAngle += trackPiece.startAngle;

        //that function does not puts the angle into the x, so it is not useful when doing turns
        if (normal)
        {
            currentAngle += startTrack.transform.localEulerAngles;
        }
        else
        {
            currentAngle += MathHelper.ConvertQuant2Euler(startTrack.gameObject.transform.localRotation);
        }

        return(currentAngle);
    }
Exemplo n.º 26
0
    private TrackPiece GetTrackPieceBelow(Vector3 position)
    {
        RaycastHit hitInfo;

        if (Physics.Raycast(position, Vector3.down, out hitInfo, 999.9f, trackLayer))
        {
            return(hitInfo.collider.gameObject.GetComponentInParent <TrackPiece>());
        }
        else
        {
            TrackPiece closestTrackPiece = null;
            float      minDist           = Mathf.Infinity;
            foreach (TrackPiece tp in trackBuilder.GetTrackPieces())
            {
                float d = Vector3.Distance(position, tp.transform.position);
                if (d < minDist)
                {
                    closestTrackPiece = tp;
                    minDist           = d;
                }
            }
            return(closestTrackPiece);
        }
    }
Exemplo n.º 27
0
 void GenerateObstacles(TrackPiece p)
 {
     p.obstacles.SpawnRandomSet(0);
 }
Exemplo n.º 28
0
 protected virtual void Awake()
 {
     m_parentPiece = FindParentPiece();
 }
Exemplo n.º 29
0
    /// <summary>
    ///     Follows a track segment to find all containing stations until it reaches another junction or determines track is invalid
    /// </summary>
    /// <param name="direction">0 - 3 representing the four directions out of the junction</param>
    /// <returns>True if it found complete segment</returns>
    public bool TrackFollow(int direction)
    {
        //Initialize the check from the junction
        long    nextX  = this.mnX;
        long    nextY  = this.mnY;
        long    nextZ  = this.mnZ;
        Vector3 dirvec = new Vector3();

        //Store the initial junction direction for later recording which direction the connected junction is associated with
        int initialdirection = direction;

        //List of freight cart stations found on this segment -> to be written to the final constructed FreightTrackSegment
        List <FreightCartStation> SegmentStations = new List <FreightCartStation>();

        //Store visited track pieces for catching when the segment enters a closed loop
        List <TrackPiece> VisitedTracks = new List <TrackPiece>();

        //Add a penalty for pathfinding in certain directions to avoid stations and other undesirable routes
        int PathfindPenalty = 0;

        //Begin loop here.  Direction can be set and used to check the next location each time through the loop
        //Allow segments only up to 2048 long due to cost of loop checking - may revise after testing
        for (int n = 0; n < 2048; n++)
        {
            switch (direction)
            {
            case 0:
                nextX++;
                dirvec = Vector3.right;
                break;

            case 1:
                nextZ++;
                dirvec = Vector3.forward;
                break;

            case 2:
                nextX--;
                dirvec = Vector3.left;
                break;

            case 3:
                nextZ--;
                dirvec = Vector3.back;
                break;

            default:
                nextX++;
                break;
            }

            ushort lValue1 = 0;
            byte   lFlags1 = 0;
            ushort type    = this.GetCube(nextX, nextY, nextZ, out lValue1, out lFlags1);
            this.mUnderSegment = this.mPrevGetSeg;
            //Debug.LogWarning("GetCube type: " + type.ToString() + " value: " + lValue1);
            bool foundslope = false;

            //Found air and need to check for a downward slope under it
            if (type == 1)
            {
                ushort  lValue2 = 0;
                byte    lFlags2 = 0;
                ushort  cube    = this.GetCube(nextX, nextY - 1L, nextZ, out lValue2, out lFlags2);
                Segment segment = this.mPrevGetSeg;
                type    = cube;
                lFlags1 = lFlags2;
                lValue1 = lValue2;
                if ((type == 538 && lValue1 == 2) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
                {
                    foundslope = true;
                    nextY--; //decrement Y level for next loop through!
                }
                else
                {
                    if (type == 0)
                    {
                        Debug.LogError("Error, track follower has null under segment!");
                    }
                    if (this.mPrevGetSeg == null)
                    {
                        Debug.LogError("Error, prevseg was null!");
                    }
                    if (segment == null)
                    {
                        Debug.LogError("Error, old was null!");
                    }
                    if (this.mPrevGetSeg != segment)
                    {
                        Debug.LogWarning(("Track follower is looking for a slope, and has had to check across segment boundaries for this![Old/New" + segment.GetName() + " -> " + this.mPrevGetSeg.GetName()));
                    }
                    return(false);
                }
            }

            Vector3 trackvec = SegmentCustomRenderer.GetRotationQuaternion(lFlags1) * Vector3.forward;
            bool    oneway   = false;
            trackvec.Normalize();
            trackvec.x = trackvec.x >= -0.5 ? (trackvec.x <= 0.5 ? 0.0f : 1f) : -1f;
            trackvec.y = trackvec.y >= -0.5 ? (trackvec.y <= 0.5 ? 0.0f : 1f) : -1f;
            trackvec.z = trackvec.z >= -0.5 ? (trackvec.z <= 0.5 ? 0.0f : 1f) : -1f;
            //Begin checking track type
            if (type == TRACKTYPE || type == ScrapTrackType)
            {
                if ((type == TRACKTYPE && (lValue1 == TRACKSTRAIGHT || lValue1 == TRACKEMPTY || lValue1 == TRACKFULL)) || (type == ScrapTrackType && lValue1 == ScrapStraightVal))
                {
                    if (trackvec.y > 0.5 || trackvec.y < -0.5)
                    {
                        return(false);
                    }
                    else if (!(trackvec == dirvec) && !(trackvec == -dirvec))
                    {
                        dirvec = new Vector3(trackvec.x, 0f, trackvec.z);
                        oneway = true; // Can't set return path as the same -> they're different!
                    }
                }
                if ((type == TRACKTYPE && lValue1 == TRACKCORNER) || (type == ScrapTrackType && lValue1 == ScrapCornerVal))
                {
                    if (dirvec == new Vector3(-trackvec.z, 0.0f, trackvec.x))
                    {
                        dirvec = new Vector3(dirvec.z, 0.0f, -dirvec.x);
                    }
                    else if (trackvec == -dirvec)
                    {
                        dirvec = new Vector3(-dirvec.z, 0.0f, dirvec.x);
                    }
                    else
                    {
                        return(false);
                    }
                }
                if ((type == TRACKTYPE && lValue1 == TRACKSLOPE) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
                {
                    Vector3 vector3_2 = trackvec;
                    dirvec.y = 0.0f;
                    dirvec.Normalize();
                    if (dirvec == trackvec)
                    {
                        if (foundslope)
                        {
                            return(false);
                        }
                        else
                        {
                            nextY++;
                        }
                    }
                    else if (dirvec == -trackvec)
                    {
                        ;
                    }
                }
                if (type == TRACKTYPE && lValue1 == TRACKBUFFER)
                {
                    dirvec = new Vector3(-dirvec.x, 0f, -dirvec.z);
                }
            }
            //Begin checking special types
            else if (type == CONTROLTYPE)
            {
                if (lValue1 == CONTROLLOAD || lValue1 == CONTROLUNLOAD || lValue1 == CONTROLTURBO)
                {
                    if ((trackvec == dirvec) || (trackvec == -dirvec))
                    {
                        //Do nothing... direction doesn't change
                        //Except turbo... reduce the penalty for this path!
                        if (lValue1 == CONTROLTURBO)
                        {
                            PathfindPenalty--;
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            //Check for freight stations
            else if (type == FREIGHTSTATIONTYPE)
            {
                if ((trackvec == dirvec) || (trackvec == -dirvec))
                {
                    Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                        if (segment == null)
                        {
                            Debug.Log((object)"Track junction track follower did not find segment");
                            return(false);
                        }
                    }
                    FreightCartStation fcs = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightCartStation;
                    if (fcs == null)
                    {
                        Debug.LogWarning("Track Junction Track Follower tried to get a freight cart station but got other mod machine instead?");
                        return(false);
                    }
                    if (!SegmentStations.Contains(fcs))
                    {
                        SegmentStations.Add(fcs);
                    }
                    fcs.ClosestJunction   = this;
                    fcs.JunctionDirection = initialdirection;
                    // Penalize this route for multidirection pathfinding due to the station
                    PathfindPenalty += 5;
                }
                else
                {
                    return(false);
                }
            }
            //Is it a junction?
            else if (type == JUNCTIONTYPE)
            {
                //Debug.LogWarning("Track follower success!  Found another junction!");
                Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                if (segment == null)
                {
                    segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        Debug.Log((object)"Track junction track follower did not find segment");
                        return(false);
                    }
                }
                FreightTrackJunction junction = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightTrackJunction;
                if (junction == null)
                {
                    Debug.LogWarning("Track Junction Track Follower tried to get a track junction but got other mod machine instead?");
                    return(false);
                }
                this.ConnectedJunctions[initialdirection] = junction;
                //Don't let segment distance be negative just to be safe!  This should rarely happen...
                if (PathfindPenalty < 0 && Math.Abs(PathfindPenalty) > n)
                {
                    PathfindPenalty = -n;
                }
                FreightTrackSegment tracksegment = new FreightTrackSegment(this, junction, n + 1 + PathfindPenalty);
                tracksegment.Stations = SegmentStations;
                //Debug.LogWarning("trackseg station count: " + tracksegment.Stations.Count);
                this.ConnectedSegments[initialdirection] = tracksegment;
                this.SegmentDistances[initialdirection]  = n + 1;
                this.LinkStatusDirty = true;

                //handle the connection for the other junction so we don't need to double the work - only if return path is valid!
                //Mirror the direction to reflect the correct side of the connecting junction
                if (!oneway)
                {
                    int mirroreddir = direction += 2;
                    if (mirroreddir > 3)
                    {
                        mirroreddir -= 4;
                    }
                    junction.ConnectedJunctions[mirroreddir] = this;
                    junction.ConnectedSegments[mirroreddir]  = tracksegment;
                    junction.SegmentDistances[mirroreddir]   = n + 1;
                    junction.LinkStatusDirty = true;
                }
                return(true);
            }
            else if (type == TOURSTATIONTYPE)
            {
                if (trackvec != -dirvec)
                {
                    return(false);
                }
                Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
                if (segment == null)
                {
                    segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
                    if (segment == null)
                    {
                        Debug.Log((object)"Track junction track follower did not find segment");
                        return(false);
                    }
                }
                TourCartStation station = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as TourCartStation;
                station.TrackNetwork      = this.TrackNetwork;
                station.ClosestJunction   = this;
                station.JunctionDirection = initialdirection;
                this.ConnectedJunctions[initialdirection] = this;
                FreightTrackSegment tracksegment = new FreightTrackSegment(this, this, 2 * n + 1);
                this.SegmentDistances[initialdirection]  = 2 * n + 1;
                this.ConnectedSegments[initialdirection] = tracksegment;
                this.LinkStatusDirty = true;
                if (!string.IsNullOrEmpty(station.StationName) && !this.TrackNetwork.TourCartStations.ContainsKey(station.StationName))
                {
                    this.TrackNetwork.TourCartStations.Add(station.StationName, station);
                }
                return(true);
            }
            else
            {
                return(false);   //Not a track type
            }
            //Update the direction int based on the changed direction vector
            if (dirvec == Vector3.right)
            {
                direction = 0;
            }
            else if (dirvec == Vector3.forward)
            {
                direction = 1;
            }
            else if (dirvec == Vector3.left)
            {
                direction = 2;
            }
            else if (dirvec == Vector3.back)
            {
                direction = 3;
            }

            TrackPiece visitedpiece = new TrackPiece(new Vector3(nextX - this.mnX, nextY - this.mnY, nextZ - this.mnZ), direction);
            //Debug.LogWarning("Visited track piece: " + new Vector4(nextX - this.mnX, nextY - mnY, nextZ - mnZ, direction).ToString());
            //Store every track piece and check every 10th for monitoring for closed, endless loops of track
            if (n % 10 == 0)
            {
                int count = VisitedTracks.Count;
                for (int m = 0; m < count; m++)
                {
                    TrackPiece piece = VisitedTracks[m];
                    if (piece.Position == visitedpiece.Position && piece.Direction == visitedpiece.Direction)
                    {
                        //Debug.LogWarning("piece position: " + piece.Position.ToString() + " visited: " + visitedpiece.Position.ToString());
                        Debug.LogWarning("TrackJunction followed track route and found a closed loop.  Ending search.");
                        return(false);
                    }
                }
            }
            VisitedTracks.Add(visitedpiece);
            if (n == 2047)
            {
                Debug.LogWarning("Track Junction Found track length > 2048m -> ending search.");
            }
        }
        return(false);
    }
Exemplo n.º 30
0
 TrackPiece InstantiateTrackPiece(TrackPiece prefab)
 {
     return(Instantiate(prefab).GetComponent <TrackPiece>());
 }
Exemplo n.º 31
0
 public void createNextTrackPiece(int pieceType, Vector3 location, int direction)
 {
     TrackPiece nextPiece = new TrackPiece((TrackPiece.TrackPieceType)pieceType, location, (TrackPiece.Direction)direction);
     finalTrack[(int)location.x,(int)location.y,(int)location.z] = nextPiece;
     currentPiece = nextPiece;
 }
Exemplo n.º 32
0
 public void createStartingTrackPiece()
 {
     TrackPiece startPiece = new TrackPiece(TrackPiece.TrackPieceType.Straight, trackCenter,TrackPiece.Direction.North);
     finalTrack[trackXLength/2, trackYHeight/2, trackZDepth/2] = startPiece;
     currentPiece = startPiece;
 }
Exemplo n.º 33
0
 // Start is called before the first frame update
 void Start()
 {
     TrackPiece.setObstacleSpawner(this);
 }