Gameplay-Edit
This commit is contained in:
55
Assets/Scripts/Objects/Crate.cs
Normal file
55
Assets/Scripts/Objects/Crate.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Level;
|
||||
using DG.Tweening;
|
||||
|
||||
namespace Objects
|
||||
{
|
||||
public class Crate : MonoBehaviour
|
||||
{
|
||||
public Vector2Int layoutPosition; // Index in layout
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
LevelBuilder.Instance.LevelDestroyed += Destroy;
|
||||
}
|
||||
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
LevelBuilder.Instance.LevelDestroyed -= Destroy;
|
||||
}
|
||||
|
||||
|
||||
public void Move(Vector2Int direction)
|
||||
{
|
||||
LayoutCell currentCell = LevelBuilder.Instance.GetCellAt(layoutPosition);
|
||||
LayoutCell targetCell = LevelBuilder.Instance.GetCellAt(layoutPosition + direction);
|
||||
|
||||
if (targetCell != null && targetCell.IsFree)
|
||||
{
|
||||
// Free previous cell
|
||||
currentCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.None;
|
||||
currentCell.crate = null;
|
||||
|
||||
// Occupy new cell
|
||||
targetCell.cellData.cellContent = LayoutCell.LayoutCellData.CellContent.Crate;
|
||||
targetCell.crate = this;
|
||||
|
||||
// Update position
|
||||
layoutPosition = layoutPosition + direction;
|
||||
|
||||
// Move object
|
||||
transform.DOMove(targetCell.worldPos, 1f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void Destroy()
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user