-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHitbox.java
More file actions
108 lines (102 loc) · 3.34 KB
/
Hitbox.java
File metadata and controls
108 lines (102 loc) · 3.34 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
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
public class Hitbox implements Position {
private String type;
private int shape;
private int x;
private int y;
private int width;
private int height;
public Hitbox (String iniType, int iniShape, int iniX, int iniY, int iniW, int iniH) {
type = iniType;
shape = iniShape;
x = iniX;
y = iniY;
width = iniW;
height = iniH;
}
public Hitbox (String iniType, int iniX, int iniY, int iniW, int iniH) {
this(iniType, 0, iniX, iniY, iniW, iniH);
}
public Hitbox (int iniType, int iniX, int iniY, int iniW, int iniH) {
this(intToLengthString(iniType, 4), iniX, iniY, iniW, iniH);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int[] getCoords() {
return new int[] {x, y};
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int[] getDimensions() {
return new int[] {width, height};
}
public int getType() {
return Integer.parseInt(type.substring(0, Math.min(4, type.length())));
}
public void setCoords (int[] coords) {
x = coords[0];
y = coords[1];
}
public void incrementX (int amount) {
x += amount;
}
public void incrementY (int amount) {
y += amount;
}
private static String intToLengthString (int num, int length) {
String ret = num + "";
if (ret.length() > length) {
ret = ret.substring(ret.length() - length, ret.length());
}
while (ret.length() < length) {
ret = '0' + ret;
}
return ret;
}
public int[] collide (Hitbox[] testWith, int except) {
Hitbox hb = new Hitbox(type, x, y, width, height); //copy of this
int damageSum = 0;
int newX = x;
int newY = y;
int horizColl = 0;
int vertColl = 0;
for (int i = 0; i < testWith.length; i++) {
if (i != except) {
if (Math.abs(testWith[i].getX() - hb.getX()) < hb.getWidth() / 2 + testWith[i].getWidth() / 2
&& Math.abs(testWith[i].getY() - hb.getY()) < hb.getHeight() / 2 + testWith[i].getHeight() / 2) {
//System.out.println("collision!");
if (testWith[i].getType() == 2) {
if (Math.abs(testWith[i].getX() - hb.getX()) > Math.abs(testWith[i].getY() - hb.getY())) {
if (hb.getX() < testWith[i].getX()) {
newX = testWith[i].getX() - (hb.getWidth() / 2 + testWith[i].getWidth() / 2);
horizColl = -1;
}
else {
newX = testWith[i].getX() + (hb.getWidth() / 2 + testWith[i].getWidth() / 2);
horizColl = 1;
}
}
else {
if (hb.getY() < testWith[i].getY()) {
newY = testWith[i].getY() - (hb.getHeight() / 2 + testWith[i].getHeight() / 2);
vertColl = -1;
}
else {
newY = testWith[i].getY() + (hb.getHeight() / 2 + testWith[i].getHeight() / 2);
vertColl = 1;
}
}
}
}
}
}
return new int[] {newX, newY, damageSum, horizColl, vertColl};
}
}