using Godot; using System; public class Chest : Entity { // Declare member variables here. Examples: // private int a = 2; // private string b = "text"; public enum LidState { Closed, Open } public LidState State = LidState.Closed; public bool IsMouseOver = false; private MeshInstance _mesh; private SpatialMaterial _previousMaterial; private AnimationPlayer _animationPlayer; [Signal] delegate void EntityClicked(Entity entity); // Called when the node enters the scene tree for the first time. public override void _Ready() { _mesh = GetNode("Armature/Skeleton/Chest"); _animationPlayer = GetNode("AnimationPlayer"); 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; } public void OnInteract() { _animationPlayer.Stop(); if (State == LidState.Closed) { State = LidState.Open; _animationPlayer.Play("ChestOpen"); } else { _animationPlayer.PlayBackwards("ChestOpen"); State = LidState.Closed; } } }