Added back block placement

This commit is contained in:
2020-04-23 13:40:33 -06:00
parent 0c01a22761
commit e25f4d70eb
6 changed files with 130 additions and 134 deletions

View File

@@ -7,7 +7,6 @@ using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using MonoGame.Extended; using MonoGame.Extended;
using MonoGame.Extended.ViewportAdapters; using MonoGame.Extended.ViewportAdapters;
using Mouse = Collector.Character.Mouse;
namespace Collector namespace Collector
{ {
@@ -17,7 +16,7 @@ namespace Collector
private SpriteBatch _spriteBatch; private SpriteBatch _spriteBatch;
private InputController _inputController; private InputController _inputController;
private Player _player; private Player _player;
private Mouse _mouse; private PlayerMouse _playerMouse;
private OrthographicCamera _cam; private OrthographicCamera _cam;
private WorldRenderer WorldRenderer { get; set; } private WorldRenderer WorldRenderer { get; set; }
@@ -41,9 +40,9 @@ namespace Collector
_cam.LookAt(new Vector2(Player.X, Player.Y)); _cam.LookAt(new Vector2(Player.X, Player.Y));
_spriteBatch = new SpriteBatch(GraphicsDevice); _spriteBatch = new SpriteBatch(GraphicsDevice);
_mouse = new Mouse(Content, _spriteBatch, _cam); _playerMouse = new PlayerMouse(Content, _spriteBatch, _cam);
_inputController = new InputController(_mouse, _cam, _spriteBatch, Content); _inputController = new InputController(_playerMouse, _cam, _spriteBatch, Content);
WorldRenderer = new WorldRenderer(_mouse, _inputController, _player, _spriteBatch, this, _cam); WorldRenderer = new WorldRenderer(_playerMouse, _inputController, _spriteBatch, this);
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
@@ -61,7 +60,7 @@ namespace Collector
base.Draw(gameTime); base.Draw(gameTime);
GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(transformMatrix: transformMatrix); _spriteBatch.Begin(transformMatrix: transformMatrix);
_mouse.Draw(); _playerMouse.Draw();
WorldRenderer.Draw(gameTime); WorldRenderer.Draw(gameTime);
_spriteBatch.End(); _spriteBatch.End();
} }

View File

@@ -1,16 +1,17 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Collector; using Collector;
using Collector.Character; using Collector.Character;
using Collector.Dimension;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using MonoGame.Extended; using MonoGame.Extended;
using Mouse = Collector.Character.Mouse;
public class InputController : IRestrictions public class InputController : IRestrictions
{ {
private readonly Mouse _mouse; private readonly PlayerMouse _playerMouse;
private readonly OrthographicCamera _cam; private readonly OrthographicCamera _cam;
private readonly Dictionary<string, Rectangle[]> _animations; private readonly Dictionary<string, Rectangle[]> _animations;
private readonly Texture2D _texture; private readonly Texture2D _texture;
@@ -20,10 +21,10 @@ public class InputController : IRestrictions
private int _timeSinceLastFrame; 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"; Input = "Down";
_mouse = mouse; _playerMouse = playerMouse;
_cam = cam; _cam = cam;
_spriteBatch = spriteBatch; _spriteBatch = spriteBatch;
_frameNumber = 0; _frameNumber = 0;
@@ -32,78 +33,78 @@ public class InputController : IRestrictions
{ {
["Up"] = new[] ["Up"] = new[]
{ {
new Rectangle(32*0, 0, 32, 32), new Rectangle(32 * 0, 0, 32, 32),
new Rectangle(32*1, 0, 32, 32), new Rectangle(32 * 1, 0, 32, 32),
new Rectangle(32*2, 0, 32, 32), new Rectangle(32 * 2, 0, 32, 32),
new Rectangle(32*3, 0, 32, 32) new Rectangle(32 * 3, 0, 32, 32)
}, },
["Right"] = new[] ["Right"] = new[]
{ {
new Rectangle(32*0 + 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 * 1 + 32 * 4, 32, 32, 32),
new Rectangle(32*2 + 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 * 3 + 32 * 4, 32, 32, 32)
}, },
["UpRight"] = new[] ["UpRight"] = new[]
{ {
new Rectangle(32*0 + 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 * 1 + 32 * 4, 32 * 2, 32, 32),
new Rectangle(32*2 + 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 * 3 + 32 * 4, 32 * 2, 32, 32)
}, },
["DownRight"] = new[] ["DownRight"] = new[]
{ {
new Rectangle(32*0 + 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 * 1 + 32 * 4, 32 * 3, 32, 32),
new Rectangle(32*2 + 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 * 3 + 32 * 4, 32 * 3, 32, 32)
}, },
["Left"] = new[] ["Left"] = new[]
{ {
new Rectangle(32*0, 32, 32, 32), new Rectangle(32 * 0, 32, 32, 32),
new Rectangle(32*1, 32, 32, 32), new Rectangle(32 * 1, 32, 32, 32),
new Rectangle(32*2, 32, 32, 32), new Rectangle(32 * 2, 32, 32, 32),
new Rectangle(32*3, 32, 32, 32) new Rectangle(32 * 3, 32, 32, 32)
}, },
["UpLeft"] = new[] ["UpLeft"] = new[]
{ {
new Rectangle(32*0, 32*2, 32, 32), new Rectangle(32 * 0, 32 * 2, 32, 32),
new Rectangle(32*1, 32*2, 32, 32), new Rectangle(32 * 1, 32 * 2, 32, 32),
new Rectangle(32*2, 32*2, 32, 32), new Rectangle(32 * 2, 32 * 2, 32, 32),
new Rectangle(32*3, 32*2, 32, 32) new Rectangle(32 * 3, 32 * 2, 32, 32)
}, },
["DownLeft"] = new[] ["DownLeft"] = new[]
{ {
new Rectangle(32*0, 32, 32, 32), new Rectangle(32 * 0, 32, 32, 32),
new Rectangle(32*1, 32, 32, 32), new Rectangle(32 * 1, 32, 32, 32),
new Rectangle(32*2, 32, 32, 32), new Rectangle(32 * 2, 32, 32, 32),
new Rectangle(32*3, 32, 32, 32) new Rectangle(32 * 3, 32, 32, 32)
}, },
["Down"] = new[] ["Down"] = new[]
{ {
new Rectangle(32*0 + 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 * 1 + 32 * 4, 0, 32, 32),
new Rectangle(32*2 + 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 * 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 keyboardState = Keyboard.GetState();
var mouseState = Mouse.GetState();
const int movementSpeed = IRestrictions.MovementSpeed; const int movementSpeed = IRestrictions.MovementSpeed;
if (keyboardState.IsKeyDown(Keys.Escape)) if (keyboardState.IsKeyDown(Keys.Escape))
{ {
Quit(main); Quit(main);
} }
if (keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.S) && keyboardState.IsKeyUp(Keys.D)) if (keyboardState.IsKeyUp(Keys.W) && keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.S) && keyboardState.IsKeyUp(Keys.D))
{ {
Input = Input.Contains("Right") ? "DownRight" : "DownLeft"; Input = Input.Contains("Right") ? "DownRight" : "DownLeft";
_frameNumber = 0; _frameNumber = 0;
_mouse.Draw(); _playerMouse.Draw();
} }
if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D)) if (keyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyDown(Keys.D))
{ {
@@ -173,7 +174,15 @@ public class InputController : IRestrictions
{ {
_cam.ZoomOut(0.01f); _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) private void UpdateAnimationFrame(GameTime gameTime)

View File

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

View File

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

View File

@@ -7,12 +7,11 @@ using Collector.ThirdPartyCode;
namespace Collector.Dimension namespace Collector.Dimension
{ {
public class Chunks: IRestrictions { public class Chunks: IRestrictions {
public static Dictionary<Tuple<int, int, int>, Block> LoadedChunks { get; } = public static Dictionary<Tuple<int, int, int>, Block> LoadedChunks { get; } = new Dictionary<Tuple<int, int, int>, Block>();
new Dictionary<Tuple<int, int, int>, Block>();
private static readonly Dictionary<Tuple<int, int, int>, Block> SavedChunks = new Dictionary<Tuple<int, int, int>, Block>(); private static readonly Dictionary<Tuple<int, int, int>, Block> SavedChunks = new Dictionary<Tuple<int, int, int>, 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() { public static void CreateStructures() {
const int i = 10; const int i = 10;
@@ -21,22 +20,30 @@ namespace Collector.Dimension
SetBlock(3 + i, 3,1, "roof"); SetBlock(3 + i, 3,1, "roof");
SetBlock(4 + i, 3,1,"roof"); SetBlock(4 + i, 3,1,"roof");
} }
public static void SetBlock(int x, int y,int z, string name) { private static void SetBlock(int x, int y, int z, string name) {
LoadedChunks.Add(new Tuple<int,int,int>(x, y, z), BlockMaterials.Materials[name]); LoadedChunks[new Tuple<int,int,int>(x, y, z)] = BlockMaterials.Materials[name];
SavedChunks.Add(new Tuple<int,int,int>(x, y, z), BlockMaterials.Materials[name]); SavedChunks[new Tuple<int,int,int>(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<int, int, int>(x, y, 1); var triple = new Tuple<int, int, int>(x, y, 1);
if (!LoadedChunks[triple].Name.Equals("air")) return; if (LoadedChunks.ContainsKey(triple))
LoadedChunks[triple] = BlockMaterials.Materials[name]; {
SavedChunks[triple] = BlockMaterials.Materials[name]; if (LoadedChunks[triple].Name.Equals("air"))
{
SetBlock(x, y, 1, name);
}
}
} }
public static void RemoveBlock(int x, int y){ public static void RemoveBlock(int x, int y){
var triple = new Tuple<int, int, int>(x, y, 1); var triple = new Tuple<int, int, int>(x, y, 1);
if (!LoadedChunks.ContainsKey(triple)) return;
if (LoadedChunks[triple].Name.Equals("air")) return; if (LoadedChunks[triple].Name.Equals("air")) return;
LoadedChunks[triple] = BlockMaterials.Materials["air"]; LoadedChunks[triple] = BlockMaterials.Materials["air"];
SavedChunks[triple] = BlockMaterials.Materials["air"]; SavedChunks[triple] = BlockMaterials.Materials["air"];
} }
@@ -97,11 +104,11 @@ namespace Collector.Dimension
} }
private static double Noise1(double nx, double ny) { 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) { 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) { private static Block GetTerrain(int x, int y) {

View File

@@ -3,30 +3,22 @@ using Collector.Character;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
namespace Collector.Dimension namespace Collector.Dimension
{ {
public class WorldRenderer : IRestrictions public class WorldRenderer : IRestrictions
{ {
private Mouse _mouse; private readonly PlayerMouse _playerMouse;
private InputController _inputController; private readonly InputController _inputController;
private Player _player; private readonly SpriteBatch _spriteBatch;
private SpriteBatch _spriteBatch; private readonly Main _main;
private Main _main;
private OrthographicCamera _orthographicCamera;
private double _elapsedTime;
public WorldRenderer(Mouse mouse, InputController inputController, Player player, SpriteBatch spriteBatch, public WorldRenderer(PlayerMouse playerMouse, InputController inputController, SpriteBatch spriteBatch, Main main)
Main main, OrthographicCamera orthographicCamera)
{ {
_mouse = mouse; _playerMouse = playerMouse;
_inputController = inputController; _inputController = inputController;
_player = player;
_spriteBatch = spriteBatch; _spriteBatch = spriteBatch;
_main = main; _main = main;
_orthographicCamera = orthographicCamera;
_elapsedTime = 0;
} }
private static void DrawWorld(SpriteBatch batch, int layer) 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) public void Draw(GameTime gameTime)
{ {
//Higher means draws in a lower layer //Higher means draws in a lower layer
DrawWorld(_spriteBatch, 0); DrawWorld(_spriteBatch, 0);
_inputController.Draw(); _inputController.Draw();
_inputController.PlayerInput(_main,_mouse,gameTime); _inputController.PlayerInput(_main,_playerMouse,gameTime);
DrawWorld(_spriteBatch, 1); DrawWorld(_spriteBatch, 1);
} }
} }