This commit is contained in:
2023-06-16 19:10:51 +03:00
parent cf6dbed1f5
commit 69f9b0bd4b
8 changed files with 1125 additions and 119 deletions

View File

@@ -9,8 +9,14 @@ namespace Objects
{
public class Player : MonoBehaviour
{
// Static
private static Player instance;
// Events
public delegate void PlayerMovedHandler();
public event PlayerMovedHandler PlayerMoved;
// Public
public Vector2Int layoutPosition;
public InputActionAsset actions;
@@ -51,7 +57,7 @@ namespace Objects
private void Update()
{
if (CanMove)
if (CanMove && moveAction.WasPressedThisFrame())
{
Vector2 directionFloat = moveAction.ReadValue<Vector2>();
directionFloat = Vector2.ClampMagnitude(directionFloat, 1f);
@@ -94,7 +100,12 @@ namespace Objects
LayoutCell currentCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition);
LayoutCell targetCell = LevelBuilder.Instance.GetCellAtLayout(layoutPosition + direction);
if (targetCell != null && targetCell.IsFree)
if (targetCell == null)
{
return;
}
if (targetCell.IsFree)
{
// Free previous cell
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
@@ -109,10 +120,12 @@ namespace Objects
isMoving = true;
transform.DOMove(targetCell.worldPos, 1f).OnComplete(() => isMoving = false);
}
else if (targetCell != null && targetCell.cellData.cellContent == LayoutCell.LayoutCellData.CellContent.Crate)
else if (targetCell.cellData.cellContent == LayoutCell.LayoutCellData.CellContent.Crate)
{
targetCell.crate.Move(direction);
}
PlayerMoved?.Invoke();
}