-
Notifications
You must be signed in to change notification settings - Fork 1
/
pc_idtech3_player.js
183 lines (156 loc) · 4.44 KB
/
pc_idtech3_player.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
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
Player = function(id) {
this.id = id;
// <nope>not viable currently, i need to rewrite bunch of C code later that those basic structs are always available at fixed addresses</nope>
this.assignDataViews();
//this.tmp_velocity = new vec3_t();
}
Player.prototype.assignDataViews = function() {
this.velocity = vec3_t.wrap(_jl_player_get_velocity(this.id));
//this.viewangles = vec3_t.wrap(_jl_player_get_viewangles(this.id));
this.tmp_viewangles = new vec3_t(0, 0, 0);
this.tmp_position = new vec3_t(0, 0, 0);
}
Player.prototype.gent = function() {
return _jl_g_entities() + this.id * _jl_g_entities_sizeof();
}
/**
* @param {boolean} state
* @example
* players[1].walk(true); await wait(100); players[1].walk(false);
*/
Player.prototype.walk = function(state) {
_player_forwardmove(this.gent(), state);
}
// players[1].moveTo(players[0].position)
Player.prototype.moveTo = async function(pos) {
// this.facePosition(pos)
vec_a.sub2(pos, this.position) // vec_a = pos - this.position;
_vectoangles(vec_a.ptr, vec_b.ptr); // save result in vec_b
this.viewangles = vec_b;
// this.walkTo(pos)
this.walk(true);
await wait(1000); // todo: proper walk speed calculation based on distance
this.walk(false);
}
// players[0].position = vec_a.set(-504, -16, 1000)
Object.defineProperty(Player.prototype, "position", {
get: function() {
this.tmp_position.ptr = _jl_entity_get_pos(this.id);
this.tmp_position.assignDataView();
return this.tmp_position;
},
set: function(value) {
return _jl_entity_set_pos(this.id, value.ptr);
}
});
Object.defineProperty(Player.prototype, "viewangles", {
get: function() {
this.tmp_viewangles.ptr = _jl_player_get_viewangles(this.id);
this.tmp_viewangles.assignDataView();
return this.tmp_viewangles;
},
set: function(value) {
return _jl_player_set_viewangles(this.id, value.ptr);
}
});
Object.defineProperty(Player.prototype, "deaths", {
get: function() {
return _player_get_deaths(this.gent());
},
set: function(value) {
return _player_set_deaths(this.gent(), value);
}
});
Object.defineProperty(Player.prototype, "health", {
get: function() {
return _player_get_health(this.gent());
},
set: function(value) {
return _player_set_health(this.gent(), value);
}
});
Object.defineProperty(Player.prototype, "maxhealth", {
get: function() {
return _player_get_maxhealth(this.gent());
},
set: function(value) {
return _player_set_maxhealth(this.gent(), value);
}
});
Object.defineProperty(Player.prototype, "score", {
get: function() {
return _player_get_score(this.gent());
},
set: function(value) {
return _player_set_score(this.gent(), value);
}
});
Object.defineProperty(Player.prototype, "team", {
get: function() {
return _player_get_team(this.gent());
},
set: function(value) {
return _player_set_team(this.gent(), value);
}
});
Player.prototype.forward = function() {
var ret = player_forward(this.id);
var arr = new Float32Array(3)
arr[0] = ret.x;
arr[1] = ret.y;
arr[2] = ret.z;
return arr;
}
// precacheSound("sound/cash_register_open_coins_cha_ching_01.wav")
// players[0].playSound("sound/cash_register_open_coins_cha_ching_01.wav");
EV_GENERAL_SOUND = 45
Player.prototype.playSound = function(path) {
var str = alloc_string(path);
var i = _G_SoundIndex(str);
_G_AddEvent(this.gent(), EV_GENERAL_SOUND, i);
_free(str);
}
Player.prototype.print = function() {
var msg = "print \"";
for (var arg of arguments)
msg += arg + " ";
msg += "\"";
var strptr = alloc_string(msg);
_jl_SV_SendServerCommand(0, strptr);
_free(strptr);
}
Player.prototype.printBold = function() {
var msg = "cp \"";
for (var arg of arguments)
msg += arg + " ";
msg += "\"";
var strptr = alloc_string(msg);
_jl_SV_SendServerCommand(0, strptr);
_free(strptr);
}
//Object.defineProperty(Player.prototype, 'pos2', {
// get: function() {
// var n = _entity_get_position(0) >> 2 // divide by 4 for HEAPF32
// var x = HEAPF32[n ]
// var y = HEAPF32[n + 1]
// var z = HEAPF32[n + 2]
// return new pc.Vec3(x/100.0, z/100, y/-100);
// }
//});
player_pos_to_vector = function(id, vec3) {
var n = _entity_get_position(0) >> 2 // divide by 4 for HEAPF32
var x = HEAPF32[n ]
var y = HEAPF32[n + 1]
var z = HEAPF32[n + 2]
vec3.set(x/100.0, z/100, y/-100);
}
// depends on PlayCanvas pc.Vec3, call after its loaded
players_init = function() {
players = Array(64)
for (var i=0; i<64; i++)
players[i] = new Player(i);
playera = players[0]
playerb = players[1]
playerc = players[2]
playerd = players[3]
}