GodotComponentTest/entities/Workbench.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2024-05-07 18:49:04 +02:00
using System.Diagnostics;
using Godot;
public class Workbench : Entity {
2024-05-07 18:49:04 +02:00
public bool IsMouseOver;
private MeshInstance _geometry;
[Signal]
public delegate void EntityClicked(Entity entity);
// Called when the node enters the scene tree for the first time.
2024-05-07 18:49:04 +02:00
public override void _Ready() {
_geometry = GetNode<MeshInstance>("Geometry/workbench");
Debug.Assert(_geometry != null);
Connect("input_event", this, nameof(OnAreaInputEvent));
Connect("mouse_entered", this, nameof(OnAreaMouseEntered));
Connect("mouse_exited", this, nameof(OnAreaMouseExited));
}
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
int shapeIndex) {
if (IsMouseOver && inputEvent is InputEventMouseButton) {
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed) {
EmitSignal("EntityClicked", this);
}
}
}
public void OnAreaMouseEntered() {
IsMouseOver = true;
SpatialMaterial overrideMaterial = new();
overrideMaterial.AlbedoColor = new Color(1, 0, 0);
_geometry.MaterialOverride = overrideMaterial;
}
public void OnAreaMouseExited() {
IsMouseOver = false;
_geometry.MaterialOverride = null;
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
// public override void _Process(float delta)
// {
//
// }
}