protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(blendState: BlendState.AlphaBlend); KeyboardState keybstate = Keyboard.GetState(); //jak mapy nie za³adowane to za³aduj... if (mapsLoadedState < 2) { mapsLoadedState++; spriteBatch.DrawString(defaultFont, "Loading maps...", new Vector2(10, (float)(resY / 2.0)), Color.White); base.Draw(gameTime); spriteBatch.End(); if (mapsLoadedState > 1) { for (int i = 0; i < routeConfigList.Count; i++) { LBMElement[,] temp = _tabLBMSerializer.LoadTabLBM(i); if (temp != null) { tabLBM[i] = temp; } } } if (tabLBM.Count() > 0) { gameState = GameState.SimulateLBM; } if (AllConfigsGenerated()) { gameState = GameState.SimulateTraffic; } return; } //logic map? spriteBatch.Draw(mapTexture, viewportRect, Color.White); //LGA if (gameState == GameState.SimulateLBM) { if (keybstate.IsKeyDown(Keys.Up)) { currentTabLBMIndex++; } else if (keybstate.IsKeyDown(Keys.Down)) { currentTabLBMIndex--; } if (keybstate.IsKeyDown(Keys.A)) { doAutomaticLBM = !doAutomaticLBM; } //zapis / odczyt if (keybstate.IsKeyDown(Keys.S)) { _tabLBMSerializer.SaveTabLBM(tabLBM[currentTabLBMIndex], currentTabLBMIndex); } else if (keybstate.IsKeyDown(Keys.L)) { tabLBM[currentTabLBMIndex] = _tabLBMSerializer.LoadTabLBM(currentTabLBMIndex); } else if (keybstate.IsKeyDown(Keys.K)) { for (int i = 0; i < routeConfigList.Count; i++) { LBMElement[,] temp = _tabLBMSerializer.LoadTabLBM(i); if (temp != null) { tabLBM[i] = temp; } } } else { for (int x = 0; x < countX; x++) { for (int y = 0; y < countY; y++) { int xelem = x * elementSize; int yelem = y * elementSize; LBMElement l = tabLBM[currentTabLBMIndex][x, y]; if (l.isWall) { spriteBatch.Draw(texWall, new Rectangle(xelem, yelem, elementSize, elementSize), Color.Gray); } else if (l.isSource) { spriteBatch.Draw(texWall, new Rectangle(xelem, yelem, elementSize, elementSize), new Color(0, 255, 0)); spriteBatch.Draw(texWall, new Rectangle(xelem + 1, yelem + 1, elementSize - 2, elementSize - 2), new Color(255, 0, 0)); } else if (l.isSink) { spriteBatch.Draw(texWall, new Rectangle(xelem, yelem, elementSize, elementSize), new Color(0, 255, 0)); spriteBatch.Draw(texWall, new Rectangle(xelem + 1, yelem + 1, elementSize - 2, elementSize - 2), new Color(0, 0, 255)); } else { //tlo spriteBatch.Draw(texWall, new Rectangle(x * elementSize, y * elementSize, elementSize, elementSize), new Color((l.density > 0 ? l.density : 0f), 0, (l.density < 0 ? -l.density : 0f))); //strzalka int xelem2 = xelem + (elementSize / 2); int yelem2 = yelem + (elementSize / 2); DrawLine(texWall, new Vector2(xelem2, yelem2), new Vector2(xelem2 + l.x * vectorLength, yelem2 + l.y * vectorLength), Color.White); spriteBatch.Draw(texWall, new Rectangle(xelem2, yelem2, 1, 1), new Color(0, 255, 0)); } } } } //malowanie stanow symulacji gazu int linePos = 0; spriteBatch.DrawString(defaultFont, "tabLBM index: " + currentTabLBMIndex.ToString(), new Vector2(1, linePos += 20), Color.White); spriteBatch.DrawString(defaultFont, "doAutomaticLBM: " + doAutomaticLBM.ToString(), new Vector2(1, linePos += 20), Color.White); MouseState mousestate = Mouse.GetState(); if ((mousestate.X > 0) && (mousestate.X < resX) && (mousestate.Y > 0) && (mousestate.Y < resY)) { LBMElement l = tabLBM[currentTabLBMIndex][mousestate.X / elementSize, mousestate.Y / elementSize]; spriteBatch.DrawString(defaultFont, "x: " + l.x.ToString() + " y: " + l.y.ToString(), new Vector2(mousestate.X, mousestate.Y + 10), Color.White); spriteBatch.DrawString(defaultFont, "d: " + l.density.ToString(), new Vector2(mousestate.X, mousestate.Y + 30), Color.White); spriteBatch.DrawString(defaultFont, "x: " + mousestate.X.ToString() + " y: " + mousestate.Y.ToString(), new Vector2(mousestate.X, mousestate.Y + 50), Color.White); DrawLine(texWall, new Vector2(mousestate.X, mousestate.Y), new Vector2(mousestate.X + tabLBM[currentTabLBMIndex][mousestate.X / elementSize, mousestate.Y / elementSize].x * 30f, mousestate.Y + tabLBM[currentTabLBMIndex][mousestate.X / elementSize, mousestate.Y / elementSize].y * 30f), new Color(0, 255, 0)); } //Check if all configs have generated if (!doAutomaticLBM) { if (AllConfigsGenerated()) { spriteBatch.DrawString(defaultFont, "Ready to simulate traffic", new Vector2(1, linePos += 20), Color.HotPink); } } } //sim if (gameState == GameState.SimulateTraffic) { if (keybstate.IsKeyDown(Keys.Q)) { AddNewCar(rand.Next(routeConfigList.Count), gameTime); } if (keybstate.IsKeyDown(Keys.A)) { doAutomaticCars = !doAutomaticCars; } //cars foreach (Car car in cars) { car.Draw(spriteBatch); } //malowanie wszystkich swiatel //kolor zalezy czy dane id sciany ktorej malujemy jest w aktualnym konfigu - jak jest to na czerwono if (lightConfigList.Count > 0) { for (int x = 0; x < lightList.Count; x++) { RouteWall rw = lightList[x]; DrawLine(texWall, new Vector2(rw.x1, rw.y1), new Vector2(rw.x2, rw.y2), (lightConfigList[lightConfigId].lightId.Contains(x) ? Color.Green : Color.Red)); } } //wybieranie aut MouseState mousestate = Mouse.GetState(); Vector2 mouseVector = new Vector2((float)mousestate.X, (float)mousestate.Y); if (mousestate.RightButton == ButtonState.Pressed) { selectedCar = null; } else if ((mousestate.X > 0) && (mousestate.X < resX) && (mousestate.Y > 0) && (mousestate.Y < resY) && (mousestate.LeftButton == ButtonState.Pressed) && cars.Count > 0) { selectedCar = cars[0]; float minLength = (float)resX; foreach (Car car in cars) { Vector2 minus = mouseVector - car.position; if (minus.Length() < minLength) { minLength = minus.Length(); selectedCar = car; } } } int lineDraw = 0; //simulation state spriteBatch.DrawString(defaultFont, "Auto: " + doAutomaticCars.ToString(), new Vector2(1, lineDraw += 20), Color.White); //light state if (lightConfigList.Count > 0) { spriteBatch.DrawString(defaultFont, string.Format("Light id:{0} c:{1}", lightConfigId, lightConfigList[lightConfigId].comment), new Vector2(1, lineDraw += 20), Color.White); } //car counter spriteBatch.DrawString(defaultFont, string.Format("cars:{0} finished:{1}", currentCarCount, finishedCarCount), new Vector2(1, lineDraw += 20), Color.White); //selected car info if (selectedCar != null && cars.Contains(selectedCar)) { //car data spriteBatch.DrawString(defaultFont, "aggressiveness: " + selectedCar.aggressiveness.ToString(), new Vector2(1, lineDraw += 20), Color.White); spriteBatch.DrawString(defaultFont, "Pos: x: " + selectedCar.position.X.ToString() + " y: " + selectedCar.position.X.ToString(), new Vector2(1, lineDraw += 20), Color.White); spriteBatch.DrawString(defaultFont, "userSteer: " + selectedCar.userSteer.ToString(), new Vector2(1, lineDraw += 20), Color.White); spriteBatch.DrawString(defaultFont, "userAcc: " + selectedCar.userAcc, new Vector2(1, lineDraw += 20), Color.White); spriteBatch.DrawString(defaultFont, "V: " + selectedCar.velocity.ToString(), new Vector2(1, lineDraw += 20), Color.White); //steering lineDraw += 20; //background spriteBatch.Draw(texWall, new Rectangle(1, lineDraw, 50, 50), Color.Black); //axis spriteBatch.Draw(texWall, new Rectangle(1, lineDraw + 25, 50, 1), Color.White); spriteBatch.Draw(texWall, new Rectangle(25, lineDraw, 1, 50), Color.White); //joystick spriteBatch.Draw(texWall, new Rectangle((int)(20 * selectedCar.userSteer) + 20, (int)(20 * -selectedCar.userAcc) + lineDraw + 20, 10, 10), Color.HotPink); //car selection DrawLine(texWall, selectedCar.framePointFL, selectedCar.framePointFR, Color.HotPink); DrawLine(texWall, selectedCar.framePointFR, selectedCar.framePointRR, Color.HotPink); DrawLine(texWall, selectedCar.framePointRR, selectedCar.framePointRL, Color.HotPink); DrawLine(texWall, selectedCar.framePointRL, selectedCar.framePointFL, Color.HotPink); //line from cursor to car DrawLine(texWall, selectedCar.position, mouseVector, Color.HotPink); } } //gameState var drawVector = new Vector2(1f, 1f); for (int i = 0; i <= 2; i++) { spriteBatch.DrawString(defaultFont, i.ToString(), drawVector, (i == (int)gameState ? Color.White : Color.Black)); drawVector.X += 10; } spriteBatch.DrawString(defaultFont, Enum.GetName(typeof(GameState), gameState), drawVector, Color.White); base.Draw(gameTime); spriteBatch.End(); }
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(); }