Beispiel #1
0
    // Call to remove any full planes on the block area
    public static void RemovePlanes()
    {
        List <int> removeQueue = new List <int> ();

        for (int y = gs.fullAreaHeight - 1; y > -1; y--)
        {
            bool full = true;

            for (int x = 0; x < gs.areaSize; x++)
            {
                for (int z = 0; z < gs.areaSize; z++)
                {
                    full &= occupied [x, y, z].OccupiedByPlantedBlock();
                }
            }

            if (full)
            {
                Spawner.SpeedUp();
                removeQueue.Add(y);

                for (int x = 0; x < gs.areaSize; x++)
                {
                    for (int z = 0; z < gs.areaSize; z++)
                    {
                        occupied [x, y, z] = new OccupationState(OccupationState.Type.LayerClear);
                    }
                }
            }
        }
        Game.AddScore(removeQueue.Count);

        BlockArea.main.StartCoroutine("RemoveLayers", removeQueue);
    }
        //================================================================================================//
        /// <summary>
        /// Restore
        /// IMPORTANT : trains are restored to dummy value, will be restored to full contents later
        /// </summary>
        public void Restore(BinaryReader inf)
        {
            if (null == inf)
            {
                throw new ArgumentNullException(nameof(inf));
            }

            int occupied = inf.ReadInt32();

            for (int train = 0; train < occupied; train++)
            {
                int               trainNumber     = inf.ReadInt32();
                int               trainRouteIndex = inf.ReadInt32();
                int               trainDirection  = inf.ReadInt32();
                Train             thisTrain       = new Train(trainNumber);
                Train.TrainRouted thisRouted      = new Train.TrainRouted(thisTrain, trainRouteIndex);
                OccupationState.Add(thisRouted, trainDirection);
            }

            int trainReserved = inf.ReadInt32();

            if (trainReserved >= 0)
            {
                int               trainRouteIndexR = inf.ReadInt32();
                Train             thisTrain        = new Train(trainReserved);
                Train.TrainRouted trainRoute       = new Train.TrainRouted(thisTrain, trainRouteIndexR);
                TrainReserved = trainRoute;
            }

            SignalReserved = inf.ReadInt32();

            int noPreReserve = inf.ReadInt32();

            for (int trainNo = 0; trainNo < noPreReserve; trainNo++)
            {
                int               trainNumber     = inf.ReadInt32();
                int               trainRouteIndex = inf.ReadInt32();
                Train             thisTrain       = new Train(trainNumber);
                Train.TrainRouted thisRouted      = new Train.TrainRouted(thisTrain, trainRouteIndex);
                TrainPreReserved.Enqueue(thisRouted);
            }

            int noClaimed = inf.ReadInt32();

            for (int trainNo = 0; trainNo < noClaimed; trainNo++)
            {
                int               trainNumber     = inf.ReadInt32();
                int               trainRouteIndex = inf.ReadInt32();
                Train             thisTrain       = new Train(trainNumber);
                Train.TrainRouted thisRouted      = new Train.TrainRouted(thisTrain, trainRouteIndex);
                TrainClaimed.Enqueue(thisRouted);
            }
            Forced = inf.ReadBoolean();
        }
Beispiel #3
0
    // TODO: rewrite this method
    public void RemoveLayers(List <int> layers)
    {
        foreach (int y in layers)
        {
            for (int x = 0; x < gs.areaSize; x++)
            {
                for (int z = 0; z < gs.areaSize; z++)
                {
                    blocks [x, y, z].GetComponent <MeshRenderer> ().material.color = Utility.Array.RandElement(LayerClearEffect.main.colors);
                }
            }
        }

        //yield return new WaitForSeconds(0.25f);

        foreach (int layer in layers)
        {
            int y = layer;

            for (int x = 0; x < gs.areaSize; x++)
            {
                for (int z = 0; z < gs.areaSize; z++)
                {
                    occupied [x, y, z] = new OccupationState(OccupationState.Type.Unoccupied);
                    GameObject.Destroy(blocks [x, y, z]);
                    blocks [x, y, z] = null;

                    LayerClearEffect.main.Emit(IndexToWorldSpace(new IntegerVector3(x, y, z)));
                }
            }
            y++;
            while (y < gs.fullAreaHeight)
            {
                for (int x = 0; x < gs.areaSize; x++)
                {
                    for (int z = 0; z < gs.areaSize; z++)
                    {
                        occupied[x, y - 1, z] = occupied[x, y, z];
                        occupied[x, y, z]     = new OccupationState(OccupationState.Type.Unoccupied);

                        blocks [x, y - 1, z] = blocks [x, y, z];
                        blocks [x, y, z]     = null;
                        if (blocks [x, y - 1, z] != null)
                        {
                            LayerClearEffect.main.Drop(blocks [x, y - 1, z]);
                        }
                    }
                }
                y++;
            }
        }
    }
Beispiel #4
0
    public static void Reset()
    {
        // Remove all blocks from the block area
        for (int x = 0; x < gs.areaSize; x++)
        {
            for (int y = 0; y < gs.fullAreaHeight; y++)
            {
                for (int z = 0; z < gs.areaSize; z++)
                {
                    if (occupied [x, y, z].Occupied())
                    {
                        Object.Destroy(BlockArea.blocks [x, y, z]);

                        blocks [x, y, z]   = null;
                        occupied [x, y, z] = new OccupationState(OccupationState.Type.Unoccupied);
                    }
                }
            }
        }
    }
Beispiel #5
0
    void Start()
    {
        main = this;

        blocks   = new GameObject[gs.areaSize, gs.fullAreaHeight, gs.areaSize];
        occupied = new OccupationState[gs.areaSize, gs.fullAreaHeight, gs.areaSize];

        for (int x = 0; x < gs.areaSize; x++)
        {
            for (int y = 0; y < gs.fullAreaHeight; y++)
            {
                for (int z = 0; z < gs.areaSize; z++)
                {
                    occupied [x, y, z] = new OccupationState();
                }
            }
        }

        heightOffset = gs.floorHeight + gs.blockSize;
        corner       = -(gs.areaSize - 1) * gs.blockSize / 2;
    }
 //================================================================================================//
 /// <summary>
 /// check if this train occupies track
 /// unrouted train
 /// </summary>
 public bool OccupiedByThisTrain(Train train)
 {
     return(OccupationState.ContainsTrain(train));
 }
 //================================================================================================//
 /// <summary>
 /// check if any trains occupy track
 /// Check for other train based on direction
 /// </summary>
 public bool OccupiedByOtherTrains(int direction, bool stationary, Train.TrainRouted train)
 {
     return(OccupationState.Where(state => (state.Key != train) && ((state.Value == direction && state.Key.Train.SpeedMpS > 0.5f) || (stationary && state.Key.Train.SpeedMpS <= 0.5))).Any());
 }
 //================================================================================================//
 /// <summary>
 /// check if any trains occupy track
 /// Check for other train without direction
 /// </summary>
 public bool OccupiedByOtherTrains(Train.TrainRouted train)
 {
     return(OccupationState.Count > 1 || (OccupationState.Count == 1 && !OccupationState.ContainsTrain(train)));
 }
 //================================================================================================//
 /// <summary>
 /// check if any trains occupy track
 /// Check based on direction
 /// </summary>
 public bool Occupied(int direction, bool stationary)
 {
     return(OccupationState.Where(state => (state.Value == direction && state.Key.Train.SpeedMpS > 0.5f) || (stationary && state.Key.Train.SpeedMpS <= 0.5)).Any());
 }
 //================================================================================================//
 /// <summary>
 /// Get list of trains occupying track
 /// Check based on direction
 /// </summary>
 public List <Train.TrainRouted> TrainsOccupying(int direction)
 {
     return(OccupationState.Where(state => state.Value == direction).Select(state => state.Key).ToList());
 }
        //================================================================================================//
        /// <summary>
        /// Reset train references after restore
        /// </summary>
        public void RestoreTrains(List <Train> trains, int sectionIndex)
        {
            // Occupy
            Dictionary <int[], int> tempTrains = new Dictionary <int[], int>();

            foreach (KeyValuePair <Train.TrainRouted, int> thisOccupy in OccupationState)
            {
                int[] trainKey = new int[2];
                trainKey[0] = thisOccupy.Key.Train.Number;
                trainKey[1] = thisOccupy.Key.TrainRouteDirectionIndex;
                int direction = thisOccupy.Value;
                tempTrains.Add(trainKey, direction);
            }

            OccupationState.Clear();

            foreach (KeyValuePair <int[], int> thisTemp in tempTrains)
            {
                int[] trainKey   = thisTemp.Key;
                int   number     = trainKey[0];
                int   routeIndex = trainKey[1];
                int   direction  = thisTemp.Value;
                Train thisTrain  = SignalEnvironment.FindTrain(number, trains);
                if (thisTrain != null)
                {
                    Train.TrainRouted thisTrainRouted = routeIndex == 0 ? thisTrain.RoutedForward : thisTrain.RoutedBackward;
                    OccupationState.Add(thisTrainRouted, direction);
                }
            }

            // Reserved

            if (TrainReserved != null)
            {
                int   number        = TrainReserved.Train.Number;
                Train reservedTrain = SignalEnvironment.FindTrain(number, trains);
                if (reservedTrain != null)
                {
                    int  reservedDirection = TrainReserved.TrainRouteDirectionIndex;
                    bool validreserve      = true;

                    // check if reserved section is on train's route except when train is in explorer or manual mode
                    if (reservedTrain.ValidRoute[reservedDirection].Count > 0 && reservedTrain.ControlMode != TrainControlMode.Explorer && reservedTrain.ControlMode != TrainControlMode.Manual)
                    {
                        _            = reservedTrain.ValidRoute[reservedDirection].GetRouteIndex(sectionIndex, reservedTrain.PresentPosition[Direction.Forward].RouteListIndex);
                        validreserve = reservedTrain.ValidRoute[reservedDirection].GetRouteIndex(sectionIndex, reservedTrain.PresentPosition[Direction.Forward].RouteListIndex) >= 0;
                    }

                    if (validreserve || reservedTrain.ControlMode == TrainControlMode.Explorer)
                    {
                        TrainReserved = reservedDirection == 0 ? reservedTrain.RoutedForward : reservedTrain.RoutedBackward;
                    }
                    else
                    {
                        Trace.TraceInformation("Invalid reservation for train : {0} [{1}], section : {2} not restored", reservedTrain.Name, reservedDirection, sectionIndex);
                    }
                }
                else
                {
                    TrainReserved = null;
                }
            }

            // PreReserved
            Queue <Train.TrainRouted> queue = new Queue <Train.TrainRouted>(TrainPreReserved);

            TrainPreReserved.Clear();

            foreach (Train.TrainRouted trainRouted in queue)
            {
                Train train      = SignalEnvironment.FindTrain(trainRouted.Train.Number, trains);
                int   routeIndex = trainRouted.TrainRouteDirectionIndex;
                if (train != null)
                {
                    TrainPreReserved.Enqueue(routeIndex == 0 ? train.RoutedForward : train.RoutedBackward);
                }
            }

            // Claimed
            queue = new Queue <Train.TrainRouted>(TrainClaimed);
            TrainClaimed.Clear();

            foreach (Train.TrainRouted trainRouted in queue)
            {
                Train train      = SignalEnvironment.FindTrain(trainRouted.Train.Number, trains);
                int   routeIndex = trainRouted.TrainRouteDirectionIndex;
                if (train != null)
                {
                    TrainClaimed.Enqueue(routeIndex == 0 ? train.RoutedForward : train.RoutedBackward);
                }
            }
        }