BGJ-2022.1/Assets/Scripts/Movement/EnemyMovement.cs

47 lines
1.1 KiB
C#

using UnityEngine;
namespace Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public class EnemyMovement : MonoBehaviour, IMovement
{
private Rigidbody2D _rigidbody2D = null;
private bool _isPaused = false;
private float moveValue = 0f;
public float BaseSpeed { get; set; } = 1f;
public bool IsPaused => _isPaused;
private void Awake()
=> _rigidbody2D = GetComponent<Rigidbody2D>();
private void FixedUpdate()
{
if (IsPaused)
return;
Vector2 velocity = _rigidbody2D.velocity;
velocity.x = moveValue;
_rigidbody2D.velocity = velocity;
}
public void Move(float value)
=> moveValue = value * BaseSpeed;
public void Pause()
{
_isPaused = true;
UpdateRigidbody();
}
public void Resume()
{
_isPaused = false;
UpdateRigidbody();
}
private void UpdateRigidbody()
=> _rigidbody2D.simulated = !_isPaused;
}
}