-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem_move.cs
More file actions
42 lines (36 loc) · 1.49 KB
/
Copy pathItem_move.cs
File metadata and controls
42 lines (36 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections;
using UnityEngine;
public class Item_move : MonoBehaviour
{
private bool isMoving = false;
private Cell[,] cellArray;
public static event Action OnTileMoved;
private void OnEnable() => Cell.OnCellEmpty += FillEmptyCell;
private void OnDisable() => Cell.OnCellEmpty -= FillEmptyCell;
private void Start() => cellArray = Cell_Setup.cellArray;
private void FillEmptyCell(Cell currentCell)
{
Cell upperCell = cellArray[currentCell.row + 1, currentCell.col];
if (currentCell.isEmpty && upperCell.cellItem != null)
StartCoroutine(MoveItem_From_To(upperCell, currentCell));
}
public IEnumerator MoveItem_From_To(Cell from, Cell to)
{
if (!isMoving) //Do not start moving the item untill previous one is moving
{
isMoving = true;
while (from.cellItem!=null && from.cellItem.transform.position != to.transform.position)
{
from.cellItem.transform.position = Vector3.MoveTowards(from.cellItem.transform.position, to.transform.position, 0.3f);
yield return new WaitForEndOfFrame();
}OnTileMoved?.Invoke();
to.isEmpty = false;
to.cellItem = from.cellItem;
to.cellItem.itemCell = to;
from.cellItem = null;
from.isEmpty = true;
isMoving =false;
}
}
}