Created
November 18, 2017 14:15
-
-
Save Mikepicker/da917414ee9d555d322fc633d225bfc7 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
bool platformCollision(float x, float y, int w, int h) { | |
return y + h > platform.y && | |
x + 48 >= platform.x && | |
x + w - 48 <= platform.x + platform.w; | |
} | |
void update() { | |
// Zombies | |
for (int i = 0; i < ZOMBIE_COUNT; i++) { | |
if (zombies[i].alive) { | |
// Apply gravity | |
zombies[i].vY += world.gravity; | |
zombies[i].y += zombies[i].vY; | |
// Motion | |
zombies[i].x += zombies[i].vX; | |
zombies[i].vX = zombieSpeed * zombies[i].dir; | |
// Platform | |
if (platformCollision(zombies[i].x, zombies[i].y, zombieWidth, zombieHeight)) { | |
zombies[i].y = platform.y - zombieHeight; | |
zombies[i].vY = 0; | |
} else { | |
zombies[i].vX = 0; | |
} | |
// Death | |
if (zombies[i].y > SCREEN_HEIGHT) { | |
zombies[i].alive = false; | |
} | |
} | |
} | |
// Spawn zombie every N seconds | |
currentTime = SDL_GetTicks(); | |
if (currentTime > lastSpawnTime + SPAWN_FREQ * 1000) { | |
spawnZombie(); | |
lastSpawnTime = currentTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment