Mouse input
This commit is contained in:
parent
6684d62ffb
commit
4e792f1937
@ -13,14 +13,16 @@ public class Game
|
||||
private int _screenHeight;
|
||||
|
||||
private Dictionary<string, Tile> _tiles = new();
|
||||
private Dictionary<Vector2, string> _worldGrid = new();
|
||||
private Dictionary<(int x, int y), string> _worldGrid = new();
|
||||
|
||||
private Vector2 _playerPosition;
|
||||
private float _playerSpeed = 2f;
|
||||
|
||||
private Vector2 _pointerPosition = Vector2.Zero;
|
||||
|
||||
private Camera2D _camera;
|
||||
|
||||
private string _currentSelectedTile = "air";
|
||||
private string _currentSelectedTile = "dirt";
|
||||
private int _currentSelectedIndex = 0;
|
||||
|
||||
public Game(int screenWidth, int screenHeight)
|
||||
@ -37,7 +39,7 @@ public class Game
|
||||
Target = _playerPosition,
|
||||
Offset = new Vector2(_screenWidth / 2, _screenHeight / 2),
|
||||
Rotation = 0f,
|
||||
Zoom = 5f
|
||||
Zoom = 4f
|
||||
};
|
||||
|
||||
SetupTiles();
|
||||
@ -55,6 +57,7 @@ public class Game
|
||||
|
||||
DrawWorld();
|
||||
DrawPlayer();
|
||||
DrawPointer();
|
||||
DrawDebug();
|
||||
|
||||
Raylib.EndMode2D();
|
||||
@ -74,21 +77,23 @@ public class Game
|
||||
_currentSelectedIndex = (_currentSelectedIndex + 1) % _tiles.Count;
|
||||
_currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex);
|
||||
}
|
||||
|
||||
if (Raylib.IsKeyReleased(KeyboardKey.Left))
|
||||
{
|
||||
_currentSelectedIndex = (_currentSelectedIndex - 1) % _tiles.Count;
|
||||
_currentSelectedIndex = (_currentSelectedIndex - 1 + _tiles.Count) % _tiles.Count;
|
||||
_currentSelectedTile = _tiles.Keys.ElementAt(_currentSelectedIndex);
|
||||
}
|
||||
|
||||
if (Raylib.IsKeyReleased(KeyboardKey.Space))
|
||||
if (Raylib.IsMouseButtonDown(MouseButton.Left))
|
||||
{
|
||||
Random random = new();
|
||||
var tile = _tiles.Keys.ElementAt(random.Next(_tiles.Keys.Count));
|
||||
SetTile(GetGridPositionFromGlobalPosition((int)_playerPosition.X, (int)_playerPosition.Y), _currentSelectedTile);
|
||||
SetTile((int)_pointerPosition.X, (int)_pointerPosition.Y, _currentSelectedTile);
|
||||
}
|
||||
if (Raylib.IsMouseButtonDown(MouseButton.Right))
|
||||
{
|
||||
SetTile((int)_pointerPosition.X, (int)_pointerPosition.Y, "air");
|
||||
}
|
||||
|
||||
if (Raylib.IsKeyReleased(KeyboardKey.Zero)) _currentSelectedTile = "air";
|
||||
if (Raylib.IsKeyReleased(KeyboardKey.Up)) _playerSpeed += 1f;
|
||||
if (Raylib.IsKeyReleased(KeyboardKey.Down)) _playerSpeed -= 1f;
|
||||
}
|
||||
|
||||
private void SetupTiles()
|
||||
@ -112,7 +117,7 @@ public class Game
|
||||
{
|
||||
for (int y = startY; y <= endY; y++)
|
||||
{
|
||||
Vector2 position = new(x, y);
|
||||
(int x, int y) position = new(x, y);
|
||||
if (_worldGrid.TryGetValue(position, out var tileType) && tileType != "air")
|
||||
{
|
||||
Raylib.DrawTexture(_tiles[tileType].Texture, x * _tileSize, y * _tileSize, Color.White);
|
||||
@ -124,9 +129,39 @@ public class Game
|
||||
|
||||
private void DrawPlayer()
|
||||
{
|
||||
Raylib.DrawRectangle((int)_playerPosition.X, (int)_playerPosition.Y, _tileSize, _tileSize, new(255,255,0,100));
|
||||
Raylib.DrawRectangle((int)_playerPosition.X, (int)_playerPosition.Y, 16, 32, new(0,0,100,255));
|
||||
}
|
||||
|
||||
private void DrawPointer()
|
||||
{
|
||||
// Get the mouse position in screen space
|
||||
Vector2 mousePosition = Raylib.GetMousePosition();
|
||||
|
||||
// Convert screen space to world space using the camera transformation
|
||||
Vector2 worldPosition = Raylib.GetScreenToWorld2D(mousePosition, _camera);
|
||||
|
||||
// Calculate the pointer's grid coordinates based on the world position
|
||||
Vector2 gridPosition = new Vector2(
|
||||
(int)(worldPosition.X / _tileSize),
|
||||
(int)(worldPosition.Y / _tileSize)
|
||||
);
|
||||
|
||||
// Update _pointerPosition to reflect top-left corner of that tile
|
||||
_pointerPosition = new Vector2(
|
||||
gridPosition.X,
|
||||
gridPosition.Y
|
||||
);
|
||||
|
||||
// Draw a semi-transparent tile over the current grid tile
|
||||
Raylib.DrawRectangle(
|
||||
(int)_pointerPosition.X*_tileSize,
|
||||
(int)_pointerPosition.Y*_tileSize,
|
||||
_tileSize,
|
||||
_tileSize,
|
||||
new Color(255, 255, 0, 80)
|
||||
);
|
||||
}
|
||||
|
||||
private void DrawDebug()
|
||||
{
|
||||
int textSize = 10;
|
||||
@ -135,30 +170,19 @@ public class Game
|
||||
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(),
|
||||
Raylib.DrawText("FPS: " + Raylib.GetFPS(), textPositionX, textPositionY, textSize, Color.White);
|
||||
Raylib.DrawText(_currentSelectedTile, textPositionX, textPositionY + (textSize*2), textSize, Color.White);
|
||||
Raylib.DrawText("Coords: " + GetGridPositionFromGlobalPosition((int)_playerPosition.X, (int)_playerPosition.Y).ToString().ToUpper(),
|
||||
textPositionX, textPositionY + (textSize * 3), textSize, Color.White);
|
||||
Raylib.DrawText("FPS: " + Raylib.GetFPS(), textPositionX, textPositionY + (textSize*4), textSize, Color.White);
|
||||
Raylib.DrawText("Speed: " + _playerSpeed, textPositionX, textPositionY + (textSize*4), textSize, Color.White);
|
||||
Raylib.DrawText("Pointer: " + _pointerPosition, textPositionX, textPositionY + (textSize*5), textSize, Color.White);
|
||||
}
|
||||
|
||||
|
||||
// private void GenerateWorld()
|
||||
// {
|
||||
// for (int x = 0; x < _worldWidth; x++)
|
||||
// {
|
||||
// for (int y = 0; y < _worldHeight; y++)
|
||||
// {
|
||||
// _worldGrid.Add(new(x, y), "dirt");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
private void GenerateWorld()
|
||||
{
|
||||
int totalHeight = _worldHeight;
|
||||
|
||||
int grassHeight = (int)(totalHeight * 0.05);
|
||||
int grassHeight = (int)(totalHeight * 0.01);
|
||||
int dirtHeight = (int)(totalHeight * 0.10);
|
||||
int airHeight = (int)(totalHeight * 0.50);
|
||||
int rockHeight = totalHeight - (airHeight + grassHeight + dirtHeight);
|
||||
@ -178,35 +202,31 @@ public class Game
|
||||
else
|
||||
tileType = "rock";
|
||||
|
||||
_worldGrid.Add(new(x, y), tileType);
|
||||
SetTile(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)
|
||||
private void SetTile(int x, int y, string tileType)
|
||||
{
|
||||
_worldGrid[position] = tileType;
|
||||
_worldGrid[(x,y)] = tileType;
|
||||
}
|
||||
|
||||
private Vector2 GetGlobalPositionFromGrid(int x, int y)
|
||||
{
|
||||
return new Vector2(x * _tileSize, y * _tileSize);
|
||||
return new(x * _tileSize, y * _tileSize);
|
||||
}
|
||||
}
|
@ -7,9 +7,18 @@ 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 static Tile GenerateTileFromJson()
|
||||
// {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static void GenerateTileJson()
|
||||
// {
|
||||
//
|
||||
// }
|
||||
|
||||
public Tile(string name, string pathToImage, bool transparent = false)
|
||||
{
|
||||
Name = name;
|
||||
|
Binary file not shown.
Binary file not shown.
@ -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+75228367d0d87d3758b20be7fd09e3038af1f1c7")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+6684d62ffbd0721e0f81217b6e1b3cb98446390d")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Tiles")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Tiles")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
@ -1 +1 @@
|
||||
cae709149bdcf00c3bc3e20eb638da90c936807ebc10f2db91589fe9edf764da
|
||||
b89132a91ec091559cc8bb94b85559caa7664e4ff5e5cd825e0f6a40705633fb
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user