Created
November 20, 2017 13:09
-
-
Save Mikepicker/ab4680357a9baa12b75e654f0e2ed65d to your computer and use it in GitHub Desktop.
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
// Check collision between two boxes (http://lazyfoo.net/tutorials/SDL/27_collision_detection/index.php) | |
bool collision(float xA, float xB, float yA, float yB, int wA, int wB, int hA, int hB) { | |
if (yA + hA <= yB) { | |
return false; | |
} | |
if (yA >= yB + hB) { | |
return false; | |
} | |
if (xA + wA <= xB) { | |
return false; | |
} | |
if (xA >= xB + wB) { | |
return false; | |
} | |
return true; | |
} | |
// Given a bullet, check if it hits a zombie | |
void hitZombies(struct Bullet *bullet) { | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { // For each zombie.. | |
// If the bullet and the zombie collide.. | |
if (zombies[i].alive && collision(bullet->x, zombies[i].x, bullet->y, zombies[i].y, bullet->w, zombies[i].w, bullet->h, zombies[i].h)) { | |
zombies[i].vX = bullet->dir * 10; // Apply horizontal impulse | |
zombies[i].state = "state_hit"; // Zombie is now hit | |
bullet->alive = false; // Destroy bullet | |
} | |
} | |
} | |
// Check if the survivor has stabbed a zombie | |
void stabZombies() { | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { | |
// If zombies is alive and it collides with the survivor.. | |
if (zombies[i].alive && collision(survivor.x, zombies[i].x, survivor.y, zombies[i].y, survivor.w, zombies[i].w, survivor.h, zombies[i].h)) { | |
zombies[i].vY = -15; // Apply a vertical impulse | |
zombies[i].vX = survivor.scaleX * 5; // Apply a horizontal impulse (wrt the survivor facing) | |
zombies[i].state = "state_hit"; // Zombie is now hit | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment