cirnogodot/Scripts/Activables/Teleporter.cs

192 lines
5.3 KiB
C#
Raw Permalink Normal View History

2025-02-06 17:57:06 +01:00
using System;
using System.Collections;
2025-02-24 11:52:50 +01:00
using System.Threading;
2025-02-06 17:57:06 +01:00
using System.Threading.Tasks;
2025-03-02 11:58:30 +01:00
using Cirno.Scripts.Components.FSM;
2025-02-06 17:57:06 +01:00
using Godot;
2025-02-24 11:52:50 +01:00
using GTweensGodot.Extensions;
2025-02-06 17:57:06 +01:00
namespace Cirno.Scripts.Activables;
2025-03-31 11:59:45 +02:00
[Tool]
2025-02-06 17:57:06 +01:00
public partial class Teleporter : Activable
{
[Export]
public bool IsEnabled { get; set; }
2025-02-20 11:10:28 +01:00
[Export]
2025-04-29 18:14:09 +02:00
public bool Invisible { get; set; } = false;
2025-03-05 12:27:15 +01:00
2025-02-06 17:57:06 +01:00
public bool IsPrimed { get; private set; }
2025-03-05 12:27:15 +01:00
2025-02-06 17:57:06 +01:00
[Export]
public Teleporter Target { get; set; }
[Export] public float ParticleEmitTime { get; private set; } = 2f;
2025-03-05 12:27:15 +01:00
[Export] public float TeleportAnimationLength { get; private set; } = 0.5f;
[Export] public Vector2 TeleportOffset { get; private set; } = new Vector2(0, -4f);
2025-02-06 17:57:06 +01:00
private double _particleTimer;
2025-03-05 12:27:15 +01:00
2025-02-07 15:43:02 +01:00
private AnimatedSprite2D _animatedSprite;
2025-03-05 12:27:15 +01:00
2025-02-06 17:57:06 +01:00
// [Export]
// public GpuParticles2D Particles { get; set; }
2025-02-21 22:48:12 +01:00
protected GpuParticles2D _particles;
2025-03-05 12:27:15 +01:00
private AudioStreamPlayer2D _teleportStartSound;
private AudioStreamPlayer2D _teleportEndSound;
2025-03-01 23:46:25 +01:00
2025-02-06 17:57:06 +01:00
public override void _Ready()
{
2025-02-07 15:43:02 +01:00
_animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
2025-03-31 11:59:45 +02:00
_animatedSprite.Play(IsEnabled ? "Active" : "Default");
if (Engine.IsEditorHint()) return;
_particles = GetNode<GpuParticles2D>("./Particles");
2025-02-06 17:57:06 +01:00
IsPrimed = true;
_particles.Emitting = false;
_particleTimer = 0;
2025-02-07 15:43:02 +01:00
2025-02-20 11:10:28 +01:00
this.Visible = !Invisible;
2025-03-31 11:59:45 +02:00
2025-03-01 23:46:25 +01:00
_teleportStartSound = GetNodeOrNull<AudioStreamPlayer2D>("TeleportStart");
_teleportEndSound = GetNodeOrNull<AudioStreamPlayer2D>("TeleportEnd");
2025-02-06 17:57:06 +01:00
}
public override void _Process(double delta)
{
2025-03-31 11:59:45 +02:00
if (Engine.IsEditorHint()) return;
2025-02-06 17:57:06 +01:00
if (!_particles.Emitting) return;
2025-03-05 12:27:15 +01:00
2025-02-06 17:57:06 +01:00
_particleTimer += delta;
if (_particleTimer >= ParticleEmitTime)
{
_particleTimer = 0;
_particles.Emitting = false;
}
}
2025-03-05 12:27:15 +01:00
2025-03-09 21:58:25 +01:00
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
2025-02-06 17:57:06 +01:00
{
switch (activationType)
{
case ActivationType.Toggle:
// Enables/Disables teleporter
break;
case ActivationType.Enable:
// Enables Teleporter
IsEnabled = true;
2025-02-07 15:43:02 +01:00
_animatedSprite.Play("Active");
2025-02-06 17:57:06 +01:00
break;
case ActivationType.Disable:
IsEnabled = false;
2025-02-07 15:43:02 +01:00
_animatedSprite.Play("Default");
2025-02-06 17:57:06 +01:00
// Disables Teleporter
break;
case ActivationType.Use:
// Teleports
break;
case ActivationType.Destroy:
// Destroys
break;
}
2025-03-09 21:58:25 +01:00
return true;
2025-02-06 17:57:06 +01:00
}
public void PrepareForReceiving()
{
IsPrimed = false;
_particles.Emitting = true;
}
public void FireParticles()
{
_particles.Emitting = true;
_particleTimer = 0;
}
2025-03-05 12:27:15 +01:00
2025-03-05 10:55:14 +01:00
private void _on_area_entered(Area2D area)
2025-02-06 17:57:06 +01:00
{
2025-03-05 12:27:15 +01:00
GD.Print("Something entered the teleporter");
2025-03-05 10:55:14 +01:00
if (!IsEnabled) return;
if (area is not InteractionController interactionController) return;
2025-03-05 12:27:15 +01:00
GD.Print("Player entered the teleporter");
2025-02-06 17:57:06 +01:00
if (!IsPrimed)
{
IsPrimed = true;
//_particles.Emitting = false;
return;
}
2025-03-05 12:27:15 +01:00
2025-02-06 17:57:06 +01:00
// Teleport player to target if active
if (!IsEnabled) return;
2025-03-05 12:27:15 +01:00
GD.Print("Teleporting...");
2025-02-06 17:57:06 +01:00
// Call Teleport here
2025-03-05 10:55:14 +01:00
_ = Teleport(interactionController.StateMachine);
2025-02-06 17:57:06 +01:00
}
2025-03-05 10:55:14 +01:00
protected virtual async Task Teleport(IStateMachine<PlayerState, CharacterBody2D> player)
2025-02-06 17:57:06 +01:00
{
2025-02-21 22:48:12 +01:00
if (Target is null) return;
2025-03-02 11:58:30 +01:00
//player.RequestMovementDisable(true);
2025-03-05 10:55:14 +01:00
player.SetState(PlayerState.Cutscene);
2025-02-06 17:57:06 +01:00
2025-03-05 10:55:14 +01:00
await TweenPlayer(player.MainObject);
2025-03-05 12:27:15 +01:00
2025-03-01 23:46:25 +01:00
PlayTeleportStartSound();
2025-02-23 19:19:12 +01:00
//_particles.Emitting = true;
FireParticles();
2025-02-22 14:45:46 +01:00
2025-03-02 11:58:30 +01:00
//await player.Teleport();
2025-03-05 10:55:14 +01:00
player.SetState(PlayerState.Teleporting);
2025-03-02 11:58:30 +01:00
await Task.Delay((int)(0.6f * 1000));
2025-03-05 12:27:15 +01:00
2025-02-06 17:57:06 +01:00
await Task.Delay((int)(TeleportAnimationLength * 1000));
2025-03-05 12:27:15 +01:00
2025-02-22 14:45:46 +01:00
Target.PrepareForReceiving();
2025-03-05 10:55:14 +01:00
player.MainObject.GlobalPosition = Target.GlobalPosition + TeleportOffset;
2025-03-01 23:46:25 +01:00
Target.PlayTeleportEndSound();
2025-03-02 11:58:30 +01:00
//await player.UnTeleport();
2025-03-05 12:27:15 +01:00
2025-03-05 10:55:14 +01:00
player.SetState(PlayerState.UnTeleporting);
2025-03-02 11:58:30 +01:00
await Task.Delay((int)(0.6f * 1000));
2025-03-05 12:27:15 +01:00
2025-03-02 11:58:30 +01:00
//player.RequestMovementDisable(false);
2025-03-05 10:55:14 +01:00
player.SetState(PlayerState.Active);
2025-02-06 17:57:06 +01:00
}
2025-03-01 23:46:25 +01:00
public void PlayTeleportStartSound()
{
_teleportStartSound?.Play();
}
public void PlayTeleportEndSound()
{
_teleportEndSound?.Play();
}
2025-03-05 10:55:14 +01:00
protected async Task TweenPlayer(CharacterBody2D player)
2025-02-06 17:57:06 +01:00
{
2025-02-24 11:52:50 +01:00
await player.TweenGlobalPosition(GlobalPosition + new Vector2(0, -4f), TeleportAnimationLength).PlayAsync(CancellationToken.None);
2025-03-05 12:27:15 +01:00
2025-02-06 17:57:06 +01:00
// Create a Tween for the teleport animation
2025-02-24 11:52:50 +01:00
// Tween tween = GetTree().CreateTween();
// tween.SetEase(Tween.EaseType.InOut);
// tween.SetTrans(Tween.TransitionType.Sine);
// tween.TweenProperty(player, "global_position", GlobalPosition + new Vector2(0,-4f), TeleportAnimationLength);
//
// // Wait for the tween to finish
// await ToSignal(tween, "finished");
2025-02-06 17:57:06 +01:00
}
}