Last active
December 19, 2015 18:38
-
-
Save pixelnerve/6000287 to your computer and use it in GitHub Desktop.
User input for camera control with a Byte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CamMoveStates | |
{ | |
CamMoveNone = 0 | |
, CamMoveForward = (1<<0) | |
, CamMoveBackwards = (1<<1) | |
, CamMoveLeft = (1<<2) | |
, CamMoveRight = (1<<3) | |
, CamMoveUp = (1<<4) | |
, CamMoveDown = (1<<5) | |
}; | |
uint8 camMoveState = CamMoveNone; | |
void Update( float time, float frametime ) | |
{ | |
// Check which bits are on and add direction vector | |
if( camMoveState ) | |
{ | |
if( camMoveState & CamMoveForward ) | |
lMoveVector += ForwardVector; | |
else if( camMoveState & CamMoveBackwards ) | |
lMoveVector += -ForwardVector; | |
if( camMoveState & CamMoveUp ) | |
lMoveVector += UpVector; | |
else if( camMoveState & CamMoveDown ) | |
lMoveVector += -UpVector; | |
if( camMoveState & CamMoveRight ) | |
lMoveVector += RightVector; | |
else if( camMoveState & CamMoveLeft ) | |
lMoveVector += -RightVector; | |
// Normalize final direction vector | |
lMoveVector.normalize(); | |
} | |
// Update camera position | |
camPos += lMoveVector; | |
} | |
bool OnKeyDown( ... ) | |
{ | |
if( event.getCode() == KeyEvent::KEY_UP ) | |
{ | |
camMoveState = camMoveState | CamMoveForward; | |
} | |
if( event.getCode() == KeyEvent::KEY_DOWN ) | |
{ | |
camMoveState = camMoveState | CamMoveBackwards; | |
} | |
if( event.getCode() == KeyEvent::KEY_q ) | |
{ | |
camMoveState = camMoveState | CamMoveUp; | |
} | |
if( event.getCode() == KeyEvent::KEY_a ) | |
{ | |
camMoveState = camMoveState | CamMoveDown; | |
} | |
if( event.getCode() == KeyEvent::KEY_LEFT ) | |
{ | |
camMoveState = camMoveState | CamMoveLeft; | |
} | |
if( event.getCode() == KeyEvent::KEY_RIGHT ) | |
{ | |
camMoveState = camMoveState | CamMoveRight; | |
} | |
} | |
bool OnKeyUp( ... ) | |
{ | |
// Cancel the bit for current direction | |
if( event.getCode() == KeyEvent::KEY_UP ) | |
{ | |
camMoveState = camMoveState & ~CamMoveForward; | |
} | |
if( event.getCode() == KeyEvent::KEY_DOWN ) | |
{ | |
camMoveState = camMoveState & ~CamMoveBackwards; | |
} | |
if( event.getCode() == KeyEvent::KEY_q ) | |
{ | |
camMoveState = camMoveState & ~CamMoveUp; | |
} | |
if( event.getCode() == KeyEvent::KEY_a ) | |
{ | |
camMoveState = camMoveState & ~CamMoveDown; | |
} | |
if( event.getCode() == KeyEvent::KEY_LEFT ) | |
{ | |
camMoveState = camMoveState & ~CamMoveLeft; | |
} | |
if( event.getCode() == KeyEvent::KEY_RIGHT ) | |
{ | |
camMoveState = camMoveState & ~CamMoveRight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment