Skip to content

Commit

Permalink
integrate ragdolling into position control
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Nov 4, 2024
1 parent 7c4e977 commit c71ff17
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7382,6 +7382,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
_movementController: {fileID: 1215874948772008144}
_animator: {fileID: 7307990926274805850}
_networkAnimator: {fileID: 5337792808514694743}
_lerpMultiplier: 5
_aimController: {fileID: 3054724180155005980}
_positionController: {fileID: 7568747683536695618}
Expand Down Expand Up @@ -7435,6 +7436,8 @@ MonoBehaviour:
_addedNetworkObject: {fileID: 241836927429143711}
_networkObjectCache: {fileID: 241836927429143711}
_feetController: {fileID: 2081771184023489930}
_getUpFromFront: {fileID: -203655887218126122, guid: b45b348d9bf3bcd4f9c45e322acd2a50, type: 3}
_getUpFromBack: {fileID: -203655887218126122, guid: 83207c9bf0069534487201e6d71c0de1, type: 3}
_position: 0
_movement: 0
_previousMovement: 0
Expand Down Expand Up @@ -7529,13 +7532,8 @@ MonoBehaviour:
_characterRigidBody: {fileID: 8635375509376998503}
_hips: {fileID: 5433592588855148907}
_character: {fileID: 3714097311666575904}
_animator: {fileID: 7307990926274805850}
_networkAnimator: {fileID: 5337792808514694743}
_humanoidLivingController: {fileID: 1215874948772008144}
_standUpFaceUpClip: {fileID: -203655887218126122, guid: 83207c9bf0069534487201e6d71c0de1, type: 3}
_standUpFaceDownClip: {fileID: -203655887218126122, guid: b45b348d9bf3bcd4f9c45e322acd2a50, type: 3}
_ragdollPartSyncInterval: 1
IsKnockedDown: 0
_positionController: {fileID: 7568747683536695618}
--- !u!114 &7601720951202063522
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -7551,7 +7549,7 @@ MonoBehaviour:
_componentIndexCache: 14
_addedNetworkObject: {fileID: 241836927429143711}
_networkObjectCache: {fileID: 241836927429143711}
_ragdoll: {fileID: 2768583506857238720}
_positionController: {fileID: 7568747683536695618}
_timeRagdolled: 3
--- !u!114 &8615535220686661918
MonoBehaviour:
Expand Down
13 changes: 10 additions & 3 deletions Assets/Scripts/SS3D/Hacks/RagdollWhenPressingButton.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SS3D.Core;
using SS3D.Core.Behaviours;
using SS3D.Systems.Animations;
using SS3D.Systems.Entities.Humanoid;
using SS3D.Systems.Inputs;
using UnityEngine;
Expand All @@ -14,7 +15,7 @@ namespace SS3D.Hacks
public class RagdollWhenPressingButton : NetworkActor
{
[SerializeField]
private Ragdoll _ragdoll;
private PositionController _positionController;

[SerializeField]
private float _timeRagdolled;
Expand All @@ -33,8 +34,14 @@ public override void OnStartClient()

private void HandleKnockdown(InputAction.CallbackContext context)
{
_ragdoll.Knockdown(_timeRagdolled);
if (_positionController.Position != PositionType.Ragdoll)
{
_positionController.KnockDown();
}
else
{
_positionController.StopRagdoll();
}
}
}
}

60 changes: 57 additions & 3 deletions Assets/Scripts/SS3D/Systems/Animations/PositionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace SS3D.Systems.Animations
public class PositionController : NetworkActor
{

public Action<PositionType> ChangedPosition;
public Action<PositionType, float> ChangedPosition;

public Action<MovementType> ChangedMovement;

Expand All @@ -22,6 +22,12 @@ public class PositionController : NetworkActor
[SerializeField]
private FeetController _feetController;

[SerializeField]
private AnimationClip _getUpFromFront;

[SerializeField]
private AnimationClip _getUpFromBack;


[SyncVar(OnChange = nameof(SyncPosition))]
private PositionType _position;
Expand All @@ -43,14 +49,28 @@ public class PositionController : NetworkActor

private const float MinFeetHealthFactorToStand = 0.5f;

private const float RagdollRecoverTime = 1f;

private const float RagdollBoneResetTime = 0.7f;

private const float DefaultTransitionDuration = 0.2f;

public PositionType Position => _position;

public PositionType PreviousPosition => _position;

public MovementType Movement => _movement;

public bool CanGrab => _standingAbility && _movement == MovementType.Normal && _position != PositionType.Sitting;

public bool CanSit => _standingAbility;

public bool CanRotateWhileAiming => Movement == MovementType.Aiming && Position != PositionType.Sitting && Position != PositionType.Proning;

public AnimationClip GetRecoverFromRagdollClip()
{
return GetComponent<Ragdoll>().IsFacingDown ? _getUpFromFront : _getUpFromBack;
}

public override void OnStartServer()
{
Expand Down Expand Up @@ -93,7 +113,18 @@ private void RpcDance()

private void SyncPosition(PositionType oldPosition, PositionType newPosition, bool asServer)
{
ChangedPosition?.Invoke(newPosition);
float duration = DefaultTransitionDuration;

if (newPosition == PositionType.ResetBones)
{
duration = RagdollBoneResetTime;
}
else if(newPosition == PositionType.RagdollRecover)
{
duration = RagdollRecoverTime;
}

ChangedPosition?.Invoke(newPosition, duration);
}

private void SyncMovement(MovementType oldPosition, MovementType newPosition, bool asServer)
Expand All @@ -109,27 +140,50 @@ private void SyncDance(bool wasDancing, bool isDancing, bool asServer)
[Client]
public void TrySit()
{
if(_position == PositionType.Ragdoll) return;
RpcChangePosition(PositionType.Sitting);
}

[Client]
public void Prone()
{
if(_position == PositionType.Ragdoll) return;
RpcChangePosition(PositionType.Proning);
}

[Client]
public void TryCrouch()
{
if(_position == PositionType.Ragdoll) return;
RpcChangePosition(_standingAbility ? PositionType.Crouching : PositionType.Proning);
}

[Client]
public void TryToStandUp()
{
if(_position == PositionType.Ragdoll) return;
RpcChangePosition(_standingAbility ? PositionType.Standing : PositionType.Proning);
}

[Client]
public void KnockDown()
{
RpcChangePosition(PositionType.Ragdoll);
}

[Client]
public void StopRagdoll()
{
RpcChangePosition(PositionType.ResetBones);
Invoke(nameof(RagdollRecover), RagdollBoneResetTime);
Invoke(nameof(TryToStandUp), RagdollRecoverTime + RagdollBoneResetTime);
}

private void RagdollRecover()
{
RpcChangePosition(PositionType.RagdollRecover);
}

[Client]
public void TryToGetToPreviousPosition()
{
Expand Down Expand Up @@ -194,7 +248,7 @@ private void OnFeetHealthChanged(float feetHealth)

if (_standingAbility == false)
{
GetComponent<Ragdoll>().Knockdown(5f);
KnockDown();
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/SS3D/Systems/Animations/PositionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ public enum PositionType
Sitting = 1,
Proning = 2,
Crouching = 3,
Ragdoll = 4,
RagdollRecover = 5,
ResetBones = 6,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override bool Start(InteractionEvent interactionEvent, InteractionReferen
if (target is IGameObjectProvider targetBehaviour && targetBehaviour.GameObject.GetComponentInParent<Entity>() != null )
{
Entity entity = targetBehaviour.GameObject.GetComponentInParent<Entity>();
entity.GetComponent<Ragdoll>().Knockdown(120f);
entity.GetComponent<PositionController>().KnockDown();
//BodyPart bodyPart = entity.GetComponentInChildren<BodyPart>();

// Inflict a fix amount and type of damages for now. Long term, should be passed in parameter and depends on weapon type, velocity ...
Expand Down
5 changes: 3 additions & 2 deletions Assets/Scripts/SS3D/Systems/Entities/Human.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine;
using Coimbra;
using SS3D.Core;
using SS3D.Systems.Animations;

namespace SS3D.Systems.Entities
{
Expand All @@ -31,9 +32,9 @@ private void BecomeGhost(GameObject player, GameObject ghost)
mindSystem.SwapMinds(originEntity, ghostEntity);

RpcUpdateGhostPosition(originEntity, ghostEntity);
if (TryGetComponent(out Ragdoll ragdoll))
if (TryGetComponent(out PositionController positionController))
{
ragdoll.KnockdownTimeless();
positionController.KnockDown();
}
RpcDestroyComponents(originEntity);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FishNet;
using FishNet.Component.Animating;
using SS3D.Core.Behaviours;
using SS3D.Systems.Animations;
using SS3D.Systems.Inventory.Containers;
Expand All @@ -12,12 +13,17 @@ public class HumanoidAnimatorController : Actor
[SerializeField] private HumanoidMovementController _movementController;

[SerializeField] private Animator _animator;

[SerializeField] private NetworkAnimator _networkAnimator;

[SerializeField] private float _lerpMultiplier;

[SerializeField] private AimController _aimController;

[SerializeField] private PositionController _positionController;

private bool _isRagdolled;

protected override void OnStart()
{
base.OnStart();
Expand Down Expand Up @@ -91,7 +97,6 @@ private void SubscribeToEvents()
_positionController.ChangedPosition += HandlePositionChanged;
_positionController.ChangedMovement += HandleMovementChanged;
_positionController.Dance += HandleDance;

}

private void HandleDance(bool isDancing)
Expand All @@ -112,8 +117,13 @@ private void HandleMovementChanged(MovementType movementType)
}
}

private void HandlePositionChanged(PositionType position)
private void HandlePositionChanged(PositionType position, float transitionDuration = 0.15f)
{
if (position != PositionType.Ragdoll && position != PositionType.ResetBones)
{
ToggleAnimator(true);
}

switch (position)
{
case PositionType.Proning:
Expand All @@ -128,9 +138,29 @@ private void HandlePositionChanged(PositionType position)
case PositionType.Sitting:
Sit();
break;
case PositionType.RagdollRecover:
RagdollRecover(transitionDuration);
break;
case PositionType.Ragdoll:
ToggleAnimator(false);
break;
}
}

private void RagdollRecover(float transitionDuration)
{
AnimationClip clipToPlay = _positionController.GetRecoverFromRagdollClip();
float playbackSpeed = clipToPlay.length / transitionDuration;
_animator.speed = playbackSpeed;
_animator.Play(clipToPlay.name);
Invoke(nameof(RestoreAnimatorSpeed), transitionDuration);
}

private void RestoreAnimatorSpeed()
{
_animator.speed = 1;
}

private void Sit(float transitionDuration = 0.15f)
{
_animator.CrossFade("Sit", transitionDuration);
Expand Down Expand Up @@ -160,7 +190,6 @@ private void HandleAimInAnimatorControler(bool isAiming, bool toThrow)
{

UnityEngine.Debug.Log("animator change aim");

_animator.SetBool("Aim", isAiming);
_animator.SetBool("TorsoGunAim", !toThrow);

Expand All @@ -187,5 +216,11 @@ private void UnsubscribeFromEvents()
_movementController.OnSpeedChangeEvent -= UpdateSpeedParamater;
}

private void ToggleAnimator(bool enabled)
{
_animator.enabled = enabled;
_networkAnimator.enabled = enabled;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace SS3D.Systems.Entities.Humanoid
public class HumanoidMovementController : NetworkActor
{
public event Action<float> OnSpeedChangeEvent;
public event Action<MovementType> OnMovementTypeChanged;

private const float DefaultSpeed = 1f;
private const float RunFactor = 2f;
Expand Down Expand Up @@ -133,7 +132,7 @@ private void ProcessCharacterMovement()
RotatePlayerToMovement(_positionController.Movement == MovementType.Dragging);
}

if (_positionController.Movement == MovementType.Aiming && _positionController.Position != PositionType.Sitting && _positionController.Position != PositionType.Proning)
if (_positionController.CanRotateWhileAiming)
{
RotatePlayerTowardTarget();
}
Expand Down Expand Up @@ -225,6 +224,12 @@ private void Setup()
_inputSystem.ToggleActionMap(_movementControls, true);
_inputSystem.ToggleActionMap(_hotkeysControls, true);
InstanceFinder.TimeManager.OnTick += HandleNetworkTick;
_positionController.ChangedPosition += HandleChangedPosition;
}

private void HandleChangedPosition(PositionType position, float durationTransition)
{
enabled = position != PositionType.RagdollRecover && position != PositionType.Ragdoll && position != PositionType.ResetBones;
}

[Client]
Expand Down
Loading

0 comments on commit c71ff17

Please sign in to comment.