/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState( ).IsKeyDown(Keys.Escape)) { Exit( ); } MouseState MouseState = Mouse.GetState( ); KeyboardState KeyboardState = Keyboard.GetState( ); MousePreviousPosition = MousePosition; MousePosition = MouseState.Position; if (MouseState.RightButton == ButtonState.Pressed) { Cam.Position = Cam.Position + (MousePreviousPosition - MousePosition).Vector2( ) / Cam.Scale; } Vector2 MouseWorldPosition = MousePosition.Vector2( ) / Cam.Scale + Cam.Position; SelectedMap = MouseWorldPosition.FloorToPoint( ) / MAP_SIZE; SelectedPoint = MouseWorldPosition.FloorToPoint( ).Mod(MAP_SIZE); if (MouseWorldPosition.X < 0) { SelectedMap.X -= 1; SelectedPoint.X = MAP_SIZE.X + SelectedPoint.X; } if (MouseWorldPosition.Y < 0) { SelectedMap.Y -= 1; SelectedPoint.Y = MAP_SIZE.Y + SelectedPoint.Y; } if (KeyboardState.IsKeyDown(Keys.D1)) { CurrentValue = Map.WALL; } else if (KeyboardState.IsKeyDown(Keys.D2)) { CurrentValue = Map.LEFT_SHELF; } else if (KeyboardState.IsKeyDown(Keys.D3)) { CurrentValue = Map.RIGHT_SHELF; } else if (KeyboardState.IsKeyDown(Keys.Tab)) { CurrentValue = Map.EMPTY; } if (MouseState.LeftButton == ButtonState.Pressed) { { // TODO: Refactor and optimize this code-block later while (SelectedMap.X < 0) { Map[][] newMaps = SimpleUtils.Create2DArray <Map>((uint)Maps.Length + 1, (uint)Maps[0].Length, null); for (uint i = 0; i < Maps.Length; i++) { for (uint j = 0; j < Maps[0].Length; j++) { newMaps[i + 1][j] = Maps[i][j]; } } for (uint y = 0; y < newMaps[0].Length; y++) { newMaps[0][y] = new Map(MAP_SIZE); } Maps = newMaps; Cam.Position += new Vector2(MAP_SIZE.X, 0f); SelectedMap.X += 1; PlayerPosition.X += MAP_SIZE.X; } while (SelectedMap.Y < 0) { Map[][] newMaps = SimpleUtils.Create2DArray <Map>((uint)Maps.Length, (uint)Maps[0].Length + 1, null); for (uint i = 0; i < Maps.Length; i++) { for (uint j = 0; j < Maps[0].Length; j++) { newMaps[i][j + 1] = Maps[i][j]; } } for (uint x = 0; x < newMaps.Length; x++) { newMaps[x][0] = new Map(MAP_SIZE); } Maps = newMaps; Cam.Position += new Vector2(0f, MAP_SIZE.Y); SelectedMap.Y += 1; PlayerPosition.Y += MAP_SIZE.X; } while (SelectedMap.X >= Maps.Length) { Map[][] newMaps = SimpleUtils.Create2DArray <Map>((uint)Maps.Length + 1, (uint)Maps[0].Length, null); for (uint i = 0; i < Maps.Length; i++) { for (uint j = 0; j < Maps[0].Length; j++) { newMaps[i][j] = Maps[i][j]; } } for (uint y = 0; y < newMaps[0].Length; y++) { newMaps[Maps.Length][y] = new Map(MAP_SIZE); } Maps = newMaps; } while (SelectedMap.Y >= Maps[0].Length) { Map[][] newMaps = SimpleUtils.Create2DArray <Map>((uint)Maps.Length, (uint)Maps[0].Length + 1, null); for (uint i = 0; i < Maps.Length; i++) { for (uint j = 0; j < Maps[0].Length; j++) { newMaps[i][j] = Maps[i][j]; } } for (uint x = 0; x < newMaps.Length; x++) { newMaps[x][Maps[0].Length] = new Map(MAP_SIZE); } Maps = newMaps; } } Point pos = SelectedMap * MAP_SIZE + SelectedPoint; if (!pos.Equals(PlayerPosition) && !pos.Equals(PlayerPosition + new Point(0, 1))) { Maps[SelectedMap.X][SelectedMap.Y][(uint)SelectedPoint.X, (uint)SelectedPoint.Y] = CurrentValue; } } if (KeyboardState.IsKeyDown(Keys.S) && !PreviousKeyboardState.IsKeyDown(Keys.S)) { Map mapToSave = Map.ConvertToBig(Maps); WinUtils.Save(mapToSave, PlayerPosition); } if (KeyboardState.IsKeyDown(Keys.O) && !PreviousKeyboardState.IsKeyDown(Keys.O)) { Tuple <Map, Point> mapToLoad = WinUtils.LoadMap( ); Maps = Map.ConvertFromBig(mapToLoad.Item1, MAP_SIZE); PlayerPosition = mapToLoad.Item2; Cam.Position = PlayerPosition.ToVector2( ) - new Vector2( Graphics.PreferredBackBufferWidth / Cam.Scale.X / 2, Graphics.PreferredBackBufferHeight / Cam.Scale.Y / 2); } PreviousKeyboardState = KeyboardState; base.Update(gameTime); }