diff --git a/Collector/Main.cs b/Collector/Main.cs index 5e3a4bf..de865b9 100644 --- a/Collector/Main.cs +++ b/Collector/Main.cs @@ -7,7 +7,6 @@ using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using MonoGame.Extended; using MonoGame.Extended.ViewportAdapters; -using Mouse = Collector.Character.Mouse; namespace Collector { @@ -17,7 +16,7 @@ namespace Collector private SpriteBatch _spriteBatch; private InputController _inputController; private Player _player; - private Mouse _mouse; + private PlayerMouse _playerMouse; private OrthographicCamera _cam; private WorldRenderer WorldRenderer { get; set; } @@ -41,9 +40,9 @@ namespace Collector _cam.LookAt(new Vector2(Player.X, Player.Y)); _spriteBatch = new SpriteBatch(GraphicsDevice); - _mouse = new Mouse(Content, _spriteBatch, _cam); - _inputController = new InputController(_mouse, _cam, _spriteBatch, Content); - WorldRenderer = new WorldRenderer(_mouse, _inputController, _player, _spriteBatch, this, _cam); + _playerMouse = new PlayerMouse(Content, _spriteBatch, _cam); + _inputController = new InputController(_playerMouse, _cam, _spriteBatch, Content); + WorldRenderer = new WorldRenderer(_playerMouse, _inputController, _spriteBatch, this); } protected override void Update(GameTime gameTime) @@ -61,7 +60,7 @@ namespace Collector base.Draw(gameTime); GraphicsDevice.Clear(Color.CornflowerBlue); _spriteBatch.Begin(transformMatrix: transformMatrix); - _mouse.Draw(); + _playerMouse.Draw(); WorldRenderer.Draw(gameTime); _spriteBatch.End(); } diff --git a/Collector/src/Character/InputController.cs b/Collector/src/Character/InputController.cs index 7cff0f4..51e333c 100644 --- a/Collector/src/Character/InputController.cs +++ b/Collector/src/Character/InputController.cs @@ -1,16 +1,17 @@ +using System; using System.Collections.Generic; using Collector; using Collector.Character; +using Collector.Dimension; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using MonoGame.Extended; -using Mouse = Collector.Character.Mouse; public class InputController : IRestrictions { - private readonly Mouse _mouse; + private readonly PlayerMouse _playerMouse; private readonly OrthographicCamera _cam; private readonly Dictionary _animations; private readonly Texture2D _texture; @@ -20,10 +21,10 @@ public class InputController : IRestrictions private int _timeSinceLastFrame; - public InputController(Mouse mouse, OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager) + public InputController(PlayerMouse playerMouse, OrthographicCamera cam, SpriteBatch spriteBatch, ContentManager contentManager) { Input = "Down"; - _mouse = mouse; + _playerMouse = playerMouse; _cam = cam; _spriteBatch = spriteBatch; _frameNumber = 0; @@ -32,78 +33,78 @@ public class InputController : IRestrictions { ["Up"] = new[] { - new Rectangle(32*0, 0, 32, 32), - new Rectangle(32*1, 0, 32, 32), - new Rectangle(32*2, 0, 32, 32), - new Rectangle(32*3, 0, 32, 32) + new Rectangle(32 * 0, 0, 32, 32), + new Rectangle(32 * 1, 0, 32, 32), + new Rectangle(32 * 2, 0, 32, 32), + new Rectangle(32 * 3, 0, 32, 32) }, ["Right"] = new[] { - new Rectangle(32*0 + 32*4, 32, 32, 32), - new Rectangle(32*1 + 32*4, 32, 32, 32), - new Rectangle(32*2 + 32*4, 32, 32, 32), - new Rectangle(32*3 + 32*4, 32, 32, 32) + new Rectangle(32 * 0 + 32 * 4, 32, 32, 32), + new Rectangle(32 * 1 + 32 * 4, 32, 32, 32), + new Rectangle(32 * 2 + 32 * 4, 32, 32, 32), + new Rectangle(32 * 3 + 32 * 4, 32, 32, 32) }, ["UpRight"] = new[] { - new Rectangle(32*0 + 32*4, 32*2, 32, 32), - new Rectangle(32*1 + 32*4, 32*2, 32, 32), - new Rectangle(32*2 + 32*4, 32*2, 32, 32), - new Rectangle(32*3 + 32*4, 32*2, 32, 32) + new Rectangle(32 * 0 + 32 * 4, 32 * 2, 32, 32), + new Rectangle(32 * 1 + 32 * 4, 32 * 2, 32, 32), + new Rectangle(32 * 2 + 32 * 4, 32 * 2, 32, 32), + new Rectangle(32 * 3 + 32 * 4, 32 * 2, 32, 32) }, ["DownRight"] = new[] { - new Rectangle(32*0 + 32*4, 32*3, 32, 32), - new Rectangle(32*1 + 32*4, 32*3, 32, 32), - new Rectangle(32*2 + 32*4, 32*3, 32, 32), - new Rectangle(32*3 + 32*4, 32*3, 32, 32) + new Rectangle(32 * 0 + 32 * 4, 32 * 3, 32, 32), + new Rectangle(32 * 1 + 32 * 4, 32 * 3, 32, 32), + new Rectangle(32 * 2 + 32 * 4, 32 * 3, 32, 32), + new Rectangle(32 * 3 + 32 * 4, 32 * 3, 32, 32) }, ["Left"] = new[] { - new Rectangle(32*0, 32, 32, 32), - new Rectangle(32*1, 32, 32, 32), - new Rectangle(32*2, 32, 32, 32), - new Rectangle(32*3, 32, 32, 32) + new Rectangle(32 * 0, 32, 32, 32), + new Rectangle(32 * 1, 32, 32, 32), + new Rectangle(32 * 2, 32, 32, 32), + new Rectangle(32 * 3, 32, 32, 32) }, ["UpLeft"] = new[] { - new Rectangle(32*0, 32*2, 32, 32), - new Rectangle(32*1, 32*2, 32, 32), - new Rectangle(32*2, 32*2, 32, 32), - new Rectangle(32*3, 32*2, 32, 32) + new Rectangle(32 * 0, 32 * 2, 32, 32), + new Rectangle(32 * 1, 32 * 2, 32, 32), + new Rectangle(32 * 2, 32 * 2, 32, 32), + new Rectangle(32 * 3, 32 * 2, 32, 32) }, ["DownLeft"] = new[] { - new Rectangle(32*0, 32, 32, 32), - new Rectangle(32*1, 32, 32, 32), - new Rectangle(32*2, 32, 32, 32), - new Rectangle(32*3, 32, 32, 32) + new Rectangle(32 * 0, 32, 32, 32), + new Rectangle(32 * 1, 32, 32, 32), + new Rectangle(32 * 2, 32, 32, 32), + new Rectangle(32 * 3, 32, 32, 32) }, ["Down"] = new[] { - new Rectangle(32*0 + 32*4, 0, 32, 32), - new Rectangle(32*1 + 32*4, 0, 32, 32), - new Rectangle(32*2 + 32*4, 0, 32, 32), - new Rectangle(32*3 + 32*4, 0, 32, 32) + new Rectangle(32 * 0 + 32 * 4, 0, 32, 32), + new Rectangle(32 * 1 + 32 * 4, 0, 32, 32), + new Rectangle(32 * 2 + 32 * 4, 0, 32, 32), + new Rectangle(32 * 3 + 32 * 4, 0, 32, 32) }, }; } - public void PlayerInput(Main main, Mouse mouse, GameTime gameTime) + public void PlayerInput(Main main, PlayerMouse playerMouse, GameTime gameTime) { var keyboardState = Keyboard.GetState(); + var mouseState = Mouse.GetState(); const int movementSpeed = IRestrictions.MovementSpeed; if (keyboardState.IsKeyDown(Keys.Escape)) { Quit(main); } - if (keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.S) && keyboardState.IsKeyUp(Keys.D)) { Input = Input.Contains("Right") ? "DownRight" : "DownLeft"; _frameNumber = 0; - _mouse.Draw(); + _playerMouse.Draw(); } if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D)) { @@ -173,7 +174,15 @@ public class InputController : IRestrictions { _cam.ZoomOut(0.01f); } - + + if (mouseState.LeftButton == ButtonState.Pressed) + { + Chunks.RemoveBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile()); + } + if (mouseState.RightButton == ButtonState.Pressed) + { + Chunks.PlaceBlock(playerMouse.GetSelectedXTile(),playerMouse.GetSelectedYTile(),"wood"); + } } private void UpdateAnimationFrame(GameTime gameTime) diff --git a/Collector/src/Character/Mouse.cs b/Collector/src/Character/Mouse.cs deleted file mode 100644 index a6003eb..0000000 --- a/Collector/src/Character/Mouse.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; -using MonoGame.Extended; - -namespace Collector.Character -{ - public class Mouse : IRestrictions - { - private SpriteBatch _spriteBatch; - private ContentManager _contentManager; - private OrthographicCamera _cam; - private Texture2D Crosshair { get; set; } - - public Mouse(ContentManager contentManager, SpriteBatch spriteBatch, OrthographicCamera cam) - { - _spriteBatch = spriteBatch; - _cam = cam; - _contentManager = contentManager; - Crosshair = _contentManager.Load("crosshair"); - } - - public void Draw() - { - _spriteBatch.Draw( - Crosshair, - new Vector2( - GetSelectedX(Microsoft.Xna.Framework.Input.Mouse.GetState().X-(IRestrictions.TileSize/2f)) + (_cam.Position.X)+(IRestrictions.TileSize/2f), - GetSelectedY(Microsoft.Xna.Framework.Input.Mouse.GetState().Y-(IRestrictions.TileSize/2f)) + (_cam.Position.Y)+(IRestrictions.TileSize/2f) - ), - Color.White - ); - } - - - private static int GetSelectedX(float x) - { - return ((int) x >> 5) << 5; - } - - private static int GetSelectedY(float y) - { - return ((int) y >> 5) << 5; - } - } -} \ No newline at end of file diff --git a/Collector/src/Character/PlayerMouse.cs b/Collector/src/Character/PlayerMouse.cs new file mode 100644 index 0000000..a2841f2 --- /dev/null +++ b/Collector/src/Character/PlayerMouse.cs @@ -0,0 +1,47 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using MonoGame.Extended; + +namespace Collector.Character +{ + public class PlayerMouse : IRestrictions + { + private readonly SpriteBatch _spriteBatch; + private readonly OrthographicCamera _cam; + private Texture2D Crosshair { get; set; } + + public PlayerMouse(ContentManager contentManager, SpriteBatch spriteBatch, OrthographicCamera cam) + { + _spriteBatch = spriteBatch; + _cam = cam; + Crosshair = contentManager.Load("crosshair"); + } + + public void Draw() + { + _spriteBatch.Draw(Crosshair,new Vector2(GetSelectedX(),GetSelectedY()), Color.White); + } + + public int GetSelectedX() + { + return ((int) (Mouse.GetState().X + _cam.Position.X) >> 5) << 5; + } + + public int GetSelectedY() + { + return ((int) (Mouse.GetState().Y + _cam.Position.Y) >> 5) << 5; + } + + public int GetSelectedXTile() + { + return (int) (Mouse.GetState().X + _cam.Position.X) >> 5; + } + + public int GetSelectedYTile() + { + return (int) (Mouse.GetState().Y + _cam.Position.Y) >> 5; + } + } +} \ No newline at end of file diff --git a/Collector/src/Dimension/Chunks.cs b/Collector/src/Dimension/Chunks.cs index 1c7d263..1c8f205 100644 --- a/Collector/src/Dimension/Chunks.cs +++ b/Collector/src/Dimension/Chunks.cs @@ -7,12 +7,11 @@ using Collector.ThirdPartyCode; namespace Collector.Dimension { public class Chunks: IRestrictions { - public static Dictionary, Block> LoadedChunks { get; } = - new Dictionary, Block>(); - + public static Dictionary, Block> LoadedChunks { get; } = new Dictionary, Block>(); private static readonly Dictionary, Block> SavedChunks = new Dictionary, Block>(); - private static readonly OpenSimplexNoise _gen1 = new OpenSimplexNoise(IRestrictions.Seed+ 1); - private static readonly OpenSimplexNoise _gen2 = new OpenSimplexNoise(IRestrictions.Seed); + + 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; @@ -21,22 +20,30 @@ namespace Collector.Dimension SetBlock(3 + i, 3,1, "roof"); SetBlock(4 + i, 3,1,"roof"); } - - public static void SetBlock(int x, int y,int z, string name) { - LoadedChunks.Add(new Tuple(x, y, z), BlockMaterials.Materials[name]); - SavedChunks.Add(new Tuple(x, y, z), BlockMaterials.Materials[name]); + + 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]; } - public static void PlaceBlock(int x, int y, string name){ + public static void PlaceBlock(int x, int y, string name) + { var triple = new Tuple(x, y, 1); - if (!LoadedChunks[triple].Name.Equals("air")) return; - LoadedChunks[triple] = BlockMaterials.Materials[name]; - SavedChunks[triple] = BlockMaterials.Materials[name]; + if (LoadedChunks.ContainsKey(triple)) + { + if (LoadedChunks[triple].Name.Equals("air")) + { + SetBlock(x, y, 1, name); + } + } } public static void RemoveBlock(int x, int y){ var triple = new Tuple(x, y, 1); + + if (!LoadedChunks.ContainsKey(triple)) return; if (LoadedChunks[triple].Name.Equals("air")) return; + LoadedChunks[triple] = BlockMaterials.Materials["air"]; SavedChunks[triple] = BlockMaterials.Materials["air"]; } @@ -97,11 +104,11 @@ namespace Collector.Dimension } private static double Noise1(double nx, double ny) { - return _gen1.Evaluate(nx, ny) / 2 + 0.5; + return Gen1.Evaluate(nx, ny) / 2 + 0.5; } private static double Noise2(double nx, double ny) { - return _gen2.Evaluate(nx, ny) / 2 + 0.5; + return Gen2.Evaluate(nx, ny) / 2 + 0.5; } private static Block GetTerrain(int x, int y) { diff --git a/Collector/src/Dimension/WorldRenderer.cs b/Collector/src/Dimension/WorldRenderer.cs index 3aac292..56944c4 100644 --- a/Collector/src/Dimension/WorldRenderer.cs +++ b/Collector/src/Dimension/WorldRenderer.cs @@ -3,30 +3,22 @@ using Collector.Character; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; -using MonoGame.Extended; namespace Collector.Dimension { public class WorldRenderer : IRestrictions { - private Mouse _mouse; - private InputController _inputController; - private Player _player; - private SpriteBatch _spriteBatch; - private Main _main; - private OrthographicCamera _orthographicCamera; - private double _elapsedTime; + private readonly PlayerMouse _playerMouse; + private readonly InputController _inputController; + private readonly SpriteBatch _spriteBatch; + private readonly Main _main; - public WorldRenderer(Mouse mouse, InputController inputController, Player player, SpriteBatch spriteBatch, - Main main, OrthographicCamera orthographicCamera) + public WorldRenderer(PlayerMouse playerMouse, InputController inputController, SpriteBatch spriteBatch, Main main) { - _mouse = mouse; + _playerMouse = playerMouse; _inputController = inputController; - _player = player; _spriteBatch = spriteBatch; _main = main; - _orthographicCamera = orthographicCamera; - _elapsedTime = 0; } private static void DrawWorld(SpriteBatch batch, int layer) @@ -45,23 +37,12 @@ namespace Collector.Dimension } } - /* - private void mouseCrosshair(SpriteBatch batch) - { - Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); - Main.cam.unproject(mousePos); - int x = mouse.getSelectedX(mousePos); - int y = mouse.getSelectedY(mousePos); - batch.draw(mouse.getCrosshair(), x, y); - } - */ - public void Draw(GameTime gameTime) { //Higher means draws in a lower layer DrawWorld(_spriteBatch, 0); _inputController.Draw(); - _inputController.PlayerInput(_main,_mouse,gameTime); + _inputController.PlayerInput(_main,_playerMouse,gameTime); DrawWorld(_spriteBatch, 1); } }