Files
Ecosystem-Project/scenes/entities/BaseEntity.cs

114 lines
2.9 KiB
C#

using Godot;
using Ecosystem.utility;
namespace Ecosystem.scenes.entities;
public partial class BaseEntity : Area2D
{
[Signal]
public delegate BaseEntity EntitySelectedEventHandler();
[Signal]
public delegate BaseEntity EntityDeselectedEventHandler();
public Data.EntityData _data;
protected Vector2 ScreenSize;
protected AnimationPlayer animationPlayer;
protected Sprite2D sprite;
protected CollisionShape2D collisionShape;
protected bool selected;
protected Label positionLabel;
public override void _Ready()
{
ScreenSize = GetViewportRect().Size;
sprite = GetNode<Sprite2D>("Sprite2D");
collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
animationPlayer = sprite.GetNode<AnimationPlayer>("AnimationPlayer");
positionLabel = GetNode<Label>("MarginContainer/VBoxContainer/Label2");
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseEvent)
{
if (mouseEvent.IsPressed() && !mouseEvent.IsEcho())
{
switch (mouseEvent.ButtonIndex)
{
case MouseButton.Left:
if (collisionShape.Shape.GetRect().HasPoint(GetLocalMousePosition())) entitySelected();
else if (selected) entityDeselected();
break;
default:
break;
}
}
}
}
public override void _Process(double delta)
{
if (selected)
positionLabel.Text = $"Position: {Position.ToString("F0")}";
}
protected void entitySelected()
{
selected = true;
GD.Print("Selected");
GetNode<Sprite2D>("SelectedSprite2D").Visible = true;
EmitSignalEntitySelected();
ShowInfo();
}
protected void entityDeselected()
{
selected = false;
GD.Print("Unselected");
GetNode<Sprite2D>("SelectedSprite2D").Visible = false;
EmitSignalEntityDeselected();
HideInfo();
}
protected void ShowInfo()
{
MarginContainer marginContainer = GetNode<MarginContainer>("MarginContainer");
Label nameLabel = GetNode<Label>("MarginContainer/VBoxContainer/Label");
nameLabel.Text = $"Name: {Name}";
marginContainer.Show();
}
protected void HideInfo()
{
MarginContainer marginContainer = GetNode<MarginContainer>("MarginContainer");
marginContainer.Hide();
}
// public override void _MouseEnter()
// {
// GD.Print("MouseEnter");
//
// if (Input.IsMouseButtonPressed(MouseButton.Left))
// {
// GD.Print("Selected");
// selected = true;
// }
// }
public virtual void Create()
{
return;
}
}