Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Right Mouse button #114

Closed
z3t0 opened this issue Apr 8, 2015 · 11 comments
Closed

Right Mouse button #114

z3t0 opened this issue Apr 8, 2015 · 11 comments

Comments

@z3t0
Copy link

z3t0 commented Apr 8, 2015

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

@shama
Copy link
Contributor

shama commented Apr 8, 2015

@z3t0
Copy link
Author

z3t0 commented Apr 8, 2015

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
Copy link
Author

z3t0 commented Apr 9, 2015

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
Copy link
Contributor

shama commented Apr 9, 2015

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
Copy link
Author

z3t0 commented Apr 9, 2015

Thanks! I am currently using this.

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

@z3t0
Copy link
Author

z3t0 commented Apr 9, 2015

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
Copy link
Contributor

shama commented Apr 9, 2015

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
Copy link
Author

z3t0 commented Apr 9, 2015

@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
Copy link
Contributor

shama commented Apr 9, 2015

@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.

@deathcap
Copy link
Collaborator

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
Copy link
Author

z3t0 commented Apr 10, 2015

@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?

@z3t0 z3t0 mentioned this issue Apr 10, 2015
@z3t0 z3t0 closed this as completed Apr 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants