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>
This commit is contained in:
parent
9ac609690d
commit
dd0c3f22d3
1 changed files with 35 additions and 0 deletions
|
@ -2,6 +2,14 @@
|
|||
|
||||
const { Vec3 } = require('vec3')
|
||||
|
||||
function getViewDirection (pitch, yaw) {
|
||||
const csPitch = Math.cos(pitch)
|
||||
const snPitch = Math.sin(pitch)
|
||||
const csYaw = Math.cos(yaw)
|
||||
const snYaw = Math.sin(yaw)
|
||||
return new Vec3(-snYaw * csPitch, snPitch, -csYaw * csPitch)
|
||||
}
|
||||
|
||||
class Cursor {
|
||||
constructor (viewer, renderer, bot) {
|
||||
// Init state
|
||||
|
@ -37,9 +45,36 @@ class Cursor {
|
|||
document.addEventListener('mouseup', (e) => {
|
||||
this.buttons[e.button] = false
|
||||
})
|
||||
|
||||
document.addEventListener('mousedown', (e) => {
|
||||
if (document.pointerLockElement !== renderer.domElement) return
|
||||
this.buttons[e.button] = true
|
||||
|
||||
const entity = bot.nearestEntity((e) => {
|
||||
if (e.position.distanceTo(bot.entity.position) <= (bot.player.gamemode === 1 ? 5 : 3)) {
|
||||
const dir = getViewDirection(bot.entity.pitch, bot.entity.yaw)
|
||||
const { width, height } = e
|
||||
const { x: eX, y: eY, z: eZ } = e.position
|
||||
const { x: bX, y: bY, z: bZ } = bot.entity.position
|
||||
const box = new THREE.Box3(
|
||||
new THREE.Vector3(eX - width / 2, eY, eZ - width / 2),
|
||||
new THREE.Vector3(eX + width / 2, eY + height, eZ + width / 2)
|
||||
)
|
||||
|
||||
const r = new THREE.Raycaster(
|
||||
new THREE.Vector3(bX, bY + 1.52, bZ),
|
||||
new THREE.Vector3(dir.x, dir.y, dir.z)
|
||||
)
|
||||
const int = r.ray.intersectBox(box, new THREE.Vector3(eX, eY, eZ))
|
||||
return int !== null
|
||||
}
|
||||
|
||||
return false
|
||||
})
|
||||
|
||||
if (entity) {
|
||||
bot.attack(entity)
|
||||
}
|
||||
})
|
||||
this.lastPlaced = 4 // ticks since last placed
|
||||
bot.on('physicsTick', () => { if (this.lastPlaced < 4) this.lastPlaced++ })
|
||||
|
|
Loading…
Reference in a new issue