Skip to content

Commit dd0c3f2

Browse files
KalmeMarqrom1504
andauthored
Hit entities (#277)
* added readme pt-pt * added url/querystring deps and fix chat pos/scale url and querystring were missing in node_modules. chat scale option wasn't implemented and chat input was on top instead of bottom. * added bot version text field and guiScale for small screens text field to choose bot version. gui scale changes on small screens (slider takes no effect then). Removed unused images. * Update index.js * bot can now hit entities Co-authored-by: Romain Beaumont <romain.rom1@gmail.com>
1 parent 9ac6096 commit dd0c3f2

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

lib/cursor.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
const { Vec3 } = require('vec3')
44

5+
function getViewDirection (pitch, yaw) {
6+
const csPitch = Math.cos(pitch)
7+
const snPitch = Math.sin(pitch)
8+
const csYaw = Math.cos(yaw)
9+
const snYaw = Math.sin(yaw)
10+
return new Vec3(-snYaw * csPitch, snPitch, -csYaw * csPitch)
11+
}
12+
513
class Cursor {
614
constructor (viewer, renderer, bot) {
715
// Init state
@@ -37,9 +45,36 @@ class Cursor {
3745
document.addEventListener('mouseup', (e) => {
3846
this.buttons[e.button] = false
3947
})
48+
4049
document.addEventListener('mousedown', (e) => {
4150
if (document.pointerLockElement !== renderer.domElement) return
4251
this.buttons[e.button] = true
52+
53+
const entity = bot.nearestEntity((e) => {
54+
if (e.position.distanceTo(bot.entity.position) <= (bot.player.gamemode === 1 ? 5 : 3)) {
55+
const dir = getViewDirection(bot.entity.pitch, bot.entity.yaw)
56+
const { width, height } = e
57+
const { x: eX, y: eY, z: eZ } = e.position
58+
const { x: bX, y: bY, z: bZ } = bot.entity.position
59+
const box = new THREE.Box3(
60+
new THREE.Vector3(eX - width / 2, eY, eZ - width / 2),
61+
new THREE.Vector3(eX + width / 2, eY + height, eZ + width / 2)
62+
)
63+
64+
const r = new THREE.Raycaster(
65+
new THREE.Vector3(bX, bY + 1.52, bZ),
66+
new THREE.Vector3(dir.x, dir.y, dir.z)
67+
)
68+
const int = r.ray.intersectBox(box, new THREE.Vector3(eX, eY, eZ))
69+
return int !== null
70+
}
71+
72+
return false
73+
})
74+
75+
if (entity) {
76+
bot.attack(entity)
77+
}
4378
})
4479
this.lastPlaced = 4 // ticks since last placed
4580
bot.on('physicsTick', () => { if (this.lastPlaced < 4) this.lastPlaced++ })

0 commit comments

Comments
 (0)