mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-10 22:15:53 +00:00
3D Switches
This commit is contained in:
parent
003dde1b7f
commit
cb60226ced
20 changed files with 267 additions and 14 deletions
|
|
@ -7,6 +7,8 @@ public interface IInteractable
|
|||
public bool Activate(ActivationType activationType = ActivationType.Toggle);
|
||||
public bool CanActivate();
|
||||
|
||||
public Vector2 GetGlobalPosition();
|
||||
public Vector2 GetGlobalPosition2D();
|
||||
|
||||
public Vector2 GetScreenPosition();
|
||||
//protected bool MeetsRequirements();
|
||||
}
|
||||
37
Scripts/Interactables/Interactable3D.cs
Normal file
37
Scripts/Interactables/Interactable3D.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using Cirno.Scripts.Misc;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Interactables;
|
||||
|
||||
public partial class Interactable3D : Area3D, IInteractable
|
||||
{
|
||||
public virtual bool Activate(ActivationType activationType = ActivationType.Toggle)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual bool MeetsRequirements()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool CanActivate()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public Vector2 GetGlobalPosition2D()
|
||||
{
|
||||
return new Vector2(this.GlobalPosition.X, this.GlobalPosition.Z);
|
||||
}
|
||||
|
||||
public Vector2 GetScreenPosition()
|
||||
{
|
||||
if (CameraController3D.Instance is null)
|
||||
{
|
||||
return this.GetGlobalPosition2D();
|
||||
}
|
||||
|
||||
return CameraController3D.Instance.UnprojectPosition(this.GlobalPosition);
|
||||
}
|
||||
}
|
||||
1
Scripts/Interactables/Interactable3D.cs.uid
Normal file
1
Scripts/Interactables/Interactable3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://7rcv7ip052ug
|
||||
46
Scripts/Interactables/Switch3D.cs
Normal file
46
Scripts/Interactables/Switch3D.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
using System.Linq;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Interactables;
|
||||
|
||||
public partial class Switch3D : Interactable3D
|
||||
{
|
||||
[Export] public Node Target { get; set; }
|
||||
[Export] public Array<Node> Targets { get; private set; } = [];
|
||||
[Export] public ActivationType ActivationType { get; set; } = ActivationType.Toggle;
|
||||
|
||||
[Signal]
|
||||
public delegate void OnActivatedEventHandler(ActivationType activationType);
|
||||
|
||||
private AudioStreamPlayer _activationSound;
|
||||
|
||||
private readonly string _activationSoundName = "ActivationSound";
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_activationSound = GetNodeOrNull<AudioStreamPlayer>(_activationSoundName);
|
||||
}
|
||||
|
||||
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
|
||||
{
|
||||
var activationTypeToUse = activationType is ActivationType.Use ? ActivationType : activationType;
|
||||
|
||||
if (!MeetsRequirements()) return false;
|
||||
_activationSound?.Play();
|
||||
|
||||
EmitSignal(SignalName.OnActivated, (int)activationTypeToUse);
|
||||
|
||||
// Compatibility for old single system
|
||||
bool success = ActivateTarget(Target, activationTypeToUse);
|
||||
|
||||
return Targets.Aggregate(success, (current, target) => ActivateTarget(target, activationTypeToUse) | success);
|
||||
}
|
||||
|
||||
private bool ActivateTarget(Node target, ActivationType activationType = ActivationType.Toggle)
|
||||
{
|
||||
if (target is not IActivable activable) return false;
|
||||
activable?.Activate(activationType);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
1
Scripts/Interactables/Switch3D.cs.uid
Normal file
1
Scripts/Interactables/Switch3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://qxh76ahgexqa
|
||||
Loading…
Add table
Add a link
Reference in a new issue