rabidus-test/Assets/Scripts/ShipMoveSides.cs

104 lines
2.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class ShipMoveSides : MonoBehaviour
{
public float MaxRollSpeed = 80;
public float MaxRollAngle = 45;
[SerializeField]
private float _radius;
[SerializeField]
[Range(-1,1)]
private float _value;
[SerializeField]
private bool _isEnable = true;
public bool IsBot = true;
private ShipPathFollower _pathFollower;
private PlayerInputHandler _playerInput;
private void Awake()
{
_pathFollower = GetComponent<ShipPathFollower>();
}
private void Start()
{
_playerInput = GetComponent<PlayerInputHandler>();
}
public float Radius => _radius;
private void Update()
{
if (!_isEnable)
{
ResetInput();
return;
}
ApplyMovement();
}
public void ToggleInput(bool value)
{
_isEnable = value;
}
private void ResetInput()
{
_value = Mathf.Lerp(_value, 0, Time.deltaTime * 0.5f);
var newPos = transform.localPosition;
newPos.x = 0;
transform.localPosition = Vector3.Lerp(transform.localPosition, newPos, Time.deltaTime * MaxRollSpeed * _pathFollower.SpeedPercent);
transform.localRotation = Quaternion.Euler(new Vector3(0, 0, _value * -MaxRollAngle * _pathFollower.SpeedPercent));
}
public void ChangeRollSpeed(float val)
{
MaxRollSpeed = val;
}
public void ChangeRollAngle(float val)
{
MaxRollAngle = val;
}
float newValue = 0;
public void UpdateInput(float val)
{
if (!_isEnable)
return;
newValue = -val;
}
public UnityEvent OnEdgeRiched;
private void ApplyMovement()
{
if (_playerInput != null)
_value = -_playerInput.Horizontal;
else
_value = Mathf.Lerp(_value, newValue, Time.deltaTime * 0.5f);
var newPos = transform.localPosition + _value * Vector3.right;
newPos.x = Mathf.Clamp(newPos.x, -_radius, _radius);
if (Mathf.Abs(newPos.x) == _radius)
{
OnEdgeRiched?.Invoke();
Debug.Log("Edge rich");
}
transform.localPosition = Vector3.Lerp(transform.localPosition, newPos, Time.deltaTime * MaxRollSpeed * _pathFollower.SpeedPercent);
transform.localRotation = Quaternion.Euler(new Vector3(0, 0, _value * -MaxRollAngle * _pathFollower.SpeedPercent));
}
}