Skip to content

Commit 418b2fd

Browse files
committed
add example for shooting auto turrets
1 parent 1bc7095 commit 418b2fd

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

camera.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,41 @@ class Camera extends EventEmitter {
363363

364364
}
365365

366+
/**
367+
* Shoots a PTZ controllable Auto Turret.
368+
*/
369+
async shoot() {
370+
371+
// press left mouse button to shoot
372+
await this.move(Camera.Buttons.FIRE_PRIMARY, 0, 0);
373+
374+
// release all mouse buttons
375+
await this.move(Camera.Buttons.NONE, 0, 0);
376+
377+
}
378+
379+
/**
380+
* Reloads a PTZ controllable Auto Turret
381+
*/
382+
async reload() {
383+
384+
// press reload button to reload turret
385+
await this.move(Camera.Buttons.RELOAD, 0, 0);
386+
387+
// release all mouse buttons
388+
await this.move(Camera.Buttons.NONE, 0, 0);
389+
390+
}
391+
392+
/**
393+
* Check if camera is an auto turret
394+
* @returns {boolean}
395+
*/
396+
isAutoTurret() {
397+
const crosshairControlFlag = Camera.ControlFlags.CROSSHAIR;
398+
return (this.cameraSubscribeInfo?.controlFlags & crosshairControlFlag) === crosshairControlFlag;
399+
}
400+
366401
}
367402

368403
class IndexGenerator {

examples/10_shoot_autoturret.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const RustPlus = require('../rustplus');
2+
const Camera = require("../camera");
3+
var rustplus = new RustPlus('ip', 'port', 'playerId', 'playerToken');
4+
5+
function delay(time) {
6+
return new Promise(resolve => setTimeout(resolve, time));
7+
}
8+
9+
rustplus.on('connected', async () => {
10+
11+
console.log("connected");
12+
13+
// get a camera (should be an autoturret for this example)
14+
const turret = rustplus.getCamera("TURRET1");
15+
16+
// wait until subscribed to autoturret camera
17+
turret.on('subscribed', async () => {
18+
19+
// check if camera is an auto turret
20+
if(!turret.isAutoTurret()){
21+
console.log("Camera is not an auto turret!");
22+
await rustplus.disconnect();
23+
return;
24+
}
25+
26+
console.log("subscribed to autoturret camera");
27+
await delay(500);
28+
29+
const shootCount = 3;
30+
const shootDelay = 250;
31+
const moveDelay = 500;
32+
const moveAmount = 5;
33+
34+
// shoot autoturret
35+
console.log("shooting");
36+
for(let i = 0; i < shootCount; i++){
37+
await delay(shootDelay);
38+
await turret.shoot();
39+
}
40+
41+
// move autoturret left
42+
console.log("moving left");
43+
await delay(moveDelay);
44+
await turret.move(Camera.Buttons.NONE, -moveAmount, 0);
45+
46+
// shoot autoturret
47+
console.log("shooting");
48+
for(let i = 0; i < shootCount; i++){
49+
await delay(shootDelay);
50+
await turret.shoot();
51+
}
52+
53+
// move autoturret up
54+
console.log("moving up");
55+
await delay(moveDelay);
56+
await turret.move(Camera.Buttons.NONE, 0, moveAmount);
57+
58+
// shoot autoturret
59+
console.log("shooting");
60+
for(let i = 0; i < shootCount; i++){
61+
await delay(shootDelay);
62+
await turret.shoot();
63+
}
64+
65+
// move autoturret right
66+
console.log("moving right");
67+
await delay(moveDelay);
68+
await turret.move(Camera.Buttons.NONE, moveAmount, 0);
69+
70+
// shoot autoturret
71+
console.log("shooting");
72+
for(let i = 0; i < shootCount; i++){
73+
await delay(shootDelay);
74+
await turret.shoot();
75+
}
76+
77+
// move autoturret down
78+
console.log("moving down");
79+
await delay(moveDelay);
80+
await turret.move(Camera.Buttons.NONE, 0, -moveAmount);
81+
82+
// shoot autoturret
83+
console.log("shooting");
84+
for(let i = 0; i < shootCount; i++){
85+
await delay(shootDelay);
86+
await turret.shoot();
87+
}
88+
89+
console.log("disconnecting");
90+
await rustplus.disconnect();
91+
92+
});
93+
94+
// subscribe to autoturret camera
95+
await turret.subscribe();
96+
97+
});
98+
99+
// connect to rust server
100+
rustplus.connect();

0 commit comments

Comments
 (0)