|
2 | 2 |
|
3 | 3 | const { Vec3 } = require('vec3')
|
4 | 4 |
|
| 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 | + |
5 | 13 | class Cursor {
|
6 | 14 | constructor (viewer, renderer, bot) {
|
7 | 15 | // Init state
|
@@ -37,9 +45,36 @@ class Cursor {
|
37 | 45 | document.addEventListener('mouseup', (e) => {
|
38 | 46 | this.buttons[e.button] = false
|
39 | 47 | })
|
| 48 | + |
40 | 49 | document.addEventListener('mousedown', (e) => {
|
41 | 50 | if (document.pointerLockElement !== renderer.domElement) return
|
42 | 51 | 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 | + } |
43 | 78 | })
|
44 | 79 | this.lastPlaced = 4 // ticks since last placed
|
45 | 80 | bot.on('physicsTick', () => { if (this.lastPlaced < 4) this.lastPlaced++ })
|
|
0 commit comments