GodotComponentTest/entities/Chest.cs

99 lines
3.0 KiB
C#
Raw Normal View History

2023-04-28 14:19:48 +02:00
using System.Linq;
using Godot;
using GodotComponentTest.components;
using GodotComponentTest.entities;
2023-01-04 22:49:00 +01:00
2023-11-18 22:32:57 +01:00
public class Chest : Entity, IInteractionInterface {
2023-04-28 14:19:48 +02:00
// resources
private readonly PackedScene _goldBarScene = GD.Load<PackedScene>("res://entities/GoldBar.tscn");
2023-01-04 22:49:00 +01:00
2023-11-18 22:32:57 +01:00
public enum LidState {
2023-02-12 21:10:28 +01:00
Closed,
Open
}
public LidState State = LidState.Closed;
public bool IsMouseOver;
2023-01-04 22:49:00 +01:00
private MeshInstance _mesh;
2023-02-12 21:10:28 +01:00
private AnimationPlayer _animationPlayer;
2023-04-28 14:19:48 +02:00
public InteractionComponent InteractionComponent { get; set; }
2023-01-04 22:49:00 +01:00
[Signal]
private delegate void EntityClicked(Entity entity);
2023-04-28 14:19:48 +02:00
[Signal]
private delegate void ChestOpened(Entity entity);
2023-04-28 14:19:48 +02:00
2023-01-04 22:49:00 +01:00
// Called when the node enters the scene tree for the first time.
2023-11-18 22:32:57 +01:00
public override void _Ready() {
_mesh = GetNode<MeshInstance>("Geometry/Skeleton/Chest");
2023-02-12 21:10:28 +01:00
_animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
2023-04-28 14:19:48 +02:00
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));
2023-04-28 14:19:48 +02:00
Connect("ChestOpened", this, nameof(ChestOpened));
2023-01-04 22:49:00 +01:00
}
2023-04-28 14:19:48 +02:00
2023-01-04 22:49:00 +01:00
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
2023-11-18 22:32:57 +01:00
int shapeIndex) {
if (IsMouseOver && inputEvent is InputEventMouseButton) {
2023-01-04 22:49:00 +01:00
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
2023-11-18 22:32:57 +01:00
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed) {
2023-01-04 22:49:00 +01:00
EmitSignal("EntityClicked", this);
}
}
}
2023-04-28 14:19:48 +02:00
2023-11-18 22:32:57 +01:00
public void OnAreaMouseEntered() {
2023-01-04 22:49:00 +01:00
IsMouseOver = true;
2023-11-18 22:32:57 +01:00
SpatialMaterial overrideMaterial = new();
2023-01-04 22:49:00 +01:00
overrideMaterial.AlbedoColor = new Color(1, 0, 0);
_mesh.MaterialOverride = overrideMaterial;
}
2023-04-28 14:19:48 +02:00
2023-11-18 22:32:57 +01:00
public void OnAreaMouseExited() {
2023-01-04 22:49:00 +01:00
IsMouseOver = false;
_mesh.MaterialOverride = null;
}
2023-02-12 21:10:28 +01:00
2023-11-18 22:32:57 +01:00
public void OnInteractionStart() {
2023-02-12 21:10:28 +01:00
_animationPlayer.Stop();
2023-04-28 14:19:48 +02:00
2023-11-18 22:32:57 +01:00
if (State == LidState.Closed) {
2023-02-12 21:10:28 +01:00
State = LidState.Open;
_animationPlayer.Play("ChestOpen");
2023-11-18 22:32:57 +01:00
} else {
2023-02-12 21:10:28 +01:00
_animationPlayer.PlayBackwards("ChestOpen");
State = LidState.Closed;
}
}
2023-04-28 14:19:48 +02:00
2023-11-18 22:32:57 +01:00
public void OnInteractionEnd() { }
2023-11-18 22:32:57 +01:00
public void OnChestOpened() {
2023-04-28 14:19:48 +02:00
GD.Print("Chest now opened!");
2023-11-18 22:32:57 +01:00
foreach (int i in Enumerable.Range(0, 10)) {
2023-04-28 14:19:48 +02:00
GoldBar bar = (GoldBar)_goldBarScene.Instance();
2023-11-18 22:32:57 +01:00
bar.Transform = new Transform(Transform.basis.Rotated(Vector3.Up, GD.Randf() * 2 * Mathf.Pi),
Transform.origin + Vector3.Up * 0.8f);
bar.Velocity = new Vector3(
2023-04-28 14:19:48 +02:00
(GD.Randf() * 2f - 1f) * 2,
2023-06-22 18:35:20 +02:00
5 + GD.Randf() * 0.3f,
2023-04-28 14:19:48 +02:00
(GD.Randf() * 2f - 1f) * 2
);
GetParent().AddChild(bar);
}
2023-11-18 22:32:57 +01:00
if (InteractionComponent != null) {
InteractionComponent.EndInteraction();
}
2023-04-28 14:19:48 +02:00
}
}