fix collision system, ladders, and enemy AI
This commit is contained in:
+39
-86
@@ -1,21 +1,18 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
public abstract class Character : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
protected Animator _animator;
|
||||
[SerializeField]
|
||||
private float MovementSpeed = 1.5f;
|
||||
private float _movementSpeed = 1.5f;
|
||||
[SerializeField]
|
||||
protected GameObject _bonesSide;
|
||||
[SerializeField]
|
||||
protected GameObject _bonesBack;
|
||||
[SerializeField]
|
||||
protected GameObject _spawnPoint;
|
||||
[SerializeField]
|
||||
protected Tilemap _tileMap;
|
||||
|
||||
[SerializeField]
|
||||
private LayerMask _mapLayer;
|
||||
@@ -40,7 +37,7 @@ public abstract class Character : MonoBehaviour
|
||||
_body = GetComponent<Rigidbody2D>();
|
||||
_capsuleCollider = GetComponent<CapsuleCollider2D>();
|
||||
|
||||
_cellSize = new Vector2(_tileMap.cellSize.x, _tileMap.cellSize.y);
|
||||
_cellSize = new Vector2(0.6f, 1f);
|
||||
}
|
||||
|
||||
protected void Spawn()
|
||||
@@ -50,25 +47,17 @@ public abstract class Character : MonoBehaviour
|
||||
|
||||
protected void MoveTo(float inputHorizontal, float inputVertical)
|
||||
{
|
||||
var downBlock = GetMapElement(Vector2.down);
|
||||
var block = GetMapElement();
|
||||
|
||||
if (downBlock == MapElementType.Water)
|
||||
if (block?.ElementSO.ElementType == MapElementType.Water)
|
||||
{
|
||||
Death();
|
||||
}
|
||||
|
||||
_isOnBridge = downBlock == MapElementType.Bridge && !_isFalling;
|
||||
GetMapElement(Vector2.up);
|
||||
if (downBlock == MapElementType.Wall || _isOnLadder || _isOnBridge || downBlock == MapElementType.BreakableWall)
|
||||
_isOnBridge = block?.ElementSO.ElementType == MapElementType.Bridge && !_isFalling;
|
||||
|
||||
if (block)
|
||||
{
|
||||
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);
|
||||
|
||||
isAllowVertical = false;
|
||||
_isFalling = false;
|
||||
float h_movement = inputHorizontal;
|
||||
@@ -79,26 +68,30 @@ public abstract class Character : MonoBehaviour
|
||||
}
|
||||
|
||||
SetWalkingAnimation(h_movement != 0);
|
||||
;
|
||||
_isOnLadder = GetMapElement(Vector2.zero) == MapElementType.Ladder || downBlock == MapElementType.Ladder;
|
||||
|
||||
_isOnLadder = block.ElementSO.ElementType == MapElementType.Ladder;
|
||||
|
||||
if (_isOnLadder)
|
||||
{
|
||||
float ladderCenterDistance = Mathf.Abs(transform.position.x - block.transform.position.x);
|
||||
|
||||
float v_movement = inputVertical;
|
||||
isAllowVertical = true;
|
||||
isAllowVertical = ladderCenterDistance < 0.3f;
|
||||
|
||||
|
||||
SetClimbingAnimation(v_movement != 0);
|
||||
_body.velocity = new Vector2(h_movement * MovementSpeed, v_movement * MovementSpeed);
|
||||
_body.velocity = new Vector2(h_movement * _movementSpeed, v_movement * _movementSpeed);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
_body.velocity = new Vector2(h_movement * MovementSpeed, _body.velocity.y);
|
||||
_body.velocity = new Vector2(h_movement * _movementSpeed, _body.velocity.y);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_isFalling = true;
|
||||
_isOnLadder = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -115,7 +108,7 @@ public abstract class Character : MonoBehaviour
|
||||
_body.velocity = new Vector2(0, _body.velocity.y);
|
||||
SetWalkingAnimation(false);
|
||||
|
||||
if (downBlock == MapElementType.Ladder)
|
||||
if (block?.ElementSO.ElementType == MapElementType.Ladder)
|
||||
{
|
||||
_body.velocity = Vector2.zero;
|
||||
_isOnLadder = true;
|
||||
@@ -131,69 +124,27 @@ public abstract class Character : MonoBehaviour
|
||||
}
|
||||
|
||||
|
||||
private MapElementType GetMapElement(Vector3 direction)
|
||||
private MapElement GetMapElement()
|
||||
{
|
||||
var bounds = _capsuleCollider.bounds.min;
|
||||
|
||||
//var cell = _tileMap.WorldToCell(bounds + direction);
|
||||
//Vector2 cell2d = new Vector2(cell.x + 0.5f, cell.y + 0.3f);
|
||||
Vector2 cellSize = _cellSize;
|
||||
|
||||
var playerBounds = _capsuleCollider.bounds.center;
|
||||
|
||||
//DrawBounds(playerBounds, cellSize, Color.cyan);
|
||||
|
||||
playerBounds = playerBounds + direction;
|
||||
|
||||
var raycastHit = BoxCast(playerBounds, cellSize, 0f, Vector3.forward, .01f, _mapLayer);
|
||||
|
||||
|
||||
|
||||
MapElementType returnValue;
|
||||
var collider = BoxCast(playerBounds, _cellSize, 0f, Vector3.forward, .01f, _mapLayer);
|
||||
Color color = Color.red;
|
||||
if (raycastHit)
|
||||
MapElement mapElement = null;
|
||||
if (collider.Length > 0)
|
||||
{
|
||||
var mapElement = raycastHit.transform.GetComponent<MapElement>();
|
||||
if (!mapElement.IsEnabled)
|
||||
var elements=collider.Select(x=>x.transform.GetComponent<MapElement>());
|
||||
mapElement = elements.Where(x => x.ElementSO.ElementType == MapElementType.Ladder).FirstOrDefault();
|
||||
if(mapElement == null)
|
||||
{
|
||||
color = Color.white;
|
||||
returnValue = MapElementType.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
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;
|
||||
}
|
||||
mapElement = elements.First();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = MapElementType.Empty;
|
||||
}
|
||||
// DrawBounds(playerBounds, cellSize, color);
|
||||
|
||||
return returnValue;
|
||||
return mapElement;
|
||||
}
|
||||
|
||||
//private static void DrawBounds(Vector2 cell2d, Vector2 cellSize, Color 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);
|
||||
//}
|
||||
|
||||
protected abstract void OnDeath();
|
||||
protected abstract void SetWalkingAnimation(bool isWalking);
|
||||
protected abstract void SetClimbingAnimation(bool isClimbing);
|
||||
@@ -209,10 +160,10 @@ public abstract class Character : MonoBehaviour
|
||||
_facingRight = !_facingRight;
|
||||
}
|
||||
|
||||
static public RaycastHit2D BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance, int mask)
|
||||
static public Collider2D[] BoxCast(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance, int mask)
|
||||
{
|
||||
RaycastHit2D hit = Physics2D.BoxCast(origin, size, angle, direction, distance, mask);
|
||||
|
||||
//RaycastHit2D hit = Physics2D.BoxCast(origin, size, angle, direction, distance, mask);
|
||||
var collider = Physics2D.OverlapBoxAll(origin, size, angle, mask);
|
||||
//Setting up the points to draw the cast
|
||||
Vector2 p1, p2, p3, p4, p5, p6, p7, p8;
|
||||
float w = size.x * 0.5f;
|
||||
@@ -241,7 +192,7 @@ public abstract class Character : MonoBehaviour
|
||||
|
||||
|
||||
//Drawing the cast
|
||||
Color castColor = hit ? Color.red : Color.green;
|
||||
Color castColor = collider.Length > 0 ? Color.red : Color.green;
|
||||
Debug.DrawLine(p1, p2, castColor);
|
||||
Debug.DrawLine(p2, p3, castColor);
|
||||
Debug.DrawLine(p3, p4, castColor);
|
||||
@@ -256,12 +207,14 @@ public abstract class Character : MonoBehaviour
|
||||
Debug.DrawLine(p2, p6, Color.grey);
|
||||
Debug.DrawLine(p3, p7, Color.grey);
|
||||
Debug.DrawLine(p4, p8, Color.grey);
|
||||
if (hit)
|
||||
{
|
||||
var color = hit.point.x > origin.x ? Color.yellow : Color.cyan;
|
||||
Debug.DrawLine(hit.point, hit.point + hit.normal.normalized * 0.2f, color);
|
||||
}
|
||||
|
||||
return hit;
|
||||
//collider
|
||||
//if (hit)
|
||||
//{
|
||||
// var color = hit.point.x > origin.x ? Color.yellow : Color.cyan;
|
||||
// Debug.DrawLine(hit.point, hit.point + hit.normal.normalized * 0.2f, color);
|
||||
//}
|
||||
|
||||
return collider;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user