GodotComponentTest/entities/Chest.cs

55 lines
1.6 KiB
C#

using Godot;
using System;
public class Chest : Entity
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
public bool IsMouseOver = false;
private MeshInstance _mesh;
private SpatialMaterial _previousMaterial;
[Signal]
delegate void EntityClicked(Entity entity);
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_mesh = GetNode<MeshInstance>("Armature/Skeleton/Chest");
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 SpatialMaterial();
overrideMaterial.AlbedoColor = new Color(1, 0, 0);
_mesh.MaterialOverride = overrideMaterial;
}
public void OnAreaMouseExited()
{
IsMouseOver = false;
_mesh.MaterialOverride = null;
}
}