From f31e1c3da34db8e4c70817d6080ad362bb28b550 Mon Sep 17 00:00:00 2001 From: Solargale Date: Tue, 30 Jun 2020 13:59:50 -0600 Subject: [PATCH] Got rid of a large amount of static variables --- Collector/Main.cs | 14 +-- Collector/src/Character/InputController.cs | 31 +++--- Collector/src/Character/Player.cs | 21 ++-- Collector/src/Dimension/Autotiling.cs | 2 +- Collector/src/Dimension/Chunks.cs | 119 ++++++++++++++------- Collector/src/Dimension/World.cs | 57 ---------- Collector/src/Dimension/WorldRenderer.cs | 18 ++-- Collector/src/UI/GUI.cs | 2 +- 8 files changed, 131 insertions(+), 133 deletions(-) delete mode 100644 Collector/src/Dimension/World.cs diff --git a/Collector/Main.cs b/Collector/Main.cs index a242f87..409dac5 100644 --- a/Collector/Main.cs +++ b/Collector/Main.cs @@ -25,7 +25,7 @@ namespace Collector private int _virtualHeight; private readonly Desktop _desktop; private Gui _gui; - private World _world; + private Chunks _chunks; private WorldRenderer WorldRenderer { get; set; } public static Dictionary Materials { get; } = new Dictionary(); @@ -57,16 +57,16 @@ namespace Collector _virtualHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, _virtualWidth, _virtualHeight); - _world = new World(); - Player1 = new Player(0, 0); + _chunks = new Chunks(); + Player1 = new Player(0, 0, _chunks); _cam = new OrthographicCamera(viewportAdapter); _cam.LookAt(new Vector2(Player.X, Player.Y)); _cam.Zoom = IRestrictions.Zoom; _spriteBatch = new SpriteBatch(GraphicsDevice); _playerMouse = new PlayerMouse(Content, _spriteBatch, _cam); - _inputController = new InputController(_cam, _spriteBatch, Content, this); - WorldRenderer = new WorldRenderer(_playerMouse, _inputController, _spriteBatch, this); + _inputController = new InputController(_cam, _spriteBatch, Content, this,_chunks,Player1); + WorldRenderer = new WorldRenderer(_playerMouse, _inputController, _spriteBatch, this,_chunks); } protected override void LoadContent() @@ -82,8 +82,8 @@ namespace Collector { // TODO: Add your update logic here/* base.Update(gameTime); - _world.LoadChunks(); - _world.UnloadChunks(); + _chunks.LoadChunks(); + _chunks.UnloadChunks(); _gui.Update(); } diff --git a/Collector/src/Character/InputController.cs b/Collector/src/Character/InputController.cs index 0bac3d3..d13d49b 100644 --- a/Collector/src/Character/InputController.cs +++ b/Collector/src/Character/InputController.cs @@ -17,15 +17,20 @@ namespace Collector.Character private string Input { get; set; } private int _frameNumber; private float _timeSinceLastFrame; - private Main _main; + private readonly Main _main; + private readonly Chunks _chunks; + private readonly Player _player; - public InputController(OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager, Main main) + + public InputController(OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager, Main main, Chunks chunks, Player player) { Input = "Down"; _cam = cam; _spriteBatch = spriteBatch; _main = main; + _chunks = chunks; + _player = player; _frameNumber = 0; _texture = contentManager.Load("man"); const int tileSize = 32; @@ -97,7 +102,7 @@ namespace Collector.Character }; } - public void PlayerInput(Main main, GameTime gameTime) + public void PlayerInput(GameTime gameTime) { const float movementSpeed = IRestrictions.MovementSpeed; var keyboardState = Keyboard.GetState(); @@ -117,42 +122,42 @@ namespace Collector.Character if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D)) { Input = "UpRight"; - Player.Move(_cam,movementSpeed*diagonalMovement,-movementSpeed*diagonalMovement); + _player.Move(_cam,movementSpeed*diagonalMovement,-movementSpeed*diagonalMovement); } else if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.A)) { - Player.Move(_cam,-movementSpeed*diagonalMovement,-movementSpeed*diagonalMovement); + _player.Move(_cam,-movementSpeed*diagonalMovement,-movementSpeed*diagonalMovement); Input = "UpLeft"; } else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.D)) { Input = "DownRight"; - Player.Move(_cam,movementSpeed*diagonalMovement,movementSpeed*diagonalMovement); + _player.Move(_cam,movementSpeed*diagonalMovement,movementSpeed*diagonalMovement); } else if (keyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyDown(Keys.A)) { Input = "DownLeft"; - Player.Move(_cam,-movementSpeed*diagonalMovement,movementSpeed*diagonalMovement); + _player.Move(_cam,-movementSpeed*diagonalMovement,movementSpeed*diagonalMovement); } else if (keyboardState.IsKeyDown(Keys.W)) { Input = "Up"; - Player.Move(_cam,0,-movementSpeed); + _player.Move(_cam,0,-movementSpeed); } else if (keyboardState.IsKeyDown(Keys.A)) { Input = "Left"; - Player.Move(_cam,-movementSpeed,0); + _player.Move(_cam,-movementSpeed,0); } else if (keyboardState.IsKeyDown(Keys.S)) { Input = "Down"; - Player.Move(_cam,0,movementSpeed); + _player.Move(_cam,0,movementSpeed); } else if (keyboardState.IsKeyDown(Keys.D)) { Input = "Right"; - Player.Move(_cam,movementSpeed,0); + _player.Move(_cam,movementSpeed,0); } if (keyboardState.IsKeyDown(Keys.Q)) { @@ -164,7 +169,7 @@ namespace Collector.Character } if (mouseState.LeftButton == ButtonState.Pressed) { - Chunks.RemoveBlock(PlayerMouse.GetSelectedX(),PlayerMouse.GetSelectedY()); + _chunks.RemoveBlock(PlayerMouse.GetSelectedX(),PlayerMouse.GetSelectedY()); } if (mouseState.RightButton == ButtonState.Pressed) { @@ -174,7 +179,7 @@ namespace Collector.Character private void PlaceSelectedBlock(Blocks selectedItem) { - Chunks.PlaceBlock(PlayerMouse.GetSelectedX(), PlayerMouse.GetSelectedY(), selectedItem); + _chunks.PlaceBlock(PlayerMouse.GetSelectedX(), PlayerMouse.GetSelectedY(), selectedItem); } private void UpdateAnimationFrame(GameTime gameTime) diff --git a/Collector/src/Character/Player.cs b/Collector/src/Character/Player.cs index 1467565..dcd7f5d 100644 --- a/Collector/src/Character/Player.cs +++ b/Collector/src/Character/Player.cs @@ -10,37 +10,38 @@ namespace Collector.Character { public static float X { get; private set; } public static float Y { get; private set; } + private RectangleF _playerBounds; + private Chunks _chunks { get; } - public static RectangleF PlayerBounds; - - public Player(int x, int y) + public Player(int x, int y, Chunks chunks) { X = x; Y = y; - PlayerBounds = new RectangleF(x, y, IRestrictions.TileSize * 0.5f, IRestrictions.TileSize * 0.35f); + _chunks = chunks; + _playerBounds = new RectangleF(x, y, IRestrictions.TileSize * 0.5f, IRestrictions.TileSize * 0.35f); } - public static void Move(OrthographicCamera cam, float x, float y) + public void Move(OrthographicCamera cam, float x, float y) { Teleport(cam, x, 0); - if (Chunks.LoadedCollisions.Values.Any(collisionsValue => collisionsValue.Rectangle.Intersects(PlayerBounds))) + if (_chunks.LoadedCollisions.Values.Any(collisionsValue => collisionsValue.Rectangle.Intersects(_playerBounds))) { Teleport(cam, -x, 0); } Teleport(cam, 0, y); - if (Chunks.LoadedCollisions.Values.Any(collisionsValue => collisionsValue.Rectangle.Intersects(PlayerBounds))) + if (_chunks.LoadedCollisions.Values.Any(collisionsValue => collisionsValue.Rectangle.Intersects(_playerBounds))) { Teleport(cam, 0, -y); } } - private static void Teleport(OrthographicCamera cam, float x, float y) + private void Teleport(OrthographicCamera cam, float x, float y) { X += x; Y += y; - PlayerBounds.X = X + 0.2f; - PlayerBounds.Y = Y + 1.5f; + _playerBounds.X = X + 0.2f; + _playerBounds.Y = Y + 1.5f; cam.Move(new Vector2(x, y)); } } diff --git a/Collector/src/Dimension/Autotiling.cs b/Collector/src/Dimension/Autotiling.cs index 36b729a..52f44e7 100644 --- a/Collector/src/Dimension/Autotiling.cs +++ b/Collector/src/Dimension/Autotiling.cs @@ -1,4 +1,4 @@ -namespace Collector.Character +namespace Collector.Dimension { public class Autotiling { diff --git a/Collector/src/Dimension/Chunks.cs b/Collector/src/Dimension/Chunks.cs index c782363..4c70b94 100644 --- a/Collector/src/Dimension/Chunks.cs +++ b/Collector/src/Dimension/Chunks.cs @@ -2,24 +2,71 @@ using System; using System.Collections.Generic; +using Collector.Character; using Collector.ThirdPartyCode; -using Collision = Collector.Character.Collision; namespace Collector.Dimension { - public static class Chunks + public class Chunks { - public static Dictionary, Blocks> LoadedChunks { get; } = new Dictionary, Blocks>(); - private static readonly Dictionary, Blocks> SavedChunks = new Dictionary, Blocks>(); - public static readonly Dictionary, Collision> LoadedCollisions = new Dictionary, Collision>(); - public static readonly Dictionary, Collision> SavedCollisions = new Dictionary, Collision>(); - public static readonly List Impassable = new List(); + public Dictionary, Blocks> LoadedChunks { get; } = new Dictionary, Blocks>(); + private readonly Dictionary, Blocks> _savedChunks = new Dictionary, Blocks>(); + public readonly Dictionary, Collision> LoadedCollisions = new Dictionary, Collision>(); + private readonly Dictionary, Collision> _savedCollisions = new Dictionary, Collision>(); + public readonly List Impassable = new List(); + private readonly OpenSimplexNoise _gen1 = new OpenSimplexNoise(IRestrictions.Seed+ 1); + private readonly OpenSimplexNoise _gen2 = new OpenSimplexNoise(IRestrictions.Seed); + + private void GenerateWorld(float x, float y) + { + if (IsEmpty(x, y)) { + GenerateChunk(x, y); + } + } + private void UngenerateWorld(float x, float y) { + if (!IsEmpty(x, y)) { + UngenerateChunk(x, y); + } + } - private static readonly OpenSimplexNoise Gen1 = new OpenSimplexNoise(IRestrictions.Seed+ 1); - private static readonly OpenSimplexNoise Gen2 = new OpenSimplexNoise(IRestrictions.Seed); + public void LoadChunks() + { + for (var i = -(IRestrictions.RenderDistance); i < IRestrictions.RenderDistance; i++) { + for (var j = -(IRestrictions.RenderDistance); j < IRestrictions.RenderDistance; j++) { + GenerateWorld( + i + Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize), + j + Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize) + ); + } + } + } - public static void CreateStructures() { + public void UnloadChunks() { + for (var i = -IRestrictions.RenderDistance; i < IRestrictions.RenderDistance+1; i++) { + //Down + UngenerateWorld( + Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i, + Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+IRestrictions.RenderDistance + ); + //Up + UngenerateWorld( + Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i, + Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)-IRestrictions.RenderDistance + ); + //Right + UngenerateWorld( + Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)-IRestrictions.RenderDistance, + Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i + ); + //Left + UngenerateWorld( + Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+IRestrictions.RenderDistance, + Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i + ); + } + } + public void CreateStructures() { const int i = 10; SetBlock(3 + i, 2,1,Blocks.BlockWall); SetBlock(4 + i, 2,1,Blocks.BlockWall); @@ -27,18 +74,18 @@ namespace Collector.Dimension SetBlock(4 + i, 3,1,Blocks.BlockRoof); } - private static void SetBlock(int x, int y, int z, Blocks name) + private void SetBlock(int x, int y, int z, Blocks name) { var tuple = new Tuple(x, y, z); var pair = new Tuple(x, y); LoadedCollisions[pair] = new Collision(x,y); - SavedCollisions[pair] = new Collision(x,y); + _savedCollisions[pair] = new Collision(x,y); LoadedChunks[tuple] = name; - SavedChunks[tuple] = name; + _savedChunks[tuple] = name; } - public static void PlaceBlock(int x, int y, Blocks name) + public void PlaceBlock(int x, int y, Blocks name) { var tuple = new Tuple(x, y, 1); if (!LoadedChunks.ContainsKey(tuple)) return; @@ -48,7 +95,7 @@ namespace Collector.Dimension } } - public static void RemoveBlock(int x, int y){ + public void RemoveBlock(int x, int y){ var tuple = new Tuple(x, y, 1); var pair = new Tuple(x,y); @@ -56,13 +103,13 @@ namespace Collector.Dimension if (LoadedChunks[tuple].Equals(Blocks.BlockAir)) return; LoadedCollisions.Remove(pair); - SavedCollisions.Remove(pair); + _savedCollisions.Remove(pair); LoadedChunks[tuple] = Blocks.BlockAir; - SavedChunks[tuple] = Blocks.BlockAir; + _savedChunks[tuple] = Blocks.BlockAir; } - public static void UngenerateChunk(float x, float y) { + public void UngenerateChunk(float x, float y) { var startX = (int)x << IRestrictions.ChunkShift; var startY = (int)y << IRestrictions.ChunkShift; var endX = startX + IRestrictions.ChunkSize; @@ -78,7 +125,7 @@ namespace Collector.Dimension } } - public static void GenerateChunk(float x, float y) { + public void GenerateChunk(float x, float y) { var startX = (int)x << IRestrictions.ChunkShift; var startY = (int)y << IRestrictions.ChunkShift; var endX = startX + IRestrictions.ChunkSize; @@ -89,13 +136,13 @@ namespace Collector.Dimension for (var j = startY; j != endY; j++) { var triple = new Tuple(i, j, 0); var pair = new Tuple(i,j); - if (SavedCollisions.ContainsKey(pair)) + if (_savedCollisions.ContainsKey(pair)) { - LoadedCollisions.Add(pair, SavedCollisions[pair]); + LoadedCollisions.Add(pair, _savedCollisions[pair]); } - if(SavedChunks.ContainsKey(triple)){ - LoadedChunks.Add(triple,SavedChunks[triple]); + if(_savedChunks.ContainsKey(triple)){ + LoadedChunks.Add(triple,_savedChunks[triple]); } else { var terrain = GetTerrain(i, j); @@ -103,11 +150,11 @@ namespace Collector.Dimension { var collision = new Collision(i,j); LoadedCollisions[pair] = collision; - SavedCollisions[pair] = collision; + _savedCollisions[pair] = collision; } LoadedChunks.Add(triple, terrain); - SavedChunks.Add(triple, terrain); + _savedChunks.Add(triple, terrain); } } } @@ -117,24 +164,24 @@ namespace Collector.Dimension for (var j = startY; j != endY; j++) { var triple = new Tuple(i, j,1); LoadedChunks[triple] = GetBlocks(i,j,1); - SavedChunks[triple] = GetBlocks(i,j,1); + _savedChunks[triple] = GetBlocks(i,j,1); } } } - private static Blocks GetBlocks(int x, int y, int z) { - return SavedChunks.GetValueOrDefault(new Tuple(x, y, z), Blocks.BlockAir); + private Blocks GetBlocks(int x, int y, int z) { + return _savedChunks.GetValueOrDefault(new Tuple(x, y, z), Blocks.BlockAir); } - private static double Noise1(double nx, double ny) { - return Gen1.Evaluate(nx, ny) / 2 + 0.5; + private double Noise1(double nx, double ny) { + return _gen1.Evaluate(nx, ny) / 2 + 0.5; } - private static double Noise2(double nx, double ny) { - return Gen2.Evaluate(nx, ny) / 2 + 0.5; + private double Noise2(double nx, double ny) { + return _gen2.Evaluate(nx, ny) / 2 + 0.5; } - private static Blocks GetTerrain(int x, int y) { + private Blocks GetTerrain(int x, int y) { const double scale = 0.01; var nx = x * scale; var ny = y * scale; @@ -157,7 +204,7 @@ namespace Collector.Dimension return GetBiomeBlock(moisture, elevation); } - private static Blocks GetBiomeBlock(double moisture, double elevation) { + private Blocks GetBiomeBlock(double moisture, double elevation) { var biome = GetBiome(moisture, elevation); if (biome.Equals("Tundra")) return Blocks.BlockSnow; if (biome.Equals("Taiga")) return Blocks.BlockSnow; @@ -179,7 +226,7 @@ namespace Collector.Dimension return biome.Equals("SubtropicalDesert") ? Blocks.BlockSand : Blocks.BlockWater; } - private static string GetBiome(double moisture, double elevation) { + private string GetBiome(double moisture, double elevation) { if (elevation < 0.1) return "Ocean"; if (elevation < 0.12) return "Beach"; @@ -205,7 +252,7 @@ namespace Collector.Dimension return moisture < 0.66 ? "TropicalSeasonalForest" : "TropicalRainForest"; } - public static bool IsEmpty(float x, float y){ + public bool IsEmpty(float x, float y){ return !LoadedChunks.ContainsKey(new Tuple((int)x * 8, (int)y * 8,0)); } } diff --git a/Collector/src/Dimension/World.cs b/Collector/src/Dimension/World.cs deleted file mode 100644 index 8d4856a..0000000 --- a/Collector/src/Dimension/World.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using Collector.Character; - -namespace Collector.Dimension -{ - public class World { - private void GenerateWorld(float x, float y) - { - if (Chunks.IsEmpty(x, y)) { - Chunks.GenerateChunk(x, y); - } - } - - private void UngenerateWorld(float x, float y) { - if (!Chunks.IsEmpty(x, y)) { - Chunks.UngenerateChunk(x, y); - } - } - - public void LoadChunks() - { - for (var i = -(IRestrictions.RenderDistance); i < IRestrictions.RenderDistance; i++) { - for (var j = -(IRestrictions.RenderDistance); j < IRestrictions.RenderDistance; j++) { - GenerateWorld( - i + Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize), - j + Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize) - ); - } - } - } - - public void UnloadChunks() { - for (var i = -IRestrictions.RenderDistance; i < IRestrictions.RenderDistance+1; i++) { - //Down - UngenerateWorld( - Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i, - Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+IRestrictions.RenderDistance - ); - //Up - UngenerateWorld( - Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i, - Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)-IRestrictions.RenderDistance - ); - //Right - UngenerateWorld( - Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)-IRestrictions.RenderDistance, - Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i - ); - //Left - UngenerateWorld( - Player.X / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+IRestrictions.RenderDistance, - Player.Y / (IRestrictions.SuperChunkSize * IRestrictions.ChunkSize)+i - ); - } - } - } -} diff --git a/Collector/src/Dimension/WorldRenderer.cs b/Collector/src/Dimension/WorldRenderer.cs index 184d5f4..3ec46c4 100644 --- a/Collector/src/Dimension/WorldRenderer.cs +++ b/Collector/src/Dimension/WorldRenderer.cs @@ -11,24 +11,26 @@ namespace Collector.Dimension private readonly InputController _inputController; private readonly SpriteBatch _spriteBatch; private readonly Main _main; + private readonly Chunks _chunks; - public WorldRenderer(PlayerMouse playerMouse, InputController inputController, SpriteBatch spriteBatch, Main main) + public WorldRenderer(PlayerMouse playerMouse, InputController inputController, SpriteBatch spriteBatch, Main main, Chunks chunks) { _playerMouse = playerMouse; _inputController = inputController; _spriteBatch = spriteBatch; _main = main; - Chunks.Impassable.Add(Blocks.BlockWater); - Chunks.Impassable.Add(Blocks.BlockRoof); - Chunks.Impassable.Add(Blocks.BlockWall); + _chunks = chunks; + _chunks.Impassable.Add(Blocks.BlockWater); + _chunks.Impassable.Add(Blocks.BlockRoof); + _chunks.Impassable.Add(Blocks.BlockWall); } - private static void DrawWorld(SpriteBatch batch, int layer) + private void DrawWorld(SpriteBatch batch, int layer) { - foreach (var chunkpair in Chunks.LoadedChunks.Keys.Where(chunkpair => layer==chunkpair.Item3)) + foreach (var chunkpair in _chunks.LoadedChunks.Keys.Where(chunkpair => layer==chunkpair.Item3)) { batch.Draw( - Main.Materials[Chunks.LoadedChunks[chunkpair]], + Main.Materials[_chunks.LoadedChunks[chunkpair]], new Rectangle( chunkpair.Item1, chunkpair.Item2, @@ -46,7 +48,7 @@ namespace Collector.Dimension _playerMouse.Draw(); DrawWorld(_spriteBatch,1); _inputController.Draw(); - _inputController.PlayerInput(_main,gameTime); + _inputController.PlayerInput(gameTime); //DrawWorld(_spriteBatch,2); } } diff --git a/Collector/src/UI/GUI.cs b/Collector/src/UI/GUI.cs index 362cf14..240c498 100644 --- a/Collector/src/UI/GUI.cs +++ b/Collector/src/UI/GUI.cs @@ -29,13 +29,13 @@ namespace Collector.UI grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); - //Position _combo = new ComboBox { GridColumn = 0, GridRow = 0 }; + _button = new TextButton { GridColumn = 1,