Skip to content

Instantly share code, notes, and snippets.

@Mikepicker
Created November 20, 2017 14:17
Show Gist options
  • Save Mikepicker/6b3dec4223043a4f2494be20c084c700 to your computer and use it in GitHub Desktop.
Save Mikepicker/6b3dec4223043a4f2494be20c084c700 to your computer and use it in GitHub Desktop.
// For each alive zombie..
for (int i = 0; i < ZOMBIE_COUNT; i++) {
if (zombies[i].alive) {
// Out of screen -> dead
if (zombies[i].y > SCREEN_HEIGHT) {
zombies[i].alive = false;
}
// Apply gravity
zombies[i].vY += world.gravity;
zombies[i].y += zombies[i].vY;
// Motion
zombies[i].x += zombies[i].vX;
// If zombie has been hit, skip (he will fall down)
if (zombies[i].state == "state_hit") {
continue;
}
// Deal with platform collision
if (platformCollision(zombies[i].x, zombies[i].y, zombies[i].w, zombies[i].h)) {
zombies[i].y = platform.y - zombies[i].h;
zombies[i].vY = 0;
if (zombies[i].state == "state_fall") {
zombies[i].state = "state_walk";
}
} else {
zombies[i].vX = 0;
zombies[i].state = "state_fall";
}
// If zombie is walking..
if (zombies[i].state == "state_walk") {
// Set velocity
zombies[i].vX = zombieSpeed * zombies[i].dir;
// Attack survivor if colliding
if (survivor.state != "state_dead" && collision(zombies[i].x, survivor.x, zombies[i].y, survivor.y, zombies[i].w, survivor.w, zombies[i].h, survivor.h)) {
// Set correct animation frames
zombies[i].frameX = 0;
zombies[i].frameY = 2;
// Stop it
zombies[i].vX = 0;
// Set "attack" flag to false (it will be set once the attack has been carried through)
zombies[i].attack = false;
// Since we're about to start the attack animation, set this flag to false
zombies[i].animCompleted = false;
// Change state
zombies[i].state = "state_attack";
}
}
// If the zombie is attacking..
if (zombies[i].state == "state_attack") {
// Attack survivor if animation is at the last frame (if survivor is jumping, he's able to avoid the attack)
if (zombies[i].frameX / zombieAnimSpeed == 3 && !zombies[i].attack && survivor.state != "state_jump") {
zombies[i].attack = true;
survivor.frameX = 0;
survivor.frameY = 4;
survivor.vX = survivor.vY = 0;
survivor.animCompleted = false;
survivor.state = "state_dead";
}
// Get back to the walk state once the job has been done
if (zombies[i].animCompleted) {
zombies[i].frameX = 0;
zombies[i].frameY = 0;
zombies[i].state = "state_walk";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment