-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixelCollisions.cs
148 lines (125 loc) · 4.67 KB
/
PixelCollisions.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Binocle
{
public enum DirectionUnit
{
Top,
Left,
Right,
Bottom
}
public static class PixelCollisions
{
private static Rect rectCheck = new Rect();
public static bool Collide(float x, float y, float width, float height, DirectionUnit dir, int layerMask, out Collider2D[] colliders)
{
Rect r = RectCollision(x, y, width, height, dir);
colliders = Physics2D.OverlapAreaAll(new Vector2(r.x, r.y), new Vector2(r.x + r.width, r.y + r.height), layerMask);
if (colliders.Length > 0)
return true;
return false;
}
public static bool Collide(float x, float y, float width, float height, DirectionUnit dir, int layerMask)
{
Rect r = RectCollision(x, y, width, height, dir);
Collider2D[] colliders = Physics2D.OverlapAreaAll(new Vector2(r.x, r.y), new Vector2(r.x + r.width, r.y + r.height), layerMask);
if (colliders.Length > 0)
return true;
return false;
}
public static Collider2D CollideCheck(float x, float y, float width, float height, int layerMask)
{
Rect r = RectCollision(x, y, width, height);
Collider2D[] colliders = Physics2D.OverlapAreaAll(new Vector2(r.x, r.y), new Vector2(r.x + r.width, r.y + r.height), layerMask);
if (colliders.Length > 0) {
return colliders[0];
}
return null;
}
public static bool CollideCheck(float x, float y, float width, float height, int layerMask, Collider2D collToCheck)
{
Rect r = RectCollision(x, y, width, height);
Collider2D[] colliders = Physics2D.OverlapAreaAll(new Vector2(r.x, r.y), new Vector2(r.x + r.width, r.y + r.height), layerMask);
if (colliders.Length > 0) {
foreach (var c in colliders) {
if (c == collToCheck) {
return true;
}
}
}
return false;
}
public static Collider2D[] CollideCheckAll(float x, float y, float width, float height, int layerMask)
{
Rect r = RectCollision(x, y, width, height);
Collider2D[] colliders = Physics2D.OverlapAreaAll(new Vector2(r.x, r.y), new Vector2(r.x + r.width, r.y + r.height), layerMask);
return colliders;
}
public static Rect RectCollision(float x, float y, float width, float height, DirectionUnit dir)
{
float halfWidth = width / 2;
float halfHeight = height / 2;
float margin = 1.0f;
float toX = 0;
float toY = 0;
float h = 0;
float w = 0;
if (dir == DirectionUnit.Top)
{
toX = x - halfWidth + margin;
toY = y + halfHeight;
h = -1.0f;
w = width - (margin * 2);
}
if (dir == DirectionUnit.Bottom)
{
toX = x - halfWidth + margin;
toY = y - halfHeight;
h = 1.0f;
w = width - (margin * 2);
}
if (dir == DirectionUnit.Right)
{
toX = x + halfWidth;
toY = y - halfHeight + margin;
h = height - (margin * 2);
w = -1.0f;
}
if (dir == DirectionUnit.Left)
{
toX = x - halfWidth;
toY = y - halfHeight + margin;
h = height - (margin * 2);
w = 1.0f;
}
rectCheck.x = (int)toX;
rectCheck.y = (int)toY;
rectCheck.width = (int)w;
rectCheck.height = (int)h;
DebugX.DrawRect(rectCheck, Color.red);
return rectCheck;
}
public static Rect RectCollision(float x, float y, float width, float height)
{
float halfWidth = width / 2;
float halfHeight = height / 2;
float margin = 1.0f;
float toX = 0;
float toY = 0;
float h = 0;
float w = 0;
toX = x - halfWidth + margin;
toY = y - halfHeight + margin;
h = height - margin * 2;
w = width - margin * 2;
rectCheck.x = (int)toX;
rectCheck.y = (int)toY;
rectCheck.width = (int)w;
rectCheck.height = (int)h;
DebugX.DrawRect(rectCheck, Color.red);
return rectCheck;
}
}
}