@@ -43,15 +43,20 @@ const controlOptions = {
4343
4444export const contro = new ControMax ( {
4545 commands : {
46- general : {
47- // movement
46+ movement : {
47+ forward : [ 'KeyW' ] ,
48+ back : [ 'KeyS' ] ,
49+ left : [ 'KeyA' ] ,
50+ right : [ 'KeyD' ] ,
4851 jump : [ 'Space' , 'A' ] ,
49- inventory : [ 'KeyE' , 'X' ] ,
50- drop : [ 'KeyQ' , 'B' ] ,
51- dropStack : [ null ] ,
5252 sneak : [ 'ShiftLeft' , 'Down' ] ,
5353 toggleSneakOrDown : [ null , 'Right Stick' ] ,
5454 sprint : [ 'ControlLeft' , 'Left Stick' ] ,
55+ } ,
56+ general : {
57+ inventory : [ 'KeyE' , 'X' ] ,
58+ drop : [ 'KeyQ' , 'B' ] ,
59+ dropStack : [ null ] ,
5560 // game interactions
5661 nextHotbarSlot : [ null , 'Right Bumper' ] ,
5762 prevHotbarSlot : [ null , 'Left Bumper' ] ,
@@ -95,7 +100,6 @@ export const contro = new ControMax({
95100 // showLookingBlockUsages: ['Numpad4']
96101 // }
97102 } satisfies Record < string , Record < string , SchemaCommandInput > > ,
98- movementKeymap : 'WASD' ,
99103 movementVector : '2d' ,
100104 groupedCommands : {
101105 general : {
@@ -181,17 +185,14 @@ contro.on('movementUpdate', ({ vector, soleVector, gamepadIndex }) => {
181185 }
182186
183187 for ( const key of [ 'forward' , 'back' , 'left' , 'right' ] as const ) {
184- if ( newState [ key ] === bot . controlState [ key ] ) continue
188+ if ( ! ! ( newState [ key ] ) === ! ! ( bot . controlState [ key ] ) ) continue
185189 const action = ! ! newState [ key ]
186190 if ( action && ! isGameActive ( true ) ) continue
187191 bot . setControlState ( key , action )
188192
189193 if ( key === 'forward' ) {
190194 // todo workaround: need to refactor
191- if ( action ) {
192- void contro . emit ( 'trigger' , { command : 'general.forward' } as any )
193- } else {
194- void contro . emit ( 'release' , { command : 'general.forward' } as any )
195+ if ( ! action ) {
195196 setSprinting ( false )
196197 }
197198 }
@@ -201,12 +202,12 @@ contro.on('movementUpdate', ({ vector, soleVector, gamepadIndex }) => {
201202let lastCommandTrigger = null as { command : string , time : number } | null
202203
203204const secondActionActivationTimeout = 300
204- const secondActionCommands = {
205- 'general .jump' ( ) {
205+ const secondActionCommands : Partial < Record < Command , ( ) => void > > = {
206+ 'movement .jump' ( ) {
206207 // if (bot.game.gameMode === 'spectator') return
207208 toggleFly ( )
208209 } ,
209- 'general .forward' ( ) {
210+ 'movement .forward' ( ) {
210211 setSprinting ( true )
211212 }
212213}
@@ -300,11 +301,10 @@ const setSneaking = (state: boolean) => {
300301const onTriggerOrReleased = ( command : Command , pressed : boolean ) => {
301302 // always allow release!
302303 if ( ! bot || ! isGameActive ( false ) ) return
303- if ( stringStartsWith ( command , 'general' ) ) {
304- // handle general commands
305- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
304+
305+ if ( stringStartsWith ( command , 'movement' ) ) {
306306 switch ( command ) {
307- case 'general .jump' :
307+ case 'movement .jump' :
308308 if ( isSpectatingEntity ( ) ) break
309309 // if (viewer.world.freeFlyMode) {
310310 // const moveSpeed = 0.5
@@ -313,27 +313,45 @@ const onTriggerOrReleased = (command: Command, pressed: boolean) => {
313313 bot . setControlState ( 'jump' , pressed )
314314 // }
315315 break
316- case 'general .sneak' :
316+ case 'movement .sneak' :
317317 // if (viewer.world.freeFlyMode) {
318318 // const moveSpeed = 0.5
319319 // viewer.world.freeFlyState.position.add(new Vec3(0, pressed ? -moveSpeed : 0, 0))
320320 // } else {
321321 setSneaking ( pressed )
322322 // }
323323 break
324- case 'general .sprint' :
324+ case 'movement .sprint' :
325325 // todo add setting to change behavior
326326 if ( pressed ) {
327327 setSprinting ( pressed )
328328 }
329329 break
330- case 'general .toggleSneakOrDown' :
330+ case 'movement .toggleSneakOrDown' :
331331 if ( gameAdditionalState . isFlying ) {
332332 setSneaking ( pressed )
333333 } else if ( pressed ) {
334334 setSneaking ( ! gameAdditionalState . isSneaking )
335335 }
336336 break
337+ case 'movement.forward' :
338+ contro . setMovement ( 'forward' , pressed )
339+ break
340+ case 'movement.back' :
341+ contro . setMovement ( 'backward' , pressed )
342+ break
343+ case 'movement.left' :
344+ contro . setMovement ( 'left' , pressed )
345+ break
346+ case 'movement.right' :
347+ contro . setMovement ( 'right' , pressed )
348+ break
349+ }
350+ }
351+ if ( stringStartsWith ( command , 'general' ) ) {
352+ // handle general commands
353+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
354+ switch ( command ) {
337355 case 'general.attackDestroy' :
338356 document . dispatchEvent ( new MouseEvent ( pressed ? 'mousedown' : 'mouseup' , { button : 0 } ) )
339357 break
@@ -479,7 +497,7 @@ contro.on('trigger', ({ command }) => {
479497 if ( secondActionCommand ) {
480498 if ( command === lastCommandTrigger ?. command && Date . now ( ) - lastCommandTrigger . time < secondActionActivationTimeout ) {
481499 const commandToTrigger = secondActionCommands [ lastCommandTrigger . command ]
482- commandToTrigger ( )
500+ commandToTrigger ?. ( )
483501 lastCommandTrigger = null
484502 } else {
485503 lastCommandTrigger = {
@@ -493,10 +511,6 @@ contro.on('trigger', ({ command }) => {
493511
494512 if ( stringStartsWith ( command , 'general' ) ) {
495513 switch ( command ) {
496- case 'general.jump' :
497- case 'general.sneak' :
498- case 'general.toggleSneakOrDown' :
499- case 'general.sprint' :
500514 case 'general.attackDestroy' :
501515 case 'general.rotateCameraLeft' :
502516 case 'general.rotateCameraRight' :
@@ -952,10 +966,6 @@ export const handleMobileButtonActionCommand = (command: ActionType | ActionHold
952966 if ( typeof commandValue === 'string' && ! stringStartsWith ( commandValue , 'custom' ) ) {
953967 const event : CommandEventArgument < typeof contro [ '_commandsRaw' ] > = {
954968 command : commandValue as Command ,
955- schema : {
956- keys : [ ] ,
957- gamepad : [ ]
958- }
959969 }
960970 if ( isDown ) {
961971 contro . emit ( 'trigger' , event )
0 commit comments