Files
Tiles/Tiles/World.cs
2024-11-18 17:42:48 -06:00

112 lines
3.8 KiB
C#

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);
}
}