camera movement

This commit is contained in:
TheK0tYaRa 2025-06-22 13:48:35 +03:00
parent d7f95860cf
commit c7d4d57dbe
10 changed files with 158 additions and 39 deletions

View file

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

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 494 B

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextureAtlas>
<Texture>images/atlas</Texture>
<Regions>
<Region name="404" x="0" y="0" width="32" height="32" />
</Regions>
</TextureAtlas>

BIN
Content/images/atlas.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 718 B

118
Game1.cs
View file

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

View file

@ -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<Point, Chunk> _loadedChunks = new Dictionary<Point, Chunk>();
private IReadOnlyDictionary<Point, Chunk> 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<Texture2D>("Tilemap.png").Width);
// _defaultTile = Texture2D.FromFile(GraphicsDevice, "Tilemap.png");
// _defaultTile = Content.Load<Texture2D>("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<Point, Chunk> _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);
}
}
}
}
}*/
}
}
}

11
tunnet.code-workspace Normal file
View file

@ -0,0 +1,11 @@
{
"folders": [
{
"path": "."
},
{
"path": "../lib-bird"
}
],
"settings": {}
}

View file

@ -5,6 +5,7 @@
<RollForward>Major</RollForward>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
<!-- <AllowUnsafeBlocks>true</AllowUnsafeBlocks> -->
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
@ -26,6 +27,9 @@
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.*" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\lib-bird\bird.csproj" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="CollectPackageReferences">
<Message Text="Restoring dotnet tools (this might take a while depending on your internet speed and should only happen upon building your project for the first time, or after upgrading MonoGame, or clearing your nuget cache)" Importance="High" />
<Exec Command="dotnet tool restore" />