1bit-game-jam/scripts/Flashlight.cs
Ivan Kuzmenko f307a7ef00 Add new cool light beam for the flashlight
Co-authored-by: Евгений Титаренко <frundle@teasanctuary.ru>
2023-08-16 00:13:37 +03:00

48 lines
No EOL
1.9 KiB
C#

using Godot;
public partial class Flashlight : Node
{
[Export] public Player Player;
[Export] public GameCamera Camera;
[Export] public Node2D Circle;
[Export] public Node2D PlayerCircle;
[Export] public Polygon2D Polygon;
private float FlashlightRadius = Constants.MaxFlashlightRadius;
public override void _Process(double delta)
{
var playerScreenCenterPosition = Player.Position - Camera.Position;
var flashlightScreenCenterPosition = Camera.FlashlightPosition - Camera.Position;
var playerScreenPosition = playerScreenCenterPosition + Constants.HalfScreenSize;
PlayerCircle.Position = playerScreenPosition;
var flashlightScreenPosition = flashlightScreenCenterPosition + Constants.HalfScreenSize;
var flashlightScale = FlashlightRadius / Constants.MaxFlashlightRadius;
Circle.Position = flashlightScreenPosition;
Circle.Scale = new Vector2(flashlightScale, flashlightScale);
var d = Camera.FlashlightPosition.DistanceTo(Player.Position);
if (d <= FlashlightRadius)
Polygon.Visible = false;
else
{
Polygon.Visible = true;
var a = Mathf.Sqrt(d * d - FlashlightRadius * FlashlightRadius);
var xy2 = Camera.FlashlightPosition - Player.Position;
var angle = xy2.Angle();
var arcsinRd = Mathf.Asin(FlashlightRadius / d);
var dslkhjdsflkhjsdfhlkjdfsjlk = angle + arcsinRd;
var xy3 = a * new Vector2(Mathf.Cos(dslkhjdsflkhjsdfhlkjdfsjlk), Mathf.Sin(dslkhjdsflkhjsdfhlkjdfsjlk));
var dslkhjdsflkhjsdfhlkjdfsjlk2 = angle - arcsinRd;
var xy4 = a * new Vector2(Mathf.Cos(dslkhjdsflkhjsdfhlkjdfsjlk2), Mathf.Sin(dslkhjdsflkhjsdfhlkjdfsjlk2));
Polygon.Polygon = new[]
{ playerScreenPosition, playerScreenPosition + xy3, playerScreenPosition + xy4 };
}
}
}