-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.cs
More file actions
59 lines (56 loc) · 1.71 KB
/
Box.cs
File metadata and controls
59 lines (56 loc) · 1.71 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Box : MonoBehaviour
{
public bool m_OnCross;
public bool Move(Vector2 direction)
{
if (BoxBlocked(transform.position, direction))
{
return false;
}
else
{
transform.Translate(direction);
TestForOnCross();
return true;
}
}
bool BoxBlocked(Vector3 position, Vector2 direction)
{
Vector2 newPos = new Vector2(position.x, position.y) + direction;
GameObject[] walls = GameObject.FindGameObjectsWithTag("Wall");
foreach (var wall in walls)
{
if (wall.transform.position.x == newPos.x && wall.transform.position.y == newPos.y)
{
return true;
}
}
GameObject[] boxes = GameObject.FindGameObjectsWithTag("Box");
foreach (var box in boxes)
{
if (box.transform.position.x == newPos.x && box.transform.position.y == newPos.y)
{
return true;
}
}
return false;
}
void TestForOnCross()
{
GameObject[] crosses = GameObject.FindGameObjectsWithTag("Cross");
foreach (var cross in crosses)
{
if (transform.position.x == cross.transform.position.x && transform.position.y == cross.transform.position.y)
{
GetComponent<SpriteRenderer>().color = Color.green;
m_OnCross = true;
return;
}
}
GetComponent<SpriteRenderer>().color = Color.white;
m_OnCross = false;
}
}