Compare commits

..

No commits in common. "laptop-dev" and "master" have entirely different histories.

22 changed files with 711 additions and 711 deletions

6
.gitignore vendored
View File

@ -1,3 +1,3 @@
.idea/ .idea/
Tiles/bin/ Tiles/bin/
Tiles/obj/ Tiles/obj/

View File

@ -1,13 +1,13 @@
# Default ignored files # Default ignored files
/shelf/ /shelf/
/workspace.xml /workspace.xml
# Rider ignored files # Rider ignored files
/modules.xml /modules.xml
/projectSettingsUpdater.xml /projectSettingsUpdater.xml
/.idea.Tiles.iml /.idea.Tiles.iml
/contentModel.xml /contentModel.xml
# Editor-based HTTP Client requests # Editor-based HTTP Client requests
/httpRequests/ /httpRequests/
# Datasource local storage ignored files # Datasource local storage ignored files
/dataSources/ /dataSources/
/dataSources.local.xml /dataSources.local.xml

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="UserContentModel"> <component name="UserContentModel">
<attachedFolders /> <attachedFolders />
<explicitIncludes /> <explicitIncludes />
<explicitExcludes /> <explicitExcludes />
</component> </component>
</project> </project>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
</component> </component>
</project> </project>

View File

@ -1,2 +1,2 @@
# TILES # TILES
A test project A test project

View File

@ -1,16 +1,16 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tiles", "Tiles\Tiles.csproj", "{DB2321A5-36C4-495E-BC59-9C7861A1EB39}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tiles", "Tiles\Tiles.csproj", "{DB2321A5-36C4-495E-BC59-9C7861A1EB39}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU Release|Any CPU = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {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}.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.ActiveCfg = Release|Any CPU
{DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Release|Any CPU.Build.0 = Release|Any CPU {DB2321A5-36C4-495E-BC59-9C7861A1EB39}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -1,2 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARaylib_002EUtils_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F1ab29dab0f18748176ed7b8e5fd3add278f2db3f2cfdcdc333bb22bc853c683_003FRaylib_002EUtils_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARaylib_002EUtils_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F1ab29dab0f18748176ed7b8e5fd3add278f2db3f2cfdcdc333bb22bc853c683_003FRaylib_002EUtils_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

View File

@ -1,37 +1,37 @@
using System.Numerics; using System.Numerics;
using Raylib_cs; using Raylib_cs;
namespace Tiles; namespace Tiles;
public class CameraController public class CameraController
{ {
private Camera2D _camera2D; private Camera2D _camera2D;
public Camera2D Camera => _camera2D; public Camera2D Camera => _camera2D;
public CameraController() public CameraController()
{ {
_camera2D = new Camera2D(); _camera2D = new Camera2D();
} }
public void SetCameraTarget(Vector2 target) public void SetCameraTarget(Vector2 target)
{ {
_camera2D.Target = target; _camera2D.Target = target;
} }
public void SetCameraOffset(Vector2 offset) public void SetCameraOffset(Vector2 offset)
{ {
_camera2D.Offset = offset; _camera2D.Offset = offset;
} }
public void SetCameraZoom(float zoom) public void SetCameraZoom(float zoom)
{ {
_camera2D.Zoom = zoom; _camera2D.Zoom = zoom;
} }
public void SetCameraRotation(float rotation) public void SetCameraRotation(float rotation)
{ {
_camera2D.Rotation = rotation; _camera2D.Rotation = rotation;
} }
} }

View File

@ -1,31 +1,31 @@
using Raylib_cs; using Raylib_cs;
namespace Tiles; namespace Tiles;
public class DebugUI public class DebugUI
{ {
public void Draw() public void Draw()
{ {
int textSize = 10; int textSize = 10;
int textPositionX = (int)(Game.Instance.CameraController.Camera.Target.X - Game.Instance.ScreenWidth / 2 / Game.Instance.CameraController.Camera.Zoom) + 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; int textPositionY = (int)(Game.Instance.CameraController.Camera.Target.Y - Game.Instance.ScreenHeight / 2 / Game.Instance.CameraController.Camera.Zoom) + 10;
string lookingAt = ""; string lookingAt = "";
if (Game.Instance.World.GetTile((int)Game.Instance.Player.PointerPosition.X, (int)Game.Instance.Player.PointerPosition.Y) != 0) 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; lookingAt = Game.Instance.TileManager.GetTileFromId(Game.Instance.World.GetTile((int)Game.Instance.Player.PointerPosition.X, (int)Game.Instance.Player.PointerPosition.Y)).Name;
} }
else else
{ {
lookingAt = "air"; lookingAt = "air";
} }
Raylib.DrawText("FPS: " + Raylib.GetFPS(), textPositionX, textPositionY, textSize, Color.White); 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(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(), 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); textPositionX, textPositionY + (textSize * 3), textSize, Color.White);
Raylib.DrawText("Speed: " + Game.Instance.Player.Speed, textPositionX, textPositionY + (textSize*4), 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); Raylib.DrawText("Pointer: " + Game.Instance.Player.PointerPosition + " " + lookingAt, textPositionX, textPositionY + (textSize*5), textSize, Color.White);
} }
} }

View File

@ -1,94 +1,94 @@
using System.Numerics; using System.Numerics;
using Raylib_cs; using Raylib_cs;
namespace Tiles; namespace Tiles;
public class Game public class Game
{ {
public static Game Instance { get; private set; } public static Game Instance { get; private set; }
public TileManager TileManager { get; private set; } public TileManager TileManager { get; private set; }
public Player Player { get; private set; } public Player Player { get; private set; }
public CameraController CameraController { get; private set; } public CameraController CameraController { get; private set; }
public World World { get; private set; } public World World { get; private set; }
public DebugUI DebugUi { get; private set; } public DebugUI DebugUi { get; private set; }
public int ScreenWidth { get; private set; } public int ScreenWidth { get; private set; }
public int ScreenHeight { get; private set; } public int ScreenHeight { get; private set; }
public Game(int screenWidth, int screenHeight) public Game(int screenWidth, int screenHeight)
{ {
ScreenWidth = screenWidth; ScreenWidth = screenWidth;
ScreenHeight = screenHeight; ScreenHeight = screenHeight;
Instance = this; Instance = this;
InitializeRaylib(); InitializeRaylib();
Init(); Init();
while (!Raylib.WindowShouldClose()) while (!Raylib.WindowShouldClose())
{ {
Draw(); Draw();
Update(); Update();
} }
} }
private void InitializeRaylib() private void InitializeRaylib()
{ {
Raylib.InitWindow(ScreenWidth, ScreenHeight, "Tiles"); Raylib.InitWindow(ScreenWidth, ScreenHeight, "Tiles");
Raylib.SetTargetFPS(60); Raylib.SetTargetFPS(60);
} }
private void Init() private void Init()
{ {
TileManager = new TileManager(); TileManager = new TileManager();
World = new World(); World = new World();
Player = new Player("Player") Player = new Player("Player")
{ {
Position = World.GetGlobalPositionFromGrid(500, 200) Position = World.GetGlobalPositionFromGrid(500, 200)
}; };
CameraController = new CameraController(); CameraController = new CameraController();
CameraController.SetCameraTarget(Player.Position); CameraController.SetCameraTarget(Player.Position);
CameraController.SetCameraOffset(new Vector2(ScreenWidth / 2, ScreenHeight / 2)); CameraController.SetCameraOffset(new Vector2(ScreenWidth / 2, ScreenHeight / 2));
CameraController.SetCameraRotation(0f); CameraController.SetCameraRotation(0f);
CameraController.SetCameraZoom(4f); CameraController.SetCameraZoom(4f);
DebugUi = new DebugUI(); DebugUi = new DebugUI();
World.GenerateWorld(); World.GenerateWorld();
} }
private void Update() private void Update()
{ {
CameraController.SetCameraTarget(Player.Position); CameraController.SetCameraTarget(Player.Position);
Player.Update(); Player.Update();
} }
private void Draw() private void Draw()
{ {
Raylib.BeginDrawing(); Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(20, 30, 100, 255)); Raylib.ClearBackground(new Color(20, 30, 100, 255));
Raylib.BeginMode2D(CameraController.Camera); Raylib.BeginMode2D(CameraController.Camera);
World.Draw(); World.Draw();
Player.Draw(); Player.Draw();
DebugUi.Draw(); DebugUi.Draw();
Raylib.EndMode2D(); Raylib.EndMode2D();
Raylib.EndDrawing(); Raylib.EndDrawing();
} }
} }

View File

@ -1,181 +1,181 @@
using System.Numerics; using System.Numerics;
using Raylib_cs; using Raylib_cs;
namespace Tiles; namespace Tiles;
public class Player public class Player
{ {
public int Id { get; private set; } public int Id { get; private set; }
public string Name { get; private set; } public string Name { get; private set; }
public Vector2 Position { get; set; } public Vector2 Position { get; set; }
public float Speed { get; set; } = 2f; public float Speed { get; set; } = 2f;
public Texture2D Texture { get; private set; } public Texture2D Texture { get; private set; }
public Vector2 PointerPosition { get; private set; } public Vector2 PointerPosition { get; private set; }
public bool IsColliding { get; private set; } = false; public bool IsColliding { get; private set; } = false;
public bool IsOnGround { get; private set; } = false; public bool IsOnGround { get; private set; } = false;
public float Gravity { get; set; } = 1f; public float Gravity { get; set; } = 1f;
private readonly string _imagePath; private readonly string _imagePath;
public int CurrentSelectedTileId = 1; public int CurrentSelectedTileId = 1;
private int _currentSelectedIndex = 0; private int _currentSelectedIndex = 0;
private int _direction = 1; private int _direction = 1;
private Rectangle _facingRightRect = new Rectangle(0, 0, 16, 24); private Rectangle _facingRightRect = new Rectangle(0, 0, 16, 24);
private Rectangle _facingLeftRect = new Rectangle(0, 0, -16, 24); private Rectangle _facingLeftRect = new Rectangle(0, 0, -16, 24);
private Rectangle _currentRect; private Rectangle _currentRect;
private Vector2 _newPosition = Vector2.Zero; private Vector2 _newPosition = Vector2.Zero;
public Player(string name, int id = 0, string imagePath = "data/core/player/dude.png") public Player(string name, int id = 0, string imagePath = "data/core/player/dude.png")
{ {
Name = name; Name = name;
Id = id; Id = id;
_imagePath = imagePath; _imagePath = imagePath;
_currentRect = _facingRightRect; _currentRect = _facingRightRect;
InitTexture(); InitTexture();
} }
public void Update() public void Update()
{ {
HandleInput(); HandleInput();
HandleMouse(); HandleMouse();
} }
public void Draw() public void Draw()
{ {
//Raylib.DrawTexture(Texture, (int)Position.X, (int)Position.Y, Color.White); //Raylib.DrawTexture(Texture, (int)Position.X, (int)Position.Y, Color.White);
if (_direction == 0) Raylib.DrawTextureRec(Texture, _facingLeftRect, Position, Color.White); if (_direction == 0) Raylib.DrawTextureRec(Texture, _facingLeftRect, Position, Color.White);
else Raylib.DrawTextureRec(Texture, _facingRightRect, Position, Color.White); else Raylib.DrawTextureRec(Texture, _facingRightRect, Position, Color.White);
DrawPointer(); DrawPointer();
} }
private void InitTexture() private void InitTexture()
{ {
Texture = Raylib.LoadTexture(_imagePath); Texture = Raylib.LoadTexture(_imagePath);
} }
private void HandleGravity() private void HandleGravity()
{ {
if (IsOnGround) return; if (IsOnGround) return;
_newPosition.Y += Gravity; _newPosition.Y += Gravity;
} }
private void HandleInput() private void HandleInput()
{ {
_newPosition = Position; _newPosition = Position;
// WASD // WASD
//if (Raylib.IsKeyDown(KeyboardKey.W)) _newPosition += new Vector2(0, -1) * Speed; //if (Raylib.IsKeyDown(KeyboardKey.W)) _newPosition += new Vector2(0, -1) * Speed;
if (Raylib.IsKeyDown(KeyboardKey.A)) if (Raylib.IsKeyDown(KeyboardKey.A))
{ {
_newPosition += new Vector2(-1, 0) * Speed; _newPosition += new Vector2(-1, 0) * Speed;
_direction = 0; _direction = 0;
} }
//if (Raylib.IsKeyDown(KeyboardKey.S)) _newPosition += new Vector2(0, 1) * Speed; //if (Raylib.IsKeyDown(KeyboardKey.S)) _newPosition += new Vector2(0, 1) * Speed;
if (Raylib.IsKeyDown(KeyboardKey.D)) if (Raylib.IsKeyDown(KeyboardKey.D))
{ {
_newPosition += new Vector2(1, 0) * Speed; _newPosition += new Vector2(1, 0) * Speed;
_direction = 1; _direction = 1;
} }
HandleGravity(); HandleGravity();
HandleCollision(_newPosition); HandleCollision(_newPosition);
if (!IsColliding) if (!IsColliding)
{ {
Position = _newPosition; Position = _newPosition;
} }
// Speed control // Speed control
if (Raylib.IsKeyReleased(KeyboardKey.Up)) Speed += 1f; if (Raylib.IsKeyReleased(KeyboardKey.Up)) Speed += 1f;
if (Raylib.IsKeyReleased(KeyboardKey.Down)) Speed -= 1f; if (Raylib.IsKeyReleased(KeyboardKey.Down)) Speed -= 1f;
// Select tiles // Select tiles
if (Raylib.IsKeyReleased(KeyboardKey.Right)) if (Raylib.IsKeyReleased(KeyboardKey.Right))
{ {
_currentSelectedIndex = (_currentSelectedIndex + 1) % Game.Instance.TileManager.TileNameToIdList.Count; _currentSelectedIndex = (_currentSelectedIndex + 1) % Game.Instance.TileManager.TileNameToIdList.Count;
if (_currentSelectedIndex == 0) if (_currentSelectedIndex == 0)
{ {
_currentSelectedIndex = 1; _currentSelectedIndex = 1;
} }
CurrentSelectedTileId = _currentSelectedIndex; CurrentSelectedTileId = _currentSelectedIndex;
} }
else if (Raylib.IsKeyReleased(KeyboardKey.Left)) else if (Raylib.IsKeyReleased(KeyboardKey.Left))
{ {
_currentSelectedIndex = (_currentSelectedIndex - 1 + Game.Instance.TileManager.TileNameToIdList.Count) % Game.Instance.TileManager.TileNameToIdList.Count; _currentSelectedIndex = (_currentSelectedIndex - 1 + Game.Instance.TileManager.TileNameToIdList.Count) % Game.Instance.TileManager.TileNameToIdList.Count;
if (_currentSelectedIndex == 0) if (_currentSelectedIndex == 0)
{ {
_currentSelectedIndex = Game.Instance.TileManager.TileNameToIdList.Count - 1; _currentSelectedIndex = Game.Instance.TileManager.TileNameToIdList.Count - 1;
} }
CurrentSelectedTileId = _currentSelectedIndex; CurrentSelectedTileId = _currentSelectedIndex;
} }
} }
private void HandleMouse() private void HandleMouse()
{ {
//Mouse clicks //Mouse clicks
if (Raylib.IsMouseButtonDown(MouseButton.Left)) if (Raylib.IsMouseButtonDown(MouseButton.Left))
{ {
Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, CurrentSelectedTileId); Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, CurrentSelectedTileId);
} }
if (Raylib.IsMouseButtonDown(MouseButton.Right)) if (Raylib.IsMouseButtonDown(MouseButton.Right))
{ {
Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, 0); Game.Instance.World.SetTile((int)PointerPosition.X, (int)PointerPosition.Y, 0);
} }
} }
private void DrawPointer() private void DrawPointer()
{ {
Vector2 mousePosition = Raylib.GetMousePosition(); Vector2 mousePosition = Raylib.GetMousePosition();
Vector2 worldPosition = Raylib.GetScreenToWorld2D(mousePosition, Game.Instance.CameraController.Camera); 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)); 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); 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)); 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) private void HandleCollision(Vector2 newPosition)
{ {
var gridPos = Game.Instance.World.GetGridPositionFromGlobalPosition((int)newPosition.X, (int)newPosition.Y); var gridPos = Game.Instance.World.GetGridPositionFromGlobalPosition((int)newPosition.X, (int)newPosition.Y);
int tileSize = Game.Instance.World.TileSize; int tileSize = Game.Instance.World.TileSize;
int startX = (int)gridPos.X; int startX = (int)gridPos.X;
int startY = (int)gridPos.Y; int startY = (int)gridPos.Y;
int endX = (int)((newPosition.X + _currentRect.Width) / tileSize); int endX = (int)((newPosition.X + _currentRect.Width) / tileSize);
int endY = (int)((newPosition.Y + _currentRect.Height) / tileSize); int endY = (int)((newPosition.Y + _currentRect.Height) / tileSize);
IsColliding = false; IsColliding = false;
IsOnGround = false; IsOnGround = false;
for (int x = startX; x <= endX; x++) for (int x = startX; x <= endX; x++)
{ {
for (int y = startY; y <= endY; y++) for (int y = startY; y <= endY; y++)
{ {
if (Game.Instance.World.GetTile(x, y) != 0) if (Game.Instance.World.GetTile(x, y) != 0)
{ {
IsColliding = true; IsColliding = true;
return; return;
} }
} }
} }
// Check if player is on the ground // Check if player is on the ground
int groundY = endY + 1; int groundY = endY + 1;
for (int x = startX; x <= endX; x++) for (int x = startX; x <= endX; x++)
{ {
if (Game.Instance.World.GetTile(x, groundY) != 0) if (Game.Instance.World.GetTile(x, groundY) != 0)
{ {
IsOnGround = true; IsOnGround = true;
break; break;
} }
} }
} }
} }

View File

@ -1,6 +1,6 @@
// Entry point // Entry point
using System.Reflection.PortableExecutable; using System.Reflection.PortableExecutable;
using Tiles; using Tiles;
Game game = new(1920, 1080); Game game = new(1920, 1080);

View File

@ -1,101 +1,101 @@
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization.Metadata; using System.Text.Json.Serialization.Metadata;
using Newtonsoft.Json; using Newtonsoft.Json;
using Raylib_cs; using Raylib_cs;
using JsonSerializer = System.Text.Json.JsonSerializer; using JsonSerializer = System.Text.Json.JsonSerializer;
namespace Tiles; namespace Tiles;
public class Tile public class Tile
{ {
public string Name { get; private set; } public string Name { get; private set; }
public string ImagePath { get; private set; } public string ImagePath { get; private set; }
public Image Image { get; private set; } public Image Image { get; private set; }
public Texture2D Texture { get; private set; } public Texture2D Texture { get; private set; }
public bool Transparent { get; private set; } public bool Transparent { get; private set; }
private static JsonSerializerOptions _jsonSerializerOptions = new() private static JsonSerializerOptions _jsonSerializerOptions = new()
{ {
WriteIndented = true, WriteIndented = true,
TypeInfoResolver = new DefaultJsonTypeInfoResolver() TypeInfoResolver = new DefaultJsonTypeInfoResolver()
}; };
public static Tile GenerateTileFromJson(string jsonPath) public static Tile GenerateTileFromJson(string jsonPath)
{ {
try try
{ {
var json = File.ReadAllText(jsonPath); var json = File.ReadAllText(jsonPath);
var tileData = JsonSerializer.Deserialize<TileData>(json); var tileData = JsonSerializer.Deserialize<TileData>(json);
if (tileData == null) throw new Exception($"Could not deserialize tile data from {jsonPath}"); if (tileData == null) throw new Exception($"Could not deserialize tile data from {jsonPath}");
var tile = new Tile() var tile = new Tile()
{ {
Name = tileData.Name, Name = tileData.Name,
ImagePath = tileData.ImagePath, ImagePath = tileData.ImagePath,
Transparent = tileData.Transparent, Transparent = tileData.Transparent,
}; };
tile.Image = tile.LoadImage(tile.ImagePath); tile.Image = tile.LoadImage(tile.ImagePath);
tile.Texture = tile.LoadTexture(tile.Image); tile.Texture = tile.LoadTexture(tile.Image);
return tile; return tile;
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"Error loading tile from {jsonPath}: {e.Message}"); Console.WriteLine($"Error loading tile from {jsonPath}: {e.Message}");
throw; throw;
} }
} }
public static void GenerateTileJson(string outputDir, string name, string imagePath, bool transparent = false) public static void GenerateTileJson(string outputDir, string name, string imagePath, bool transparent = false)
{ {
var tile = new TileData() var tile = new TileData()
{ {
Name = name, Name = name,
ImagePath = imagePath, ImagePath = imagePath,
Transparent = transparent Transparent = transparent
}; };
string json = JsonSerializer.Serialize(tile, _jsonSerializerOptions); string json = JsonSerializer.Serialize(tile, _jsonSerializerOptions);
File.WriteAllText(outputDir + name + ".json", json); File.WriteAllText(outputDir + name + ".json", json);
Console.WriteLine($"Tile JSON saved to {outputDir + name + ".json"}"); Console.WriteLine($"Tile JSON saved to {outputDir + name + ".json"}");
} }
private Image LoadImage(string path) private Image LoadImage(string path)
{ {
try try
{ {
Image img = Raylib.LoadImage(path); Image img = Raylib.LoadImage(path);
return img; return img;
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); Console.WriteLine(e);
throw; throw;
} }
} }
private Texture2D LoadTexture(Image img) private Texture2D LoadTexture(Image img)
{ {
try try
{ {
var texture = Raylib.LoadTextureFromImage(img); var texture = Raylib.LoadTextureFromImage(img);
return texture; return texture;
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"Failed to load texture: {e}"); Console.WriteLine($"Failed to load texture: {e}");
throw; throw;
} }
} }
} }
[Serializable] [Serializable]
public class TileData public class TileData
{ {
public string Name { get; set; } public string Name { get; set; }
public string ImagePath { get; set; } public string ImagePath { get; set; }
public bool Transparent { get; set; } public bool Transparent { get; set; }
} }

View File

@ -1,67 +1,67 @@
namespace Tiles; namespace Tiles;
public class TileManager public class TileManager
{ {
private Dictionary<string, int> _tileNameToId; private Dictionary<string, int> _tileNameToId;
private Dictionary<int, Tile> _tileIdToTile; private Dictionary<int, Tile> _tileIdToTile;
public Dictionary<string, int> TileNameToIdList => _tileNameToId; public Dictionary<string, int> TileNameToIdList => _tileNameToId;
public TileManager() public TileManager()
{ {
_tileNameToId = new Dictionary<string, int>(); _tileNameToId = new Dictionary<string, int>();
_tileIdToTile = new Dictionary<int, Tile>(); _tileIdToTile = new Dictionary<int, Tile>();
SetupTiles(); SetupTiles();
} }
public void SetupTiles() public void SetupTiles()
{ {
_tileNameToId.Add("air", 0); _tileNameToId.Add("air", 0);
int index = 1; int index = 1;
foreach (var path in Directory.GetFiles("data/core/tiles/", "*.json")) foreach (var path in Directory.GetFiles("data/core/tiles/", "*.json"))
{ {
Tile tile; Tile tile;
try try
{ {
tile = Tile.GenerateTileFromJson(path); tile = Tile.GenerateTileFromJson(path);
if (tile == null) throw new Exception($"Couldn't load tile {path}"); if (tile == null) throw new Exception($"Couldn't load tile {path}");
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); Console.WriteLine(e);
throw; throw;
} }
Console.WriteLine($"Tile {tile.Name} loaded"); Console.WriteLine($"Tile {tile.Name} loaded");
_tileIdToTile.Add(index, tile); _tileIdToTile.Add(index, tile);
_tileNameToId.Add(tile.Name, index); _tileNameToId.Add(tile.Name, index);
index++; index++;
} }
} }
public Tile GetTileFromId(int id) public Tile GetTileFromId(int id)
{ {
if (!_tileIdToTile.ContainsKey(id)) if (!_tileIdToTile.ContainsKey(id))
{ {
throw new Exception($"Tile {id} not found"); throw new Exception($"Tile {id} not found");
} }
return _tileIdToTile[id]; return _tileIdToTile[id];
} }
public int GetTileIdFromName(string name) public int GetTileIdFromName(string name)
{ {
if (!_tileNameToId.ContainsKey(name)) if (!_tileNameToId.ContainsKey(name))
{ {
throw new Exception($"Tile {name} not found"); throw new Exception($"Tile {name} not found");
} }
return _tileNameToId[name]; return _tileNameToId[name];
} }
} }

View File

@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile> <PublishSingleFile>true</PublishSingleFile>
<PublishFileExclusionsFromSatelliteAssemblies>$(PublishFileExclusionsFromSatelliteAssemblies);data/**/*.*</PublishFileExclusionsFromSatelliteAssemblies> <PublishFileExclusionsFromSatelliteAssemblies>$(PublishFileExclusionsFromSatelliteAssemblies);data/**/*.*</PublishFileExclusionsFromSatelliteAssemblies>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Raylib-cs" Version="6.1.1" /> <PackageReference Include="Raylib-cs" Version="6.1.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="data/**/*.*" CopyToOutputDirectory="Always" /> </ItemGroup> <Content Include="data/**/*.*" CopyToOutputDirectory="Always" /> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="data\core\" /> <Folder Include="data\core\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,112 +1,112 @@
using System.Numerics; using System.Numerics;
using Raylib_cs; using Raylib_cs;
namespace Tiles; namespace Tiles;
public class World public class World
{ {
public int TileSize { get; } public int TileSize { get; }
public int WorldWidth { get; } public int WorldWidth { get; }
public int WorldHeight { get; } public int WorldHeight { get; }
private Dictionary<(int x, int y), int> _worldGrid; private Dictionary<(int x, int y), int> _worldGrid;
public World(int worldWidth = 2000, int worldHeight = 1000, int tileSize = 8) public World(int worldWidth = 2000, int worldHeight = 1000, int tileSize = 8)
{ {
WorldWidth = worldWidth; WorldWidth = worldWidth;
WorldHeight = worldHeight; WorldHeight = worldHeight;
TileSize = tileSize; TileSize = tileSize;
_worldGrid = new Dictionary<(int x, int y), int>(); _worldGrid = new Dictionary<(int x, int y), int>();
} }
public void Update() public void Update()
{ {
} }
public void Draw() public void Draw()
{ {
// Calculate the boundaries of the visible region in grid coordinates // 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 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 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 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); 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 // Loop only through the visible tiles
for (int x = startX; x <= endX; x++) for (int x = startX; x <= endX; x++)
{ {
for (int y = startY; y <= endY; y++) for (int y = startY; y <= endY; y++)
{ {
(int x, int y) position = new(x, y); (int x, int y) position = new(x, y);
if (_worldGrid.TryGetValue(position, out var tileType) && tileType != 0) if (_worldGrid.TryGetValue(position, out var tileType) && tileType != 0)
{ {
Raylib.DrawTexture(Game.Instance.TileManager.GetTileFromId(tileType).Texture, x * TileSize, y * TileSize, Color.White); Raylib.DrawTexture(Game.Instance.TileManager.GetTileFromId(tileType).Texture, x * TileSize, y * TileSize, Color.White);
} }
} }
} }
} }
public void GenerateWorld() public void GenerateWorld()
{ {
int totalHeight = WorldHeight; int totalHeight = WorldHeight;
int grassHeight = (int)(totalHeight * 0.01); int grassHeight = (int)(totalHeight * 0.01);
int dirtHeight = (int)(totalHeight * 0.10); int dirtHeight = (int)(totalHeight * 0.10);
int airHeight = (int)(totalHeight * 0.50); int airHeight = (int)(totalHeight * 0.50);
int rockHeight = totalHeight - (airHeight + grassHeight + dirtHeight); int rockHeight = totalHeight - (airHeight + grassHeight + dirtHeight);
for (int x = 0; x < WorldWidth; x++) for (int x = 0; x < WorldWidth; x++)
{ {
for (int y = 0; y < totalHeight; y++) for (int y = 0; y < totalHeight; y++)
{ {
string tileType; string tileType;
if (y >= airHeight && y < airHeight + grassHeight) if (y >= airHeight && y < airHeight + grassHeight)
{ {
tileType = "grass"; tileType = "grass";
SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType));
} }
else if (y >= airHeight + grassHeight && y < airHeight + grassHeight + dirtHeight) else if (y >= airHeight + grassHeight && y < airHeight + grassHeight + dirtHeight)
{ {
tileType = "dirt"; tileType = "dirt";
SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType));
} }
else if (y >= airHeight + grassHeight + dirtHeight) else if (y >= airHeight + grassHeight + dirtHeight)
{ {
tileType = "rock"; tileType = "rock";
SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType)); SetTile(x, y, Game.Instance.TileManager.GetTileIdFromName(tileType));
} }
} }
} }
} }
public Vector2 GetGridPositionFromGlobalPosition(int x, int y) public Vector2 GetGridPositionFromGlobalPosition(int x, int y)
{ {
int gridX = x / TileSize; int gridX = x / TileSize;
int gridY = y / TileSize; int gridY = y / TileSize;
if (gridX >= 0 && gridX < WorldWidth && gridY >= 0 && gridY < WorldHeight) if (gridX >= 0 && gridX < WorldWidth && gridY >= 0 && gridY < WorldHeight)
{ {
return new Vector2(gridX, gridY); return new Vector2(gridX, gridY);
} }
return new Vector2(0, 0); return new Vector2(0, 0);
} }
public void SetTile(int x, int y, int id) public void SetTile(int x, int y, int id)
{ {
_worldGrid[(x, y)] = id; _worldGrid[(x, y)] = id;
} }
public int GetTile(int x, int y) public int GetTile(int x, int y)
{ {
if (_worldGrid.ContainsKey((x,y))) return _worldGrid[(x,y)]; if (_worldGrid.ContainsKey((x,y))) return _worldGrid[(x,y)];
return 0; return 0;
} }
public Vector2 GetGlobalPositionFromGrid(int x, int y) public Vector2 GetGlobalPositionFromGrid(int x, int y)
{ {
return new(x * TileSize, y * TileSize); return new(x * TileSize, y * TileSize);
} }
} }

View File

@ -1,5 +1,5 @@
{ {
"Name": "bedrock", "Name": "bedrock",
"ImagePath": "data/core/tiles/images/bedrock.png", "ImagePath": "data/core/tiles/images/bedrock.png",
"Transparent": false "Transparent": false
} }

View File

@ -1,5 +1,5 @@
{ {
"Name": "dirt", "Name": "dirt",
"ImagePath": "data/core/tiles/images/dirt.png", "ImagePath": "data/core/tiles/images/dirt.png",
"Transparent": false "Transparent": false
} }

View File

@ -1,5 +1,5 @@
{ {
"Name": "glass", "Name": "glass",
"ImagePath": "data/core/tiles/images/glass.png", "ImagePath": "data/core/tiles/images/glass.png",
"Transparent": false "Transparent": false
} }

View File

@ -1,5 +1,5 @@
{ {
"Name": "grass", "Name": "grass",
"ImagePath": "data/core/tiles/images/grass.png", "ImagePath": "data/core/tiles/images/grass.png",
"Transparent": false "Transparent": false
} }

View File

@ -1,5 +1,5 @@
{ {
"Name": "rock", "Name": "rock",
"ImagePath": "data/core/tiles/images/rock.png", "ImagePath": "data/core/tiles/images/rock.png",
"Transparent": false "Transparent": false
} }

View File

@ -1,5 +1,5 @@
{ {
"Name": "wood", "Name": "wood",
"ImagePath": "data/core/tiles/images/wood.png", "ImagePath": "data/core/tiles/images/wood.png",
"Transparent": false "Transparent": false
} }