diff --git a/scripts/GameCamera.cs b/scripts/GameCamera.cs index 2106ca0..556961f 100644 --- a/scripts/GameCamera.cs +++ b/scripts/GameCamera.cs @@ -2,48 +2,54 @@ using Godot; public partial class GameCamera : Camera2D { - [Export] public Player Player; - [Export] public Vector2 CameraBounds; - - /// - /// World position of the flashlight - /// - public Vector2 FlashlightPosition; + [Export] public Player Player; + [Export] public Vector2 CameraBounds = new(40, 30); + [Export] public Vector2 CameraFollowBounds = new(20, 10); + [Export] public float Speed = 0.5f; - public override void _PhysicsProcess(double delta) - { - var relativePlayerPosition = Player.Position - Position; - var halfCameraBounds = CameraBounds / 2; - float x = 0, y = 0; - if (relativePlayerPosition.X > halfCameraBounds.X) - { - x = relativePlayerPosition.X - halfCameraBounds.X; - } - else if (relativePlayerPosition.X < -halfCameraBounds.X) - { - x = relativePlayerPosition.X + halfCameraBounds.X; - } - if (relativePlayerPosition.Y > halfCameraBounds.Y) - { - y = relativePlayerPosition.Y - halfCameraBounds.Y; - } - else if (relativePlayerPosition.Y < -halfCameraBounds.Y) - { - y = relativePlayerPosition.Y + halfCameraBounds.Y; - } + /// + /// World position of the flashlight + /// + public Vector2 FlashlightPosition; - var difference = new Vector2(x, y); - Position += difference; - FlashlightPosition += difference; - } + public override void _PhysicsProcess(double delta) + { + var difference = Vector2.Zero; - public override void _Input(InputEvent @event) - { - base._Input(@event); + var relativePlayerPosition = Player.Position - Position; + var halfCameraBounds = CameraBounds / 2; + var halfCameraFollowBounds = CameraFollowBounds / 2; + var hardLimit = relativePlayerPosition.Clamp(-halfCameraBounds, halfCameraBounds); + difference = relativePlayerPosition - hardLimit; + GD.Print($"HardDiff {difference}"); + if (difference.IsZeroApprox()) + { + float x = 0, y = 0; + if (Mathf.Abs(relativePlayerPosition.X) >= halfCameraFollowBounds.X) + { + x = relativePlayerPosition.X * Speed * (float)delta; + } - if (@event is InputEventMouseMotion eventMouseMotion) - { - FlashlightPosition = eventMouseMotion.Position - Constants.HalfScreenSize + Position; - } - } -} + if (Mathf.Abs(relativePlayerPosition.Y) > halfCameraFollowBounds.Y) + { + y = relativePlayerPosition.Y * Speed * (float)delta; + } + + difference = new Vector2(x, y); + GD.Print($"SmoothDiff {difference}"); + } + + Position = (Position + difference).Round(); + FlashlightPosition = (FlashlightPosition + difference).Round(); + } + + public override void _Input(InputEvent @event) + { + base._Input(@event); + + if (@event is InputEventMouseMotion eventMouseMotion) + { + FlashlightPosition = eventMouseMotion.Position - Constants.HalfScreenSize + Position; + } + } +} \ No newline at end of file