forked from dmitmel/crosscode-render-hitboxes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprestart.js
143 lines (122 loc) · 3.51 KB
/
prestart.js
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
// see https://github.com/CCDirectLink/crosscode-re-docs/blob/master/renderer.md
export {};
const COLL_BOUNDS_PADDING = 0;
let collMinX = 0;
let collMinY = 0;
let collMaxX = 0;
let collMaxY = 0;
let tmpVec2 = Vec2.create();
function initCollBounds() {
let pos = ig.system.getZoomMinOffset(tmpVec2);
collMinX = pos.x + ig.game.screen.x;
collMinY = pos.y + ig.game.screen.y;
collMaxX = collMinX + ig.system.width / ig.system.zoom;
collMaxY = collMinY + ig.system.height / ig.system.zoom;
}
// prettier-ignore
function collEntryIsVisible(px, py, pz, sx, sy, sz) {
return (
(px + sx + COLL_BOUNDS_PADDING > collMinX) &&
(px - COLL_BOUNDS_PADDING < collMaxX) &&
(py - pz + sy + COLL_BOUNDS_PADDING > collMinY) &&
(py - pz - sz - COLL_BOUNDS_PADDING < collMaxY)
);
}
let v000 = Vec2.create();
let v001 = Vec2.create();
let v010 = Vec2.create();
let v011 = Vec2.create();
let v100 = Vec2.create();
let v101 = Vec2.create();
let v110 = Vec2.create();
let v111 = Vec2.create();
function mapPos3ToScreenPos2(outVec2, x, y, z) {
ig.system.getScreenFromMapPos(outVec2, x, y - z);
outVec2.x = ig.system.getDrawPos(outVec2.x);
outVec2.y = ig.system.getDrawPos(outVec2.y);
}
sc.HitboxRendererMod = ig.GameAddon.extend({
init() {
this.parent('HitboxRendererMod');
},
postDrawOrder: 100,
onPostDraw() {
initCollBounds();
let ctx = ig.system.context;
ctx.save();
ctx.lineWidth = 1;
ctx.strokeStyle = 'red';
ctx.globalAlpha = 0.75;
let allEntities = ig.game.shownEntities;
for (let i = 0, len = allEntities.length; i < len; i++) {
let entity = allEntities[i];
if (entity == null || entity._killed) continue;
let { pos, size, alwaysRender } = entity.coll;
let px = pos.x;
let py = pos.y;
let pz = pos.z;
let sx = size.x;
let sy = size.y;
let sz = size.z;
if (!collEntryIsVisible(px, py, pz, sx, sy, sz) && !alwaysRender) {
continue;
}
//
// ^ z
// |
// o--> x
// |
// v y
//
// v001----v101
// | |
// | |
// | |
// | |
// v011----v111
//
//
//
// v000----v100
// | |
// | |
// | |
// | |
// v010----v110
//
// prettier-ignore
{
mapPos3ToScreenPos2(v000, px , py , pz );
mapPos3ToScreenPos2(v100, px + sx, py , pz );
mapPos3ToScreenPos2(v010, px , py + sy, pz );
mapPos3ToScreenPos2(v110, px + sx, py + sy, pz );
mapPos3ToScreenPos2(v001, px , py , pz + sz);
mapPos3ToScreenPos2(v101, px + sx, py , pz + sz);
mapPos3ToScreenPos2(v011, px , py + sy, pz + sz);
mapPos3ToScreenPos2(v111, px + sx, py + sy, pz + sz);
}
ctx.beginPath();
// bottom face
ctx.moveTo(v000.x, v000.y);
ctx.lineTo(v100.x, v100.y);
ctx.lineTo(v110.x, v110.y);
ctx.lineTo(v010.x, v010.y);
ctx.closePath();
// top face
ctx.moveTo(v001.x, v001.y);
ctx.lineTo(v101.x, v101.y);
ctx.lineTo(v111.x, v111.y);
ctx.lineTo(v011.x, v011.y);
ctx.closePath();
// front left edge
ctx.moveTo(v011.x, v011.y);
ctx.lineTo(v010.x, v010.y);
// front right edge
ctx.moveTo(v111.x, v111.y);
ctx.lineTo(v110.x, v110.y);
ctx.stroke();
}
ctx.restore();
},
});
ig.addGameAddon(() => new sc.HitboxRendererMod());