181 lines
5.6 KiB
C#
181 lines
5.6 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
} |