Виртуальный курсор (для управления с геймпада)

This commit is contained in:
Евгений Титаренко 2024-08-09 19:07:35 +03:00
parent d42bd00c3d
commit bbb66a5273
12 changed files with 135 additions and 8 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Godot;
public partial class Flashlight : Node
@ -23,7 +24,7 @@ public partial class Flashlight : Node
[Export] public Curve BrightnessCurve;
[Export] public AudioStreamPlayer CrankSoundPlayer;
private float FlashlightRadius = Constants.MaxFlashlightRadius;
private float FlashlightEnergy = 0;
private float FlashlightChargeTimeout = 1;

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using Godot;
public partial class GameCamera : Camera2D
@ -6,7 +7,10 @@ public partial class GameCamera : Camera2D
[Export] public Vector2 CameraBounds = new(40, 30);
[Export] public Vector2 CameraFollowBounds = new(20, 10);
[Export] public float Speed = 0.5f;
[Export] public VirtualCursor Cursor;
[Export] public const float CursorSpeed = 2.0f;
/// <summary>
/// World position of the flashlight
/// </summary>
@ -14,6 +18,12 @@ public partial class GameCamera : Camera2D
public override void _PhysicsProcess(double delta)
{
Vector2 direction = Input.GetVector("cursor_left", "cursor_right", "cursor_up", "cursor_down");
if (direction != Vector2.Zero)
{
FlashlightPosition += direction * CursorSpeed;
}
var difference = Vector2.Zero;
var relativePlayerPosition = Player.Position - Position;
@ -39,6 +49,8 @@ public partial class GameCamera : Camera2D
Position = (Position + difference).Round();
FlashlightPosition = (FlashlightPosition + difference).Round();
Cursor.Position = FlashlightPosition;
}
public override void _Input(InputEvent @event)
@ -48,6 +60,7 @@ public partial class GameCamera : Camera2D
if (@event is InputEventMouseMotion eventMouseMotion)
{
FlashlightPosition = eventMouseMotion.Position - Constants.HalfScreenSize + Position;
//Cursor.Position = eventMouseMotion.Position - Constants.HalfScreenSize + Position;
}
}
}

View file

@ -10,6 +10,7 @@ public partial class Menu : Node2D
private Array<string> _translations = new Array<string>{"en", "ru"};
private int _languagesCount;
private int _currentLanguage = 0;
private Resource _cursorTexture;
public override void _Ready()
{
@ -31,6 +32,7 @@ public partial class Menu : Node2D
_animationPlayer.Play("thunder");
_thunderclap.Play();
_timer.Start();
DisplayServer.MouseSetMode(DisplayServer.MouseMode.Hidden);
}
else if (@event.IsActionPressed("ui_change_language"))
{

30
scripts/VirtualCursor.cs Normal file
View file

@ -0,0 +1,30 @@
using Godot;
using System;
public partial class VirtualCursor : Node2D
{
//[Export] public Node2D Cursor;
protected Sprite2D CursorSprite;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
CursorSprite = (Sprite2D)FindChild("CursorSprite");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public override void _Input(InputEvent @event)
{
// base._Input(@event);
//
// if (@event is InputEventMouseMotion eventMouseMotion)
// {
// CursorSprite.Position = eventMouseMotion.Position - Constants.HalfScreenSize + Position;
// }
}
}