IntroGames

Untitled

Nov 16th, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.90 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerBehaviour : MonoBehaviour, ITakeIcicleDamage
  6. {
  7.     [SerializeField] private CameraBehaviour _cameraBehaviour;
  8.     [SerializeField] private Camera _camera;
  9.  
  10.     [SerializeField] private float _maxVerticalSpeed;
  11.     [SerializeField] private float _maxHorizontalSpeed;
  12.  
  13.     private PlayerState _state;
  14.     private Rigidbody _rigidbody;
  15.     private Animator _animator;
  16.     private BoxCollider _boxCollider;
  17.  
  18.     private Vector3 _finishPosition;
  19.     private Vector3 _currentTouchPosition;
  20.     private Vector3 _lastTouchPosition;
  21.  
  22.     private float _verticalSpeed;
  23.     private float _horizontalSpeed;
  24.  
  25.     private bool _canMove = true;
  26.  
  27.     private void Start()
  28.     {
  29.         _animator = GetComponent<Animator>();
  30.         _rigidbody = GetComponent<Rigidbody>();
  31.         _boxCollider = GetComponent<BoxCollider>();
  32.  
  33.         _horizontalSpeed = _maxHorizontalSpeed;
  34.         _verticalSpeed = _maxVerticalSpeed;
  35.     }
  36.  
  37.     private void ControlInput()
  38.     {
  39.         if (Input.GetMouseButton(0))
  40.         {
  41.             _currentTouchPosition = _camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Mathf.Abs(_cameraBehaviour.GetOffset().z)));
  42.  
  43.             if (_lastTouchPosition == Vector3.zero)
  44.                 _lastTouchPosition = _currentTouchPosition;
  45.  
  46.             Vector3 offset = _currentTouchPosition - _lastTouchPosition;
  47.  
  48.             MoveHorizontal(offset.x);
  49.             MoveVertical();
  50.  
  51.             offset = Vector3.zero;
  52.             _lastTouchPosition = _currentTouchPosition;
  53.  
  54.         }
  55.         else
  56.         {
  57.             _rigidbody.velocity = Vector3.zero;
  58.             _currentTouchPosition = Vector3.zero;
  59.             _lastTouchPosition = Vector3.zero;
  60.  
  61.             _state = PlayerState.Idle;
  62.         }
  63.     }
  64.  
  65.     private void SetAnimation()
  66.     {
  67.         _animator.SetInteger("State", (int)_state);
  68.     }
  69.  
  70.     private void MoveHorizontal(float distance)
  71.     {
  72.         if (Mathf.Abs(distance) > 0.01f)
  73.         {
  74.             _verticalSpeed = _maxVerticalSpeed * 0.2f;
  75.  
  76.             distance = Mathf.Clamp(distance, -0.5f, 0.5f);
  77.  
  78.             transform.RotateAround(Vector3.zero, -Vector3.up, distance * _horizontalSpeed * Time.deltaTime * 10f);
  79.             transform.LookAt(new Vector3(0, transform.position.y, 0));
  80.  
  81.             _state = PlayerState.ClimbingHorizontal;
  82.         }
  83.         else
  84.         {
  85.             _verticalSpeed = _maxVerticalSpeed;
  86.  
  87.             _state = PlayerState.Idle;
  88.         }
  89.     }
  90.  
  91.     private void MoveVertical()
  92.     {
  93.         if (_state != PlayerState.ClimbingHorizontal)
  94.         {
  95.             _state = PlayerState.ClimbingVertical;
  96.         }
  97.  
  98.         _rigidbody.velocity = new Vector3(0, _verticalSpeed * Time.deltaTime, 0);
  99.     }
  100.  
  101.     private void ClampPosition()
  102.     {
  103.         Vector3 zeroY = new Vector3(transform.position.x, 0, transform.position.z);
  104.         float distance = Mathf.Abs(Vector3.Distance(zeroY, Vector3.zero));
  105.  
  106.         if (distance > 2.5f)
  107.         {
  108.             Vector3 clamedPosition = zeroY.normalized * 2.5f;
  109.             clamedPosition.y = transform.position.y;
  110.  
  111.             transform.position = clamedPosition;
  112.         }
  113.     }
  114.  
  115.     private bool CheckPosition()
  116.     {
  117.         return transform.position.y >= _finishPosition.y;
  118.     }
  119.  
  120.     private void Kill()
  121.     {
  122.         DisableMovement();
  123.         Drop();
  124.  
  125.         _state = PlayerState.Falling;
  126.         EventManager.Call(LevelEvents.ClimbingFailed);
  127.     }
  128.  
  129.     private void DisableMovement()
  130.     {
  131.         _canMove = false;
  132.         _rigidbody.velocity = Vector3.zero;
  133.     }
  134.  
  135.     private void Drop()
  136.     {
  137.         _rigidbody.useGravity = true;
  138.         _boxCollider.isTrigger = true;
  139.  
  140.         Vector3 direction = transform.position.normalized;
  141.         direction.y = -0.2f;
  142.  
  143.         _rigidbody.AddForce(direction * 2.5f, ForceMode.Impulse);
  144.     }
  145.  
  146.     void ITakeIcicleDamage.TakeIcicleDamage()
  147.     {
  148.         Kill();
  149.  
  150.         Debug.Log("You lose");
  151.     }
  152.  
  153.     private void Update()
  154.     {
  155.         if (_canMove)
  156.         {
  157.             ControlInput();
  158.             ClampPosition();
  159.  
  160.             if (CheckPosition())
  161.             {
  162.                 DisableMovement();
  163.  
  164.                 EventManager.Call(LevelEvents.ClimbingFinished);
  165.                 Debug.Log("Finish");
  166.             }
  167.         }
  168.  
  169.         SetAnimation();
  170.     }
  171.  
  172.     private void GetFinishPosition(object[] parameters)
  173.     {
  174.         _finishPosition = LevelManager.Instance.GetFinishPosition();
  175.     }
  176.  
  177.     private void OnEnable()
  178.     {
  179.         EventManager.Subscribe(LevelEvents.ChunksSpawned, GetFinishPosition);
  180.     }
  181.  
  182.     private void OnDisable()
  183.     {
  184.         EventManager.Unsubscribe(LevelEvents.ChunksSpawned, GetFinishPosition);
  185.     }
  186. }
  187.  
  188. public enum PlayerState
  189. {
  190.     Idle,
  191.     ClimbingVertical,
  192.     ClimbingHorizontal,
  193.     Falling
  194. }
Advertisement
Add Comment
Please, Sign In to add comment