Files
Gnome-s-Bounty/Assets/Scripts/Character.cs
T
2023-07-01 11:08:57 +03:00

145 lines
4.1 KiB
C#

using UnityEngine;
public abstract class Character : MonoBehaviour
{
[SerializeField]
protected Animator _animator;
[SerializeField]
private float MovementSpeed = 1.5f;
[SerializeField]
protected GameObject _bonesSide;
[SerializeField]
protected GameObject _bonesBack;
[SerializeField]
private LayerMask _mapLayer;
private Rigidbody2D _body;
//private BoxCollider2D _boxCollider;
private CapsuleCollider2D _boxCollider;
protected bool _isOnBridge;
protected bool _isOnLadder;
protected bool _isFalling;
protected bool _facingRight = true;
protected bool isAllowVertical = true;
protected bool isAllowRight = true;
protected bool isAllowLeft = true;
float v_movement = 0;
private void Start()
{
_body = GetComponent<Rigidbody2D>();
_boxCollider = GetComponent<CapsuleCollider2D>();
}
protected void MoveTo(float inputHorizontal, float inputVertical)
{
var mapElement = GetMapElement(Vector2.down);
_isOnBridge = mapElement == MapElementType.Bridge && !_isFalling;
if (mapElement == MapElementType.Wall || _isOnLadder
|| _isOnBridge || mapElement == MapElementType.BreakableWall)
{
var leftCheck = GetMapElement(Vector2.left);
var rightCheck = GetMapElement(Vector2.right);
isAllowLeft = !(leftCheck == MapElementType.Wall || leftCheck == MapElementType.BreakableWall);
isAllowRight = !(rightCheck == MapElementType.Wall || rightCheck == MapElementType.BreakableWall);
isAllowVertical = false;
_isFalling = false;
float h_movement = inputHorizontal;
if (h_movement > 0 && !_facingRight || h_movement < 0 && _facingRight)
{
FlipCharacter();
}
SetWalkingAnimation(h_movement != 0);
var upperElement = GetMapElement(Vector2.up);
_isOnLadder = mapElement == MapElementType.Ladder || upperElement == MapElementType.Ladder;
if (_isOnLadder)
{
isAllowVertical = true;
_isOnLadder = true;
float v_movement = inputVertical;
if (v_movement > 0 && upperElement != MapElementType.Ladder)
{
v_movement = 0;
}
SetClimbingAnimation(v_movement != 0);
_body.velocity = new Vector2(h_movement * MovementSpeed, v_movement * MovementSpeed);
}
else
{
_body.velocity = new Vector2(h_movement * MovementSpeed, _body.velocity.y);
}
}
else
{
_isFalling = true;
}
}
private MapElementType GetMapElement(Vector2 direction)
{
var raycastHit = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, 0.01f, _mapLayer);
if (raycastHit)
{
var mapElement = raycastHit.transform.GetComponent<MapElement>();
if (mapElement == null || !mapElement.IsEnabled)
{
return MapElementType.Empty;
}
return mapElement.ElementSO.ElementType;
}
return MapElementType.Empty;
}
protected abstract void SetWalkingAnimation(bool isWalking);
protected abstract void SetClimbingAnimation(bool isClimbing);
private void FixedUpdate()
{
if (_isOnLadder || _isOnBridge)
{
_body.gravityScale = 0;
}
else
{
_body.gravityScale = 1;
}
if (_isFalling)
{
_body.velocity = new Vector2(0, _body.velocity.y);
SetWalkingAnimation(false);
if (GetMapElement(Vector2.down) == MapElementType.Ladder)
{
_body.velocity = Vector2.zero;
_isOnLadder = true;
}
}
}
private void FlipCharacter()
{
Vector3 currentScale = gameObject.transform.localScale;
currentScale.x *= -1;
gameObject.transform.localScale = currentScale;
_facingRight = !_facingRight;
}
}