max-mapper/voxel-engine

Right Mouse button

Closed this issue · 11 comments

z3t0 commented

There seems to be no way to detect the right mouse button..

z3t0 commented

Sorry I'm kinda new to this, could you please provide a short example? I managed to get this working with gamejs but that is giving me a few annoying bugs

z3t0 commented

This is how I managed to get it working now.

// Controls

controls()

function controls(){

//Mouse Input
    document.body.addEventListener('mousedown', mousePressed)
    function mousePressed(event){
        switch(event.button){
            case 0: // Left mouse button
                var position = block
                if(game.getBlock(position) != 0){
                game.setBlock(position, 0)
                console.log('Break: ' + position)
                } else{
                    console.log('No Block')
                }

                break;

            case 1: // Middle mouse button
                var position = block
                var type = game.getBlock(position)
                console.log('BlockType: ' + type + ' : ' + position)
                break;

            case 2: // Right mouse button
            console.log(adjacentBlock)
                var position = adjacentBlock
                console.log(position)
                game.setBlock(position, 1)
                console.log('Placed: ' + position)

        }
    }

}
shama commented

Ah sorry I didn't understand your original request. contextmenu won't be useful in that case.

Here is an example which raycasts from the player's position and vector to determine the block to set (in which game is an instance of voxel-engine).

function raycast(dist) {
  dist = dist || 100
  var pos = game.cameraPosition()
  var vec = game.cameraVector()
  return game.raycastVoxels(pos, vec, dist)
}
document.addEventListener('mousedown', function(e) {
  if (e.button === 2) {
    var block = raycast()
    if (block) game.setBlock(block.position, 1)
  }
})

Also check out https://github.com/maxogden/voxel-highlight which does this and https://github.com/maxogden/voxel-hello-world/blob/master/index.js for a more in depth example.

z3t0 commented

Thanks! I am currently using this.

hl.on('highlight-adjacent', function(voxelPos) {
    adjacentBlock = voxelPos
    game.setBlock(adjacentBlock, 1)
    console.log('Placed: ' + adjacentBlock)
});
z3t0 commented

I was thinking of making some sort of wiki for voxel-engine and similar packages where we can put important things like this, what do you think? Also, is voxel-hello-world broken?

shama commented

You'd want to talk to @kumavis (as I think he is primary maintainer now?)

I'm not up to date on the current status of all the voxel- projects. I know a great deal of the effort has been move towards http://stack.gl/ and I think the plan is to circle back to the voxel- projects once the resources there are sufficient.

z3t0 commented

@shama That sounds interesting, but as I understand it voxel-engine uses Three.js right? So wouldn't that require rewriting it in stack.gl?

shama commented

@z3t0 I don't think any parts of three.js need to be rewritten. We just write the modules we need and try to avoid creating too much interdependence (as that would give us the same issues we have with three.js now).

A lot of these modules (or at least the foundation) already exist:

It's a bit all over the place atm but the holes are slowly being filled in. Once they are, making voxel modules and apps will become much easier and we'll be able to do a lot more interesting work.

The biggest missing piece in the ndarray/stackgl branch (#103) right now is player avatars, there isn't anything like we had in three.js ported over, afaik.

For the original question of detecting right-click, #112 has some relevant pointers. I use https://github.com/deathcap/voxel-reach to handle DOM mousedown events; https://github.com/mikolalysenko/game-shell also listens for this event and provides a polling-style interface, which I use via descriptive keybinding names in https://github.com/deathcap/voxel-keys - this plugin also handles preventing the "default" browser action for both keys and mouse actions, so you can use them uninterrupted in your game (that is, it listens for DOM keydown and contextmenu events and calls preventDefault())

z3t0 commented

@deathcap @shama Thanks for the informative replies! We should probably create some sort of overarching wiki for the whole voxel-* project(s) because things like this are really confusing when you first start out.

P.S: This might be kinda stupid but... Is there some wiki out there I am not aware of? Because that would help a lot 😄. Otherwise I could work on building the skeleton for one and you guys could fill in the technical details?