Advertisement
aleksandr34

Untitled

Sep 27th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using _src.Scripts.PlayerFeatures.PlayerEntity;
  3. using _src.Scripts.PlayerFeatures.PlayerManagement;
  4. using _src.Scripts.PlayerFeatures.PlayerManagement.Enums;
  5. using Invector.vCharacterController;
  6. using Obi;
  7. using UnityEngine;
  8. using Zenject;
  9.  
  10.  
  11. namespace _src.Scripts.Core.Connection
  12. {
  13.     public class LengthController : MonoBehaviour
  14.     {
  15.         private ObiRope _obiRope;
  16.  
  17.         private ObiRopeCursor _cursor;
  18.  
  19.  
  20.         [SerializeField]
  21.         private float _ropeLengthResizeSpeed = 1;
  22.  
  23.  
  24.         [SerializeField]
  25.         private float _ropeMinimalLenght = 1;
  26.  
  27.  
  28.         [SerializeField]
  29.         private float _ropeMaximalLenght;
  30.  
  31.  
  32.         [SerializeField]
  33.         private List<RopeClimbing.RopeClimbing> _ropeClimbing = new();
  34.  
  35.  
  36.         [Inject]
  37.         private PlayersContainer _playersContainer;
  38.  
  39.  
  40.         private PlayerEntityAdapter _fathersEntity;
  41.  
  42.  
  43.         private PlayerEntityAdapter _sonEntity;
  44.  
  45.  
  46.         private float _previousRopeLenght;
  47.  
  48.  
  49.         private void Start()
  50.         {
  51.             _fathersEntity = _playersContainer.GetPlayerEntity(PlayerCharacter.Father);
  52.             _sonEntity = _playersContainer.GetPlayerEntity(PlayerCharacter.Son);
  53.             _obiRope = GetComponent<ObiRope>();
  54.             _cursor = GetComponent<ObiRopeCursor>();
  55.             _ropeClimbing[0] = _fathersEntity.GetComponent<RopeClimbing.RopeClimbing>();
  56.             _ropeClimbing[1] = _sonEntity.GetComponent<RopeClimbing.RopeClimbing>();
  57.             _obiRope.OnBeginStep += UpdateRopeLenght;
  58.         }
  59.        
  60.  
  61.         private void UpdateRopeLenght(ObiActor actor, float stepTime)
  62.         {
  63.             var playerDistance = Vector3.Distance(_fathersEntity.transform.position, _sonEntity.transform.position);
  64.             _fathersEntity.GetComponent<ObiRigidbody>().kinematicForParticles = !_ropeClimbing[0].IsCatchingRope;
  65.             _sonEntity.GetComponent<ObiRigidbody>().kinematicForParticles = !_ropeClimbing[1].IsCatchingRope;
  66.  
  67.  
  68.             if (_obiRope.restLength < _ropeMinimalLenght || !_fathersEntity.VThirdPersonController.isGrounded || !_sonEntity.VThirdPersonController.isGrounded)
  69.                 return;
  70.  
  71.             if (_ropeClimbing[0].IsCatchingRope || _ropeClimbing[1].IsCatchingRope && IsPlayersOnGround())
  72.             {
  73.                 if (_previousRopeLenght > playerDistance)
  74.                 {
  75.                     _cursor.ChangeLength(Mathf.Clamp(playerDistance, _ropeMinimalLenght, _ropeMaximalLenght));
  76.                 }
  77.             }
  78.  
  79.             _previousRopeLenght = playerDistance - 0.01f;
  80.         }
  81.  
  82.         private bool IsPlayersOnGround()
  83.             => _fathersEntity.VThirdPersonController.isGrounded && _sonEntity.VThirdPersonController.isGrounded;
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement