GodotComponentTest/entities/Chest.cs

101 lines
2.7 KiB
C#
Raw Normal View History

2023-01-04 22:49:00 +01:00
using Godot;
using System;
2023-04-28 14:19:48 +02:00
using System.Linq;
2023-01-04 22:49:00 +01:00
public class Chest : Entity
{
2023-04-28 14:19:48 +02:00
// resources
private PackedScene _goldBarScene = GD.Load<PackedScene>("res://entities/GoldBar.tscn");
2023-01-04 22:49:00 +01:00
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;
2023-02-12 21:10:28 +01:00
private AnimationPlayer _animationPlayer;
2023-04-28 14:19:48 +02:00
2023-01-04 22:49:00 +01:00
[Signal]
delegate void EntityClicked(Entity entity);
2023-04-28 14:19:48 +02:00
[Signal]
delegate void ChestOpened(Entity entity);
2023-01-04 22:49:00 +01:00
// 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-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,
int shapeIndex)
{
if (IsMouseOver && inputEvent is InputEventMouseButton)
{
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed)
{
EmitSignal("EntityClicked", this);
}
}
}
2023-04-28 14:19:48 +02: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-04-28 14:19:48 +02: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();
2023-04-28 14:19:48 +02:00
2023-02-12 21:10:28 +01:00
if (State == LidState.Closed)
{
State = LidState.Open;
_animationPlayer.Play("ChestOpen");
}
else
{
_animationPlayer.PlayBackwards("ChestOpen");
State = LidState.Closed;
}
}
2023-04-28 14:19:48 +02:00
public void OnChestOpened()
{
GD.Print("Chest now opened!");
foreach (int i in Enumerable.Range(0, 10))
{
GoldBar bar = (GoldBar)_goldBarScene.Instance();
bar.Transform = new Transform(Transform.basis.Rotated(Vector3.Up, GD.Randf() * 2 * Mathf.Pi), Transform.origin + Vector3.Up * 0.8f);
bar.velocity = new Vector3(
(GD.Randf() * 2f - 1f) * 2,
5,
(GD.Randf() * 2f - 1f) * 2
);
GetParent().AddChild(bar);
}
}
}