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

47 lines
1.1 KiB
C#
Raw Normal View History

using UnityEngine;
namespace Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public class EnemyMovement : MonoBehaviour, IMovement
{
protected Rigidbody2D _rigidbody2D = null;
protected bool _isPaused = false;
protected float moveValue = 0f;
public float BaseSpeed { get; set; } = 1f;
public bool IsPaused => _isPaused;
protected virtual void Awake()
=> _rigidbody2D = GetComponent<Rigidbody2D>();
protected virtual 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();
}
protected void UpdateRigidbody()
=> _rigidbody2D.simulated = !_isPaused;
}
}