mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-11 02:45:53 +00:00
Door player detection
This commit is contained in:
parent
87438dff2b
commit
f7334c056b
14 changed files with 1631 additions and 1361 deletions
181
Scripts/Actors/3D/Door3D.cs
Normal file
181
Scripts/Actors/3D/Door3D.cs
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
using System;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Actors._3D;
|
||||
|
||||
[Tool]
|
||||
public partial class Door3D : AnimatableBody3D, IActivable
|
||||
{
|
||||
[Export] public string GroupName { get; set; }
|
||||
[Export] public DoorState State { get; set; } = DoorState.Closed;
|
||||
|
||||
[Signal] public delegate void OpeningEventHandler();
|
||||
[Signal] public delegate void ClosingEventHandler();
|
||||
|
||||
[Export] public AnimationPlayer AnimationPlayer { get; set; }
|
||||
|
||||
[Export] public string OpenAnimationName { get; set; } = "Open";
|
||||
|
||||
[Export] public string CloseAnimationName { get; set; } = "Close";
|
||||
|
||||
[Export] public string OpenedAnimationName { get; set; } = "Opened";
|
||||
|
||||
[Export] public string ClosedAnimationName { get; set; } = "Closed";
|
||||
|
||||
private bool _isAnimating = false;
|
||||
private bool _isPlayerIncollider = false;
|
||||
|
||||
private CollisionShape3D _collisionShape;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (Engine.IsEditorHint()) return;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(GroupName))
|
||||
{
|
||||
this.AddToGroup(GroupName);
|
||||
}
|
||||
|
||||
_collisionShape = GetNode<CollisionShape3D>("CollisionShape3D");
|
||||
|
||||
SyncAnimation();
|
||||
|
||||
AnimationPlayer.AnimationFinished += AnimationPlayerOnAnimationFinished;
|
||||
}
|
||||
|
||||
private void SyncAnimation()
|
||||
{
|
||||
if (State is DoorState.Closed)
|
||||
{
|
||||
AnimationPlayer.Play(ClosedAnimationName);
|
||||
}
|
||||
else
|
||||
{
|
||||
AnimationPlayer.Play(OpenedAnimationName);
|
||||
}
|
||||
}
|
||||
|
||||
private void AnimationPlayerOnAnimationFinished(StringName animName)
|
||||
{
|
||||
if (Engine.IsEditorHint()) return;
|
||||
|
||||
GD.Print($"Animation finished {animName}");
|
||||
|
||||
if ((animName == OpenAnimationName || animName == OpenedAnimationName))
|
||||
{
|
||||
State = DoorState.Open;
|
||||
CallDeferred(MethodName.ToggleCollisionDeferred, false);
|
||||
}
|
||||
else if (animName == CloseAnimationName || animName == ClosedAnimationName)
|
||||
{
|
||||
State = DoorState.Closed;
|
||||
CallDeferred(MethodName.ToggleCollisionDeferred, true);
|
||||
}
|
||||
_isAnimating = false;
|
||||
}
|
||||
|
||||
public void _func_godot_apply_properties(Dictionary props)
|
||||
{
|
||||
GroupName = (string)props["targetname"];
|
||||
}
|
||||
|
||||
public bool Activate(ActivationType activationType = ActivationType.Toggle)
|
||||
{
|
||||
if (_isAnimating) return false;
|
||||
switch (activationType)
|
||||
{
|
||||
case ActivationType.Toggle:
|
||||
case ActivationType.Use:
|
||||
if (State is DoorState.Open && _isPlayerIncollider)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Toggle();
|
||||
break;
|
||||
case ActivationType.Enable:
|
||||
case ActivationType.Close:
|
||||
if (State is DoorState.Open && _isPlayerIncollider)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ChangeState(DoorState.Closed);
|
||||
break;
|
||||
case ActivationType.Disable:
|
||||
case ActivationType.Open:
|
||||
ChangeState(DoorState.Open);
|
||||
break;
|
||||
case ActivationType.Destroy:
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(activationType), activationType, null);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ChangeState(DoorState newState)
|
||||
{
|
||||
if (_isAnimating) return;
|
||||
if (State == newState) return;
|
||||
|
||||
switch (newState)
|
||||
{
|
||||
case DoorState.Open:
|
||||
AnimationPlayer.Play(OpenAnimationName);
|
||||
AnimationPlayer.SetSpeedScale(1);
|
||||
//CallDeferred(MethodName.ToggleCollisionDeferred, false);
|
||||
break;
|
||||
case DoorState.Closed:
|
||||
AnimationPlayer.Play(CloseAnimationName);
|
||||
AnimationPlayer.SetSpeedScale(1);
|
||||
//CallDeferred(MethodName.ToggleCollisionDeferred, true);
|
||||
break;
|
||||
}
|
||||
|
||||
//State = newState;
|
||||
|
||||
_isAnimating = true;
|
||||
}
|
||||
|
||||
private void ToggleCollisionDeferred(bool toggle)
|
||||
{
|
||||
_collisionShape.Disabled = !toggle;
|
||||
}
|
||||
|
||||
public void Toggle()
|
||||
{
|
||||
if (_isAnimating) return;
|
||||
ChangeState(State is DoorState.Open ? DoorState.Closed : DoorState.Open);
|
||||
}
|
||||
|
||||
public void OnPlayerDetected(Node3D body)
|
||||
{
|
||||
_isPlayerIncollider = true;
|
||||
// if (!_isAnimating) return;
|
||||
// if (State is DoorState.Open) // It means it's closing
|
||||
// {
|
||||
// AnimationPlayer.SetSpeedScale(-1);
|
||||
// }
|
||||
}
|
||||
|
||||
public void OnPlayerUndetected(Node3D body)
|
||||
{
|
||||
_isPlayerIncollider = false;
|
||||
}
|
||||
|
||||
// private bool IsPlayerInArea()
|
||||
// {
|
||||
// var spaceState = GetWorld3D().DirectSpaceState;
|
||||
//
|
||||
// var query = new PhysicsShapeQueryParameters3D()
|
||||
// {
|
||||
// CollideWithBodies = true,
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// //query.CollideWithBodies = true;
|
||||
// }
|
||||
}
|
||||
1
Scripts/Actors/3D/Door3D.cs.uid
Normal file
1
Scripts/Actors/3D/Door3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b5yceosypf3mu
|
||||
|
|
@ -13,21 +13,24 @@ 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 AudioStreamPlayer _denySound;
|
||||
|
||||
private readonly string _activationSoundName = "ActivationSound";
|
||||
private readonly string _denySoundName = "ActivationSound";
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (Engine.IsEditorHint()) return;
|
||||
|
||||
|
||||
_activationSound = GetNodeOrNull<AudioStreamPlayer>(_activationSoundName);
|
||||
_denySound = GetNodeOrNull<AudioStreamPlayer>(_denySoundName);
|
||||
}
|
||||
|
||||
|
||||
public void _func_godot_apply_properties(Dictionary<string, string> props)
|
||||
{
|
||||
TargetGroup = props["target"];
|
||||
|
|
@ -35,6 +38,7 @@ public partial class Switch3D : Interactable3D
|
|||
{
|
||||
RequirementKeys = [prop];
|
||||
}
|
||||
|
||||
if (props.TryGetValue("activationtype", out var type))
|
||||
{
|
||||
var t = Enum.TryParse(type, true, out ActivationType activationType);
|
||||
|
|
@ -46,16 +50,19 @@ public partial class Switch3D : Interactable3D
|
|||
//TargetFunc = props["targetfunc"];
|
||||
//TargetName = props["targetname"];
|
||||
}
|
||||
|
||||
|
||||
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
|
||||
{
|
||||
var activationTypeToUse = activationType is ActivationType.Use ? ActivationType : activationType;
|
||||
|
||||
if (!MeetsRequirements()) return false;
|
||||
_activationSound?.Play();
|
||||
|
||||
if (!MeetsRequirements())
|
||||
{
|
||||
_denySound?.Play();
|
||||
return false;
|
||||
}
|
||||
|
||||
EmitSignal(SignalName.OnActivated, (int)activationTypeToUse);
|
||||
|
||||
|
||||
// Compatibility for old single system
|
||||
bool success = ActivateTarget(Target, activationTypeToUse);
|
||||
|
||||
|
|
@ -63,8 +70,20 @@ public partial class Switch3D : Interactable3D
|
|||
{
|
||||
ActivationHelper.UseTargets(this, TargetGroup, activationType);
|
||||
}
|
||||
|
||||
return Targets.Aggregate(success, (current, target) => ActivateTarget(target, activationTypeToUse) | success);
|
||||
|
||||
var result = Targets.Aggregate(success,
|
||||
(current, target) => ActivateTarget(target, activationTypeToUse) | success);
|
||||
|
||||
if (result)
|
||||
{
|
||||
_activationSound?.Play();
|
||||
}
|
||||
else
|
||||
{
|
||||
_denySound?.Play();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private bool ActivateTarget(Node target, ActivationType activationType = ActivationType.Toggle)
|
||||
|
|
@ -73,7 +92,7 @@ public partial class Switch3D : Interactable3D
|
|||
activable?.Activate(activationType);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// private void UseTargets(Node activator, string target)
|
||||
// {
|
||||
// GD.Print($"Trying to use targets called: {target}");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue