From 69c93ea4275fbac9136bcbd092bf51c02c819ad2 Mon Sep 17 00:00:00 2001 From: Solargale Date: Sun, 21 Jun 2020 20:17:59 -0600 Subject: [PATCH] Got rid of previous material system, now uses enumerators and item ids. Also imported QuadTree.cs so we can have entity collision. --- .idea/.idea.Collector/.idea/misc.xml | 3 + Collector.sln.DotSettings.user | 1 + Collector/Collector.csproj | 1 + Collector/Content/{air.png => BlockAir.png} | Bin .../Content/{grass.png => BlockGrass.png} | Bin .../{jungleGrass.png => BlockJungleGrass.png} | Bin Collector/Content/{roof.png => BlockRoof.png} | Bin Collector/Content/{sand.png => BlockSand.png} | Bin Collector/Content/{snow.png => BlockSnow.png} | Bin .../Content/{stone.png => BlockStone.png} | Bin Collector/Content/{wall.png => BlockWall.png} | Bin .../Content/{water.png => BlockWater.png} | Bin Collector/Content/{wood.png => BlockWood.png} | Bin Collector/Content/Content.mgcb | 212 ++++++------ Collector/Main.cs | 12 +- Collector/Restrictions.cs | 2 +- Collector/src/Character/InputController.cs | 2 +- Collector/src/Dimension/Block.cs | 8 - Collector/src/Dimension/BlockMaterials.cs | 32 -- Collector/src/Dimension/Blocks.cs | 15 + Collector/src/Dimension/Chunks.cs | 77 ++--- Collector/src/Dimension/Inventory.cs | 6 +- Collector/src/Dimension/ItemStack.cs | 4 +- Collector/src/Dimension/WorldRenderer.cs | 10 +- Collector/src/ThirdPartyCode/QuadTree.cs | 320 ++++++++++++++++++ 25 files changed, 510 insertions(+), 195 deletions(-) rename Collector/Content/{air.png => BlockAir.png} (100%) rename Collector/Content/{grass.png => BlockGrass.png} (100%) rename Collector/Content/{jungleGrass.png => BlockJungleGrass.png} (100%) rename Collector/Content/{roof.png => BlockRoof.png} (100%) rename Collector/Content/{sand.png => BlockSand.png} (100%) rename Collector/Content/{snow.png => BlockSnow.png} (100%) rename Collector/Content/{stone.png => BlockStone.png} (100%) rename Collector/Content/{wall.png => BlockWall.png} (100%) rename Collector/Content/{water.png => BlockWater.png} (100%) rename Collector/Content/{wood.png => BlockWood.png} (100%) delete mode 100644 Collector/src/Dimension/Block.cs delete mode 100644 Collector/src/Dimension/BlockMaterials.cs create mode 100644 Collector/src/Dimension/Blocks.cs create mode 100644 Collector/src/ThirdPartyCode/QuadTree.cs diff --git a/.idea/.idea.Collector/.idea/misc.xml b/.idea/.idea.Collector/.idea/misc.xml index 28a804d..16d1225 100644 --- a/.idea/.idea.Collector/.idea/misc.xml +++ b/.idea/.idea.Collector/.idea/misc.xml @@ -3,4 +3,7 @@ + + \ No newline at end of file diff --git a/Collector.sln.DotSettings.user b/Collector.sln.DotSettings.user index 7b5f6af..2b5821a 100644 --- a/Collector.sln.DotSettings.user +++ b/Collector.sln.DotSettings.user @@ -2,5 +2,6 @@ True True True + True True True \ No newline at end of file diff --git a/Collector/Collector.csproj b/Collector/Collector.csproj index 9520261..806d792 100644 --- a/Collector/Collector.csproj +++ b/Collector/Collector.csproj @@ -11,6 +11,7 @@ + diff --git a/Collector/Content/air.png b/Collector/Content/BlockAir.png similarity index 100% rename from Collector/Content/air.png rename to Collector/Content/BlockAir.png diff --git a/Collector/Content/grass.png b/Collector/Content/BlockGrass.png similarity index 100% rename from Collector/Content/grass.png rename to Collector/Content/BlockGrass.png diff --git a/Collector/Content/jungleGrass.png b/Collector/Content/BlockJungleGrass.png similarity index 100% rename from Collector/Content/jungleGrass.png rename to Collector/Content/BlockJungleGrass.png diff --git a/Collector/Content/roof.png b/Collector/Content/BlockRoof.png similarity index 100% rename from Collector/Content/roof.png rename to Collector/Content/BlockRoof.png diff --git a/Collector/Content/sand.png b/Collector/Content/BlockSand.png similarity index 100% rename from Collector/Content/sand.png rename to Collector/Content/BlockSand.png diff --git a/Collector/Content/snow.png b/Collector/Content/BlockSnow.png similarity index 100% rename from Collector/Content/snow.png rename to Collector/Content/BlockSnow.png diff --git a/Collector/Content/stone.png b/Collector/Content/BlockStone.png similarity index 100% rename from Collector/Content/stone.png rename to Collector/Content/BlockStone.png diff --git a/Collector/Content/wall.png b/Collector/Content/BlockWall.png similarity index 100% rename from Collector/Content/wall.png rename to Collector/Content/BlockWall.png diff --git a/Collector/Content/water.png b/Collector/Content/BlockWater.png similarity index 100% rename from Collector/Content/water.png rename to Collector/Content/BlockWater.png diff --git a/Collector/Content/wood.png b/Collector/Content/BlockWood.png similarity index 100% rename from Collector/Content/wood.png rename to Collector/Content/BlockWood.png diff --git a/Collector/Content/Content.mgcb b/Collector/Content/Content.mgcb index 9fcab5a..c6502b0 100644 --- a/Collector/Content/Content.mgcb +++ b/Collector/Content/Content.mgcb @@ -13,7 +13,7 @@ #---------------------------------- Content ---------------------------------# -#begin air.png +#begin BlockAir.png /importer:TextureImporter /processor:TextureProcessor /processorParam:ColorKeyColor=255,0,255,255 @@ -23,7 +23,115 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:air.png +/build:BlockAir.png + +#begin BlockGrass.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockGrass.png + +#begin BlockJungleGrass.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockJungleGrass.png + +#begin BlockRoof.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockRoof.png + +#begin BlockSand.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockSand.png + +#begin BlockSnow.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockSnow.png + +#begin BlockStone.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockStone.png + +#begin BlockWall.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockWall.png + +#begin BlockWater.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockWater.png + +#begin BlockWood.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:BlockWood.png #begin crosshair.png /importer:TextureImporter @@ -37,7 +145,7 @@ /processorParam:TextureFormat=Color /build:crosshair.png -#begin grass.png +#begin man.png /importer:TextureImporter /processor:TextureProcessor /processorParam:ColorKeyColor=255,0,255,255 @@ -47,67 +155,7 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:grass.png - -#begin jungleGrass.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:jungleGrass.png - -#begin roof.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:roof.png - -#begin sand.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:sand.png - -#begin snow.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:snow.png - -#begin stone.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:stone.png +/build:man.png #begin tree.png /importer:TextureImporter @@ -121,39 +169,3 @@ /processorParam:TextureFormat=Color /build:tree.png -#begin wall.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:wall.png - -#begin water.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:water.png - -#begin wood.png -/importer:TextureImporter -/processor:TextureProcessor -/processorParam:ColorKeyColor=255,0,255,255 -/processorParam:ColorKeyEnabled=True -/processorParam:GenerateMipmaps=False -/processorParam:PremultiplyAlpha=True -/processorParam:ResizeToPowerOfTwo=False -/processorParam:MakeSquare=False -/processorParam:TextureFormat=Color -/build:wood.png - diff --git a/Collector/Main.cs b/Collector/Main.cs index de865b9..ab342ef 100644 --- a/Collector/Main.cs +++ b/Collector/Main.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using Collector.Character; using Collector.Dimension; @@ -19,12 +20,15 @@ namespace Collector private PlayerMouse _playerMouse; private OrthographicCamera _cam; private WorldRenderer WorldRenderer { get; set; } + public static Dictionary Materials { get; } = new Dictionary(); + public Main() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; + IsFixedTimeStep = true; } protected override void Initialize() @@ -32,8 +36,11 @@ namespace Collector // TODO: Add your initialization logic here base.Initialize(); - BlockMaterials.Initialize(Content); - + foreach (Blocks name in Enum.GetValues(typeof(Blocks))) + { + Materials.Add(name,Content.Load(name.ToString())); + } + var viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 480); _player = new Player(0, 0); _cam = new OrthographicCamera(viewportAdapter); @@ -49,7 +56,6 @@ namespace Collector { // TODO: Add your update logic here/* base.Update(gameTime); - World.LoadChunks(); World.UnloadChunks(); } diff --git a/Collector/Restrictions.cs b/Collector/Restrictions.cs index 0e2d43e..c26f56d 100644 --- a/Collector/Restrictions.cs +++ b/Collector/Restrictions.cs @@ -8,7 +8,7 @@ namespace Collector public const int Seed = 1; public const float ViewportHeight = 9/2f; public const float ViewportWidth = 16/2f; - public const int MovementSpeed = 16; + public const int MovementSpeed = 1; public const int FreeSpeed = 2; public const int KeyDelay = 0; public const int TileSize = 32; diff --git a/Collector/src/Character/InputController.cs b/Collector/src/Character/InputController.cs index 51e333c..1d47fe7 100644 --- a/Collector/src/Character/InputController.cs +++ b/Collector/src/Character/InputController.cs @@ -181,7 +181,7 @@ public class InputController : IRestrictions } if (mouseState.RightButton == ButtonState.Pressed) { - Chunks.PlaceBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile(),"wood"); + Chunks.PlaceBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile(),Blocks.BlockWood); } } diff --git a/Collector/src/Dimension/Block.cs b/Collector/src/Dimension/Block.cs deleted file mode 100644 index f91c5c3..0000000 --- a/Collector/src/Dimension/Block.cs +++ /dev/null @@ -1,8 +0,0 @@ - -public class Block { - public string Name { get; } - - public Block(string name) { - Name = name; - } -} diff --git a/Collector/src/Dimension/BlockMaterials.cs b/Collector/src/Dimension/BlockMaterials.cs deleted file mode 100644 index 08a93f8..0000000 --- a/Collector/src/Dimension/BlockMaterials.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Collections.Generic; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; - -namespace Collector.Dimension -{ - public static class BlockMaterials { - public static readonly Dictionary Materials = new Dictionary(); - public static readonly Dictionary Textures = new Dictionary(); - - //Private so the singleton can't be instantiated - static BlockMaterials() {} - - public static void Initialize(ContentManager content){ - Materials.Add("grass",new Block("grass")); - Materials.Add("wood",new Block("wood")); - Materials.Add("water",new Block("water")); - Materials.Add("stone",new Block("stone")); - Materials.Add("snow",new Block("snow")); - Materials.Add("sand",new Block("sand")); - Materials.Add("air",new Block("air")); - Materials.Add("roof",new Block("roof")); - Materials.Add("wall",new Block("wall")); - - foreach (var name in Materials.Keys) - { - Textures.Add(name,content.Load(name)); - } - } - } -} diff --git a/Collector/src/Dimension/Blocks.cs b/Collector/src/Dimension/Blocks.cs new file mode 100644 index 0000000..2f8ba28 --- /dev/null +++ b/Collector/src/Dimension/Blocks.cs @@ -0,0 +1,15 @@ +namespace Collector.Dimension +{ + public enum Blocks + { + BlockAir, + BlockGrass, + BlockWood, + BlockStone, + BlockSnow, + BlockSand, + BlockRoof, + BlockWall, + BlockWater + } +} \ No newline at end of file diff --git a/Collector/src/Dimension/Chunks.cs b/Collector/src/Dimension/Chunks.cs index 1c8f205..af3c4fe 100644 --- a/Collector/src/Dimension/Chunks.cs +++ b/Collector/src/Dimension/Chunks.cs @@ -3,38 +3,39 @@ using System; using System.Collections.Generic; using Collector.ThirdPartyCode; +using Microsoft.Xna.Framework.Graphics; namespace Collector.Dimension { - public class Chunks: IRestrictions { - public static Dictionary, Block> LoadedChunks { get; } = new Dictionary, Block>(); - private static readonly Dictionary, Block> SavedChunks = new Dictionary, Block>(); + public static class Chunks + { + public static Dictionary, Blocks> LoadedChunks { get; } = new Dictionary, Blocks>(); + private static readonly Dictionary, Blocks> SavedChunks = new Dictionary, Blocks>(); private static readonly OpenSimplexNoise Gen1 = new OpenSimplexNoise(IRestrictions.Seed+ 1); private static readonly OpenSimplexNoise Gen2 = new OpenSimplexNoise(IRestrictions.Seed); public static void CreateStructures() { const int i = 10; - SetBlock(3 + i, 2,1,"wall"); - SetBlock(4 + i, 2,1,"wall"); - SetBlock(3 + i, 3,1, "roof"); - SetBlock(4 + i, 3,1,"roof"); + SetBlock(3 + i, 2,1,Blocks.BlockWall); + SetBlock(4 + i, 2,1,Blocks.BlockWall); + SetBlock(3 + i, 3,1, Blocks.BlockRoof); + SetBlock(4 + i, 3,1,Blocks.BlockRoof); } - private static void SetBlock(int x, int y, int z, string name) { - LoadedChunks[new Tuple(x, y, z)] = BlockMaterials.Materials[name]; - SavedChunks[new Tuple(x, y, z)] = BlockMaterials.Materials[name]; + private static void SetBlock(int x, int y, int z, Blocks name) + { + LoadedChunks[new Tuple(x, y, z)] = name; + SavedChunks[new Tuple(x, y, z)] = name; } - public static void PlaceBlock(int x, int y, string name) + public static void PlaceBlock(int x, int y, Blocks name) { var triple = new Tuple(x, y, 1); - if (LoadedChunks.ContainsKey(triple)) + if (!LoadedChunks.ContainsKey(triple)) return; + if (LoadedChunks[triple].Equals(Blocks.BlockAir)) { - if (LoadedChunks[triple].Name.Equals("air")) - { - SetBlock(x, y, 1, name); - } + SetBlock(x, y, 1, name); } } @@ -42,10 +43,10 @@ namespace Collector.Dimension var triple = new Tuple(x, y, 1); if (!LoadedChunks.ContainsKey(triple)) return; - if (LoadedChunks[triple].Name.Equals("air")) return; + if (LoadedChunks[triple].Equals(Blocks.BlockAir)) return; - LoadedChunks[triple] = BlockMaterials.Materials["air"]; - SavedChunks[triple] = BlockMaterials.Materials["air"]; + LoadedChunks[triple] = Blocks.BlockAir; + SavedChunks[triple] = Blocks.BlockAir; } public static void UngenerateChunk(int x, int y) { @@ -99,8 +100,8 @@ namespace Collector.Dimension } } - private static Block GetBlocks(int x, int y, int z) { - return SavedChunks.GetValueOrDefault(new Tuple(x, y, z), new Block("air")); + private static 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) { @@ -111,7 +112,7 @@ namespace Collector.Dimension return Gen2.Evaluate(nx, ny) / 2 + 0.5; } - private static Block GetTerrain(int x, int y) { + private static Blocks GetTerrain(int x, int y) { const double scale = 0.01; var nx = x * scale; var ny = y * scale; @@ -134,26 +135,26 @@ namespace Collector.Dimension return GetBiomeBlock(moisture, elevation); } - private static Block GetBiomeBlock(double moisture, double elevation) { + private static Blocks GetBiomeBlock(double moisture, double elevation) { var biome = GetBiome(moisture, elevation); - if (biome.Equals("Tundra")) return BlockMaterials.Materials["snow"]; - if (biome.Equals("Taiga")) return BlockMaterials.Materials["snow"]; - if (biome.Equals("Snow")) return BlockMaterials.Materials["snow"]; + if (biome.Equals("Tundra")) return Blocks.BlockSnow; + if (biome.Equals("Taiga")) return Blocks.BlockSnow; + if (biome.Equals("Snow")) return Blocks.BlockSnow; - if (biome.Equals("Grassland")) return BlockMaterials.Materials["grass"]; - if (biome.Equals("Shrubland")) return BlockMaterials.Materials["grass"]; - if (biome.Equals("TemperateDeciduousForest")) return BlockMaterials.Materials["grass"]; + if (biome.Equals("Grassland")) return Blocks.BlockGrass; + if (biome.Equals("Shrubland")) return Blocks.BlockGrass; + if (biome.Equals("TemperateDeciduousForest")) return Blocks.BlockGrass; - if (biome.Equals("TemperateRainForest")) return BlockMaterials.Materials["grass"]; - if (biome.Equals("TropicalSeasonalForest")) return BlockMaterials.Materials["grass"]; - if (biome.Equals("TropicalForest")) return BlockMaterials.Materials["grass"]; - if (biome.Equals("TropicalRainForest")) return BlockMaterials.Materials["grass"]; + if (biome.Equals("TemperateRainForest")) return Blocks.BlockGrass; + if (biome.Equals("TropicalSeasonalForest")) return Blocks.BlockGrass; + if (biome.Equals("TropicalForest")) return Blocks.BlockGrass; + if (biome.Equals("TropicalRainForest")) return Blocks.BlockGrass; - if (biome.Equals("Beach")) return BlockMaterials.Materials["sand"]; - if (biome.Equals("Bare")) return BlockMaterials.Materials["sand"]; - if (biome.Equals("Scorched")) return BlockMaterials.Materials["sand"]; - if (biome.Equals("TemperateDesert")) return BlockMaterials.Materials["sand"]; - return biome.Equals("SubtropicalDesert") ? BlockMaterials.Materials["sand"] : BlockMaterials.Materials["water"]; + if (biome.Equals("Beach")) return Blocks.BlockSand; + if (biome.Equals("Bare")) return Blocks.BlockSand; + if (biome.Equals("Scorched")) return Blocks.BlockSand; + if (biome.Equals("TemperateDesert")) return Blocks.BlockSand; + return biome.Equals("SubtropicalDesert") ? Blocks.BlockSand : Blocks.BlockWater; } private static string GetBiome(double moisture, double elevation) { diff --git a/Collector/src/Dimension/Inventory.cs b/Collector/src/Dimension/Inventory.cs index 8228ceb..95b3b12 100644 --- a/Collector/src/Dimension/Inventory.cs +++ b/Collector/src/Dimension/Inventory.cs @@ -11,11 +11,7 @@ public class Inventory { public void SetInventory(LinkedList inventory) { this.inventory = inventory; } - - public void AddItem(Block block){ - inventory.AddFirst((ItemStack) block); - } - + public void RemoveItem(ItemStack itemStack){ inventory.Remove(itemStack); } diff --git a/Collector/src/Dimension/ItemStack.cs b/Collector/src/Dimension/ItemStack.cs index de9c435..5ae79bf 100644 --- a/Collector/src/Dimension/ItemStack.cs +++ b/Collector/src/Dimension/ItemStack.cs @@ -1,11 +1,11 @@ namespace Collector.Dimension { - public class ItemStack: Block { + public class ItemStack { int value; int quantity; - public ItemStack(string name, int value, int quantity) : base(name) + public ItemStack(Blocks id, int value, int quantity) { this.value = value; this.quantity = quantity; diff --git a/Collector/src/Dimension/WorldRenderer.cs b/Collector/src/Dimension/WorldRenderer.cs index 56944c4..9498221 100644 --- a/Collector/src/Dimension/WorldRenderer.cs +++ b/Collector/src/Dimension/WorldRenderer.cs @@ -1,7 +1,6 @@ using System.Linq; using Collector.Character; using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace Collector.Dimension @@ -23,10 +22,10 @@ namespace Collector.Dimension private static void DrawWorld(SpriteBatch batch, int layer) { - foreach (var chunkpair in Chunks.LoadedChunks.Keys.Where(chunkpair => chunkpair.Item3 == layer)) + foreach (var chunkpair in Chunks.LoadedChunks.Keys.Where(chunkpair => layer==chunkpair.Item3)) { batch.Draw( - BlockMaterials.Textures[Chunks.LoadedChunks[chunkpair].Name], + Main.Materials[Chunks.LoadedChunks[chunkpair]], new Rectangle( chunkpair.Item1 << IRestrictions.TileShift, chunkpair.Item2 << IRestrictions.TileShift, @@ -40,10 +39,11 @@ namespace Collector.Dimension public void Draw(GameTime gameTime) { //Higher means draws in a lower layer - DrawWorld(_spriteBatch, 0); + DrawWorld(_spriteBatch,0); + DrawWorld(_spriteBatch,1); _inputController.Draw(); _inputController.PlayerInput(_main,_playerMouse,gameTime); - DrawWorld(_spriteBatch, 1); + DrawWorld(_spriteBatch,2); } } } \ No newline at end of file diff --git a/Collector/src/ThirdPartyCode/QuadTree.cs b/Collector/src/ThirdPartyCode/QuadTree.cs new file mode 100644 index 0000000..f8d8090 --- /dev/null +++ b/Collector/src/ThirdPartyCode/QuadTree.cs @@ -0,0 +1,320 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +//Quadtree code +//https://github.com/futurechris/QuadTree + +namespace QuadTree +{ + + public class QuadTree + { + private static Stack branchPool = new Stack(); + private static Stack leafPool = new Stack(); + + private readonly Branch _root; + private readonly int _splitCount; + private readonly int _depthLimit; + private readonly Dictionary _leafLookup = new Dictionary(); + + + public QuadTree(int splitCount, int depthLimit, ref Quad region) + { + _splitCount = splitCount; + _depthLimit = depthLimit; + _root = CreateBranch(this, null, 0, ref region); + } + + public QuadTree(int splitCount, int depthLimit, Quad region) + : this(splitCount, depthLimit, ref region) + { + + } + + public QuadTree(int splitCount, int depthLimit, float x, float y, float width, float height) + : this(splitCount, depthLimit, new Quad(x, y, x + width, y + height)) + { + + } + + + public void Clear() + { + _root.Clear(); + _root.Tree = this; + _leafLookup.Clear(); + } + + + public static void ClearPools() + { + branchPool = new Stack(); + leafPool = new Stack(); + } + + + private void Insert(T value, ref Quad quad) + { + if (!_leafLookup.TryGetValue(value, out var leaf)) + { + leaf = CreateLeaf(value, ref quad); + _leafLookup.Add(value, leaf); + } + _root.Insert(leaf); + } + + public void Insert(T value, Quad quad) + { + Insert(value, ref quad); + } + + public void Insert(T value, float x, float y, float width, float height) + { + var quad = new Quad(x, y, x + width, y + height); + Insert(value, ref quad); + } + + private bool SearchArea(ref Quad quad, ref List values) + { + if (values != null) + values.Clear(); + else + values = new List(); + _root.SearchQuad(ref quad, values); + return values.Count > 0; + } + + public bool SearchArea(Quad quad, ref List values) + { + return SearchArea(ref quad, ref values); + } + + public bool SearchArea(float x, float y, float width, float height, ref List values) + { + var quad = new Quad(x, y, x + width, y + height); + return SearchArea(ref quad, ref values); + } + + public bool SearchPoint(float x, float y, ref List values) + { + if (values != null) + values.Clear(); + else + values = new List(); + _root.SearchPoint(x, y, values); + return values.Count > 0; + } + + public bool FindCollisions(T value, ref List values) + { + if (values != null) + values.Clear(); + else + values = new List(_leafLookup.Count); + + if (!_leafLookup.TryGetValue(value, out var leaf)) return false; + var branch = leaf.Branch; + + //Add the leaf's siblings (prevent it from colliding with itself) + if (branch.Leaves.Count > 0) + foreach (var t in branch.Leaves) + if (leaf != t && leaf.Quad.Intersects(ref t.Quad)) + values.Add(t.Value); + + //Add the branch's children + if (branch.Split) + for (var i = 0; i < 4; ++i) + if (branch.Branches[i] != null) + branch.Branches[i].SearchQuad(ref leaf.Quad, values); + + //Add all leaves back to the root + branch = branch.Parent; + while (branch != null) + { + if (branch.Leaves.Count > 0) + foreach (var t in branch.Leaves) + if (leaf.Quad.Intersects(ref t.Quad)) + values.Add(t.Value); + + branch = branch.Parent; + } + return false; + } + + public int CountBranches() + { + var count = 0; + CountBranches(_root, ref count); + return count; + } + + private static void CountBranches(Branch branch, ref int count) + { + ++count; + if (!branch.Split) return; + for (var i = 0; i < 4; ++i) + if (branch.Branches[i] != null) + CountBranches(branch.Branches[i], ref count); + } + + private static Branch CreateBranch(QuadTree tree, Branch parent, int branchDepth, ref Quad quad) + { + var branch = branchPool.Count > 0 ? branchPool.Pop() : new Branch(); + branch.Tree = tree; + branch.Parent = parent; + branch.Split = false; + branch.Depth = branchDepth; + var midX = quad.MinX + (quad.MaxX - quad.MinX) * 0.5f; + var midY = quad.MinY + (quad.MaxY - quad.MinY) * 0.5f; + branch.Quads[0].Set(quad.MinX, quad.MinY, midX, midY); + branch.Quads[1].Set(midX, quad.MinY, quad.MaxX, midY); + branch.Quads[2].Set(midX, midY, quad.MaxX, quad.MaxY); + branch.Quads[3].Set(quad.MinX, midY, midX, quad.MaxY); + return branch; + } + + private static Leaf CreateLeaf(T value, ref Quad quad) + { + var leaf = leafPool.Count > 0 ? leafPool.Pop() : new Leaf(); + leaf.Value = value; + leaf.Quad = quad; + return leaf; + } + + public class Branch + { + internal QuadTree Tree; + internal Branch Parent; + internal readonly Quad[] Quads = new Quad[4]; + internal readonly Branch[] Branches = new Branch[4]; + internal readonly List Leaves = new List(); + internal bool Split; + internal int Depth; + + internal void Clear() + { + Tree = null; + Parent = null; + Split = false; + + for (var i = 0; i < 4; ++i) + { + if (Branches[i] == null) continue; + branchPool.Push(Branches[i]); + Branches[i].Clear(); + Branches[i] = null; + } + + foreach (var t in Leaves) + { + leafPool.Push(t); + t.Branch = null; + t.Value = default; + } + + Leaves.Clear(); + } + + internal void Insert(Leaf leaf) + { + //If this branch is already split + if (Split) + { + for (var i = 0; i < 4; ++i) + { + if (!Quads[i].Contains(ref leaf.Quad)) continue; + Branches[i] ??= CreateBranch(Tree, this, Depth + 1, ref Quads[i]); + Branches[i].Insert(leaf); + return; + } + + Leaves.Add(leaf); + leaf.Branch = this; + } + else + { + //Add the leaf to this node + Leaves.Add(leaf); + leaf.Branch = this; + + //Once I have reached capacity, split the node + if (Leaves.Count >= Tree._splitCount && Depth < Tree._depthLimit) + { + Split = true; + } + } + } + + internal void SearchQuad(ref Quad quad, List values) + { + if (Leaves.Count > 0) + foreach (var t in Leaves) + if (quad.Intersects(ref t.Quad)) + values.Add(t.Value); + + for (var i = 0; i < 4; ++i) + if (Branches[i] != null) + Branches[i].SearchQuad(ref quad, values); + } + + internal void SearchPoint(float x, float y, List values) + { + if (Leaves.Count > 0) values.AddRange(from t in Leaves where t.Quad.Contains(x, y) select t.Value); + + for (var i = 0; i < 4; ++i) + if (Branches[i] != null) + Branches[i].SearchPoint(x, y, values); + } + } + + internal class Leaf + { + internal Branch Branch; + internal T Value; + internal Quad Quad; + } + } + + + public struct Quad + { + public float MinX; + public float MinY; + public float MaxX; + public float MaxY; + + public Quad(float minX, float minY, float maxX, float maxY) + { + MinX = minX; + MinY = minY; + MaxX = maxX; + MaxY = maxY; + } + + public void Set(float minX, float minY, float maxX, float maxY) + { + MinX = minX; + MinY = minY; + MaxX = maxX; + MaxY = maxY; + } + + + public bool Intersects(ref Quad other) + { + return MinX < other.MaxX && MinY < other.MaxY && MaxX > other.MinX && MaxY > other.MinY; + } + + public bool Contains(ref Quad other) + { + return other.MinX >= MinX && other.MinY >= MinY && other.MaxX <= MaxX && other.MaxY <= MaxY; + } + + public bool Contains(float x, float y) + { + return x > MinX && y > MinY && x < MaxX && y < MaxY; + } + } +} +