From: Joshua Grams Date: Thu, 28 Apr 2005 21:49:56 +0000 (+0000) Subject: rock generation mostly works now. X-Git-Tag: 0.3~16 X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=commitdiff_plain;h=21570ddf054d9850eb924bcdc53be9e19581232a rock generation mostly works now. --- diff --git a/globals.h b/globals.h index 7eeefb3..ae1bb1c 100644 --- a/globals.h +++ b/globals.h @@ -60,6 +60,7 @@ extern char *initerror; extern struct shape shipshape; extern float shipx,shipy; // X position, 0..XSIZE extern float shipdx,shipdy; // Change in X position per tick. +extern float screendx, screendy; extern float gamerate; // this controls the speed of everything that moves. extern float yscroll; extern float scrollvel; diff --git a/main.c b/main.c index 1680129..bd0c158 100644 --- a/main.c +++ b/main.c @@ -70,9 +70,9 @@ char *initerror = ""; struct shape shipshape; float shipx,shipy = 240.0; // X position, 0..XSIZE float shipdx,shipdy; // Change in X position per tick. +float screendx = 7.5, screendy = 0.0; +float xscroll, yscroll; float gamerate; // this controls the speed of everything that moves. -float yscroll; -float scrollvel; int nships,score,initticks,ticks_since_last, last_ticks; int gameover; @@ -227,7 +227,7 @@ draw_bang_dots(SDL_Surface *s) { rawpixel[(int)(s->pitch/2*(int)(bdot[i].y)) + (int)(bdot[i].x)] = bdot[i].c ? bdot[i].c : heatcolor[(int)(bdot[i].life*3)]; bdot[i].life -= bdot[i].decay; bdot[i].x += bdot[i].dx*gamerate; - bdot[i].y += bdot[i].dy*gamerate + yscroll; + bdot[i].y += bdot[i].dy*gamerate-yscroll; if(bdot[i].life<0) bdot[i].active = 0; @@ -258,7 +258,7 @@ draw_space_dots(SDL_Surface *s) { } rawpixel[(int)(s->pitch/2*(int)sdot[i].y) + (int)(sdot[i].x)] = sdot[i].color; sdot[i].x += sdot[i].dx*gamerate; - sdot[i].y += yscroll; + sdot[i].y -= yscroll; if(sdot[i].y > YSIZE) { sdot[i].y -= YSIZE; } else if(sdot[i].y < 0) { @@ -279,7 +279,7 @@ draw_engine_dots(SDL_Surface *s) { for(i = 0; iYSIZE) { edot[i].active = 0; } else if(edot[i].x<0 || edot[i].x>XSIZE) { @@ -666,6 +666,7 @@ draw() { int gameloop() { Uint8 *keystate; + float tmp; for(;;) { @@ -735,18 +736,18 @@ gameloop() { shipdy *= pow((double)0.9,(double)gamerate); } + // SCROLLING + tmp = shipy - (YSIZE / 2); + tmp += 25 * shipdy; + tmp /= -25; + tmp = ((screendy * (gamerate - 12)) + (tmp * gamerate)) / 12; + screendy = -tmp; + xscroll = screendx * gamerate; + yscroll = screendy * gamerate; + // INERTIA shipx += shipdx*gamerate; - shipy += shipdy*gamerate; - - // SCROLLING - yscroll = shipy - (YSIZE / 2); - yscroll += shipdy * 25; - yscroll /= -25; - yscroll = ((scrollvel * (12 - gamerate)) + (yscroll * gamerate)) / 12; - scrollvel = yscroll; - yscroll = yscroll*gamerate; - shipy += yscroll; + shipy += shipdy*gamerate - yscroll; move_rocks(); diff --git a/rocks.c b/rocks.c index 1e69ce2..7de0c2f 100644 --- a/rocks.c +++ b/rocks.c @@ -19,23 +19,22 @@ struct rock_struct { struct rock_struct rock[MAXROCKS], *rockptr = rock; -float rockrate; - SDL_Surface *surf_rock[NROCKS]; struct shape rock_shapes[NROCKS]; -float rockhtimer = 0; -int nrocks = 41; +// timers for rock generation. +float rtimers[4]; +int nrocks; + +// constants for rock generation. +#define KH 32.0 // 32 s for a speed=1 rock to cross the screen horizontally. +#define KV 24.0 // 24 s for a speed=1 rock to cross the screen vertically. +#define RDX 2.5 // range for rock dx values (+/-) +#define RDY 2.5 // range for rock dy values (+/-) float rnd(void); -// used for rock generation. -#define KH (1.0/32.0) -#define KV (1.0/24.0) -#define RXMIN 5.0 -#define RXMAX 10.0 -#define RYMIN (-0.5) -#define RYMAX 0.5 +#define crnd() (2*(rnd()-0.5)) int @@ -51,6 +50,7 @@ init_rocks(void) NULLERROR(surf_rock[i] = SDL_DisplayFormat(temp)); get_shape(surf_rock[i], &rock_shapes[i]); } + nrocks = 41; return 0; } @@ -59,33 +59,97 @@ reset_rocks(void) { int i; - for(i = 0; i= 1) { - while(rockptr->active && i= MAXROCKS) rockptr = rock; - i++; - } - if(!rockptr->active) { - rockhtimer -= 1; - rockptr->dx = -5.0*(1.0 + rnd()); - rockptr->dy = rnd()-0.5; - rockptr->type_number = random() % NROCKS; - rockptr->image = surf_rock[rockptr->type_number]; - rockptr->shape = &rock_shapes[rockptr->type_number]; - rockptr->x = (float)XSIZE; - rockptr->y = rnd()*(YSIZE + rockptr->image->h); - rockptr->active = 1; + int i,j; + float ti[4]; + float x, y; + + rock_timer_increments(ti); + + for(i=0; i<4; i++) { + rtimers[i] += ti[i]*gamerate/20; + if(rtimers[i] >= 1) { + j=0; + while(rockptr->active && j= MAXROCKS) rockptr = rock; + j++; + } + if(!rockptr->active) { + rtimers[i] -= 1; + rockptr->type_number = random() % NROCKS; + rockptr->image = surf_rock[rockptr->type_number]; + rockptr->shape = &rock_shapes[rockptr->type_number]; + switch(i) { + case RIGHT: + rockptr->x = XSIZE; + rockptr->y = rnd()*(YSIZE + rockptr->image->h); + break; + case LEFT: + rockptr->x = -rockptr->image->w; + rockptr->y = rnd()*(YSIZE + rockptr->image->h); + break; + case BOTTOM: + rockptr->x = rnd()*(XSIZE + rockptr->image->w); + rockptr->y = YSIZE; + break; + case TOP: + rockptr->x = rnd()*(XSIZE + rockptr->image->w); + rockptr->y = -rockptr->image->h; + break; + } + + j=0; + do { + rockptr->dx = RDX*crnd(); + rockptr->dy = RDY*crnd(); + x = (rockptr->dx-screendx)*gamerate; + y = (rockptr->dy-screendy)*gamerate; + j++; + } while(x < -rockptr->image->w || x >= XSIZE + || y < -rockptr->image->h || y >= YSIZE); + if(j > 1) printf("had to try %d times.\n", j); + + rockptr->active = 1; + } } } } @@ -99,15 +163,13 @@ move_rocks(void) for(i = 0; i < MAXROCKS; i++) { if(rock[i].active) { // move - rock[i].x += rock[i].dx*gamerate; - rock[i].y += rock[i].dy*gamerate + yscroll; + rock[i].x += (rock[i].dx-screendx)*gamerate; + rock[i].y += (rock[i].dy-screendy)*gamerate; // clip - if(rock[i].y < -rock[i].image->h || rock[i].y > YSIZE) { - // rock[i].active = 0; - rock[i].y = (YSIZE - rock[i].image->h) - rock[i].y; - rock[i].y += (rock[i].dy*gamerate + yscroll) * 1.01; + if(rock[i].x < -rock[i].image->w || rock[i].x >= XSIZE + || rock[i].y < -rock[i].image->h || rock[i].y >= YSIZE) { + rock[i].active = 0; } - if(rock[i].x < -rock[i].image->w || rock[i].x > XSIZE) rock[i].active = 0; } } } @@ -162,7 +224,7 @@ blast_rocks(float x, float y, float radius, int onlyslow) // This makes it so your explosion from dying magically doesn't leave // any rocks that aren't moving much on the x axis. If onlyslow is set, // only rocks that are barely moving will be pushed. - if(onlyslow && (rock[i].dx < -4 || rock[i].dx > 3)) continue; + if(onlyslow && (rock[i].dx-screendx < -4 || rock[i].dx-screendx > 3)) continue; dx = rock[i].x - x; dy = rock[i].y - y; @@ -170,8 +232,8 @@ blast_rocks(float x, float y, float radius, int onlyslow) n = sqrt(dx*dx + dy*dy); if(n < radius) { n *= 20; - rock[i].dx += rockrate*(dx+30)/n; - rock[i].dy += rockrate*dy/n; + rock[i].dx += 54.0*(dx+30)/n; + rock[i].dy += 54.0*dy/n; } } }