Beispiel #1
0
        protected override void Initialize()
        {
            mapTexture = LoadMap(Path.Combine("Configs", Config.ConfigDir, "map.tga"));
            SetVariablesBasedOnMap(mapTexture);

            _tabLBMSerializer    = new TabLBMSerializer(Path.Combine("Configs", Config.ConfigDir));
            _lbmController       = new LBMController(countX, countY, elementSize);
            GeneralHelper.parent = this;

            graphics.PreferredBackBufferWidth  = resX;
            graphics.PreferredBackBufferHeight = resY;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();

            if (Config.RecordToCsv)
            {
                var csvFileName = string.Format("TrafficSym_{0}-{1}.csv", Config.ConfigDir, DateTime.Now.ToString("dd_MM_yy_HH_mm"));
                _csvRecorder = new CsvRecorder(Path.Combine("Configs", Config.ConfigDir, csvFileName));
            }

            base.Initialize();
        }
Beispiel #2
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);
        }
Beispiel #3
0
        protected override void LoadContent()
        {
            this.IsMouseVisible = true;
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch  = new SpriteBatch(GraphicsDevice);
            viewportRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);

            //font
            defaultFont = Content.Load <SpriteFont>("arial12");

            //car textures
            texAcc   = Content.Load <Texture2D>("car_gadgets\\acc");
            texBrake = Content.Load <Texture2D>("car_gadgets\\brake");
            texSteer = Content.Load <Texture2D>("car_gadgets\\steer");

            //LBM textures
            texWall = Content.Load <Texture2D>("gas\\wall");

            //config load
            var xmlSettings = XmlSettings.CreateFromFile(Path.Combine("Configs", Config.ConfigDir, "settings.xml"), this);

            this.routeConfigList = xmlSettings.RouteConfigList;
            this.lightList       = xmlSettings.LightList;
            this.lightConfigList = xmlSettings.LightConfigList;

            //inicjacja tabLBM
            lightTabLBM = new LBMElement[countX, countY];
            tabLBM      = new LBMElement[routeConfigList.Count][, ];
            for (int i = 0; i < routeConfigList.Count; i++)
            {
                tabLBM[i] = new LBMElement[countX, countY];
            }

            //getting all color data beforehand
            _colorMap = new Color[resX * resY];
            mapTexture.GetData <Color>(0, new Rectangle(0, 0, resX, resY), _colorMap, 0, resX * resY);

            //okreslanie tabLogicMap + instancjonowanie tabeli elementow
            for (int x = 0; x < countX; x++)
            {
                for (int y = 0; y < countY; y++)
                {
                    //wyciaganie koloru z logic map
                    bool wall = false;

                    if (x == 0 || y == 0 || x == countX - 1 || y == countY - 1)
                    {
                        wall = true;
                    }

                    for (int tx = x * elementSize; ((tx < x * elementSize + elementSize) && (tx < resX)); tx++)
                    {
                        if (wall)
                        {
                            break;
                        }
                        for (int ty = y * elementSize; ((ty < y * elementSize + elementSize) && (ty < resY)); ty++)
                        {
                            if (wall)
                            {
                                break;
                            }
                            var retrievedColor = _colorMap[ty * resX + tx];
                            if ((retrievedColor.A > 254) && (retrievedColor.G > 128))
                            {
                                wall = true;
                            }
                        }
                    }

                    lightTabLBM[x, y] = new LBMElement();
                    for (int i = 0; i < routeConfigList.Count; i++)
                    {
                        tabLBM[i][x, y] = new LBMElement();

                        if (wall)
                        {
                            tabLBM[i][x, y].isWall = true;
                        }
                        else
                        {
                            tabLBM[i][x, y].isNormal = true;
                        }
                    }
                }
            }

            //wkladanie w odpowiednie tabLBM pkt startowe, koncowe i sciany
            for (int i = 0; i < routeConfigList.Count; i++)
            {
                RouteConfig rc = routeConfigList[i];
                foreach (RouteStart rs in rc.routeStart)
                {
                    LBMController.DrawLineLBM(tabLBM[i], elementSize, rs.x1, rs.y1, rs.x2, rs.y2, LBMNodeType.Source);
                }
                foreach (RouteEnd re in rc.routeEnd)
                {
                    LBMController.DrawLineLBM(tabLBM[i], elementSize, re.x1, re.y1, re.x2, re.y2, LBMNodeType.Sink);
                }
                foreach (RouteWall rw in rc.routeWall)
                {
                    LBMController.DrawLineLBM(tabLBM[i], elementSize, rw.x1, rw.y1, rw.x2, rw.y2, LBMNodeType.Wall);
                }
            }

            base.LoadContent();
        }