diff --git a/.idea/.idea.Tiles/.idea/vcs.xml b/.idea/.idea.Tiles/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/.idea.Tiles/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Tiles.sln.DotSettings.user b/Tiles.sln.DotSettings.user
new file mode 100644
index 0000000..ce2970b
--- /dev/null
+++ b/Tiles.sln.DotSettings.user
@@ -0,0 +1,2 @@
+
+ ForceIncluded
\ No newline at end of file
diff --git a/Tiles/Game.cs b/Tiles/Game.cs
index 4f8df70..38d6a20 100644
--- a/Tiles/Game.cs
+++ b/Tiles/Game.cs
@@ -6,8 +6,8 @@ namespace Tiles;
public class Game
{
const int _tileSize = 8;
- const int _worldWidth = 1920/8;
- const int _worldHeight = 1080/8;
+ const int _worldWidth = 1000;
+ const int _worldHeight = 1000;
private int _screenWidth;
private int _screenHeight;
@@ -15,12 +15,30 @@ public class Game
private Dictionary _tiles = new();
private Dictionary _worldGrid = new();
+ private Vector2 _playerPosition;
+ private float _playerSpeed = 2f;
+
+ private Camera2D _camera;
+
+ private string _currentSelectedTile = "air";
+ private int _currentSelectedIndex = 0;
+
public Game(int screenWidth, int screenHeight)
{
_screenWidth = screenWidth;
_screenHeight = screenHeight;
Raylib.InitWindow(_screenWidth, _screenHeight, "Tiles");
Raylib.SetTargetFPS(60);
+
+ _playerPosition = GetGlobalPositionFromGrid(_worldWidth/2, _worldHeight/2);
+
+ _camera = new Camera2D()
+ {
+ Target = _playerPosition,
+ Offset = new Vector2(_screenWidth / 2, _screenHeight / 2),
+ Rotation = 0f,
+ Zoom = 5f
+ };
SetupTiles();
@@ -28,36 +46,100 @@ public class Game
while (!Raylib.WindowShouldClose())
{
+ HandleInput();
+ _camera.Target = _playerPosition;
+
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(20, 70, 100, 255));
+ Raylib.BeginMode2D(_camera);
DrawWorld();
-
+ DrawPlayer();
+ DrawDebug();
+
+ Raylib.EndMode2D();
Raylib.EndDrawing();
}
}
+ private void HandleInput()
+ {
+ if (Raylib.IsKeyDown(KeyboardKey.W)) _playerPosition += new Vector2(0, -1) * _playerSpeed;
+ if (Raylib.IsKeyDown(KeyboardKey.A)) _playerPosition += new Vector2(-1, 0) * _playerSpeed;
+ if (Raylib.IsKeyDown(KeyboardKey.S)) _playerPosition += new Vector2(0, 1) * _playerSpeed;
+ if (Raylib.IsKeyDown(KeyboardKey.D)) _playerPosition += new Vector2(1, 0) * _playerSpeed;
+
+ if (Raylib.IsKeyReleased(KeyboardKey.Right))
+ {
+ _currentSelectedIndex = (_currentSelectedIndex + 1) % _tiles.Count;
+ _currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex);
+ }
+
+ if (Raylib.IsKeyReleased(KeyboardKey.Left))
+ {
+ _currentSelectedIndex = (_currentSelectedIndex - 1) % _tiles.Count;
+ _currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex);
+ }
+
+ if (Raylib.IsKeyReleased(KeyboardKey.Space))
+ {
+ Random random = new();
+ var tile = _tiles.Keys.ElementAt(random.Next(_tiles.Keys.Count));
+ SetTile(GetGridPositionFromGlobalPosition((int)_playerPosition.X, (int)_playerPosition.Y), _currentSelectedTile);
+ }
+
+ if (Raylib.IsKeyReleased(KeyboardKey.Zero)) _currentSelectedTile = "air";
+ }
+
private void SetupTiles()
{
_tiles.Add("dirt", new("dirt", "data/tiles/images/dirt.png"));
_tiles.Add("grass", new("grass", "data/tiles/images/grass.png"));
_tiles.Add("rock", new("rock", "data/tiles/images/rock.png"));
+ _tiles.Add("bedrock", new("bedrock", "data/tiles/images/bedrock.png"));
}
private void DrawWorld()
{
- foreach (var tile in _worldGrid)
+ // Calculate the boundaries of the visible region in grid coordinates
+ int startX = Math.Max((int)(_camera.Target.X - (_screenWidth / 2 / _camera.Zoom)) / _tileSize, 0);
+ int startY = Math.Max((int)(_camera.Target.Y - (_screenHeight / 2 / _camera.Zoom)) / _tileSize, 0);
+ int endX = Math.Min((int)(_camera.Target.X + (_screenWidth / 2 / _camera.Zoom)) / _tileSize, _worldWidth - 1);
+ int endY = Math.Min((int)(_camera.Target.Y + (_screenHeight / 2 / _camera.Zoom)) / _tileSize, _worldHeight - 1);
+
+ // Loop only through the visible tiles
+ for (int x = startX; x <= endX; x++)
{
- if (tile.Value == "air")
+ for (int y = startY; y <= endY; y++)
{
- Raylib.DrawRectangle((int)tile.Key.X * _tileSize,(int)tile.Key.Y * _tileSize, _tileSize, _tileSize, new(0,0,0,0));
- }
- else
- {
- Raylib.DrawTexture(_tiles[tile.Value].Texture, (int)tile.Key.X * _tileSize,(int)tile.Key.Y * _tileSize, Color.White);
+ Vector2 position = new(x, y);
+ if (_worldGrid.TryGetValue(position, out var tileType) && tileType != "air")
+ {
+ Raylib.DrawTexture(_tiles[tileType].Texture, x * _tileSize, y * _tileSize, Color.White);
+ }
}
}
}
+
+
+ private void DrawPlayer()
+ {
+ Raylib.DrawRectangle((int)_playerPosition.X, (int)_playerPosition.Y, _tileSize, _tileSize, new(255,255,0,100));
+ }
+
+ private void DrawDebug()
+ {
+ int textSize = 10;
+
+ // Position the text relative to the camera's offset
+ int textPositionX = (int)(_camera.Target.X - _screenWidth / 2 / _camera.Zoom) + 10;
+ int textPositionY = (int)(_camera.Target.Y - _screenHeight / 2 / _camera.Zoom) + 10;
+
+ Raylib.DrawText(_currentSelectedTile, textPositionX, textPositionY, textSize, Color.White);
+ Raylib.DrawText("Global: " + _playerPosition.ToString(), textPositionX, textPositionY + (textSize*2), textSize, Color.White);
+ Raylib.DrawText("Grid: " + GetGridPositionFromGlobalPosition((int)_playerPosition.X, (int)_playerPosition.Y).ToString().ToUpper(),
+ textPositionX, textPositionY + (textSize * 3), textSize, Color.White);
+ }
// private void GenerateWorld()
@@ -98,5 +180,32 @@ public class Game
_worldGrid.Add(new(x, y), tileType);
}
}
+
+ _worldGrid[new(0,72)] = "air";
+ }
+
+ private Vector2 GetGridPositionFromGlobalPosition(int x, int y)
+ {
+ int gridX = x / _tileSize;
+ int gridY = y / _tileSize;
+
+ // Check if the calculated grid position is within the bounds of the world grid
+ if (gridX >= 0 && gridX < _worldWidth && gridY >= 0 && gridY < _worldHeight)
+ {
+ return new Vector2(gridX, gridY);
+ }
+
+ // Return (0, 0) as a failsafe if outside the world grid
+ return new Vector2(0, 0);
+ }
+
+ private void SetTile(Vector2 position, string tileType)
+ {
+ _worldGrid[position] = tileType;
+ }
+
+ private Vector2 GetGlobalPositionFromGrid(int x, int y)
+ {
+ return new Vector2(x * _tileSize, y * _tileSize);
}
}
\ No newline at end of file
diff --git a/Tiles/Tile.cs b/Tiles/Tile.cs
index 2587973..f4c2f52 100644
--- a/Tiles/Tile.cs
+++ b/Tiles/Tile.cs
@@ -7,12 +7,15 @@ public class Tile
public string Name { get; private set; } = string.Empty;
public Image Image { get; private set; }
public Texture2D Texture { get; private set; }
+
+ public bool Transparent { get; private set; }
- public Tile(string name, string pathToImage)
+ public Tile(string name, string pathToImage, bool transparent = false)
{
Name = name;
Image = LoadImage(pathToImage);
Texture = LoadTexture(Image);
+ Transparent = transparent;
}
private Image LoadImage(string path)
diff --git a/Tiles/bin/Debug/net8.0/Tiles.dll b/Tiles/bin/Debug/net8.0/Tiles.dll
index 0558232..8c96e22 100644
Binary files a/Tiles/bin/Debug/net8.0/Tiles.dll and b/Tiles/bin/Debug/net8.0/Tiles.dll differ
diff --git a/Tiles/bin/Debug/net8.0/Tiles.pdb b/Tiles/bin/Debug/net8.0/Tiles.pdb
index c5e64e1..1e37c2f 100644
Binary files a/Tiles/bin/Debug/net8.0/Tiles.pdb and b/Tiles/bin/Debug/net8.0/Tiles.pdb differ
diff --git a/Tiles/bin/Debug/net8.0/data/tiles/images/bedrock.png b/Tiles/bin/Debug/net8.0/data/tiles/images/bedrock.png
new file mode 100644
index 0000000..d5410b1
Binary files /dev/null and b/Tiles/bin/Debug/net8.0/data/tiles/images/bedrock.png differ
diff --git a/Tiles/data/tiles/images/bedrock.png b/Tiles/data/tiles/images/bedrock.png
new file mode 100644
index 0000000..d5410b1
Binary files /dev/null and b/Tiles/data/tiles/images/bedrock.png differ
diff --git a/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfo.cs b/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfo.cs
index 3203fbb..2bf0b31 100644
--- a/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfo.cs
+++ b/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfo.cs
@@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Tiles")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
-[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
+[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ea6267d407dae7d4981288325746fe1899ea0d97")]
[assembly: System.Reflection.AssemblyProductAttribute("Tiles")]
[assembly: System.Reflection.AssemblyTitleAttribute("Tiles")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
diff --git a/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfoInputs.cache b/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfoInputs.cache
index 442bb18..26831e9 100644
--- a/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfoInputs.cache
+++ b/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfoInputs.cache
@@ -1 +1 @@
-314703ce6030ab92b0403937767ee96c9e692da1446a0bca83eb671836323b97
+751e661809e7029162ecad1e1e6ec0d5e40f6b6270e4727ea3241e818e0aec19
diff --git a/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt b/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt
index 107e51f..e3b43b5 100644
--- a/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt
+++ b/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt
@@ -24,3 +24,4 @@
/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/dirt.png
/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/grass.png
/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/rock.png
+/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/bedrock.png
diff --git a/Tiles/obj/Debug/net8.0/Tiles.dll b/Tiles/obj/Debug/net8.0/Tiles.dll
index 0558232..8c96e22 100644
Binary files a/Tiles/obj/Debug/net8.0/Tiles.dll and b/Tiles/obj/Debug/net8.0/Tiles.dll differ
diff --git a/Tiles/obj/Debug/net8.0/Tiles.pdb b/Tiles/obj/Debug/net8.0/Tiles.pdb
index c5e64e1..1e37c2f 100644
Binary files a/Tiles/obj/Debug/net8.0/Tiles.pdb and b/Tiles/obj/Debug/net8.0/Tiles.pdb differ
diff --git a/Tiles/obj/Debug/net8.0/ref/Tiles.dll b/Tiles/obj/Debug/net8.0/ref/Tiles.dll
index 77f1c59..4d09e40 100644
Binary files a/Tiles/obj/Debug/net8.0/ref/Tiles.dll and b/Tiles/obj/Debug/net8.0/ref/Tiles.dll differ
diff --git a/Tiles/obj/Debug/net8.0/refint/Tiles.dll b/Tiles/obj/Debug/net8.0/refint/Tiles.dll
index 77f1c59..4d09e40 100644
Binary files a/Tiles/obj/Debug/net8.0/refint/Tiles.dll and b/Tiles/obj/Debug/net8.0/refint/Tiles.dll differ