Added camera, added ability to change tiles, added 'player', basic input, etc

This commit is contained in:
Chris Bell 2024-11-16 23:33:57 -06:00
parent ea6267d407
commit 75228367d0
15 changed files with 134 additions and 13 deletions

View File

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

View File

@ -0,0 +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">
<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

@ -6,8 +6,8 @@ namespace Tiles;
public class Game
{
const int _tileSize = 8;
const int _worldWidth = 1920/8;
const int _worldHeight = 1080/8;
const int _worldWidth = 1000;
const int _worldHeight = 1000;
private int _screenWidth;
private int _screenHeight;
@ -15,12 +15,30 @@ public class Game
private Dictionary<string, Tile> _tiles = new();
private Dictionary<Vector2, string> _worldGrid = new();
private Vector2 _playerPosition;
private float _playerSpeed = 2f;
private Camera2D _camera;
private string _currentSelectedTile = "air";
private int _currentSelectedIndex = 0;
public Game(int screenWidth, int screenHeight)
{
_screenWidth = screenWidth;
_screenHeight = screenHeight;
Raylib.InitWindow(_screenWidth, _screenHeight, "Tiles");
Raylib.SetTargetFPS(60);
_playerPosition = GetGlobalPositionFromGrid(_worldWidth/2, _worldHeight/2);
_camera = new Camera2D()
{
Target = _playerPosition,
Offset = new Vector2(_screenWidth / 2, _screenHeight / 2),
Rotation = 0f,
Zoom = 5f
};
SetupTiles();
@ -28,36 +46,100 @@ public class Game
while (!Raylib.WindowShouldClose())
{
HandleInput();
_camera.Target = _playerPosition;
Raylib.BeginDrawing();
Raylib.ClearBackground(new Color(20, 70, 100, 255));
Raylib.BeginMode2D(_camera);
DrawWorld();
DrawPlayer();
DrawDebug();
Raylib.EndMode2D();
Raylib.EndDrawing();
}
}
private void HandleInput()
{
if (Raylib.IsKeyDown(KeyboardKey.W)) _playerPosition += new Vector2(0, -1) * _playerSpeed;
if (Raylib.IsKeyDown(KeyboardKey.A)) _playerPosition += new Vector2(-1, 0) * _playerSpeed;
if (Raylib.IsKeyDown(KeyboardKey.S)) _playerPosition += new Vector2(0, 1) * _playerSpeed;
if (Raylib.IsKeyDown(KeyboardKey.D)) _playerPosition += new Vector2(1, 0) * _playerSpeed;
if (Raylib.IsKeyReleased(KeyboardKey.Right))
{
_currentSelectedIndex = (_currentSelectedIndex + 1) % _tiles.Count;
_currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex);
}
if (Raylib.IsKeyReleased(KeyboardKey.Left))
{
_currentSelectedIndex = (_currentSelectedIndex - 1) % _tiles.Count;
_currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex);
}
if (Raylib.IsKeyReleased(KeyboardKey.Space))
{
Random random = new();
var tile = _tiles.Keys.ElementAt(random.Next(_tiles.Keys.Count));
SetTile(GetGridPositionFromGlobalPosition((int)_playerPosition.X, (int)_playerPosition.Y), _currentSelectedTile);
}
if (Raylib.IsKeyReleased(KeyboardKey.Zero)) _currentSelectedTile = "air";
}
private void SetupTiles()
{
_tiles.Add("dirt", new("dirt", "data/tiles/images/dirt.png"));
_tiles.Add("grass", new("grass", "data/tiles/images/grass.png"));
_tiles.Add("rock", new("rock", "data/tiles/images/rock.png"));
_tiles.Add("bedrock", new("bedrock", "data/tiles/images/bedrock.png"));
}
private void DrawWorld()
{
foreach (var tile in _worldGrid)
// Calculate the boundaries of the visible region in grid coordinates
int startX = Math.Max((int)(_camera.Target.X - (_screenWidth / 2 / _camera.Zoom)) / _tileSize, 0);
int startY = Math.Max((int)(_camera.Target.Y - (_screenHeight / 2 / _camera.Zoom)) / _tileSize, 0);
int endX = Math.Min((int)(_camera.Target.X + (_screenWidth / 2 / _camera.Zoom)) / _tileSize, _worldWidth - 1);
int endY = Math.Min((int)(_camera.Target.Y + (_screenHeight / 2 / _camera.Zoom)) / _tileSize, _worldHeight - 1);
// Loop only through the visible tiles
for (int x = startX; x <= endX; x++)
{
if (tile.Value == "air")
for (int y = startY; y <= endY; y++)
{
Raylib.DrawRectangle((int)tile.Key.X * _tileSize,(int)tile.Key.Y * _tileSize, _tileSize, _tileSize, new(0,0,0,0));
}
else
{
Raylib.DrawTexture(_tiles[tile.Value].Texture, (int)tile.Key.X * _tileSize,(int)tile.Key.Y * _tileSize, Color.White);
Vector2 position = new(x, y);
if (_worldGrid.TryGetValue(position, out var tileType) && tileType != "air")
{
Raylib.DrawTexture(_tiles[tileType].Texture, x * _tileSize, y * _tileSize, Color.White);
}
}
}
}
private void DrawPlayer()
{
Raylib.DrawRectangle((int)_playerPosition.X, (int)_playerPosition.Y, _tileSize, _tileSize, new(255,255,0,100));
}
private void DrawDebug()
{
int textSize = 10;
// Position the text relative to the camera's offset
int textPositionX = (int)(_camera.Target.X - _screenWidth / 2 / _camera.Zoom) + 10;
int textPositionY = (int)(_camera.Target.Y - _screenHeight / 2 / _camera.Zoom) + 10;
Raylib.DrawText(_currentSelectedTile, textPositionX, textPositionY, textSize, Color.White);
Raylib.DrawText("Global: " + _playerPosition.ToString(), textPositionX, textPositionY + (textSize*2), textSize, Color.White);
Raylib.DrawText("Grid: " + GetGridPositionFromGlobalPosition((int)_playerPosition.X, (int)_playerPosition.Y).ToString().ToUpper(),
textPositionX, textPositionY + (textSize * 3), textSize, Color.White);
}
// private void GenerateWorld()
@ -98,5 +180,32 @@ public class Game
_worldGrid.Add(new(x, y), tileType);
}
}
_worldGrid[new(0,72)] = "air";
}
private Vector2 GetGridPositionFromGlobalPosition(int x, int y)
{
int gridX = x / _tileSize;
int gridY = y / _tileSize;
// Check if the calculated grid position is within the bounds of the world grid
if (gridX >= 0 && gridX < _worldWidth && gridY >= 0 && gridY < _worldHeight)
{
return new Vector2(gridX, gridY);
}
// Return (0, 0) as a failsafe if outside the world grid
return new Vector2(0, 0);
}
private void SetTile(Vector2 position, string tileType)
{
_worldGrid[position] = tileType;
}
private Vector2 GetGlobalPositionFromGrid(int x, int y)
{
return new Vector2(x * _tileSize, y * _tileSize);
}
}

View File

@ -7,12 +7,15 @@ public class Tile
public string Name { get; private set; } = string.Empty;
public Image Image { get; private set; }
public Texture2D Texture { get; private set; }
public bool Transparent { get; private set; }
public Tile(string name, string pathToImage)
public Tile(string name, string pathToImage, bool transparent = false)
{
Name = name;
Image = LoadImage(pathToImage);
Texture = LoadTexture(Image);
Transparent = transparent;
}
private Image LoadImage(string path)

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Tiles")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ea6267d407dae7d4981288325746fe1899ea0d97")]
[assembly: System.Reflection.AssemblyProductAttribute("Tiles")]
[assembly: System.Reflection.AssemblyTitleAttribute("Tiles")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
314703ce6030ab92b0403937767ee96c9e692da1446a0bca83eb671836323b97
751e661809e7029162ecad1e1e6ec0d5e40f6b6270e4727ea3241e818e0aec19

View File

@ -24,3 +24,4 @@
/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/dirt.png
/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/grass.png
/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/rock.png
/home/chris/mnt/data/Projects/tiles/Tiles/Tiles/bin/Debug/net8.0/data/tiles/images/bedrock.png

Binary file not shown.

Binary file not shown.

Binary file not shown.