Skip to content

Instantly share code, notes, and snippets.

@georgiee
Last active August 29, 2015 13:56
Show Gist options
  • Save georgiee/9074497 to your computer and use it in GitHub Desktop.
Save georgiee/9074497 to your computer and use it in GitHub Desktop.
//https://stackoverflow.com/questions/1855211/how-to-store-several-states-in-one-variable
//http://graphics.stanford.edu/~seander/bithacks.html
OtherGameStates = {}
OtherGameStates.GAME_PAUSED = 1 //'0001'
OtherGameStates.USER_PAUSED = 2 //'0010'
SoundStates = {}
SoundStates.USER_MUTED = 4 //'0100'
SoundStates.GAME_MUTED = 8 //'1000'
SoundStates.TOTALLY_UNRELATED_STATE = 16 //'10000'
console.log('OtherGameStates.GAME_PAUSED is', asBinary(OtherGameStates.GAME_PAUSED))
console.log ('OtherGameStates.USER_PAUSED is', asBinary(OtherGameStates.USER_PAUSED))
console.log('SoundStates.USER_MUTED is', asBinary(SoundStates.USER_MUTED))
console.log('SoundStates.GAME_MUTED is', asBinary(SoundStates.GAME_MUTED))
//set states
gameStates = 0
//user mutes and pauses now
gameStates |= SoundStates.USER_MUTED | SoundStates.GAME_MUTED
//game mutes and pauses now
gameStates |= OtherGameStates.USER_PAUSED | OtherGameStates.GAME_PAUSED
gameStates |= SoundStates.TOTALLY_UNRELATED_STATE
console.log('starting with: MUTED by GAME and USER and additionally game PAUSED by GAME and USER', asBinary(gameStates))
//game unmutes now
gameStates &= ~SoundStates.GAME_MUTED
console.log('action: unmuted by game', asBinary(gameStates))
//state checking (after an event for example)
if ((gameStates & (SoundStates.GAME_MUTED | SoundStates.USER_MUTED)) != 0){
if ((gameStates & SoundStates.USER_MUTED) == SoundStates.USER_MUTED){
console.log('** but only by user', gameStates)
}
}
//user unmutes now
gameStates &= ~SoundStates.USER_MUTED
console.log('action: unmuted by user', asBinary(gameStates))
if ((gameStates & (SoundStates.GAME_MUTED | SoundStates.USER_MUTED)) == 0){
console.log('** nothing is muted')
}
if ((gameStates & OtherGameStates.GAME_PAUSED) != 0){
console.log('game is paused')
}else{
console.log('game is running')
}
//unpaused by game
gameStates &= ~OtherGameStates.GAME_PAUSED
if ((gameStates & OtherGameStates.GAME_PAUSED) != 0){
console.log('game is paused')
}else{
console.log('game is running')
}
//a state can be maintained over chaining several other states. So this
//will be true as it's been set at the beginning
console.log('gameStates', asBinary( gameStates & (OtherGameStates.GAME_PAUSED | OtherGameStates.USER_PAUSED)) )
if ( (gameStates & (OtherGameStates.GAME_PAUSED | OtherGameStates.USER_PAUSED)) == OtherGameStates.USER_PAUSED){
console.log('game is still paused, but only by user')
}
//helper functions
function asBinary(value){
return pad(value.toString(2), 4)
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
/*
//console output
OtherGameStates.GAME_PAUSED is 0001 test_states.js?body=1:13
OtherGameStates.USER_PAUSED is 0010 test_states.js?body=1:14
SoundStates.USER_MUTED is 0100 test_states.js?body=1:15
SoundStates.GAME_MUTED is 1000 test_states.js?body=1:16
starting with: MUTED by GAME and USER and additionally game PAUSED by GAME and USER 11111 test_states.js?body=1:28
action: unmuted by game 10111 test_states.js?body=1:32
** but only by user 23 test_states.js?body=1:37
action: unmuted by user 10011 test_states.js?body=1:43
** nothing is muted test_states.js?body=1:46
game is paused test_states.js?body=1:50
game is running test_states.js?body=1:62
gameStates 0010 test_states.js?body=1:67
game is still paused, but only by user test_states.js?body=1:70
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment