Add new cool light beam for the flashlight
Co-authored-by: Евгений Титаренко <frundle@teasanctuary.ru>
This commit is contained in:
parent
3832a05c1b
commit
f307a7ef00
20 changed files with 230 additions and 68 deletions
48
scripts/Flashlight.cs
Normal file
48
scripts/Flashlight.cs
Normal file
|
@ -0,0 +1,48 @@
|
|||
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 };
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue