示例#1
0
        public Car(CarPos carPos, CarDirection carDirection, String carGraphic, Program program1, Road carRoad, SemaphoreSlim godOfRoad)
        {
            program = program1;
            road    = carRoad;
            roadGod = godOfRoad;
            speed   = randSpeed.Next(100, 500);
            carDir  = carDirection;
            switch (carDirection)
            {
            case CarDirection.FORWARD:
                switch (carPos)
                {
                case CarPos.TOP:
                    carDestination = CarPos.BOTTOM;
                    break;

                case CarPos.RIGHT:
                    carDestination = CarPos.LEFT;
                    break;

                case CarPos.BOTTOM:
                    carDestination = CarPos.TOP;
                    break;

                case CarPos.LEFT:
                    carDestination = CarPos.RIGHT;
                    break;
                }
                break;

            case CarDirection.TURN_RIGHT:
                switch (carPos)
                {
                case CarPos.TOP:
                    carDestination = CarPos.LEFT;
                    break;

                case CarPos.RIGHT:
                    carDestination = CarPos.TOP;
                    break;

                case CarPos.BOTTOM:
                    carDestination = CarPos.RIGHT;
                    break;

                case CarPos.LEFT:
                    carDestination = CarPos.BOTTOM;
                    break;
                }
                break;

            case CarDirection.TURN_LEFT:
                switch (carPos)
                {
                case CarPos.TOP:
                    carDestination = CarPos.RIGHT;
                    break;

                case CarPos.RIGHT:
                    carDestination = CarPos.BOTTOM;
                    break;

                case CarPos.BOTTOM:
                    carDestination = CarPos.LEFT;
                    break;

                case CarPos.LEFT:
                    carDestination = CarPos.TOP;
                    break;
                }
                break;
            }

            switch (carDestination)
            {
            case CarPos.TOP:
                carColor = new Terminal.Gui.Attribute(Color.BrightGreen, Color.Black);
                break;

            case CarPos.RIGHT:
                carColor = new Terminal.Gui.Attribute(Color.BrightYellow, Color.Black);
                break;

            case CarPos.BOTTOM:
                carColor = new Terminal.Gui.Attribute(Color.BrightRed, Color.Black);
                break;

            case CarPos.LEFT:
                carColor = new Terminal.Gui.Attribute(Color.BrightBlue, Color.Black);
                break;
            }
            //pozniej zalezne od kierunku
            graphic       = carGraphic;
            positionMutex = new Mutex();
            lockPosition();
            pos_X       = road.getStartPointX(carPos);
            pos_Y       = road.getStartPointY(carPos);
            posOrigin_X = pos_X;
            posOrigin_Y = pos_Y;
            unlockPosition();
            switch (carPos)
            {
            case CarPos.TOP:
                carRot = CarRotation.DOWN;
                break;

            case CarPos.RIGHT:
                carRot = CarRotation.LEFT;
                break;

            case CarPos.BOTTOM:
                carRot = CarRotation.UP;
                break;

            case CarPos.LEFT:
                carRot = CarRotation.RIGHT;
                break;
            }
        }
示例#2
0
        private void GenerateCarsProc()
        {
            int MAX_CAR_NUMBER = simRoad.getMAX_CAR_NUMBER();
            //int CURRENT_CAR_NUMBER = 0;
            int          frequency = 200;
            int          genX      = 0;
            int          genY      = 0;
            CarPos       newCarPos = CarPos.TOP;
            CarDirection newCarDir = CarDirection.FORWARD;

            while (getPrun())
            {
                //zmienic na dopoki kolejka ma symbole
                if (simRoad.getQueueLength() > 0)//CURRENT_CAR_NUMBER < MAX_CAR_NUMBER)
                {
                    //losuj carPos
                    int temp = 0;
                    lock (randLock)
                    {
                        temp = rand.Next() % 4;
                    }
                    switch (temp)
                    {
                    case 0:
                        newCarPos = CarPos.TOP;
                        break;

                    case 1:
                        newCarPos = CarPos.RIGHT;
                        break;

                    case 2:
                        newCarPos = CarPos.BOTTOM;
                        break;

                    case 3:
                        newCarPos = CarPos.LEFT;
                        break;
                    }
                    genX = simRoad.getStartPointX(newCarPos);
                    genY = simRoad.getStartPointY(newCarPos);
                    simRoad.GetRoadMutex(genX, genY).WaitOne();
                    // :::::::::::
                    if (simRoad.checkSpace(genX, genY))
                    {
                        lock (randLock)
                        {
                            temp = rand.Next() % 3;
                        }
                        switch (temp)
                        {
                        case 0:
                            newCarDir = CarDirection.FORWARD;
                            break;

                        case 1:
                            newCarDir = CarDirection.TURN_LEFT;
                            break;

                        case 2:
                            newCarDir = CarDirection.TURN_RIGHT;
                            break;
                        }

                        Car    newCar       = new Car(newCarPos, newCarDir, simRoad.popCharacter(), this, simRoad, godOfRoad);
                        Thread newCarThread = new Thread(new ThreadStart(newCar.ThreadProc));
                        //newCarThread.Name= String.Format("{0}", i);
                        newCarThread.Start();
                        newCar.setThread(newCarThread);
                        cars.Add(newCar);
                        //CURRENT_CAR_NUMBER++;
                    }
                    simRoad.GetRoadMutex(genX, genY).ReleaseMutex();
                }
                Thread.Sleep(frequency);
            }
        }