collision and gravity test
This commit is contained in:
parent
6d9628e7b1
commit
6839bdd05b
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,3 +1,3 @@
|
||||
.idea/
|
||||
Tiles/bin/
|
||||
Tiles/obj/
|
||||
.idea/
|
||||
Tiles/bin/
|
||||
Tiles/obj/
|
||||
|
26
.idea/.idea.Tiles/.idea/.gitignore
vendored
26
.idea/.idea.Tiles/.idea/.gitignore
vendored
@ -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
|
||||
|
@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="UserContentModel">
|
||||
<attachedFolders />
|
||||
<explicitIncludes />
|
||||
<explicitExcludes />
|
||||
</component>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="UserContentModel">
|
||||
<attachedFolders />
|
||||
<explicitIncludes />
|
||||
<explicitExcludes />
|
||||
</component>
|
||||
</project>
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
32
Tiles.sln
32
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
|
||||
|
@ -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>
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
186
Tiles/Game.cs
186
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
360
Tiles/Player.cs
360
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
200
Tiles/Tile.cs
200
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<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)
|
||||
{
|
||||
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<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)
|
||||
{
|
||||
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; }
|
||||
}
|
@ -1,67 +1,67 @@
|
||||
namespace Tiles;
|
||||
|
||||
public class TileManager
|
||||
{
|
||||
|
||||
private Dictionary<string, int> _tileNameToId;
|
||||
private Dictionary<int, Tile> _tileIdToTile;
|
||||
|
||||
public Dictionary<string, int> TileNameToIdList => _tileNameToId;
|
||||
|
||||
public TileManager()
|
||||
{
|
||||
_tileNameToId = new Dictionary<string, int>();
|
||||
_tileIdToTile = new Dictionary<int, Tile>();
|
||||
|
||||
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<string, int> _tileNameToId;
|
||||
private Dictionary<int, Tile> _tileIdToTile;
|
||||
|
||||
public Dictionary<string, int> TileNameToIdList => _tileNameToId;
|
||||
|
||||
public TileManager()
|
||||
{
|
||||
_tileNameToId = new Dictionary<string, int>();
|
||||
_tileIdToTile = new Dictionary<int, Tile>();
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
@ -1,21 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishSingleFile>true</PublishSingleFile>
|
||||
<PublishFileExclusionsFromSatelliteAssemblies>$(PublishFileExclusionsFromSatelliteAssemblies);data/**/*.*</PublishFileExclusionsFromSatelliteAssemblies>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Raylib-cs" Version="6.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="data/**/*.*" CopyToOutputDirectory="Always" /> </ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="data\core\" />
|
||||
</ItemGroup>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishSingleFile>true</PublishSingleFile>
|
||||
<PublishFileExclusionsFromSatelliteAssemblies>$(PublishFileExclusionsFromSatelliteAssemblies);data/**/*.*</PublishFileExclusionsFromSatelliteAssemblies>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Raylib-cs" Version="6.1.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="data/**/*.*" CopyToOutputDirectory="Always" /> </ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="data\core\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
222
Tiles/World.cs
222
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);
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user