using System; using Movement; using UnityEngine; namespace Platforms { public class MovingPlatform : MonoBehaviour, IMovement { public float xOffset; public float yOffset; private Vector3 _originalPos; private Vector3 _futurePos; private const float VerificationOffset = 0.04f; private bool _goingToFuturePos; private Rigidbody2D _platformRigidbody; private void Awake() { _platformRigidbody = GetComponent(); } private void Start() { _originalPos = transform.position; _futurePos = new Vector3(_originalPos.x + xOffset, _originalPos.y + yOffset, _originalPos.z); _goingToFuturePos = true; } private void Update() { if (!IsPaused) Move(BaseSpeed); } // PAUSING METHODS public bool IsPaused { get; private set; } public void Pause() { IsPaused = true; _platformRigidbody.simulated = !IsPaused; } public void Resume() { IsPaused = false; _platformRigidbody.simulated = !IsPaused; } // MOVEMENT METHODS public float BaseSpeed { get; set; } public void Move(float value) { switch (_goingToFuturePos) { case true: _platformRigidbody.AddForce(Vector2.left * 100.0f * Time.deltaTime, ForceMode2D.Impulse); if (Math.Abs(_futurePos.x - transform.position.x) < VerificationOffset && Math.Abs(_futurePos.y - transform.position.y) < VerificationOffset) _goingToFuturePos = false; break; case false: _platformRigidbody.AddForce(Vector2.left * -100.0f * Time.deltaTime, ForceMode2D.Impulse); if (Math.Abs(_originalPos.x - transform.position.x) < VerificationOffset && Math.Abs(_originalPos.y - transform.position.y) < VerificationOffset) _goingToFuturePos = true; break; } } } }