Got rid of a large amount of static variables

This commit is contained in:
2020-06-30 13:59:50 -06:00
parent a98b44b7bd
commit f31e1c3da3
8 changed files with 131 additions and 133 deletions

View File

@@ -1,4 +1,4 @@
namespace Collector.Character
namespace Collector.Dimension
{
public class Autotiling
{

View File

@@ -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<Tuple<int, int, int>, Blocks> LoadedChunks { get; } = new Dictionary<Tuple<int, int, int>, Blocks>();
private static readonly Dictionary<Tuple<int, int, int>, Blocks> SavedChunks = new Dictionary<Tuple<int, int, int>, Blocks>();
public static readonly Dictionary<Tuple<int, int>, Collision> LoadedCollisions = new Dictionary<Tuple<int, int>, Collision>();
public static readonly Dictionary<Tuple<int, int>, Collision> SavedCollisions = new Dictionary<Tuple<int, int>, Collision>();
public static readonly List<Blocks> Impassable = new List<Blocks>();
public Dictionary<Tuple<int, int, int>, Blocks> LoadedChunks { get; } = new Dictionary<Tuple<int, int, int>, Blocks>();
private readonly Dictionary<Tuple<int, int, int>, Blocks> _savedChunks = new Dictionary<Tuple<int, int, int>, Blocks>();
public readonly Dictionary<Tuple<int, int>, Collision> LoadedCollisions = new Dictionary<Tuple<int, int>, Collision>();
private readonly Dictionary<Tuple<int, int>, Collision> _savedCollisions = new Dictionary<Tuple<int, int>, Collision>();
public readonly List<Blocks> Impassable = new List<Blocks>();
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<int, int, int>(x, y, z);
var pair = new Tuple<int, int>(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<int, int, int>(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<int, int, int>(x, y, 1);
var pair = new Tuple<int,int>(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<int, int, int>(i, j, 0);
var pair = new Tuple<int, int>(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<int, int, int>(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<int, int, int>(x, y, z), Blocks.BlockAir);
private Blocks GetBlocks(int x, int y, int z) {
return _savedChunks.GetValueOrDefault(new Tuple<int, int, int>(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,int,int>((int)x * 8, (int)y * 8,0));
}
}

View File

@@ -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
);
}
}
}
}

View File

@@ -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);
}
}