Skip to content

Instantly share code, notes, and snippets.

@Mikepicker
Last active November 17, 2017 19:33
Show Gist options
  • Save Mikepicker/879f051709afb164be49b163c8a2814f to your computer and use it in GitHub Desktop.
Save Mikepicker/879f051709afb164be49b163c8a2814f to your computer and use it in GitHub Desktop.
void update() {
// Apply gravity
survivor.vY += world.gravity;
survivor.y += survivor.vY;
// Motion
survivor.x += survivor.vX;
// If survivor is colliding with the platform..
if (survivor.y + survivor.h > platform.y &&
survivor.x + 48 >= platform.x &&
survivor.x + survivor.w - 48 <= platform.x + platform.w) {
// .. place it on the platform and stop its vertical velocity
survivor.y = platform.y - survivor.h;
survivor.vY = 0;
// Also, if he was either falling or jumping, get him back to the idle state
if (survivor.state == "state_fall" || survivor.state == "state_jump") {
survivor.frameY = 0;
survivor.state = "state_idle";
}
} else if (survivor.state != "state_jump") { // Otherwise, if he's not touching the platform and he's not jumping..
// .. it means that he's falling of course :)
survivor.state = "state_fall";
}
// Input processing
const Uint8* keys = SDL_GetKeyboardState(NULL);
// If survivor is not doing any unstoppable action (shooting, stabbing or falling)..
if (survivor.state != "state_shoot" && survivor.state != "state_stab" && survivor.state != "state_fall") {
// If he's jumping
if (survivor.state == "state_jump") {
// Allow to control his movement while jumping
if (keys[SDL_SCANCODE_D]) {
survivor.vX = survivor.speed; // We're tweaking horizontal velocity here..
survivor.scaleX = 1;
} else if (keys[SDL_SCANCODE_A]) {
survivor.vX = -survivor.speed; // .. and here to control flight
survivor.scaleX = -1;
} else { // Otherwise, if no keys are currently pressed..
survivor.vX = 0; // ..stop horizontal velocity (let him fall vertically)
}
// Otherwise, if he's either walking or standing still..
} else {
// Detect keyboard keys for jumping, moving or stabbing
if (keys[SDL_SCANCODE_W]) {
survivor.vY = -survivor.jumpSpeed; // Notice how we set an initial velocity here!
survivor.frameY = 3;
survivor.state = "state_jump";
} else if (keys[SDL_SCANCODE_J]) {
survivor.frameX = 0;
survivor.frameY = 2;
survivor.vX = 0; // Also walking is now set using velocity
survivor.animCompleted = false;
survivor.shot = false;
survivor.state = "state_shoot";
} else if (keys[SDL_SCANCODE_K]) {
survivor.frameX = 0;
survivor.frameY = 5;
survivor.vX = 0;
survivor.animCompleted = false;
survivor.state = "state_stab";
} else if (keys[SDL_SCANCODE_D]) {
survivor.vX = survivor.speed;
survivor.scaleX = 1;
survivor.frameY = 1;
survivor.state = "state_walk";
} else if (keys[SDL_SCANCODE_A]) {
survivor.vX = -survivor.speed;
survivor.scaleX = -1;
survivor.frameY = 1;
survivor.state = "state_walk";
} else {
survivor.frameY = 0;
survivor.vX = 0;
survivor.state = "state_idle";
}
}
}
// Player states
// If survivor is shooting..
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";
}
}
// If survivor is stabbing (todo: hit zombies)..
if (survivor.state == "state_stab") {
if (survivor.animCompleted) {
survivor.frameY = 0;
survivor.state = "state_idle";
}
}
// Bullets (nothing new here)
for (int i = 0; i < BULLET_COUNT; i++) {
if (bullets[i].alive) {
bullets[i].x += bulletSpeed * bullets[i].dir;
if (bullets[i].x + bulletWidth < 0 || bullets[i].x > SCREEN_WIDTH) {
bullets[i].alive = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment