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

55 lines
1.4 KiB
C#
Raw Normal View History

using UnityEngine;
namespace Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public class EnemyMovement : MonoBehaviour, IMovement
{
protected Rigidbody2D _rigidbody2D = null;
protected Vector2 gravityForce = Vector2.down;
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>();
_rigidbody2D.gravityScale = 0f;
gravityForce *= -Physics2D.gravity.y * _rigidbody2D.mass;
}
protected virtual void FixedUpdate()
{
_rigidbody2D.AddRelativeForce(gravityForce);
if (IsPaused)
return;
Vector2 velocity = transform.InverseTransformDirection(_rigidbody2D.velocity);
velocity.x = moveValue;
_rigidbody2D.velocity = transform.TransformDirection(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;
}
}