feat: camera following player
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
using BakeryGame.Components.Common;
|
||||
using BakeryGame.Components.Player;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Systems.Player;
|
||||
|
||||
public class CameraSystem : ISystem
|
||||
{
|
||||
private Filter _filter;
|
||||
|
||||
public CameraSystem(World world)
|
||||
{
|
||||
World = world;
|
||||
}
|
||||
|
||||
public World World { get; set; }
|
||||
|
||||
|
||||
public void OnAwake()
|
||||
{
|
||||
_filter = World.Filter.With<CameraComponent>().Build();
|
||||
}
|
||||
|
||||
public void OnUpdate(float deltaTime)
|
||||
{
|
||||
foreach (var entity in _filter)
|
||||
{
|
||||
var position = entity.GetComponent<PositionComponent>().Position;
|
||||
ref var cameraComponent = ref entity.GetComponent<CameraComponent>();
|
||||
|
||||
ref var camera = ref cameraComponent.Camera;
|
||||
// Camera target follows player
|
||||
//camera.Target.X = position.X;
|
||||
//camera.Target.Z = position.Z;
|
||||
|
||||
// // Camera rotation controls
|
||||
// if (IsKeyDown(KEY_A)) camera.rotation--;
|
||||
// else if (IsKeyDown(KEY_S)) camera.rotation++;
|
||||
|
||||
// Limit camera rotation to 80 degrees (-40 to 40)
|
||||
// if (camera.Rotation > 40) camera.Rotation = 40;
|
||||
// else if (camera.Rotation < -40) camera.Rotation = -40;
|
||||
|
||||
// Camera zoom controls
|
||||
// camera.Zoom += Raylib.GetMouseWheelMove() * 0.05f;
|
||||
//
|
||||
// if (camera.Zoom > 3.0f) camera.Zoom = 3.0f;
|
||||
// else if (camera.Zoom < 0.1f) camera.Zoom = 0.1f;
|
||||
|
||||
// // Camera reset (zoom and rotation)
|
||||
// if (IsKeyPressed(KEY_R))
|
||||
// {
|
||||
// camera.zoom = 1.0f;
|
||||
// camera.rotation = 0.0f;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
46
Systems/Rendering/CameraSystem.cs
Normal file
46
Systems/Rendering/CameraSystem.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using BakeryGame.Components.Common;
|
||||
using BakeryGame.Components.Player;
|
||||
using Raylib_cs;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Systems.Rendering;
|
||||
|
||||
public class CameraSystem: ILateSystem
|
||||
{
|
||||
private Filter _player;
|
||||
float sliderValue = 50.0f; // Initial value of the slider
|
||||
float minValue = -20.0f; // Minimum value of the slider
|
||||
float maxValue = 20.0f; // Maximum value of the slider
|
||||
Rectangle sliderRect = new Rectangle( 100, 50, 200, 20 ); // Rectangle defining the slider's position and size
|
||||
bool isSliderActive = false;
|
||||
private const float CameraSpeed = 0.1f;
|
||||
private const float DistanceToPlayer = 10.0f;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnAwake()
|
||||
{
|
||||
_player = World.Filter.With<PlayerComponent>().Build();
|
||||
}
|
||||
|
||||
public World World { get; set; }
|
||||
|
||||
private float Lerp(float a, float b, float t)
|
||||
{
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
|
||||
public void OnUpdate(float deltaTime)
|
||||
{
|
||||
var player = _player.First();
|
||||
ref var cameraComponent = ref player.GetComponent<CameraComponent>();
|
||||
var camera = cameraComponent.Camera.GetCamera3D();
|
||||
var playerPosition = player.GetComponent<PositionComponent>().Position;
|
||||
// Update camera position to follow the player
|
||||
cameraComponent.Camera.SetPosition(camera.Position with {X = Lerp(camera.Position.X, playerPosition.X, CameraSpeed), Z = Lerp(camera.Position.Z, playerPosition.Z + DistanceToPlayer, CameraSpeed)});
|
||||
// Update camera target to look at the player
|
||||
cameraComponent.Camera.SetTarget(playerPosition);
|
||||
}
|
||||
}
|
||||
75
Systems/Rendering/SliderSystem.cs
Normal file
75
Systems/Rendering/SliderSystem.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using BakeryGame.Components.Common;
|
||||
using BakeryGame.Components.Player;
|
||||
using Raylib_cs;
|
||||
using Scellecs.Morpeh;
|
||||
|
||||
namespace BakeryGame.Systems.Rendering;
|
||||
|
||||
public class SliderSystem:ILateSystem
|
||||
{
|
||||
private Filter _player;
|
||||
float sliderValue = 50.0f; // Initial value of the slider
|
||||
float minValue = -20.0f; // Minimum value of the slider
|
||||
float maxValue = 20.0f; // Maximum value of the slider
|
||||
Rectangle sliderRect = new Rectangle( 100, 50, 200, 20 ); // Rectangle defining the slider's position and size
|
||||
bool isSliderActive = false;
|
||||
private const float CameraSpeed = 0.1f;
|
||||
private const float DistanceToPlayer = 10.0f;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void OnAwake()
|
||||
{
|
||||
_player = World.Filter.With<PlayerComponent>().Build();
|
||||
}
|
||||
|
||||
public World World { get; set; }
|
||||
|
||||
|
||||
private float Lerp(float a, float b, float t)
|
||||
{
|
||||
return a + (b - a) * t;
|
||||
}
|
||||
|
||||
public void OnUpdate(float deltaTime)
|
||||
{
|
||||
|
||||
if (Raylib.IsMouseButtonPressed(MouseButton.Left)) {
|
||||
// Check if mouse is clicked within the slider rectangle
|
||||
if (Raylib.CheckCollisionPointRec(Raylib.GetMousePosition(), sliderRect)) {
|
||||
isSliderActive = true; // Slider is being interacted with
|
||||
}
|
||||
}
|
||||
|
||||
if (Raylib.IsMouseButtonReleased(MouseButton.Left)) {
|
||||
isSliderActive = false; // Stop slider interaction
|
||||
}
|
||||
|
||||
// Update slider value when slider is active
|
||||
if (isSliderActive) {
|
||||
// // Calculate new slider value based on mouse position
|
||||
float mousePosX = Raylib.GetMouseX();
|
||||
sliderValue = minValue + (mousePosX - sliderRect.X) / sliderRect.Width * (maxValue - minValue);
|
||||
|
||||
// Clamp slider value within the valid range
|
||||
if (sliderValue < minValue) sliderValue = minValue;
|
||||
if (sliderValue > maxValue) sliderValue = maxValue;
|
||||
|
||||
// ref var cameraComponent = ref player.GetComponent<CameraComponent>();
|
||||
// var camera = cameraComponent.Camera.GetCamera3D();
|
||||
// cameraComponent.Camera.SetTarget(camera.Target with {X = sliderValue});
|
||||
}
|
||||
|
||||
// Draw slider background
|
||||
Raylib.DrawRectangleRec(sliderRect, Color.LightGray);
|
||||
|
||||
// Draw slider handle
|
||||
float sliderHandlePosX = sliderRect.X + (sliderValue - minValue) / (maxValue - minValue) * sliderRect.Width;
|
||||
var sliderHandleRect = new Rectangle( sliderHandlePosX - 5, sliderRect.Y - 5, 10, sliderRect.Height + 10 );
|
||||
Raylib.DrawRectangleRec(sliderHandleRect, Color.Blue);
|
||||
isSliderActive = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user