-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInteractableArea.cs
More file actions
71 lines (64 loc) · 2.4 KB
/
InteractableArea.cs
File metadata and controls
71 lines (64 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Godot;
using Object = Godot.Object;
namespace Game1.Core.Scripts
{
public class InteractableArea : Area2D
{
[Export] public string text = "Interacted";
private RichTextLabel dialogText;
private bool dialogActive = false;
private KinematicBody2D player;
private CollisionShape2D collisionShape;
private RectangleShape2D rectangleShape;
private Rect2 rect;
public override void _Ready()
{
SetProcessInput(true);
dialogText = GetTree().Root.GetNode<RichTextLabel>("MainScene/HUDLayer/DialogBox/DialogText");
Connect(nameof(SendTextToDialog), dialogText, "_ReceiveText");
dialogText.Connect("SignalDialogActive", this, "SetDialog");
player = GetTree().Root.GetNode<KinematicBody2D>("MainScene/Player");
collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
rectangleShape = (RectangleShape2D) collisionShape.Shape;
rect = new Rect2(collisionShape.GlobalPosition.x - rectangleShape.Extents.x,
collisionShape.GlobalPosition.y - rectangleShape.Extents.y, rectangleShape.Extents.x * 2, rectangleShape.Extents.y * 2);
}
public override void _InputEvent(Object viewport, InputEvent @event, int shapeIdx)
{
if (dialogActive)
{
return;
}
if (@event.IsActionPressed("left_click"))
{
GetTree().SetInputAsHandled();
if (GlobalPosition.DistanceTo(player.GlobalPosition) < 16)
{
EmitSignal(nameof(SendTextToDialog), text);
}
else
{
player.Set("targetX", new Vector2(GetGlobalMousePosition().x, 0));
player.Set("targetY", new Vector2(0, GetGlobalMousePosition().y));
}
}
}
public override void _Process(float delta)
{
if (rect.HasPoint(GetGlobalMousePosition()))
{
Input.SetDefaultCursorShape(Input.CursorShape.PointingHand);
}
else
{
Input.SetDefaultCursorShape();
}
}
public void SetDialog(bool active)
{
dialogActive = active;
}
[Signal]
delegate void SendTextToDialog(string text);
}
}