177 lines
4.9 KiB
C#
177 lines
4.9 KiB
C#
using System;
|
|
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]
|
|
protected GameObject _spawnPoint;
|
|
|
|
|
|
[SerializeField]
|
|
private LayerMask _mapLayer;
|
|
|
|
|
|
|
|
private Rigidbody2D _body;
|
|
private CapsuleCollider2D _capsuleCollider;
|
|
protected bool _isOnBridge;
|
|
protected bool _isOnLadder = false;
|
|
protected bool _isFalling;
|
|
protected bool _facingRight = true;
|
|
|
|
|
|
protected bool isAllowVertical = true;
|
|
protected bool isAllowRight = true;
|
|
protected bool isAllowLeft = true;
|
|
|
|
private void Start()
|
|
{
|
|
_body = GetComponent<Rigidbody2D>();
|
|
_capsuleCollider = GetComponent<CapsuleCollider2D>();
|
|
}
|
|
|
|
protected void Spawn()
|
|
{
|
|
transform.position=_spawnPoint.transform.position;
|
|
}
|
|
|
|
protected void MoveTo(float inputHorizontal, float inputVertical)
|
|
{
|
|
var mapElement = GetMapElement(Vector2.down);
|
|
|
|
if(mapElement==MapElementType.Water)
|
|
{
|
|
Death();
|
|
}
|
|
|
|
_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);
|
|
|
|
_isOnLadder = leftCheck == MapElementType.Ladder && rightCheck == MapElementType.Ladder;
|
|
|
|
if (leftCheck == MapElementType.Ladder && rightCheck == MapElementType.Ladder)
|
|
{
|
|
float v_movement = inputVertical;
|
|
isAllowVertical = true;
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
if (_isOnLadder || _isOnBridge)
|
|
{
|
|
_body.gravityScale = 0;
|
|
}
|
|
else
|
|
{
|
|
_body.gravityScale = 1;
|
|
}
|
|
if (_isFalling)
|
|
{
|
|
_body.velocity = new Vector2(0, _body.velocity.y);
|
|
SetWalkingAnimation(false);
|
|
|
|
if (mapElement == MapElementType.Ladder)
|
|
{
|
|
_body.velocity = Vector2.zero;
|
|
_isOnLadder = true;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private void Death()
|
|
{
|
|
OnDeath();
|
|
|
|
}
|
|
|
|
private MapElementType GetMapElement(Vector2 direction)
|
|
{
|
|
Vector2 rayStartPoint;
|
|
if (direction == Vector2.down)
|
|
rayStartPoint = new Vector2(_capsuleCollider.bounds.center.x + direction.x / 2, _capsuleCollider.bounds.min.y - 0.1f);
|
|
else
|
|
rayStartPoint = new Vector2(_capsuleCollider.bounds.center.x, _capsuleCollider.bounds.min.y + 0.05f);
|
|
var boundsSize = new Vector2(_capsuleCollider.bounds.size.x - 0.05f, 0.1f);
|
|
|
|
var raycastHit = Physics2D.BoxCast(rayStartPoint, boundsSize, 0f, direction, 0.1f, _mapLayer);
|
|
MapElementType returnValue;
|
|
Color color = Color.red;
|
|
if (raycastHit)
|
|
{
|
|
var mapElement = raycastHit.transform.GetComponent<MapElement>();
|
|
if (!mapElement.IsEnabled)
|
|
{
|
|
color = Color.white;
|
|
returnValue = MapElementType.Empty;
|
|
}
|
|
else
|
|
{
|
|
color = Color.green;
|
|
returnValue = mapElement.ElementSO.ElementType;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
returnValue = MapElementType.Empty;
|
|
}
|
|
Debug.DrawRay(rayStartPoint, direction, color);
|
|
return returnValue;
|
|
}
|
|
|
|
protected abstract void OnDeath();
|
|
protected abstract void SetWalkingAnimation(bool isWalking);
|
|
protected abstract void SetClimbingAnimation(bool isClimbing);
|
|
|
|
|
|
|
|
private void FlipCharacter()
|
|
{
|
|
Vector3 currentScale = gameObject.transform.localScale;
|
|
currentScale.x *= -1;
|
|
gameObject.transform.localScale = currentScale;
|
|
|
|
_facingRight = !_facingRight;
|
|
}
|
|
|
|
|
|
}
|