Beispiel #1
0
        protected override void Update(GameTime gameTime)
        {
            _frameCount++;
            //GameTime customGameTime = new GameTime(gameTime.TotalRealTime, new TimeSpan(200000), gameTime.TotalGameTime, gameTime.ElapsedGameTime);

            // Allows the game to exit
            KeyboardState keybstate = Keyboard.GetState();

            if (keybstate.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            //stany symulacji
            foreach (Keys k in keybstate.GetPressedKeys())
            {
                switch (k)
                {
                case Keys.D0: gameState = GameState.PauseEdit; break;

                case Keys.D1: gameState = GameState.SimulateLBM; break;

                case Keys.D2: gameState = GameState.SimulateTraffic; break;
                }
            }

            if (gameState == GameState.PauseEdit)
            {
                MouseState mousestate = Mouse.GetState();
                if (prevMouseState == null)
                {
                    prevMouseState = mousestate;
                }

                if ((prevMouseState.LeftButton == ButtonState.Released) && (mousestate.LeftButton == ButtonState.Pressed))
                {
                    posx = mousestate.X;
                    posy = mousestate.Y;
                }

                if ((prevMouseState.LeftButton == ButtonState.Pressed) && (mousestate.LeftButton == ButtonState.Released))
                {
                    if (!((mousestate.X == posx) && (mousestate.Y == posy)))
                    {
                        //zapis do pliku
                        FileStream   fs = File.Open("temp.txt", FileMode.Append);
                        StreamWriter sw = new StreamWriter(fs);
                        String       s  = "<wall x1=\"" + posx.ToString() + "\" y1=\"" + posy.ToString() + "\" x2=\"" + mousestate.X.ToString() + "\" y2=\"" + mousestate.Y.ToString() + "\"/>";
                        sw.WriteLine(s);
                        sw.Close();
                        fs.Close();
                    }
                }
                //na koniec
                prevMouseState = mousestate;
            }

            //LBM
            if ((gameState == GameState.SimulateLBM))
            {
                _lbmController.Update(tabLBM[currentTabLBMIndex]);

                //Automatic LBM maps generation - stops when all vector maps have valid routes
                if (doAutomaticLBM && _lbmController.HasRouteVectorMapGenerated(tabLBM[currentTabLBMIndex], routeConfigList[currentTabLBMIndex]))
                {
                    _tabLBMSerializer.SaveTabLBM(tabLBM[currentTabLBMIndex], currentTabLBMIndex);
                    if (currentTabLBMIndex == routeConfigList.Count - 1)
                    {
                        doAutomaticLBM = false;
                    }
                    else
                    {
                        currentTabLBMIndex++;
                    }
                }
            }

            if (gameState == GameState.SimulateTraffic)
            {
                //Automatic car adding
                if (doAutomaticCars)
                {
                    for (int i = 0; i < routeConfigList.Count; i++)
                    {
                        RouteConfig rc = routeConfigList[i];
                        if (gameTime.TotalGameTime.TotalMilliseconds > (rc.lastCarOutTime.TotalMilliseconds + rc.timeBetweenCarsMs))
                        {
                            rc.lastCarOutTime = gameTime.TotalGameTime;

                            if (Config.EndAfterCarsSpawned > 0)
                            {
                                if (_carIndex > Config.EndAfterCarsSpawned)
                                {
                                    if (cars.Count == 0)
                                    {
                                        this.Exit();
                                    }
                                    break;
                                }
                            }

                            AddNewCar(i, gameTime);
                        }
                    }
                }

                //Lights handling
                if (lightConfigList.Count > 0)
                {
                    if (lightConfigId == -1)
                    {
                        lightConfigId       = 0;
                        lightLastChangeTime = gameTime.TotalGameTime;
                        for (int x = 0; x < lightList.Count; x++)
                        {
                            if (!lightConfigList[lightConfigId].lightId.Contains(x)) //jak zawiera oznacza ze ma byc to swiatlo zielone i nie malowac...
                            {
                                RouteWall rw = lightList[x];
                                LBMController.DrawLineLBM(lightTabLBM, elementSize, rw.x1, rw.y1, rw.x2, rw.y2, LBMNodeType.Wall);
                            }
                        }
                    }

                    //zmiana swiatel
                    if (gameTime.TotalGameTime.TotalMilliseconds > (lightLastChangeTime.TotalMilliseconds + lightConfigList[lightConfigId].timeToWaitMs))
                    {
                        //malowanie powrotne pustych kratek
                        for (int x = 0; x < lightList.Count; x++)
                        {
                            RouteWall rw = lightList[x];
                            LBMController.DrawLineLBM(lightTabLBM, elementSize, rw.x1, rw.y1, rw.x2, rw.y2, 0);
                        }
                        //malowanie nowych scian
                        lightConfigId++;
                        if (lightConfigId > (lightConfigList.Count - 1))
                        {
                            lightConfigId = 0;
                        }
                        lightLastChangeTime = gameTime.TotalGameTime;
                        for (int x = 0; x < lightList.Count; x++)
                        {
                            if (!lightConfigList[lightConfigId].lightId.Contains(x)) //jak zawiera oznacza ze ma byc to swiatlo zielone i nie malowac...
                            {
                                RouteWall rw = lightList[x];
                                LBMController.DrawLineLBM(lightTabLBM, elementSize, rw.x1, rw.y1, rw.x2, rw.y2, LBMNodeType.Wall);
                            }
                        }
                    }
                }

                List <Car> carsToRemove = new List <Car>();

                //cars
                Parallel.ForEach(cars, car =>
                {
                    try
                    {
                        //sterowanie ai
                        if (doAi)
                        {
                            car.DoAI(tabLBM[car.tabLBMIndex]);
                        }
                        if (car.Equals(selectedCar))
                        {
                            car.DoManualSteer(keybstate);
                        }

                        car.Update(gameTime);

                        if (_csvRecorder != null)
                        {
                            int currentlyCrossedLightId = GetCarInterectingLightId(car);
                            _csvRecorder.AddData(_frameCount, gameTime.TotalGameTime, lightConfigId, car, currentlyCrossedLightId);
                        }

                        //wywalanie z listy jak dojechal do konca
                        if (
                            ((car.position.X / elementSize) >= countX - 1) || ((car.position.Y / elementSize) >= countY - 1) ||
                            ((car.position.X / elementSize) <= 1) || ((car.position.Y / elementSize) <= 1) ||
                            (tabLBM[car.tabLBMIndex][(int)car.position.X / elementSize, (int)car.position.Y / elementSize].isSink) ||
                            (tabLBM[car.tabLBMIndex][(int)car.framePointF.X / elementSize, (int)car.framePointF.Y / elementSize].isSink)
                            )
                        {
                            carsToRemove.Add(car);
                        }
                    }
                    catch (Exception e)
                    {
#if DEBUG
                        throw;
#endif
                        carsToRemove.Add(car);
                    }
                });

                currentCarCount   = cars.Count;
                finishedCarCount += carsToRemove.Count;

                if (_csvRecorder != null)
                {
                    _csvRecorder.Flush();
                }

                foreach (Car car in carsToRemove)
                {
                    cars.Remove(car);
                }
            }

            base.Update(gameTime);
        }