Skip to content

Instantly share code, notes, and snippets.

@Mikepicker
Created November 16, 2017 18:17
Show Gist options
  • Save Mikepicker/3bdabc689765f8daadb788885c15a4e4 to your computer and use it in GitHub Desktop.
Save Mikepicker/3bdabc689765f8daadb788885c15a4e4 to your computer and use it in GitHub Desktop.
#2 - player
void update() {
// Physics
survivor.y += world.gravity;
// Platform collision
if (survivor.y + survivor.h > platform.y &&
survivor.x + 48 >= platform.x &&
survivor.x + survivor.w - 48 <= platform.x + platform.w &&
survivor.y + (survivor.h/2) < platform.y) {
survivor.y = platform.y - survivor.h;
}
// Input processing
const Uint8* keys = SDL_GetKeyboardState(NULL);
if (survivor.state != "state_shoot") {
if (keys[SDL_SCANCODE_D]) {
survivor.x += survivor.speed;
survivor.scaleX = 1;
survivor.frameY = 1;
survivor.state = "state_walk";
} else if (keys[SDL_SCANCODE_A]) {
survivor.x -= survivor.speed;
survivor.scaleX = -1;
survivor.frameY = 1;
survivor.state = "state_walk";
} else if (keys[SDL_SCANCODE_J]) {
survivor.frameX = 0;
survivor.frameY = 2;
survivor.animCompleted = false;
survivor.shot = false;
survivor.state = "state_shoot";
} else {
survivor.frameX = 0;
survivor.frameY = 0;
survivor.state = "state_idle";
}
}
// Player states
if (survivor.state == "state_shoot") {
if (survivor.frameX / survivor.animSpeed == 2 && !survivor.shot) {
survivor.shot = true;
shootBullet();
}
if (survivor.animCompleted) {
survivor.frameY = 0;
survivor.state = "state_idle";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment