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

@@ -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<Texture2D>("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)

View File

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

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

View File

@@ -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,