diff --git a/Tiles/Game.cs b/Tiles/Game.cs index 4ce1245..02a0fee 100644 --- a/Tiles/Game.cs +++ b/Tiles/Game.cs @@ -7,13 +7,15 @@ public class Game { const int _tileSize = 8; const int _worldWidth = 2000; - const int _worldHeight = 2000; + const int _worldHeight = 1000; private int _screenWidth; private int _screenHeight; - private Dictionary _tiles = new(); - private Dictionary<(int x, int y), string> _worldGrid = new(); + + private Dictionary _tileNameToId = new(); + private Dictionary _tileIdToTile = new(); + private Dictionary<(int x, int y), int> _worldGrid = new(); private Vector2 _playerPosition; private float _playerSpeed = 2f; @@ -22,7 +24,7 @@ public class Game private Camera2D _camera; - private string _currentSelectedTile = "dirt"; + private int _currentSelectedTileId = 1; private int _currentSelectedIndex = 0; public Game(int screenWidth, int screenHeight) @@ -32,7 +34,7 @@ public class Game Raylib.InitWindow(_screenWidth, _screenHeight, "Tiles"); Raylib.SetTargetFPS(60); - _playerPosition = GetGlobalPositionFromGrid(_worldWidth/2, _worldHeight/2); + _playerPosition = GetGlobalPositionFromGrid(500, 500); _camera = new Camera2D() { @@ -44,7 +46,7 @@ public class Game SetupTiles(); - GenerateWorld(); + GenerateWorld(); while (!Raylib.WindowShouldClose()) { @@ -72,24 +74,24 @@ public class Game 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) % _tiles.Count; - _currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex); - } + // if (Raylib.IsKeyReleased(KeyboardKey.Right)) + // { + // _currentSelectedIndex = (_currentSelectedIndex + 1) % _tiles.Count; + // _currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex); + // } + // if (Raylib.IsKeyReleased(KeyboardKey.Left)) + // { + // _currentSelectedIndex = (_currentSelectedIndex - 1 + _tileIdToTile.Count) % _tileIdToTile.Count; + // _currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex); + // } if (Raylib.IsMouseButtonDown(MouseButton.Left)) { - SetTile((int)_pointerPosition.X, (int)_pointerPosition.Y, _currentSelectedTile); + SetTile((int)_pointerPosition.X, (int)_pointerPosition.Y, _currentSelectedTileId); } if (Raylib.IsMouseButtonDown(MouseButton.Right)) { - SetTile((int)_pointerPosition.X, (int)_pointerPosition.Y, "air"); + SetTile((int)_pointerPosition.X, (int)_pointerPosition.Y, 0); } if (Raylib.IsKeyReleased(KeyboardKey.Up)) _playerSpeed += 1f; @@ -98,10 +100,31 @@ public class Game 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")); + _tileNameToId.Add("air", 0); + + int index = 1; + + foreach (var path in Directory.GetFiles("data/core/tiles/", "*.json")) + { + Tile tile; + + try + { + tile = Tile.GenerateTileFromJson(path); + + if (tile == null) throw new Exception($"Couldn't load tile {path}"); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + + Console.WriteLine($"Tile {tile.Name} loaded"); + _tileIdToTile.Add(index, tile); + _tileNameToId.Add(tile.Name, index); + index++; + } } private void DrawWorld() @@ -118,9 +141,9 @@ public class Game for (int y = startY; y <= endY; y++) { (int x, int y) position = new(x, y); - if (_worldGrid.TryGetValue(position, out var tileType) && tileType != "air") + if (_worldGrid.TryGetValue(position, out var tileType) && tileType != 0) { - Raylib.DrawTexture(_tiles[tileType].Texture, x * _tileSize, y * _tileSize, Color.White); + Raylib.DrawTexture(_tileIdToTile[tileType].Texture, x * _tileSize, y * _tileSize, Color.White); } } } @@ -134,48 +157,37 @@ public class Game private void DrawPointer() { - // Get the mouse position in screen space Vector2 mousePosition = Raylib.GetMousePosition(); - - // Convert screen space to world space using the camera transformation Vector2 worldPosition = Raylib.GetScreenToWorld2D(mousePosition, _camera); - - // Calculate the pointer's grid coordinates based on the world position - Vector2 gridPosition = new Vector2( - (int)(worldPosition.X / _tileSize), - (int)(worldPosition.Y / _tileSize) - ); - - // Update _pointerPosition to reflect top-left corner of that tile - _pointerPosition = new Vector2( - gridPosition.X, - gridPosition.Y - ); - - // Draw a semi-transparent tile over the current grid tile - Raylib.DrawRectangle( - (int)_pointerPosition.X*_tileSize, - (int)_pointerPosition.Y*_tileSize, - _tileSize, - _tileSize, - new Color(255, 255, 0, 80) - ); + Vector2 gridPosition = new Vector2((int)(worldPosition.X / _tileSize), (int)(worldPosition.Y / _tileSize)); + _pointerPosition = new Vector2(gridPosition.X, gridPosition.Y); + + Raylib.DrawRectangle((int)_pointerPosition.X*_tileSize, (int)_pointerPosition.Y*_tileSize, _tileSize, _tileSize, new Color(255, 255, 0, 80)); } 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; + + string lookingAt = ""; + if (GetTile((int)_pointerPosition.X, (int)_pointerPosition.Y) != 0) + { + lookingAt = _tileIdToTile[GetTile((int)_pointerPosition.X, (int)_pointerPosition.Y)].Name; + } + else + { + lookingAt = "air"; + } Raylib.DrawText("FPS: " + Raylib.GetFPS(), textPositionX, textPositionY, textSize, Color.White); - Raylib.DrawText(_currentSelectedTile, textPositionX, textPositionY + (textSize*2), textSize, Color.White); + Raylib.DrawText(_tileIdToTile[_currentSelectedTileId].Name, textPositionX, textPositionY + (textSize*2), textSize, Color.White); Raylib.DrawText("Coords: " + GetGridPositionFromGlobalPosition((int)_playerPosition.X, (int)_playerPosition.Y).ToString().ToUpper(), textPositionX, textPositionY + (textSize * 3), textSize, Color.White); Raylib.DrawText("Speed: " + _playerSpeed, textPositionX, textPositionY + (textSize*4), textSize, Color.White); - Raylib.DrawText("Pointer: " + _pointerPosition, textPositionX, textPositionY + (textSize*5), textSize, Color.White); + Raylib.DrawText("Pointer: " + _pointerPosition + " " + lookingAt, textPositionX, textPositionY + (textSize*5), textSize, Color.White); } private void GenerateWorld() @@ -192,17 +204,22 @@ public class Game for (int y = 0; y < totalHeight; y++) { string tileType; - - if (y < airHeight) - tileType = "air"; - else if (y < airHeight + grassHeight) + + if (y >= airHeight && y < airHeight + grassHeight) + { tileType = "grass"; - else if (y < airHeight + grassHeight + dirtHeight) + SetTile(x, y, _tileNameToId[tileType]); + } + else if (y >= airHeight + grassHeight && y < airHeight + grassHeight + dirtHeight) + { tileType = "dirt"; - else + SetTile(x, y, _tileNameToId[tileType]); + } + else if (y >= airHeight + grassHeight + dirtHeight) + { tileType = "rock"; - - SetTile(x, y, tileType); + SetTile(x, y, _tileNameToId[tileType]); + } } } } @@ -220,9 +237,15 @@ public class Game return new Vector2(0, 0); } - private void SetTile(int x, int y, string tileType) + private void SetTile(int x, int y, int id) { - _worldGrid[(x,y)] = tileType; + _worldGrid[(x, y)] = id; + } + + private int GetTile(int x, int y) + { + if (_worldGrid.ContainsKey((x,y))) return _worldGrid[(x,y)]; + return 0; } private Vector2 GetGlobalPositionFromGrid(int x, int y) diff --git a/Tiles/Program.cs b/Tiles/Program.cs index 8eb5900..c03e2cf 100644 --- a/Tiles/Program.cs +++ b/Tiles/Program.cs @@ -1,5 +1,6 @@ // Entry point +using System.Reflection.PortableExecutable; using Tiles; Game game = new(1920, 1080); \ No newline at end of file diff --git a/Tiles/Tile.cs b/Tiles/Tile.cs index c8ee61f..a436340 100644 --- a/Tiles/Tile.cs +++ b/Tiles/Tile.cs @@ -1,30 +1,64 @@ +using System.Text.Json; +using Newtonsoft.Json; using Raylib_cs; +using JsonSerializer = System.Text.Json.JsonSerializer; namespace Tiles; public class Tile { - public string Name { get; private set; } = string.Empty; + public string Name { get; private set; } + public string ImagePath { get; private set; } public Image Image { get; private set; } public Texture2D Texture { get; private set; } public bool Transparent { get; private set; } - // public static Tile GenerateTileFromJson() - // { - // - // } - // - // public static void GenerateTileJson() - // { - // - // } - - public Tile(string name, string pathToImage, bool transparent = false) + private static JsonSerializerOptions _jsonSerializerOptions = new() { - Name = name; - Image = LoadImage(pathToImage); - Texture = LoadTexture(Image); - Transparent = transparent; + WriteIndented = true + }; + + public static Tile GenerateTileFromJson(string jsonPath) + { + try + { + var json = File.ReadAllText(jsonPath); + var tileData = JsonSerializer.Deserialize(json); + + if (tileData == null) throw new Exception($"Could not deserialize tile data from {jsonPath}"); + + var tile = new Tile() + { + Name = tileData.Name, + ImagePath = tileData.ImagePath, + Transparent = tileData.Transparent, + }; + + tile.Image = tile.LoadImage(tile.ImagePath); + tile.Texture = tile.LoadTexture(tile.Image); + + return tile; + } + catch (Exception e) + { + Console.WriteLine($"Error loading tile from {jsonPath}: {e.Message}"); + throw; + } + } + + public static void GenerateTileJson(string outputDir, string name, string imagePath, bool transparent = false) + { + var tile = new TileData() + { + Name = name, + ImagePath = imagePath, + Transparent = transparent + }; + + string json = JsonSerializer.Serialize(tile, _jsonSerializerOptions); + + File.WriteAllText(outputDir + name + ".json", json); + Console.WriteLine($"Tile JSON saved to {outputDir + name + ".json"}"); } private Image LoadImage(string path) @@ -43,6 +77,23 @@ public class Tile private Texture2D LoadTexture(Image img) { - return Raylib.LoadTextureFromImage(img); + try + { + var texture = Raylib.LoadTextureFromImage(img); + return texture; + } + catch (Exception e) + { + Console.WriteLine($"Failed to load texture: {e}"); + throw; + } } +} + +[Serializable] +public class TileData +{ + public string Name { get; set; } + public string ImagePath { get; set; } + public bool Transparent { get; set; } } \ No newline at end of file diff --git a/Tiles/Tiles.csproj b/Tiles/Tiles.csproj index bf55cb8..3971e1d 100644 --- a/Tiles/Tiles.csproj +++ b/Tiles/Tiles.csproj @@ -15,5 +15,9 @@ + + + + diff --git a/Tiles/bin/Debug/net8.0/Tiles.dll b/Tiles/bin/Debug/net8.0/Tiles.dll index 4cbeb67..6403b3e 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 c5d764c..06a4bad 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/data/core/tiles/images/bedrock.png similarity index 100% rename from Tiles/bin/Debug/net8.0/data/tiles/images/bedrock.png rename to Tiles/data/core/tiles/images/bedrock.png diff --git a/Tiles/bin/Debug/net8.0/data/tiles/images/dirt.png b/Tiles/data/core/tiles/images/dirt.png similarity index 100% rename from Tiles/bin/Debug/net8.0/data/tiles/images/dirt.png rename to Tiles/data/core/tiles/images/dirt.png diff --git a/Tiles/bin/Debug/net8.0/data/tiles/images/grass.png b/Tiles/data/core/tiles/images/grass.png similarity index 100% rename from Tiles/bin/Debug/net8.0/data/tiles/images/grass.png rename to Tiles/data/core/tiles/images/grass.png diff --git a/Tiles/bin/Debug/net8.0/data/tiles/images/rock.png b/Tiles/data/core/tiles/images/rock.png similarity index 100% rename from Tiles/bin/Debug/net8.0/data/tiles/images/rock.png rename to Tiles/data/core/tiles/images/rock.png diff --git a/Tiles/data/tiles/images/bedrock.png b/Tiles/data/tiles/images/bedrock.png deleted file mode 100644 index d5410b1..0000000 Binary files a/Tiles/data/tiles/images/bedrock.png and /dev/null differ diff --git a/Tiles/data/tiles/images/dirt.png b/Tiles/data/tiles/images/dirt.png deleted file mode 100644 index 745819f..0000000 Binary files a/Tiles/data/tiles/images/dirt.png and /dev/null differ diff --git a/Tiles/data/tiles/images/grass.png b/Tiles/data/tiles/images/grass.png deleted file mode 100644 index 72d92f8..0000000 Binary files a/Tiles/data/tiles/images/grass.png and /dev/null differ diff --git a/Tiles/data/tiles/images/rock.png b/Tiles/data/tiles/images/rock.png deleted file mode 100644 index 1c7c2a5..0000000 Binary files a/Tiles/data/tiles/images/rock.png and /dev/null differ diff --git a/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfo.cs b/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfo.cs index bcf3764..95c4bbd 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+6684d62ffbd0721e0f81217b6e1b3cb98446390d")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4e792f1937f7dc281b5a56279f8f427a8b6b0e88")] [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 58259a2..f34a6f1 100644 --- a/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfoInputs.cache +++ b/Tiles/obj/Debug/net8.0/Tiles.AssemblyInfoInputs.cache @@ -1 +1 @@ -b89132a91ec091559cc8bb94b85559caa7664e4ff5e5cd825e0f6a40705633fb +878acde2fee11315f28b912e289b8837693e33e44d350f59ac560f4d3c05e70f diff --git a/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt b/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt index e3b43b5..bfafaca 100644 --- a/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt +++ b/Tiles/obj/Debug/net8.0/Tiles.csproj.FileListAbsolute.txt @@ -21,7 +21,11 @@ /home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/runtimes/win-x86/native/raylib.dll /home/chris/mnt/data/Projects/tiles/Tiles/Tiles/obj/Debug/net8.0/Tiles.csproj.AssemblyReference.cache /home/chris/mnt/data/Projects/tiles/Tiles/Tiles/obj/Debug/net8.0/Tiles.csproj.Up2Date -/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 +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/images/bedrock.png +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/images/dirt.png +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/images/grass.png +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/images/rock.png +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/bedrock.json +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/dirt.json +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/grass.json +/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/core/tiles/rock.json diff --git a/Tiles/obj/Debug/net8.0/Tiles.dll b/Tiles/obj/Debug/net8.0/Tiles.dll index 4cbeb67..6403b3e 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 c5d764c..06a4bad 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 9e87990..48c867e 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 9e87990..48c867e 100644 Binary files a/Tiles/obj/Debug/net8.0/refint/Tiles.dll and b/Tiles/obj/Debug/net8.0/refint/Tiles.dll differ