// Game initialization function // ========================================================== void init(void) { // init random seed unsigned int seed = time(NULL); srand(seed); // Init asteroid object array asteroids = (object *)malloc(sizeof(object)*MAXASTEROIDS); int i; for(i=0;i<MAXASTEROIDS;i++) { // Create an asteroid at a random location on screen vector p = makeVec(10.0-(float)(rand()%25), 10.0-(float)(rand()%25)); initAsteroid(asteroids+i, &p, 3.0); // If asteroid spawns too close to ship, make it somewhere else while(lengthVec(&(asteroids[i].pos)) < 5.0) { p = makeVec(10.0-(float)(rand()%25), 10.0-(float)(rand()%25)); initAsteroid(asteroids+i, &p, 3.0); } // Activate 4 big asteroids at the beginning if (i%4!=0) asteroids[i].state &= !ASTEROID_ACTIVE; else asteroids[i].state |= ASTEROID_BIG; } // Init asteroid gibs (inactive) asteroidGibs = (object**)malloc(sizeof(object*)*MAXASTEROIDS); int ab; for (ab=0; ab<MAXASTEROIDS; ab++) { asteroidGibs[ab] = (object*)malloc(sizeof(object)*asteroids[ab].size); initGib(asteroidGibs[ab], asteroids+ab, 0); } // init ship ships = malloc(sizeof(object)*MAXSHIPS); for (i = 0; i < MAXSHIPS; i++) { initShip(ships+i); } myship = ships; myship->state = 0; // init ship stuff //initMissile(); // init ship gibs (inactive) shipGibs = (object*)malloc(sizeof(object)*ships[0].size*MAXSHIPS); for (i=0; i < MAXSHIPS; i++) { initGib(shipGibs+i*ships[0].size, ships, 0); } // init stars stars = (object*)malloc(sizeof(object)*MAXSTARS); int si; for(si=0;si<MAXSTARS; si++) { vector v = makeVec((float)(rand()%50)-25.0, (float)(rand()%50)-25.0); initStar(stars+si, &v); } }
void destroy_asteroid(celestial_object aste_destroy, list *asteroid_list, SDL_Surface **surfaces) { celestial_object aste; int i, num; if (aste_destroy.sprite_size == BIG) { score += 10; } else if (aste_destroy.sprite_size == MEDIUM) { score += 20; } else { score += 30; } if (aste_destroy.sprite_size != SMALL) { for (i = 0; i < 3; i++) { if (aste_destroy.sprite_size == BIG) initAsteroid(&aste, MEDIUM, surfaces); if (aste_destroy.sprite_size == MEDIUM) initAsteroid(&aste, SMALL, surfaces); num = rand() % 3; if (num == 0) { aste.position.x = aste_destroy.position.x + 10; } else if (num == 1) { aste.position.x = aste_destroy.position.x - 10; } else { aste.position.x = aste_destroy.position.x; } num = rand() % 3; if (num == 0) { aste.position.y = aste_destroy.position.y + 10; } else if (num == 1) { aste.position.y = aste_destroy.position.y - 10; } else { aste.position.y = aste_destroy.position.y; } *asteroid_list = push(aste, *asteroid_list); } } }
// Destroys an asteroid void killAsteroid(object *o) { // If a big asteroid, create 2 medium asteroids if (o->state & ASTEROID_BIG) { initAsteroid(o, &(o->pos), 2.0); initAsteroid(o+2, &(o->pos), 2.0); o->state &= !ASTEROID_BIG; o->state |= ASTEROID_MEDIUM | ASTEROID_ACTIVE; (o+2)->state &= !ASTEROID_BIG; (o+2)->state |= ASTEROID_MEDIUM | ASTEROID_ACTIVE; // If a medium asteroid, create 2 small asteroids } else if (o->state & ASTEROID_MEDIUM) { initAsteroid(o, &(o->pos), 1.0); initAsteroid(o+1, &(o->pos), 1.0); o->state &= !ASTEROID_MEDIUM; o->state |= ASTEROID_SMALL | ASTEROID_ACTIVE; (o+1)->state &= !ASTEROID_MEDIUM; (o+1)->state |= ASTEROID_SMALL | ASTEROID_ACTIVE; // If a small asteroid, deactivate it } else if (o->state & ASTEROID_SMALL) { o->state &= !ASTEROID_ACTIVE; } }