using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum WaveMovementDirection
{
horizontal,
vertical
}
public class WaveMovement : MonoBehaviour
{
[SerializeField] private bool playOnStart = false;
[SerializeField] private bool disableEventCall = false;
[Header("Wave Settings")]
[SerializeField] private Transform _parentTransform;
[SerializeField] private WaveMovementDirection waveDirection;
[SerializeField] private float maxWave = 10f;
[Header("Movement Settings")]
[SerializeField] private bool moveObject = false;
[SerializeField] private float moveSpeed = 0f;
[SerializeField] private Vector2 moveDirection;
//Internal vars
private bool _canMove = false;
private float _waveNum = 0f;
private float _yMotion = 0f;
private float _xMotion = 0f;
private Vector3 _movementVector;
private Transform _transform;
private float _xPos;
private float _yPos;
private void Awake()
{
//Cache the transform
_transform = transform;
}
// Start is called before the first frame update
void Start()
{
//Check if the motion should play on start
if (playOnStart)
_canMove = true;
}
// Update is called once per frame
void FixedUpdate()
{
//Check if we can move
if(_canMove)
WaveMove();
}
private void WaveMove()
{
//Get the current x and y positions
_xPos = _transform.position.x;
_yPos = _transform.position.y;
//Check which direction we need to calculate
if (waveDirection == WaveMovementDirection.horizontal)
{
//Calculate horizontal motion
_xMotion = _parentTransform != null ? _parentTransform.position.x : _transform.position.x;
_xPos = _xMotion + Mathf.Cos(_waveNum) * maxWave;
//Check if the object should move vertically
if (moveObject)
_yPos += moveSpeed * moveDirection.y;
}
else
{
//Calculate vertical motion
_yMotion = _parentTransform != null ? _parentTransform.position.y : _transform.position.y;
_yPos = _yMotion + Mathf.Cos(_waveNum) * maxWave;
//Check if the object should move horizontally
if (moveObject)
_xPos += moveSpeed * moveDirection.x;
}
//Assign x and y positions to transform
_movementVector.Set(_xPos, _yPos, 0);
_transform.position = _movementVector;
//Increment _waveNum to get the motion going
_waveNum += 0.1f;
}
public void PlayWaveMovement(bool value)
{
if(!disableEventCall)
_canMove = value;
}
}






