boundings

This commit is contained in:
2023-07-12 19:43:46 +03:00
parent 03cd45110d
commit 94b9c7cc0c
3 changed files with 167 additions and 27 deletions
+39 -22
View File
@@ -33,36 +33,39 @@ public abstract class Character : MonoBehaviour
protected bool isAllowVertical = true;
protected bool isAllowRight = true;
protected bool isAllowLeft = true;
private Vector2 _cellSize;
private void Start()
{
_body = GetComponent<Rigidbody2D>();
_capsuleCollider = GetComponent<CapsuleCollider2D>();
_cellSize = new Vector2(_tileMap.cellSize.x, _tileMap.cellSize.y);
}
protected void Spawn()
{
transform.position=_spawnPoint.transform.position;
transform.position = _spawnPoint.transform.position;
}
protected void MoveTo(float inputHorizontal, float inputVertical)
{
var mapElement = GetMapElement(Vector2.down);
var downBlock = GetMapElement(Vector2.down);
if(mapElement==MapElementType.Water)
if (downBlock == MapElementType.Water)
{
Death();
}
_isOnBridge = mapElement == MapElementType.Bridge && !_isFalling;
_isOnBridge = downBlock == MapElementType.Bridge && !_isFalling;
if (mapElement == MapElementType.Wall || _isOnLadder || _isOnBridge || mapElement == MapElementType.BreakableWall)
if (downBlock == MapElementType.Wall || _isOnLadder || _isOnBridge || downBlock == MapElementType.BreakableWall)
{
var leftCheck = GetMapElement(Vector2.left);
var rightCheck = GetMapElement(Vector2.right);
//print($"LeftCheck:{leftCheck} right:{rightCheck}");
isAllowLeft = !(leftCheck == MapElementType.Wall || leftCheck == MapElementType.BreakableWall);
isAllowRight = !(rightCheck == MapElementType.Wall || rightCheck == MapElementType.BreakableWall);
@@ -76,10 +79,10 @@ public abstract class Character : MonoBehaviour
}
SetWalkingAnimation(h_movement != 0);
;
_isOnLadder = GetMapElement(Vector2.zero) == MapElementType.Ladder || downBlock == MapElementType.Ladder;
_isOnLadder = leftCheck == MapElementType.Ladder && rightCheck == MapElementType.Ladder;
if (leftCheck == MapElementType.Ladder && rightCheck == MapElementType.Ladder)
if (_isOnLadder)
{
float v_movement = inputVertical;
isAllowVertical = true;
@@ -112,7 +115,7 @@ public abstract class Character : MonoBehaviour
_body.velocity = new Vector2(0, _body.velocity.y);
SetWalkingAnimation(false);
if (mapElement == MapElementType.Ladder)
if (downBlock == MapElementType.Ladder)
{
_body.velocity = Vector2.zero;
_isOnLadder = true;
@@ -127,18 +130,16 @@ public abstract class Character : MonoBehaviour
}
private MapElementType GetMapElement(Vector2 direction)
private MapElementType GetMapElement(Vector3 direction)
{
var cell=_tileMap.WorldToCell(transform.position);
var bounds = _facingRight ? _capsuleCollider.bounds.min: _capsuleCollider.bounds.center;
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.06f);
var boundsSize = new Vector2(_capsuleCollider.bounds.size.x - 0.05f, 0.1f);
var cell = _tileMap.WorldToCell(bounds + direction);
Vector2 cell2d = new Vector2(cell.x + 0.5f, cell.y + 0.3f);
Vector2 cellSize = new Vector2(_cellSize.x - 0.3f, _cellSize.y - 0.05f);
var raycastHit = Physics2D.BoxCast(rayStartPoint, boundsSize, 0f, direction, 0.1f, _mapLayer);
cellSize = cellSize / 2;
var raycastHit = Physics2D.BoxCast(cell2d, cellSize, 0f, Vector3.zero, 0.01f, _mapLayer);
MapElementType returnValue;
Color color = Color.red;
if (raycastHit)
@@ -151,15 +152,31 @@ public abstract class Character : MonoBehaviour
}
else
{
color = Color.green;
returnValue = mapElement.ElementSO.ElementType;
switch (returnValue)
{
case MapElementType.Wall:
color = Color.green;
break;
case MapElementType.Ladder:
color = Color.yellow;
break;
case MapElementType.BreakableWall:
color = Color.magenta;
break;
}
}
}
else
{
returnValue = MapElementType.Empty;
}
Debug.DrawRay(rayStartPoint, direction, color);
Debug.DrawLine(cell2d, cell2d + new Vector2(cellSize.x, 0), color);
Debug.DrawLine(cell2d + new Vector2(cellSize.x, 0), cell2d + new Vector2(cellSize.x, cellSize.y), color);
Debug.DrawLine(cell2d, cell2d + new Vector2(0, cellSize.y), color);
Debug.DrawLine(cell2d + new Vector2(0, cellSize.y), cell2d + new Vector2(cellSize.x, cellSize.y), color);
return returnValue;
}