55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
using Code.Infrastructure.Configs;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Example.Platformer.Entity
|
|
{
|
|
public abstract class Entity : MonoBehaviour
|
|
{
|
|
private static readonly int VelocityY = Animator.StringToHash("VelocityY");
|
|
private static readonly int VelocityX = Animator.StringToHash("VelocityX");
|
|
private static readonly int OnGround = Animator.StringToHash("OnGround");
|
|
|
|
[SerializeField] protected Rigidbody2D rigidbody2D;
|
|
[SerializeField] protected Animator animator;
|
|
[SerializeField] protected SpriteRenderer spriteRenderer;
|
|
[SerializeField] private Vector2 groundPoint;
|
|
|
|
private PlatformerEntityData _data;
|
|
|
|
public void SetData(PlatformerEntityData data)
|
|
{
|
|
_data = data;
|
|
}
|
|
|
|
protected bool onGround;
|
|
|
|
protected virtual void Update()
|
|
{
|
|
animator.SetFloat(VelocityX, rigidbody2D.linearVelocityX);
|
|
animator.SetFloat(VelocityY, rigidbody2D.linearVelocityY);
|
|
}
|
|
|
|
protected virtual void HandleFlipX() => spriteRenderer.flipX = rigidbody2D.linearVelocity.x < 0.01f;
|
|
|
|
protected virtual void FixedUpdate()
|
|
{
|
|
onGround = CheckGround();
|
|
animator.SetBool(OnGround, onGround);
|
|
|
|
Move();
|
|
|
|
if(rigidbody2D.linearVelocity.x is > 0.01f or < -0.01f) HandleFlipX();
|
|
}
|
|
|
|
protected virtual bool CheckGround()
|
|
{
|
|
UnityEngine.Debug.DrawLine(transform.position,(Vector2)transform.position + groundPoint, Color.green);
|
|
return Physics2D.OverlapPoint((Vector2)transform.position + groundPoint, LayerMasks.Masks[LayerMaskType.PlatformerGround]);
|
|
}
|
|
|
|
protected abstract void Move();
|
|
|
|
public virtual void OnGetDamage(){}
|
|
public virtual void OnDeath(){}
|
|
}
|
|
} |