Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerBehaviour : MonoBehaviour, ITakeIcicleDamage
- {
- [SerializeField] private CameraBehaviour _cameraBehaviour;
- [SerializeField] private Camera _camera;
- [SerializeField] private float _maxVerticalSpeed;
- [SerializeField] private float _maxHorizontalSpeed;
- private PlayerState _state;
- private Rigidbody _rigidbody;
- private Animator _animator;
- private BoxCollider _boxCollider;
- private Vector3 _finishPosition;
- private Vector3 _currentTouchPosition;
- private Vector3 _lastTouchPosition;
- private float _verticalSpeed;
- private float _horizontalSpeed;
- private bool _canMove = true;
- private void Start()
- {
- _animator = GetComponent<Animator>();
- _rigidbody = GetComponent<Rigidbody>();
- _boxCollider = GetComponent<BoxCollider>();
- _horizontalSpeed = _maxHorizontalSpeed;
- _verticalSpeed = _maxVerticalSpeed;
- }
- private void ControlInput()
- {
- if (Input.GetMouseButton(0))
- {
- _currentTouchPosition = _camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Mathf.Abs(_cameraBehaviour.GetOffset().z)));
- if (_lastTouchPosition == Vector3.zero)
- _lastTouchPosition = _currentTouchPosition;
- Vector3 offset = _currentTouchPosition - _lastTouchPosition;
- MoveHorizontal(offset.x);
- MoveVertical();
- offset = Vector3.zero;
- _lastTouchPosition = _currentTouchPosition;
- }
- else
- {
- _rigidbody.velocity = Vector3.zero;
- _currentTouchPosition = Vector3.zero;
- _lastTouchPosition = Vector3.zero;
- _state = PlayerState.Idle;
- }
- }
- private void SetAnimation()
- {
- _animator.SetInteger("State", (int)_state);
- }
- private void MoveHorizontal(float distance)
- {
- if (Mathf.Abs(distance) > 0.01f)
- {
- _verticalSpeed = _maxVerticalSpeed * 0.2f;
- distance = Mathf.Clamp(distance, -0.5f, 0.5f);
- transform.RotateAround(Vector3.zero, -Vector3.up, distance * _horizontalSpeed * Time.deltaTime * 10f);
- transform.LookAt(new Vector3(0, transform.position.y, 0));
- _state = PlayerState.ClimbingHorizontal;
- }
- else
- {
- _verticalSpeed = _maxVerticalSpeed;
- _state = PlayerState.Idle;
- }
- }
- private void MoveVertical()
- {
- if (_state != PlayerState.ClimbingHorizontal)
- {
- _state = PlayerState.ClimbingVertical;
- }
- _rigidbody.velocity = new Vector3(0, _verticalSpeed * Time.deltaTime, 0);
- }
- private void ClampPosition()
- {
- Vector3 zeroY = new Vector3(transform.position.x, 0, transform.position.z);
- float distance = Mathf.Abs(Vector3.Distance(zeroY, Vector3.zero));
- if (distance > 2.5f)
- {
- Vector3 clamedPosition = zeroY.normalized * 2.5f;
- clamedPosition.y = transform.position.y;
- transform.position = clamedPosition;
- }
- }
- private bool CheckPosition()
- {
- return transform.position.y >= _finishPosition.y;
- }
- private void Kill()
- {
- DisableMovement();
- Drop();
- _state = PlayerState.Falling;
- EventManager.Call(LevelEvents.ClimbingFailed);
- }
- private void DisableMovement()
- {
- _canMove = false;
- _rigidbody.velocity = Vector3.zero;
- }
- private void Drop()
- {
- _rigidbody.useGravity = true;
- _boxCollider.isTrigger = true;
- Vector3 direction = transform.position.normalized;
- direction.y = -0.2f;
- _rigidbody.AddForce(direction * 2.5f, ForceMode.Impulse);
- }
- void ITakeIcicleDamage.TakeIcicleDamage()
- {
- Kill();
- Debug.Log("You lose");
- }
- private void Update()
- {
- if (_canMove)
- {
- ControlInput();
- ClampPosition();
- if (CheckPosition())
- {
- DisableMovement();
- EventManager.Call(LevelEvents.ClimbingFinished);
- Debug.Log("Finish");
- }
- }
- SetAnimation();
- }
- private void GetFinishPosition(object[] parameters)
- {
- _finishPosition = LevelManager.Instance.GetFinishPosition();
- }
- private void OnEnable()
- {
- EventManager.Subscribe(LevelEvents.ChunksSpawned, GetFinishPosition);
- }
- private void OnDisable()
- {
- EventManager.Unsubscribe(LevelEvents.ChunksSpawned, GetFinishPosition);
- }
- }
- public enum PlayerState
- {
- Idle,
- ClimbingVertical,
- ClimbingHorizontal,
- Falling
- }
Advertisement
Add Comment
Please, Sign In to add comment