diff --git a/Content/Content.mgcb b/Content/Content.mgcb index e1a4b9c..15f5d4e 100644 --- a/Content/Content.mgcb +++ b/Content/Content.mgcb @@ -13,7 +13,10 @@ #---------------------------------- Content ---------------------------------# -#begin Tilemap.png +#begin images/atlas-definition.xml +/copy:images/atlas-definition.xml + +#begin images/atlas.png /importer:TextureImporter /processor:TextureProcessor /processorParam:ColorKeyColor=255,0,255,255 @@ -23,5 +26,5 @@ /processorParam:ResizeToPowerOfTwo=False /processorParam:MakeSquare=False /processorParam:TextureFormat=Color -/build:Tilemap.png +/build:images/atlas.png diff --git a/Content/Tilemap.kra~ b/Content/Tilemap.kra~ deleted file mode 100644 index e1e7c66..0000000 Binary files a/Content/Tilemap.kra~ and /dev/null differ diff --git a/Content/Tilemap.png b/Content/Tilemap.png deleted file mode 100644 index c1695d5..0000000 Binary files a/Content/Tilemap.png and /dev/null differ diff --git a/Content/images/atlas-definition.xml b/Content/images/atlas-definition.xml new file mode 100755 index 0000000..4002841 --- /dev/null +++ b/Content/images/atlas-definition.xml @@ -0,0 +1,7 @@ + + + images/atlas + + + + diff --git a/Content/Tilemap.kra b/Content/images/atlas.kra similarity index 81% rename from Content/Tilemap.kra rename to Content/images/atlas.kra index 11da0a8..32e204d 100644 Binary files a/Content/Tilemap.kra and b/Content/images/atlas.kra differ diff --git a/Content/images/atlas.png b/Content/images/atlas.png new file mode 100644 index 0000000..02346da Binary files /dev/null and b/Content/images/atlas.png differ diff --git a/Game1.cs b/Game1.cs index 9ad43a7..2715c9e 100644 --- a/Game1.cs +++ b/Game1.cs @@ -1,45 +1,49 @@ -// using System.Collections.Generic; +using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; -using static tunnet.World.World; + +using MonoGameLibrary; +using MonoGameLibrary.Graphics; + +// using tunnet.World; namespace tunnet; -public class Game1 : Game +public class Game1 : Core { - private GraphicsDeviceManager _graphics; - private SpriteBatch _spriteBatch; - // - private Map _world; + // private World.Map _world; + // private Texture2D _logo; - public Game1() + public Game1() : base("Tunnet but not", 1280, 720, fullScreen: false) { - _graphics = new GraphicsDeviceManager(this); - Content.RootDirectory = "Content"; - IsMouseVisible = true; + // Content.RootDirectory = "Content"; + // IsMouseVisible = true; } protected override void Initialize() { // TODO: Add your initialization logic here - _world = new Map(mapId: 0); - _world.LoadMap(); + // _world = new World.Map(mapId: 0); + // _world.LoadMap(); // base.Initialize(); } + private Sprite _404; + // private AnimatedSprite _animatedSprite; protected override void LoadContent() { - _spriteBatch = new SpriteBatch(GraphicsDevice); - // _world.setContent(Content); + TextureAtlas atlas = TextureAtlas.FromFile(Content, "images/atlas-definition.xml"); - // TODO: use this.Content to load your game content here - // _world._defaultTile = Texture2D.FromFile(GraphicsDevice, "src/Content/Tilemap.png"); - _world._defaultTile = Content.Load("Tilemap.png"); + _404 = atlas.CreateSprite("404"); + // _animatedSprite = atlas.CreateAnimatedSprite("404"); } + private const int _CAMERA_SPEED = 5; + Viewport _cameraPosition; + Point _mousePosition; protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) @@ -47,21 +51,89 @@ public class Game1 : Game // TODO: Add your update logic here + // Animations + // _animatedSprite.Update(gameTime); + + // Inputs + // https://docs.monogame.net/articles/tutorials/building_2d_games/10_handling_input/ + + if (_cameraPosition.Equals(new Viewport())) _cameraPosition = GraphicsDevice.Viewport; + + CheckKeyboardInput(); + CheckMouseInput(); + base.Update(gameTime); } + private void CheckKeyboardInput() + { + // Get the state of keyboard input + KeyboardState keyboardState = Keyboard.GetState(); + + // If the space key is held down, the movement speed increases by 1.5 + int speed = _CAMERA_SPEED; + if (keyboardState.IsKeyDown(Keys.Space)) + { + speed = (int)Math.Floor(speed * 1.5); + } + if (keyboardState.IsKeyDown(Keys.W) || keyboardState.IsKeyDown(Keys.Up)) + { + _cameraPosition.Y += speed; + } + if (keyboardState.IsKeyDown(Keys.S) || keyboardState.IsKeyDown(Keys.Down)) + { + _cameraPosition.Y -= speed; + } + if (keyboardState.IsKeyDown(Keys.A) || keyboardState.IsKeyDown(Keys.Left)) + { + _cameraPosition.X += speed; + } + if (keyboardState.IsKeyDown(Keys.D) || keyboardState.IsKeyDown(Keys.Right)) + { + _cameraPosition.X -= speed; + } + // + GraphicsDevice.Viewport = _cameraPosition; + } + private void CheckMouseInput() + { + // Get the state of mouse input + MouseState mouseState = Mouse.GetState(); + + //Move camera with right mouse button + if (mouseState.RightButton == ButtonState.Pressed) + { + _mousePosition -= mouseState.Position; // delta + + _cameraPosition!.X = _cameraPosition.X - _mousePosition.X; + _cameraPosition.Y = _cameraPosition.Y - _mousePosition.Y; + } + // + GraphicsDevice.Viewport = _cameraPosition; + _mousePosition = mouseState.Position; // here because of wasd breaking it + } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); - // TODO: Add your drawing code here - _spriteBatch.Begin(); + SpriteBatch.Begin(sortMode: SpriteSortMode.Deferred); - _world.Draw(ref _spriteBatch); + // World.Map.Draw draw = new World.Map.Draw(in _spriteBatch); //TODO: fix + // _spriteBatch.Draw(_world._defaultTile, _world._defaultTile.Bounds.Center.ToVector2(), Color.White); //TODO: fix - _spriteBatch.Draw(_world._defaultTile, Vector2.Zero, Color.White); + /// TODO: Draw + /// 1. floor + /// 2. wires + /// 3. buildings + /// 4. entities + /// 5. walls + /// + /// MINIMIZE texture swaps + /// MAXIMIZE drawRectangle usage per texture - _spriteBatch.End(); + _404.Draw(SpriteBatch, Vector2.Zero); + + SpriteBatch.End(); // base.Draw(gameTime); } diff --git a/World/World.cs b/World/World.cs index 0182580..693b544 100644 --- a/World/World.cs +++ b/World/World.cs @@ -1,11 +1,13 @@ // using System; using System.Collections.Generic; using System.IO; +using System.Threading; using Microsoft.Xna.Framework; // using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace tunnet.World; + struct World { private class Tile // TODO: sprite, buildings[] @@ -21,8 +23,8 @@ struct World } private class Chunk { - public const int Width = 16; - public const int Height = 16; + public const int Width = 32; + public const int Height = 32; internal Tile[,] Tiles { get; private set; } public Chunk() @@ -89,21 +91,19 @@ struct World return null; } } - public class Map : Game + public class Map { private readonly Dictionary _loadedChunks = new Dictionary(); private IReadOnlyDictionary Chunks => _loadedChunks; + private Mutex _loadWait = new(); private readonly int id; - private readonly int TileSize; - public Texture2D _defaultTile; + // private readonly int TileSize; + // public Texture2D _defaultTile; public Map(int mapId) { id = mapId; - TileSize = 32; - // Console.WriteLine(Content.Load("Tilemap.png").Width); - // _defaultTile = Texture2D.FromFile(GraphicsDevice, "Tilemap.png"); - // _defaultTile = Content.Load("Tilemap.png"); + // TileSize = 32; } // Load the map by ID @@ -161,9 +161,30 @@ struct World keysToRemove.ForEach(k => _loadedChunks.Remove(k)); } - public void Draw(ref SpriteBatch spriteBatch) + /*public class Draw(in SpriteBatch spriteBatch) { - foreach (var kvp in _loadedChunks) + private SpriteBatch spriteBatch = spriteBatch; + }*/ + + // private ref struct Draw + // { + // private ref SpriteBatch _spriteBatch; + // public Draw(ref SpriteBatch spriteBatch) + // { + // _spriteBatch = ref spriteBatch; + // } + // } + + // private readonly IReadOnlyDictionary _chunks; + + /*public void Draw() + { + if (_chunks.Count == 0) + { + Console.WriteLine("No chunks loaded"); + return; + } + foreach (var kvp in _chunks) { Point chunkCoord = kvp.Key; // var chunk = kvp.Value; @@ -179,10 +200,11 @@ struct World ); // draw the default tile for now - spriteBatch.Draw(_defaultTile, pos, Color.White); + Console.WriteLine("Sprite location: ", pos); + _spriteBatch.Draw(_defaultTile, pos, Color.White); } } } - } + }*/ } -} \ No newline at end of file +} diff --git a/tunnet.code-workspace b/tunnet.code-workspace new file mode 100644 index 0000000..de141ec --- /dev/null +++ b/tunnet.code-workspace @@ -0,0 +1,11 @@ +{ + "folders": [ + { + "path": "." + }, + { + "path": "../lib-bird" + } + ], + "settings": {} +} \ No newline at end of file diff --git a/tunnet.csproj b/tunnet.csproj index 06680d2..3a77701 100644 --- a/tunnet.csproj +++ b/tunnet.csproj @@ -5,6 +5,7 @@ Major false false + app.manifest @@ -26,6 +27,9 @@ + + +