kumavis / node-warrior

:tiger: 3D multiplayer voxel sandbox to teach programming

Home Page:https://kumavis.github.io/node-warrior

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Right click to place a block

knod opened this issue · comments

commented

The user could right-click to place a block.

This happens via voxel-highlight
implemented here
and used in same file here

the voxel-highlight option adjacentActive defaults to game.controls.state.alt

Here controls are being initialized in voxel-engine
Here are the default controls in voxel-engine
Looks like alt and firealt are different. firealt is not being used so it might as well just use alt.

These options are passed down from the options hash from when we instantiate the game. We are not specifying controls so it is just using the defaults.

Adding the modified controls hash to the defaults should work. Alternatively/additionally we can file a pull request (PR) on voxel-engine to change the default controls.

Another solution would be to configure voxel-highlight to use either value alt or firealt.

from irc
@knod said: Since 'mouse 2' doesn't seem to be in there at all, 'firealt' seems to be the way to go. From what I understand 'alt' just makes the alternate possible, it doesn't actually fire the mouse action, so we'd want 'firealt'
It seems fine to do it with our hash and if I can find it I'll try to change that. I think also filing a pull request sounds great as it seems to me a very sensical key binding to create
I didn't completely understand the voxel-highlight thing, so I don't really know how to make a judgement on that part.

@knod good point about making alternate possible vs actually firing the action, I hadn't realized that.

So to clarify the goal here we want to:

  • preserve the current behavior
  • in addition we want mouse 2 to act as if we performed a left click with the alt key pressed.
    At this time it seems right clicking just performs the same as a left click.

If you get a chance, try adding options to the highlight instantiation:
change this to this:

var hl = game.highlighter = highlight(game, {
  color: 0xff0000,
  adjacentActive: function(){
    return game.controls.state.alt || game.controls.state.altfire
  },
})

update! @kumavis is a total dingbat. Above line from code snippet should read:

return game.controls.state.alt || game.controls.state.firealt

firealt not altfire

did not solve it though - this is surprisingly difficult : P

commented

Color me surprised, it works.
This is the code I've been working with in client.js, with some surrounding code to give context. Haven't tried it yet, but since I know 'fire' will happen nullifying what I might be able to do with 'firealt' I suspect there won't be any results:

(The surrounding code has since been changed, so it may not accurately reflect the current state of the code)

  toolSelector.on('select',function(index){
    var newTool = playerTools[index]
    self.codeBeam.setCurrentTool(newTool)
  })
  toolSelector.switchToolbar(0)

  // game right click event, place block
  // detect right click (mouse 3, 'firealt' in voxel something)
  // set alt to true, as holding ctrl does
  // fire, as left click does
  // get whatever block is in the menu
  // set alt to what it was before
  game.on('firealt', function (target, state) {
    debugger
    // I don't see target or state being used here anywhere
    // // Store what alt was before
    // previousAlt = game.controls.state.alt
    // // Ummm, I want to both trigger alt and fire, not sure how
    // game.controls.state.alt = true
    // // game.controls.state.alt.emit(voxelPos)?
    // Set the block placing variable to a position in the world
    blockPosPlace = voxelPos // emit to h1.on() instead?

    self.codeBeam.runCode({
      game: game,
      avatar: avatar,
      // I don't see hitblock being used anywhere else in here, what is it used for?
      hitBlock: blockPosPlace || blockPosErase,
      // What is secondaryClick supposed to be? and why does it set the opposite of blocksPosPlace?
      // and what is the opposite of a voxelPos or null?
      secondaryClick: !!blockPosPlace,
      client: self,
      // Why do we set the block before getting the block?
      setBlock: setBlock,
      getBlock: game.getBlock,
      // Why is require set to itself? Is it reimporting those dependancies? Should it have a comma at the end?
      require: require,
    })
    // // reset state.alt to what it was before
    // game.controls.state.alt = previousAlt
  })
  // Now how to make sure 'fire' doesn't also apply to 'firealt'?

  // game click event, run code
  game.on('fire', function (target, state) {
    self.codeBeam.runCode({
      game: game,
      avatar: avatar,
      hitBlock: blockPosPlace || blockPosErase,
      secondaryClick: !!blockPosPlace,
      client: self,
      setBlock: setBlock,
      getBlock: game.getBlock,
      require: require,
    })
  })

Huzzah!
(Working on cleaning up for a commit now)

commented

I have quite possibly pushed the change now.

Good job @knod! You can tie issues and commits together by including Fixes #17 or Updates #17 in the commit message. Doing Fixes will automatically close the issue.

commented

This has been taken care of in this commit: 0f3a37d

👍