GodotComponentTest/entities/Chest.cs

83 lines
2.1 KiB
C#
Raw Normal View History

2023-01-04 22:49:00 +01:00
using Godot;
using System;
public class Chest : Entity
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
2023-02-12 21:10:28 +01:00
public enum LidState
{
Closed,
Open
}
public LidState State = LidState.Closed;
2023-01-04 22:49:00 +01:00
public bool IsMouseOver = false;
private MeshInstance _mesh;
private SpatialMaterial _previousMaterial;
2023-02-12 21:10:28 +01:00
private AnimationPlayer _animationPlayer;
2023-01-04 22:49:00 +01:00
[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");
2023-02-12 21:10:28 +01:00
_animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
2023-01-04 22:49:00 +01:00
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);
}
}
}
2023-02-12 21:10:28 +01:00
2023-01-04 22:49:00 +01:00
public void OnAreaMouseEntered()
{
IsMouseOver = true;
SpatialMaterial overrideMaterial = new SpatialMaterial();
overrideMaterial.AlbedoColor = new Color(1, 0, 0);
_mesh.MaterialOverride = overrideMaterial;
}
2023-02-12 21:10:28 +01:00
2023-01-04 22:49:00 +01:00
public void OnAreaMouseExited()
{
IsMouseOver = false;
_mesh.MaterialOverride = null;
}
2023-02-12 21:10:28 +01:00
public void OnInteract()
{
_animationPlayer.Stop();
if (State == LidState.Closed)
{
State = LidState.Open;
_animationPlayer.Play("ChestOpen");
}
else
{
_animationPlayer.PlayBackwards("ChestOpen");
State = LidState.Closed;
}
}
2023-01-04 22:49:00 +01:00
}