2022-02-21 15:53:23 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Movement
|
|
|
|
{
|
|
|
|
[RequireComponent(typeof(Rigidbody2D))]
|
|
|
|
public class EnemyMovement : MonoBehaviour, IMovement
|
|
|
|
{
|
2022-02-21 19:30:46 +03:00
|
|
|
protected Rigidbody2D _rigidbody2D = null;
|
2022-02-22 00:47:26 +03:00
|
|
|
protected Vector2 gravityForce = Vector2.down;
|
2022-02-21 19:30:46 +03:00
|
|
|
protected bool _isPaused = false;
|
|
|
|
protected float moveValue = 0f;
|
2022-02-21 15:53:23 +03:00
|
|
|
|
|
|
|
public float BaseSpeed { get; set; } = 1f;
|
|
|
|
public bool IsPaused => _isPaused;
|
|
|
|
|
2022-02-21 19:30:46 +03:00
|
|
|
protected virtual void Awake()
|
2022-02-22 00:47:26 +03:00
|
|
|
{
|
|
|
|
_rigidbody2D = GetComponent<Rigidbody2D>();
|
|
|
|
|
|
|
|
_rigidbody2D.gravityScale = 0f;
|
|
|
|
gravityForce *= -Physics2D.gravity.y * _rigidbody2D.mass;
|
|
|
|
}
|
2022-02-21 15:53:23 +03:00
|
|
|
|
2022-02-21 19:30:46 +03:00
|
|
|
protected virtual void FixedUpdate()
|
2022-02-21 15:53:23 +03:00
|
|
|
{
|
2022-02-22 00:47:26 +03:00
|
|
|
_rigidbody2D.AddRelativeForce(gravityForce);
|
|
|
|
|
2022-02-21 15:53:23 +03:00
|
|
|
if (IsPaused)
|
|
|
|
return;
|
|
|
|
|
2022-02-22 00:47:26 +03:00
|
|
|
Vector2 velocity = transform.InverseTransformDirection(_rigidbody2D.velocity);
|
2022-02-21 15:53:23 +03:00
|
|
|
velocity.x = moveValue;
|
2022-02-22 00:47:26 +03:00
|
|
|
_rigidbody2D.velocity = transform.TransformDirection(velocity);
|
2022-02-21 15:53:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Move(float value)
|
|
|
|
=> moveValue = value * BaseSpeed;
|
|
|
|
|
|
|
|
public void Pause()
|
|
|
|
{
|
|
|
|
_isPaused = true;
|
|
|
|
UpdateRigidbody();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Resume()
|
|
|
|
{
|
|
|
|
_isPaused = false;
|
|
|
|
UpdateRigidbody();
|
|
|
|
}
|
|
|
|
|
2022-02-21 19:30:46 +03:00
|
|
|
protected void UpdateRigidbody()
|
2022-02-21 15:53:23 +03:00
|
|
|
=> _rigidbody2D.simulated = !_isPaused;
|
|
|
|
}
|
|
|
|
}
|