fenomas / noa

Experimental voxel game engine.
MIT License
616 stars 91 forks source link

How to lock / unlock mouse? #61

Closed EliteAsian123 closed 5 years ago

EliteAsian123 commented 5 years ago

Hello there, I was recently thinking about working on a GUI system for the game I was making in this engine. However, I can't seem to figure out how to lock / unlock the mouse. I've done coding a lot before, but I'm fairly new to javascript so sorry if this question has a obvious answer. Thanks!

fenomas commented 5 years ago

Hi, no worries! The super-general answer is that mouse lock in the browser is done through the pointer lock API. However the noa-specific answer is that noa.container attempts to abstract this, so you should be able to do:

noa.container.setPointerLock( bool )
noa.container.hasPointerLock() // returns true or false

Beware that there are browser-specific details - e.g. just asking for pointer lock isn't guaranteed to work - the browser may show the user a prompt or whatever - so you need to ask for it and then catch events or check later to see if it worked.

Also in making my own game UI, I've found that browsers sometimes handle edge cases inconsistently - e.g. if you request pointer lock while the mouse is outside the browser window, etc.

Good luck!

EliteAsian123 commented 5 years ago

Hello! Thanks for the response! It seems to be working just fine, but how do I disable the input of the player? Also it seems that noa.container.hasPointerLock() is not a function. Again, I am new to javascript so I don't know if I'm doing this correctly. if (noa.container.hasPointerLock()) { ... } Is this correct?

fenomas commented 5 years ago

noa.container.hasPointerLock() is not a function

Sorry about that, I was looking at an old version. It's just a boolean, you're right!

Re: character movement: the short answer is, I think this will probably do what you want:

noa.entities.removeComponent(noa.playerEntity, noa.ents.names.receivesInputs)
// .. later
noa.entities.addComponent(noa.playerEntity, noa.ents.names.receivesInputs)

The longer answer is, noa uses an ECS to manage entities and their behavior. By default, the player entity gets initialized with two components (among others) - the movement component, which applies physics forces for moving/jumping/etc., and the receivesInputs component, which basically looks at the key inputs and sends messages to the movement component. Hence removing and re-adding the latter components causes key inputs to stop/start affecting the movement state.

It sounds complicated, but the reasoning is that some games will want to implement their own movement rules. So those games can simply remove both components from the player entity, and then add their own components (with whatever arbitrary movement logic).

Incidentally you can also unbind/rebind the key inputs - related methods are in noa.inputs.

Hope that makes sense!

(btw, feel free to close this issue if that works for you, and open new issues if you have other questions. Should make it easier for others to search, and maybe someday I'll convert some of these answers to documentation..)

EliteAsian123 commented 5 years ago

Thanks for help! This works perfectly! This engine is probably one of the best javascript engines I've come across. The only thing it's lacking is a documentation. Have a great day or night!