1bit-game-jam/scripts/GameCamera.cs

49 lines
1.2 KiB
C#

using Godot;
public partial class GameCamera : Camera2D
{
[Export] public Player Player;
[Export] public Vector2 CameraBounds;
/// <summary>
/// World position of the flashlight
/// </summary>
public Vector2 FlashlightPosition;
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;
}
var difference = new Vector2(x, y);
Position += difference;
FlashlightPosition += difference;
}
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (@event is InputEventMouseMotion eventMouseMotion)
{
FlashlightPosition = eventMouseMotion.Position - Constants.HalfScreenSize + Position;
}
}
}