cirnogodot/Scripts/Activables/ScriptableBase.cs

76 lines
1.6 KiB
C#
Raw Normal View History

2025-02-13 16:10:22 +01:00
using System;
2025-03-09 21:58:25 +01:00
using Cirno.Scripts.Interactables;
2025-02-13 16:10:22 +01:00
using Cirno.Scripts.Resources.Events;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Activables;
2025-02-14 13:27:30 +01:00
public partial class ScriptableBase : Node2D, IActivable
2025-02-13 16:10:22 +01:00
{
2025-03-10 15:49:28 +01:00
[Export] public Array<EventResource> Events { get; set; } = [];
2025-02-13 16:10:22 +01:00
private EventResource CurrentEvent => Events[_currentEventIndex];
private int _currentEventIndex = 0;
private bool _started = false;
public override void _Ready()
{
2025-02-16 16:26:05 +01:00
foreach (var item in Events)
{
item.Init(this);
}
2025-02-13 16:10:22 +01:00
}
public void Start()
{
_started = true;
StartEvent(CurrentEvent);
}
private void StartEvent(EventResource eventResource)
{
eventResource.Start(this);
}
public override void _Process(double delta)
{
if (!_started) return;
CurrentEvent.UpdateEvent(delta);
if (!CurrentEvent.WaitForCompletion || CurrentEvent.IsComplete())
{
// This loops
_currentEventIndex++;
if (_currentEventIndex >= Events.Count)
{
// It's over
_started = false;
_currentEventIndex = 0;
return;
}
CurrentEvent.Start(this);
}
}
2025-02-14 13:27:30 +01:00
2025-03-09 21:58:25 +01:00
public bool Activate(ActivationType activationType = ActivationType.Toggle)
2025-02-14 13:27:30 +01:00
{
Start();
2025-03-09 21:58:25 +01:00
return true;
}
2025-06-18 11:33:27 +02:00
public void Toggle()
{
this.Activate();
}
2025-03-09 21:58:25 +01:00
public bool CanActivate()
{
return true;
2025-02-14 13:27:30 +01:00
}
2025-02-13 16:10:22 +01:00
}