Add PlayerGraphicsComponent to manage player sprite visibility based on movement abilities
This commit is contained in:
43
scripts/components/PlayerGraphicsComponent.cs
Normal file
43
scripts/components/PlayerGraphicsComponent.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class PlayerGraphicsComponent : Node
|
||||
{
|
||||
[Export] public Node2D DefaultSprite { get; set; }
|
||||
[Export] public Node2D SpaceshipSprite { get; set; }
|
||||
|
||||
private PlayerController _playerController;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_playerController = GetOwner<PlayerController>();
|
||||
if (_playerController == null)
|
||||
{
|
||||
GD.PrintErr("PlayerGraphicsComponent must be a child of a PlayerController.");
|
||||
SetProcess(false);
|
||||
return;
|
||||
}
|
||||
|
||||
_playerController.MovementAbilitiesChanged += OnMovementAbilitiesChanged;
|
||||
|
||||
UpdateGraphics();
|
||||
}
|
||||
|
||||
private void OnMovementAbilitiesChanged()
|
||||
{
|
||||
UpdateGraphics();
|
||||
}
|
||||
|
||||
private void UpdateGraphics()
|
||||
{
|
||||
var isSpaceship = _playerController.GetActiveAbilities().Any(ability => ability is SpaceshipMovementAbility);
|
||||
|
||||
if (SpaceshipSprite != null)
|
||||
{
|
||||
SpaceshipSprite.Visible = isSpaceship;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user