Load tiles from json
139
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<string, Tile> _tiles = new();
|
||||
private Dictionary<(int x, int y), string> _worldGrid = new();
|
||||
|
||||
private Dictionary<string, int> _tileNameToId = new();
|
||||
private Dictionary<int, Tile> _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()
|
||||
{
|
||||
@ -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);
|
||||
Vector2 gridPosition = new Vector2((int)(worldPosition.X / _tileSize), (int)(worldPosition.Y / _tileSize));
|
||||
_pointerPosition = new Vector2(gridPosition.X, gridPosition.Y);
|
||||
|
||||
// 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)
|
||||
);
|
||||
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()
|
||||
@ -193,16 +205,21 @@ public class Game
|
||||
{
|
||||
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)
|
||||
|
@ -1,5 +1,6 @@
|
||||
// Entry point
|
||||
|
||||
using System.Reflection.PortableExecutable;
|
||||
using Tiles;
|
||||
|
||||
Game game = new(1920, 1080);
|
@ -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<TileData>(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; }
|
||||
}
|
@ -16,4 +16,8 @@
|
||||
<Content Include="data/**/*.*" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="data\core\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 674 B After Width: | Height: | Size: 674 B |
Before Width: | Height: | Size: 654 B After Width: | Height: | Size: 654 B |
Before Width: | Height: | Size: 678 B After Width: | Height: | Size: 678 B |
Before Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 674 B |
Before Width: | Height: | Size: 654 B |
Before Width: | Height: | Size: 678 B |
@ -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")]
|
||||
|
@ -1 +1 @@
|
||||
b89132a91ec091559cc8bb94b85559caa7664e4ff5e5cd825e0f6a40705633fb
|
||||
878acde2fee11315f28b912e289b8837693e33e44d350f59ac560f4d3c05e70f
|
||||
|
@ -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
|
||||
|