-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeItemCell.cs
More file actions
42 lines (31 loc) · 1.31 KB
/
Copy pathChangeItemCell.cs
File metadata and controls
42 lines (31 loc) · 1.31 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 UnityEngine;
public class ChangeItemCell : MonoBehaviour
{
public static FieldItem sourceItem;
public static FieldItem targetItem;
public static event Action OnMoveDone;
private void OnEnable() => FieldItem.SetItemsChange += ItemsChange;
private void OnDisable() => FieldItem.SetItemsChange -= ItemsChange;
public void ItemsChange()
{
Cell sourceCell = sourceItem.itemCell;
Cell targetCell = targetItem.itemCell;
//Check if items are neighbours by row OR by collumn
if ((Math.Abs(targetCell.row - sourceCell.row) == 1 && targetCell.col == sourceCell.col)||
(Math.Abs(targetCell.col - sourceCell.col) == 1 && targetCell.row == sourceCell.row))
{
sourceCell.cellItem = targetItem;
targetCell.cellItem = sourceItem;
targetItem.transform.position = sourceCell.transform.position;
sourceItem.transform.position = targetCell.transform.position;
targetItem.itemCell = sourceCell;
sourceItem.itemCell = targetCell;
}
sourceItem.DeactivateItem();
targetItem.DeactivateItem();
sourceItem = null;
targetItem = null;
OnMoveDone?.Invoke();
}
}