diff --git a/.gitignore b/.gitignore index 544ec09..007961d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -.idea/ -Tiles/bin/ -Tiles/obj/ +.idea/ +Tiles/bin/ +Tiles/obj/ diff --git a/.idea/.idea.Tiles/.idea/.gitignore b/.idea/.idea.Tiles/.idea/.gitignore index 4445aaf..5497197 100644 --- a/.idea/.idea.Tiles/.idea/.gitignore +++ b/.idea/.idea.Tiles/.idea/.gitignore @@ -1,13 +1,13 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Rider ignored files -/modules.xml -/projectSettingsUpdater.xml -/.idea.Tiles.iml -/contentModel.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/projectSettingsUpdater.xml +/.idea.Tiles.iml +/contentModel.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.Tiles/.idea/indexLayout.xml b/.idea/.idea.Tiles/.idea/indexLayout.xml index 7b08163..f5a863a 100644 --- a/.idea/.idea.Tiles/.idea/indexLayout.xml +++ b/.idea/.idea.Tiles/.idea/indexLayout.xml @@ -1,8 +1,8 @@ - - - - - - - + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.Tiles/.idea/vcs.xml b/.idea/.idea.Tiles/.idea/vcs.xml index 94a25f7..9661ac7 100644 --- a/.idea/.idea.Tiles/.idea/vcs.xml +++ b/.idea/.idea.Tiles/.idea/vcs.xml @@ -1,6 +1,6 @@ - - - - - + + + + + \ No newline at end of file diff --git a/README.md b/README.md index f3fd97a..02b4b53 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# TILES +# TILES A test project \ No newline at end of file diff --git a/Tiles.sln b/Tiles.sln index cda0d2f..27b2e76 100644 --- a/Tiles.sln +++ b/Tiles.sln @@ -1,16 +1,16 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tiles", "Tiles\Tiles.csproj", "{DB2321A5-36C4-495E-BC59-9C7861A1EB39}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tiles", "Tiles\Tiles.csproj", "{DB2321A5-36C4-495E-BC59-9C7861A1EB39}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Tiles.sln.DotSettings.user b/Tiles.sln.DotSettings.user index ce2970b..1810707 100644 --- a/Tiles.sln.DotSettings.user +++ b/Tiles.sln.DotSettings.user @@ -1,2 +1,2 @@ - + ForceIncluded \ No newline at end of file diff --git a/Tiles/CameraController.cs b/Tiles/CameraController.cs index 15e168e..3760124 100644 --- a/Tiles/CameraController.cs +++ b/Tiles/CameraController.cs @@ -1,37 +1,37 @@ -using System.Numerics; -using Raylib_cs; - -namespace Tiles; - -public class CameraController -{ - private Camera2D _camera2D; - public Camera2D Camera => _camera2D; - - - public CameraController() - { - _camera2D = new Camera2D(); - } - - public void SetCameraTarget(Vector2 target) - { - _camera2D.Target = target; - } - - public void SetCameraOffset(Vector2 offset) - { - _camera2D.Offset = offset; - } - - public void SetCameraZoom(float zoom) - { - _camera2D.Zoom = zoom; - } - - public void SetCameraRotation(float rotation) - { - _camera2D.Rotation = rotation; - } - +using System.Numerics; +using Raylib_cs; + +namespace Tiles; + +public class CameraController +{ + private Camera2D _camera2D; + public Camera2D Camera => _camera2D; + + + public CameraController() + { + _camera2D = new Camera2D(); + } + + public void SetCameraTarget(Vector2 target) + { + _camera2D.Target = target; + } + + public void SetCameraOffset(Vector2 offset) + { + _camera2D.Offset = offset; + } + + public void SetCameraZoom(float zoom) + { + _camera2D.Zoom = zoom; + } + + public void SetCameraRotation(float rotation) + { + _camera2D.Rotation = rotation; + } + } \ No newline at end of file diff --git a/Tiles/DebugUI.cs b/Tiles/DebugUI.cs index 21e4df8..e043343 100644 --- a/Tiles/DebugUI.cs +++ b/Tiles/DebugUI.cs @@ -1,31 +1,31 @@ -using Raylib_cs; - -namespace Tiles; - -public class DebugUI -{ - public void Draw() - { - int textSize = 10; - - int textPositionX = (int)(Game.Instance.CameraController.Camera.Target.X - Game.Instance.ScreenWidth / 2 / Game.Instance.CameraController.Camera.Zoom) + 10; - int textPositionY = (int)(Game.Instance.CameraController.Camera.Target.Y - Game.Instance.ScreenHeight / 2 / Game.Instance.CameraController.Camera.Zoom) + 10; - - string lookingAt = ""; - if (Game.Instance.World.GetTile((int)Game.Instance.Player.PointerPosition.X, (int)Game.Instance.Player.PointerPosition.Y) != 0) - { - lookingAt = Game.Instance.TileManager.GetTileFromId(Game.Instance.World.GetTile((int)Game.Instance.Player.PointerPosition.X, (int)Game.Instance.Player.PointerPosition.Y)).Name; - } - else - { - lookingAt = "air"; - } - - Raylib.DrawText("FPS: " + Raylib.GetFPS(), textPositionX, textPositionY, textSize, Color.White); - Raylib.DrawText(Game.Instance.TileManager.GetTileFromId(Game.Instance.Player.CurrentSelectedTileId).Name, textPositionX, textPositionY + (textSize*2), textSize, Color.White); - Raylib.DrawText("Coords: " + Game.Instance.World.GetGridPositionFromGlobalPosition((int)Game.Instance.Player.Position.X, (int)Game.Instance.Player.Position.Y).ToString().ToUpper(), - textPositionX, textPositionY + (textSize * 3), textSize, Color.White); - Raylib.DrawText("Speed: " + Game.Instance.Player.Speed, textPositionX, textPositionY + (textSize*4), textSize, Color.White); - Raylib.DrawText("Pointer: " + Game.Instance.Player.PointerPosition + " " + lookingAt, textPositionX, textPositionY + (textSize*5), textSize, Color.White); - } +using Raylib_cs; + +namespace Tiles; + +public class DebugUI +{ + public void Draw() + { + int textSize = 10; + + int textPositionX = (int)(Game.Instance.CameraController.Camera.Target.X - Game.Instance.ScreenWidth / 2 / Game.Instance.CameraController.Camera.Zoom) + 10; + int textPositionY = (int)(Game.Instance.CameraController.Camera.Target.Y - Game.Instance.ScreenHeight / 2 / Game.Instance.CameraController.Camera.Zoom) + 10; + + string lookingAt = ""; + if (Game.Instance.World.GetTile((int)Game.Instance.Player.PointerPosition.X, (int)Game.Instance.Player.PointerPosition.Y) != 0) + { + lookingAt = Game.Instance.TileManager.GetTileFromId(Game.Instance.World.GetTile((int)Game.Instance.Player.PointerPosition.X, (int)Game.Instance.Player.PointerPosition.Y)).Name; + } + else + { + lookingAt = "air"; + } + + Raylib.DrawText("FPS: " + Raylib.GetFPS(), textPositionX, textPositionY, textSize, Color.White); + Raylib.DrawText(Game.Instance.TileManager.GetTileFromId(Game.Instance.Player.CurrentSelectedTileId).Name, textPositionX, textPositionY + (textSize*2), textSize, Color.White); + Raylib.DrawText("Coords: " + Game.Instance.World.GetGridPositionFromGlobalPosition((int)Game.Instance.Player.Position.X, (int)Game.Instance.Player.Position.Y).ToString().ToUpper(), + textPositionX, textPositionY + (textSize * 3), textSize, Color.White); + Raylib.DrawText("Speed: " + Game.Instance.Player.Speed, textPositionX, textPositionY + (textSize*4), textSize, Color.White); + Raylib.DrawText("Pointer: " + Game.Instance.Player.PointerPosition + " " + lookingAt, textPositionX, textPositionY + (textSize*5), textSize, Color.White); + } } \ No newline at end of file diff --git a/Tiles/Game.cs b/Tiles/Game.cs index 4184842..8ba2472 100644 --- a/Tiles/Game.cs +++ b/Tiles/Game.cs @@ -1,94 +1,94 @@ -using System.Numerics; -using Raylib_cs; - -namespace Tiles; - -public class Game -{ - public static Game Instance { get; private set; } - - public TileManager TileManager { get; private set; } - public Player Player { get; private set; } - public CameraController CameraController { get; private set; } - public World World { get; private set; } - public DebugUI DebugUi { get; private set; } - - public int ScreenWidth { get; private set; } - public int ScreenHeight { get; private set; } - - public Game(int screenWidth, int screenHeight) - { - ScreenWidth = screenWidth; - ScreenHeight = screenHeight; - - Instance = this; - InitializeRaylib(); - Init(); - - while (!Raylib.WindowShouldClose()) - { - Draw(); - Update(); - } - } - - private void InitializeRaylib() - { - Raylib.InitWindow(ScreenWidth, ScreenHeight, "Tiles"); - Raylib.SetTargetFPS(60); - } - - private void Init() - { - TileManager = new TileManager(); - - World = new World(); - - Player = new Player("Player") - { - Position = World.GetGlobalPositionFromGrid(500, 200) - }; - - CameraController = new CameraController(); - CameraController.SetCameraTarget(Player.Position); - CameraController.SetCameraOffset(new Vector2(ScreenWidth / 2, ScreenHeight / 2)); - CameraController.SetCameraRotation(0f); - CameraController.SetCameraZoom(4f); - - DebugUi = new DebugUI(); - - World.GenerateWorld(); - } - - private void Update() - { - CameraController.SetCameraTarget(Player.Position); - Player.Update(); - } - - private void Draw() - { - Raylib.BeginDrawing(); - Raylib.ClearBackground(new Color(20, 30, 100, 255)); - Raylib.BeginMode2D(CameraController.Camera); - - World.Draw(); - Player.Draw(); - DebugUi.Draw(); - - Raylib.EndMode2D(); - Raylib.EndDrawing(); - } - - - - - - - - - - - - +using System.Numerics; +using Raylib_cs; + +namespace Tiles; + +public class Game +{ + public static Game Instance { get; private set; } + + public TileManager TileManager { get; private set; } + public Player Player { get; private set; } + public CameraController CameraController { get; private set; } + public World World { get; private set; } + public DebugUI DebugUi { get; private set; } + + public int ScreenWidth { get; private set; } + public int ScreenHeight { get; private set; } + + public Game(int screenWidth, int screenHeight) + { + ScreenWidth = screenWidth; + ScreenHeight = screenHeight; + + Instance = this; + InitializeRaylib(); + Init(); + + while (!Raylib.WindowShouldClose()) + { + Draw(); + Update(); + } + } + + private void InitializeRaylib() + { + Raylib.InitWindow(ScreenWidth, ScreenHeight, "Tiles"); + Raylib.SetTargetFPS(60); + } + + private void Init() + { + TileManager = new TileManager(); + + World = new World(); + + Player = new Player("Player") + { + Position = World.GetGlobalPositionFromGrid(500, 200) + }; + + CameraController = new CameraController(); + CameraController.SetCameraTarget(Player.Position); + CameraController.SetCameraOffset(new Vector2(ScreenWidth / 2, ScreenHeight / 2)); + CameraController.SetCameraRotation(0f); + CameraController.SetCameraZoom(4f); + + DebugUi = new DebugUI(); + + World.GenerateWorld(); + } + + private void Update() + { + CameraController.SetCameraTarget(Player.Position); + Player.Update(); + } + + private void Draw() + { + Raylib.BeginDrawing(); + Raylib.ClearBackground(new Color(20, 30, 100, 255)); + Raylib.BeginMode2D(CameraController.Camera); + + World.Draw(); + Player.Draw(); + DebugUi.Draw(); + + Raylib.EndMode2D(); + Raylib.EndDrawing(); + } + + + + + + + + + + + + } \ No newline at end of file diff --git a/Tiles/Player.cs b/Tiles/Player.cs index 8f65576..0d197ea 100644 --- a/Tiles/Player.cs +++ b/Tiles/Player.cs @@ -1,181 +1,181 @@ -using System.Numerics; -using Raylib_cs; - -namespace Tiles; - -public class Player -{ - public int Id { get; private set; } - public string Name { get; private set; } - public Vector2 Position { get; set; } - public float Speed { get; set; } = 2f; - public Texture2D Texture { get; private set; } - public Vector2 PointerPosition { get; private set; } - public bool IsColliding { get; private set; } = false; - public bool IsOnGround { get; private set; } = false; - public float Gravity { get; set; } = 1f; - - private readonly string _imagePath; - - public int CurrentSelectedTileId = 1; - private int _currentSelectedIndex = 0; - - private int _direction = 1; - - private Rectangle _facingRightRect = new Rectangle(0, 0, 16, 24); - private Rectangle _facingLeftRect = new Rectangle(0, 0, -16, 24); - - private Rectangle _currentRect; - - private Vector2 _newPosition = Vector2.Zero; - - public Player(string name, int id = 0, string imagePath = "data/core/player/dude.png") - { - Name = name; - Id = id; - _imagePath = imagePath; - - _currentRect = _facingRightRect; - - InitTexture(); - } - - public void Update() - { - HandleInput(); - HandleMouse(); - } - - public void Draw() - { - //Raylib.DrawTexture(Texture, (int)Position.X, (int)Position.Y, Color.White); - if (_direction == 0) Raylib.DrawTextureRec(Texture, _facingLeftRect, Position, Color.White); - else Raylib.DrawTextureRec(Texture, _facingRightRect, Position, Color.White); - DrawPointer(); - } - - private void InitTexture() - { - Texture = Raylib.LoadTexture(_imagePath); - } - - - - private void HandleGravity() - { - if (IsOnGround) return; - - _newPosition.Y += Gravity; - } - - private void HandleInput() - { - _newPosition = Position; - - // WASD - //if (Raylib.IsKeyDown(KeyboardKey.W)) _newPosition += new Vector2(0, -1) * Speed; - if (Raylib.IsKeyDown(KeyboardKey.A)) - { - _newPosition += new Vector2(-1, 0) * Speed; - _direction = 0; - } - //if (Raylib.IsKeyDown(KeyboardKey.S)) _newPosition += new Vector2(0, 1) * Speed; - if (Raylib.IsKeyDown(KeyboardKey.D)) - { - _newPosition += new Vector2(1, 0) * Speed; - _direction = 1; - } - - HandleGravity(); - HandleCollision(_newPosition); - - if (!IsColliding) - { - Position = _newPosition; - } - - // Speed control - if (Raylib.IsKeyReleased(KeyboardKey.Up)) Speed += 1f; - if (Raylib.IsKeyReleased(KeyboardKey.Down)) Speed -= 1f; - - // Select tiles - if (Raylib.IsKeyReleased(KeyboardKey.Right)) - { - _currentSelectedIndex = (_currentSelectedIndex + 1) % Game.Instance.TileManager.TileNameToIdList.Count; - if (_currentSelectedIndex == 0) - { - _currentSelectedIndex = 1; - } - CurrentSelectedTileId = _currentSelectedIndex; - } - else if (Raylib.IsKeyReleased(KeyboardKey.Left)) - { - _currentSelectedIndex = (_currentSelectedIndex - 1 + Game.Instance.TileManager.TileNameToIdList.Count) % Game.Instance.TileManager.TileNameToIdList.Count; - if (_currentSelectedIndex == 0) - { - _currentSelectedIndex = Game.Instance.TileManager.TileNameToIdList.Count - 1; - } - CurrentSelectedTileId = _currentSelectedIndex; - } - } - - private void HandleMouse() - { - //Mouse clicks - if (Raylib.IsMouseButtonDown(MouseButton.Left)) - { - Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, CurrentSelectedTileId); - } - if (Raylib.IsMouseButtonDown(MouseButton.Right)) - { - Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, 0); - } - } - - private void DrawPointer() - { - Vector2 mousePosition = Raylib.GetMousePosition(); - Vector2 worldPosition = Raylib.GetScreenToWorld2D(mousePosition, Game.Instance.CameraController.Camera); - Vector2 gridPosition = new Vector2((int)(worldPosition.X / Game.Instance.World.TileSize), (int)(worldPosition.Y / Game.Instance.World.TileSize)); - PointerPosition = new Vector2(gridPosition.X, gridPosition.Y); - - Raylib.DrawRectangle((int)PointerPosition.X*Game.Instance.World.TileSize, (int)PointerPosition.Y*Game.Instance.World.TileSize, Game.Instance.World.TileSize, Game.Instance.World.TileSize, new Color(255, 255, 0, 80)); - } - - private void HandleCollision(Vector2 newPosition) - { - var gridPos = Game.Instance.World.GetGridPositionFromGlobalPosition((int)newPosition.X, (int)newPosition.Y); - int tileSize = Game.Instance.World.TileSize; - - int startX = (int)gridPos.X; - int startY = (int)gridPos.Y; - int endX = (int)((newPosition.X + _currentRect.Width) / tileSize); - int endY = (int)((newPosition.Y + _currentRect.Height) / tileSize); - - IsColliding = false; - IsOnGround = false; - - for (int x = startX; x <= endX; x++) - { - for (int y = startY; y <= endY; y++) - { - if (Game.Instance.World.GetTile(x, y) != 0) - { - IsColliding = true; - return; - } - } - } - - // Check if player is on the ground - int groundY = endY + 1; - for (int x = startX; x <= endX; x++) - { - if (Game.Instance.World.GetTile(x, groundY) != 0) - { - IsOnGround = true; - break; - } - } - } +using System.Numerics; +using Raylib_cs; + +namespace Tiles; + +public class Player +{ + public int Id { get; private set; } + public string Name { get; private set; } + public Vector2 Position { get; set; } + public float Speed { get; set; } = 2f; + public Texture2D Texture { get; private set; } + public Vector2 PointerPosition { get; private set; } + public bool IsColliding { get; private set; } = false; + public bool IsOnGround { get; private set; } = false; + public float Gravity { get; set; } = 1f; + + private readonly string _imagePath; + + public int CurrentSelectedTileId = 1; + private int _currentSelectedIndex = 0; + + private int _direction = 1; + + private Rectangle _facingRightRect = new Rectangle(0, 0, 16, 24); + private Rectangle _facingLeftRect = new Rectangle(0, 0, -16, 24); + + private Rectangle _currentRect; + + private Vector2 _newPosition = Vector2.Zero; + + public Player(string name, int id = 0, string imagePath = "data/core/player/dude.png") + { + Name = name; + Id = id; + _imagePath = imagePath; + + _currentRect = _facingRightRect; + + InitTexture(); + } + + public void Update() + { + HandleInput(); + HandleMouse(); + } + + public void Draw() + { + //Raylib.DrawTexture(Texture, (int)Position.X, (int)Position.Y, Color.White); + if (_direction == 0) Raylib.DrawTextureRec(Texture, _facingLeftRect, Position, Color.White); + else Raylib.DrawTextureRec(Texture, _facingRightRect, Position, Color.White); + DrawPointer(); + } + + private void InitTexture() + { + Texture = Raylib.LoadTexture(_imagePath); + } + + + + private void HandleGravity() + { + if (IsOnGround) return; + + _newPosition.Y += Gravity; + } + + private void HandleInput() + { + _newPosition = Position; + + // WASD + //if (Raylib.IsKeyDown(KeyboardKey.W)) _newPosition += new Vector2(0, -1) * Speed; + if (Raylib.IsKeyDown(KeyboardKey.A)) + { + _newPosition += new Vector2(-1, 0) * Speed; + _direction = 0; + } + //if (Raylib.IsKeyDown(KeyboardKey.S)) _newPosition += new Vector2(0, 1) * Speed; + if (Raylib.IsKeyDown(KeyboardKey.D)) + { + _newPosition += new Vector2(1, 0) * Speed; + _direction = 1; + } + + HandleGravity(); + HandleCollision(_newPosition); + + if (!IsColliding) + { + Position = _newPosition; + } + + // Speed control + if (Raylib.IsKeyReleased(KeyboardKey.Up)) Speed += 1f; + if (Raylib.IsKeyReleased(KeyboardKey.Down)) Speed -= 1f; + + // Select tiles + if (Raylib.IsKeyReleased(KeyboardKey.Right)) + { + _currentSelectedIndex = (_currentSelectedIndex + 1) % Game.Instance.TileManager.TileNameToIdList.Count; + if (_currentSelectedIndex == 0) + { + _currentSelectedIndex = 1; + } + CurrentSelectedTileId = _currentSelectedIndex; + } + else if (Raylib.IsKeyReleased(KeyboardKey.Left)) + { + _currentSelectedIndex = (_currentSelectedIndex - 1 + Game.Instance.TileManager.TileNameToIdList.Count) % Game.Instance.TileManager.TileNameToIdList.Count; + if (_currentSelectedIndex == 0) + { + _currentSelectedIndex = Game.Instance.TileManager.TileNameToIdList.Count - 1; + } + CurrentSelectedTileId = _currentSelectedIndex; + } + } + + private void HandleMouse() + { + //Mouse clicks + if (Raylib.IsMouseButtonDown(MouseButton.Left)) + { + Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, CurrentSelectedTileId); + } + if (Raylib.IsMouseButtonDown(MouseButton.Right)) + { + Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, 0); + } + } + + private void DrawPointer() + { + Vector2 mousePosition = Raylib.GetMousePosition(); + Vector2 worldPosition = Raylib.GetScreenToWorld2D(mousePosition, Game.Instance.CameraController.Camera); + Vector2 gridPosition = new Vector2((int)(worldPosition.X / Game.Instance.World.TileSize), (int)(worldPosition.Y / Game.Instance.World.TileSize)); + PointerPosition = new Vector2(gridPosition.X, gridPosition.Y); + + Raylib.DrawRectangle((int)PointerPosition.X*Game.Instance.World.TileSize, (int)PointerPosition.Y*Game.Instance.World.TileSize, Game.Instance.World.TileSize, Game.Instance.World.TileSize, new Color(255, 255, 0, 80)); + } + + private void HandleCollision(Vector2 newPosition) + { + var gridPos = Game.Instance.World.GetGridPositionFromGlobalPosition((int)newPosition.X, (int)newPosition.Y); + int tileSize = Game.Instance.World.TileSize; + + int startX = (int)gridPos.X; + int startY = (int)gridPos.Y; + int endX = (int)((newPosition.X + _currentRect.Width) / tileSize); + int endY = (int)((newPosition.Y + _currentRect.Height) / tileSize); + + IsColliding = false; + IsOnGround = false; + + for (int x = startX; x <= endX; x++) + { + for (int y = startY; y <= endY; y++) + { + if (Game.Instance.World.GetTile(x, y) != 0) + { + IsColliding = true; + return; + } + } + } + + // Check if player is on the ground + int groundY = endY + 1; + for (int x = startX; x <= endX; x++) + { + if (Game.Instance.World.GetTile(x, groundY) != 0) + { + IsOnGround = true; + break; + } + } + } } \ No newline at end of file diff --git a/Tiles/Program.cs b/Tiles/Program.cs index c03e2cf..727e0ff 100644 --- a/Tiles/Program.cs +++ b/Tiles/Program.cs @@ -1,6 +1,6 @@ -// Entry point - -using System.Reflection.PortableExecutable; -using Tiles; - +// 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 2000d87..cd23b0f 100644 --- a/Tiles/Tile.cs +++ b/Tiles/Tile.cs @@ -1,101 +1,101 @@ -using System.Text.Json; -using System.Text.Json.Serialization.Metadata; -using Newtonsoft.Json; -using Raylib_cs; -using JsonSerializer = System.Text.Json.JsonSerializer; - -namespace Tiles; - -public class Tile -{ - 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; } - - private static JsonSerializerOptions _jsonSerializerOptions = new() - { - WriteIndented = true, - TypeInfoResolver = new DefaultJsonTypeInfoResolver() - }; - - 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) - { - try - { - Image img = Raylib.LoadImage(path); - return img; - } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } - } - - private Texture2D LoadTexture(Image 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; } +using System.Text.Json; +using System.Text.Json.Serialization.Metadata; +using Newtonsoft.Json; +using Raylib_cs; +using JsonSerializer = System.Text.Json.JsonSerializer; + +namespace Tiles; + +public class Tile +{ + 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; } + + private static JsonSerializerOptions _jsonSerializerOptions = new() + { + WriteIndented = true, + TypeInfoResolver = new DefaultJsonTypeInfoResolver() + }; + + 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) + { + try + { + Image img = Raylib.LoadImage(path); + return img; + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } + + private Texture2D LoadTexture(Image 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/TileManager.cs b/Tiles/TileManager.cs index 8de9f93..a97718f 100644 --- a/Tiles/TileManager.cs +++ b/Tiles/TileManager.cs @@ -1,67 +1,67 @@ -namespace Tiles; - -public class TileManager -{ - - private Dictionary _tileNameToId; - private Dictionary _tileIdToTile; - - public Dictionary TileNameToIdList => _tileNameToId; - - public TileManager() - { - _tileNameToId = new Dictionary(); - _tileIdToTile = new Dictionary(); - - SetupTiles(); - } - - public void SetupTiles() - { - _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++; - } - } - - public Tile GetTileFromId(int id) - { - if (!_tileIdToTile.ContainsKey(id)) - { - throw new Exception($"Tile {id} not found"); - } - - return _tileIdToTile[id]; - } - - public int GetTileIdFromName(string name) - { - if (!_tileNameToId.ContainsKey(name)) - { - throw new Exception($"Tile {name} not found"); - } - - return _tileNameToId[name]; - } +namespace Tiles; + +public class TileManager +{ + + private Dictionary _tileNameToId; + private Dictionary _tileIdToTile; + + public Dictionary TileNameToIdList => _tileNameToId; + + public TileManager() + { + _tileNameToId = new Dictionary(); + _tileIdToTile = new Dictionary(); + + SetupTiles(); + } + + public void SetupTiles() + { + _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++; + } + } + + public Tile GetTileFromId(int id) + { + if (!_tileIdToTile.ContainsKey(id)) + { + throw new Exception($"Tile {id} not found"); + } + + return _tileIdToTile[id]; + } + + public int GetTileIdFromName(string name) + { + if (!_tileNameToId.ContainsKey(name)) + { + throw new Exception($"Tile {name} not found"); + } + + return _tileNameToId[name]; + } } \ No newline at end of file diff --git a/Tiles/Tiles.csproj b/Tiles/Tiles.csproj index 8d34196..5bb487d 100644 --- a/Tiles/Tiles.csproj +++ b/Tiles/Tiles.csproj @@ -1,21 +1,21 @@ - - - Exe - net8.0 - enable - enable - true - $(PublishFileExclusionsFromSatelliteAssemblies);data/**/*.* - - - - - - - - - - - - + + + Exe + net8.0 + enable + enable + true + $(PublishFileExclusionsFromSatelliteAssemblies);data/**/*.* + + + + + + + + + + + + \ No newline at end of file diff --git a/Tiles/World.cs b/Tiles/World.cs index 41dc938..022613b 100644 --- a/Tiles/World.cs +++ b/Tiles/World.cs @@ -1,112 +1,112 @@ -using System.Numerics; -using Raylib_cs; - -namespace Tiles; - -public class World -{ - public int TileSize { get; } - public int WorldWidth { get; } - public int WorldHeight { get; } - - private Dictionary<(int x, int y), int> _worldGrid; - - public World(int worldWidth = 2000, int worldHeight = 1000, int tileSize = 8) - { - WorldWidth = worldWidth; - WorldHeight = worldHeight; - TileSize = tileSize; - - _worldGrid = new Dictionary<(int x, int y), int>(); - } - - public void Update() - { - - } - - public void Draw() - { - // Calculate the boundaries of the visible region in grid coordinates - int startX = Math.Max((int)(Game.Instance.CameraController.Camera.Target.X - (Game.Instance.ScreenWidth / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, 0); - int startY = Math.Max((int)(Game.Instance.CameraController.Camera.Target.Y - (Game.Instance.ScreenHeight / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, 0); - int endX = Math.Min((int)(Game.Instance.CameraController.Camera.Target.X + (Game.Instance.ScreenWidth / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, WorldWidth - 1); - int endY = Math.Min((int)(Game.Instance.CameraController.Camera.Target.Y + (Game.Instance.ScreenHeight / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, WorldHeight - 1); - - // Loop only through the visible tiles - for (int x = startX; x <= endX; x++) - { - for (int y = startY; y <= endY; y++) - { - (int x, int y) position = new(x, y); - if (_worldGrid.TryGetValue(position, out var tileType) && tileType != 0) - { - Raylib.DrawTexture(Game.Instance.TileManager.GetTileFromId(tileType).Texture, x * TileSize, y * TileSize, Color.White); - } - } - } - } - - public void GenerateWorld() - { - int totalHeight = WorldHeight; - - int grassHeight = (int)(totalHeight * 0.01); - int dirtHeight = (int)(totalHeight * 0.10); - int airHeight = (int)(totalHeight * 0.50); - int rockHeight = totalHeight - (airHeight + grassHeight + dirtHeight); - - for (int x = 0; x < WorldWidth; x++) - { - for (int y = 0; y < totalHeight; y++) - { - string tileType; - - if (y >= airHeight && y < airHeight + grassHeight) - { - tileType = "grass"; - SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); - } - else if (y >= airHeight + grassHeight && y < airHeight + grassHeight + dirtHeight) - { - tileType = "dirt"; - SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); - } - else if (y >= airHeight + grassHeight + dirtHeight) - { - tileType = "rock"; - SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); - } - } - } - } - - public Vector2 GetGridPositionFromGlobalPosition(int x, int y) - { - int gridX = x / TileSize; - int gridY = y / TileSize; - - if (gridX >= 0 && gridX < WorldWidth && gridY >= 0 && gridY < WorldHeight) - { - return new Vector2(gridX, gridY); - } - - return new Vector2(0, 0); - } - - public void SetTile(int x, int y, int id) - { - _worldGrid[(x, y)] = id; - } - - public int GetTile(int x, int y) - { - if (_worldGrid.ContainsKey((x,y))) return _worldGrid[(x,y)]; - return 0; - } - - public Vector2 GetGlobalPositionFromGrid(int x, int y) - { - return new(x * TileSize, y * TileSize); - } +using System.Numerics; +using Raylib_cs; + +namespace Tiles; + +public class World +{ + public int TileSize { get; } + public int WorldWidth { get; } + public int WorldHeight { get; } + + private Dictionary<(int x, int y), int> _worldGrid; + + public World(int worldWidth = 2000, int worldHeight = 1000, int tileSize = 8) + { + WorldWidth = worldWidth; + WorldHeight = worldHeight; + TileSize = tileSize; + + _worldGrid = new Dictionary<(int x, int y), int>(); + } + + public void Update() + { + + } + + public void Draw() + { + // Calculate the boundaries of the visible region in grid coordinates + int startX = Math.Max((int)(Game.Instance.CameraController.Camera.Target.X - (Game.Instance.ScreenWidth / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, 0); + int startY = Math.Max((int)(Game.Instance.CameraController.Camera.Target.Y - (Game.Instance.ScreenHeight / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, 0); + int endX = Math.Min((int)(Game.Instance.CameraController.Camera.Target.X + (Game.Instance.ScreenWidth / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, WorldWidth - 1); + int endY = Math.Min((int)(Game.Instance.CameraController.Camera.Target.Y + (Game.Instance.ScreenHeight / 2 / Game.Instance.CameraController.Camera.Zoom)) / TileSize, WorldHeight - 1); + + // Loop only through the visible tiles + for (int x = startX; x <= endX; x++) + { + for (int y = startY; y <= endY; y++) + { + (int x, int y) position = new(x, y); + if (_worldGrid.TryGetValue(position, out var tileType) && tileType != 0) + { + Raylib.DrawTexture(Game.Instance.TileManager.GetTileFromId(tileType).Texture, x * TileSize, y * TileSize, Color.White); + } + } + } + } + + public void GenerateWorld() + { + int totalHeight = WorldHeight; + + int grassHeight = (int)(totalHeight * 0.01); + int dirtHeight = (int)(totalHeight * 0.10); + int airHeight = (int)(totalHeight * 0.50); + int rockHeight = totalHeight - (airHeight + grassHeight + dirtHeight); + + for (int x = 0; x < WorldWidth; x++) + { + for (int y = 0; y < totalHeight; y++) + { + string tileType; + + if (y >= airHeight && y < airHeight + grassHeight) + { + tileType = "grass"; + SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); + } + else if (y >= airHeight + grassHeight && y < airHeight + grassHeight + dirtHeight) + { + tileType = "dirt"; + SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); + } + else if (y >= airHeight + grassHeight + dirtHeight) + { + tileType = "rock"; + SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); + } + } + } + } + + public Vector2 GetGridPositionFromGlobalPosition(int x, int y) + { + int gridX = x / TileSize; + int gridY = y / TileSize; + + if (gridX >= 0 && gridX < WorldWidth && gridY >= 0 && gridY < WorldHeight) + { + return new Vector2(gridX, gridY); + } + + return new Vector2(0, 0); + } + + public void SetTile(int x, int y, int id) + { + _worldGrid[(x, y)] = id; + } + + public int GetTile(int x, int y) + { + if (_worldGrid.ContainsKey((x,y))) return _worldGrid[(x,y)]; + return 0; + } + + public Vector2 GetGlobalPositionFromGrid(int x, int y) + { + return new(x * TileSize, y * TileSize); + } } \ No newline at end of file diff --git a/Tiles/data/core/tiles/bedrock.json b/Tiles/data/core/tiles/bedrock.json index 67e14f7..5a1ac42 100644 --- a/Tiles/data/core/tiles/bedrock.json +++ b/Tiles/data/core/tiles/bedrock.json @@ -1,5 +1,5 @@ -{ - "Name": "bedrock", - "ImagePath": "data/core/tiles/images/bedrock.png", - "Transparent": false +{ + "Name": "bedrock", + "ImagePath": "data/core/tiles/images/bedrock.png", + "Transparent": false } \ No newline at end of file diff --git a/Tiles/data/core/tiles/dirt.json b/Tiles/data/core/tiles/dirt.json index e137765..0c9c293 100644 --- a/Tiles/data/core/tiles/dirt.json +++ b/Tiles/data/core/tiles/dirt.json @@ -1,5 +1,5 @@ -{ - "Name": "dirt", - "ImagePath": "data/core/tiles/images/dirt.png", - "Transparent": false +{ + "Name": "dirt", + "ImagePath": "data/core/tiles/images/dirt.png", + "Transparent": false } \ No newline at end of file diff --git a/Tiles/data/core/tiles/glass.json b/Tiles/data/core/tiles/glass.json index 51a544e..4d77c3d 100644 --- a/Tiles/data/core/tiles/glass.json +++ b/Tiles/data/core/tiles/glass.json @@ -1,5 +1,5 @@ -{ - "Name": "glass", - "ImagePath": "data/core/tiles/images/glass.png", - "Transparent": false +{ + "Name": "glass", + "ImagePath": "data/core/tiles/images/glass.png", + "Transparent": false } \ No newline at end of file diff --git a/Tiles/data/core/tiles/grass.json b/Tiles/data/core/tiles/grass.json index b257c28..96ae2f9 100644 --- a/Tiles/data/core/tiles/grass.json +++ b/Tiles/data/core/tiles/grass.json @@ -1,5 +1,5 @@ -{ - "Name": "grass", - "ImagePath": "data/core/tiles/images/grass.png", - "Transparent": false +{ + "Name": "grass", + "ImagePath": "data/core/tiles/images/grass.png", + "Transparent": false } \ No newline at end of file diff --git a/Tiles/data/core/tiles/rock.json b/Tiles/data/core/tiles/rock.json index d87b3b9..2281ca6 100644 --- a/Tiles/data/core/tiles/rock.json +++ b/Tiles/data/core/tiles/rock.json @@ -1,5 +1,5 @@ -{ - "Name": "rock", - "ImagePath": "data/core/tiles/images/rock.png", - "Transparent": false +{ + "Name": "rock", + "ImagePath": "data/core/tiles/images/rock.png", + "Transparent": false } \ No newline at end of file diff --git a/Tiles/data/core/tiles/wood.json b/Tiles/data/core/tiles/wood.json index e6f7a84..e03f566 100644 --- a/Tiles/data/core/tiles/wood.json +++ b/Tiles/data/core/tiles/wood.json @@ -1,5 +1,5 @@ -{ - "Name": "wood", - "ImagePath": "data/core/tiles/images/wood.png", - "Transparent": false +{ + "Name": "wood", + "ImagePath": "data/core/tiles/images/wood.png", + "Transparent": false } \ No newline at end of file