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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
package main;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.WindowConstants;
public class Main extends JFrame{
private static final long serialVersionUID = 1L;
private static JComponent view;
static class Role{
public int curHp;
public int maxHp;
public float size;
public float x;
public float y;
public float z;
public int[][] screen = new int[8][2];
public int[] hpPos = new int[2];
}
static Role[] role = new Role[50];
static int count = 0;
public Main() {
for(int i = 0 ; i < 50 ; i ++) {
role[i] = new Role();
}
this.setSize(800, 600);
this.setTitle("bilibili: 赶码人");
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setUndecorated(true);
this.setOpacity(0.5f);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
view = new JComponent() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(2));
g2d.setColor(Color.RED);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setFont(new Font("宋体", Font.PLAIN, 18));
g2d.drawString("bilibili: 赶码人", 30, 30);
for(int i = 0 ; i < count ; i ++) {
g2d.setColor(Color.RED);
g2d.drawLine(role[i].screen[0][0], role[i].screen[0][1], role[i].screen[1][0], role[i].screen[1][1]);
g2d.drawLine(role[i].screen[1][0], role[i].screen[1][1], role[i].screen[2][0], role[i].screen[2][1]);
g2d.drawLine(role[i].screen[2][0], role[i].screen[2][1], role[i].screen[3][0], role[i].screen[3][1]);
g2d.drawLine(role[i].screen[3][0], role[i].screen[3][1], role[i].screen[0][0], role[i].screen[0][1]);
g2d.drawLine(role[i].screen[4][0], role[i].screen[4][1], role[i].screen[5][0], role[i].screen[5][1]);
g2d.drawLine(role[i].screen[5][0], role[i].screen[5][1], role[i].screen[6][0], role[i].screen[6][1]);
g2d.drawLine(role[i].screen[6][0], role[i].screen[6][1], role[i].screen[7][0], role[i].screen[7][1]);
g2d.drawLine(role[i].screen[7][0], role[i].screen[7][1], role[i].screen[4][0], role[i].screen[4][1]);
g2d.drawLine(role[i].screen[0][0], role[i].screen[0][1], role[i].screen[4][0], role[i].screen[4][1]);
g2d.drawLine(role[i].screen[1][0], role[i].screen[1][1], role[i].screen[5][0], role[i].screen[5][1]);
g2d.drawLine(role[i].screen[2][0], role[i].screen[2][1], role[i].screen[6][0], role[i].screen[6][1]);
g2d.drawLine(role[i].screen[3][0], role[i].screen[3][1], role[i].screen[7][0], role[i].screen[7][1]);
String hp = role[i].curHp + "/" + role[i].maxHp;
g2d.setColor(Color.YELLOW);
g2d.drawString(hp, role[i].hpPos[0] - 20, role[i].hpPos[1]);
}
}
};
this.add(view);
this.setVisible(true);
}
public static void main(String[] args) {
new Main();
byte[] b = new byte[64 + 4 + 50 * (2 + 3 + 1) * 4];
try(DatagramSocket ds = new DatagramSocket(6666)) {
DatagramPacket dp = new DatagramPacket(b, b.length);
float[] matrix = new float[16];
while(true) {
ds.receive(dp);
for(int i = 0 ; i < 16; i++) {
matrix[i] = Float.intBitsToFloat(byteToInt(b, i * 4));
}
count = byteToInt(b, 64);
for(int i = 0 ; i < count ; i++) {
int offset = 68 + i * 24;
role[i].curHp = byteToInt(b, offset);
role[i].maxHp = byteToInt(b, offset + 4);
role[i].x = Float.intBitsToFloat(byteToInt(b, offset + 8));
role[i].y = Float.intBitsToFloat(byteToInt(b, offset + 12));
role[i].z = Float.intBitsToFloat(byteToInt(b, offset + 16));
role[i].size = Float.intBitsToFloat(byteToInt(b, offset + 20));
calcScreenPos(matrix, role[i].x, role[i].y + role[i].size + 2, role[i].z, role[i].hpPos);
calcScreenPos(matrix,
role[i].x - role[i].size,
role[i].y,
role[i].z + role[i].size,
role[i].screen[0]);
calcScreenPos(matrix,
role[i].x - role[i].size,
role[i].y,
role[i].z - role[i].size,
role[i].screen[1]);
calcScreenPos(matrix,
role[i].x + role[i].size,
role[i].y,
role[i].z - role[i].size,
role[i].screen[2]);
calcScreenPos(matrix,
role[i].x + role[i].size,
role[i].y,
role[i].z + role[i].size,
role[i].screen[3]);
calcScreenPos(matrix,
role[i].x - role[i].size,
role[i].y + role[i].size,
role[i].z + role[i].size,
role[i].screen[4]);
calcScreenPos(matrix,
role[i].x - role[i].size,
role[i].y + role[i].size,
role[i].z - role[i].size,
role[i].screen[5]);
calcScreenPos(matrix,
role[i].x + role[i].size,
role[i].y + role[i].size,
role[i].z - role[i].size,
role[i].screen[6]);
calcScreenPos(matrix,
role[i].x + role[i].size,
role[i].y + role[i].size,
role[i].z + role[i].size,
role[i].screen[7]);
}
view.repaint();
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void calcScreenPos(float[] matrix, float x, float y, float z, int[] screenPos) {
float res0 = matrix[0] * x + matrix[1] * y + matrix[2] * z + matrix[3];
float res1 = matrix[4] * x + matrix[5] * y + matrix[6] * z + matrix[7];
float res3 = matrix[12] * x + matrix[13] * y + matrix[14] * z + matrix[15];
float ndcX = res0 / res3;
float ndcY = res1 / res3;
screenPos[0] = (int) ((ndcX + 1) / 2f * view.getWidth());
screenPos[1] = (int) ( (1- (ndcY + 1) / 2.0) * view.getHeight());
}
public static int byteToInt(byte[] b, int index) {
int i = b[index + 3];
i = i << 8;
i = i | (b[index + 2] & 0xff);
i = i << 8;
i = i | (b[index + 1] & 0xff);
i = i << 8;
i = i | (b[index] & 0xff);
return i;
}
}
|