JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
number of rocks on screen now starts at 25, gradually increasing
[vor.git] / main.c
diff --git a/main.c b/main.c
index bd0c158..61b2caa 100644 (file)
--- a/main.c
+++ b/main.c
@@ -68,13 +68,15 @@ char topline[1024];
 char *initerror = "";
 
 struct shape shipshape;
-float shipx,shipy = 240.0;     // X position, 0..XSIZE
-float shipdx,shipdy;   // Change in X position per tick.
+float shipx = XSIZE/2, shipy = YSIZE/2;        // X position, 0..XSIZE
+float shipdx = 8, shipdy = 0;  // 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.
 
-int nships,score,initticks,ticks_since_last, last_ticks;
+float bangx, bangy, bangdx, bangdy;
+
+int nships,score,ticks_since_last, last_ticks;
 int gameover;
 int maneuver = 0;
 int sound_flag, music_flag;
@@ -128,25 +130,18 @@ init_engine_dots() {
 
 void
 init_space_dots() {
-       int i,intensity;
+       int i,b;
        for(i = 0; i<MAXSPACEDOTS; i++) {
-               float r;
-
                sdot[i].x = rnd()*(XSIZE-5);
                sdot[i].y = rnd()*(YSIZE-5);
-
-               r = rnd()*rnd();
-
-               sdot[i].dx = -r*4;
-               // -1/((1-r) + .3);
-               intensity = (int)(r*180 + 70);
-               sdot[i].color = SDL_MapRGB(surf_screen->format,intensity,intensity,intensity);
-
+               sdot[i].z = MAXDUSTDEPTH*sqrt(rnd());
+               b = (MAXDUSTDEPTH - sdot[i].z) * 255.0 / MAXDUSTDEPTH;
+               sdot[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
        }
 }
 
 void
-makebangdots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
+make_bang_dots(int xbang, int ybang, int dx, int dy, SDL_Surface *s, int power) {
 
        // TODO - stop generating dots after a certain amount of time has passed, to cope with slower CPUs.
        // TODO - generate and display dots in a circular buffer
@@ -226,8 +221,8 @@ draw_bang_dots(SDL_Surface *s) {
                        //last_i = i + 1;
                        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].x += bdot[i].dx*gamerate - xscroll;
+                       bdot[i].y += bdot[i].dy*gamerate - yscroll;
 
                        if(bdot[i].life<0)
                        bdot[i].active = 0;
@@ -257,16 +252,12 @@ draw_space_dots(SDL_Surface *s) {
                        sdot[i].y = 0;
                }
                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;
-               if(sdot[i].y > YSIZE) {
-                       sdot[i].y -= YSIZE;
-               } else if(sdot[i].y < 0) {
-                       sdot[i].y += YSIZE;
-               }
-               if(sdot[i].x<0) {
-                       sdot[i].x = XSIZE;
-               }
+               sdot[i].x -= xscroll / (1.3 + sdot[i].z);
+               sdot[i].y -= yscroll / (1.3 + sdot[i].z);
+               if(sdot[i].y >= XSIZE) sdot[i].x -= XSIZE;
+               else if(sdot[i].x < 0) sdot[i].x = XSIZE-1;
+               if(sdot[i].y > YSIZE) sdot[i].y -= YSIZE;
+               else if(sdot[i].y < 0) sdot[i].y += YSIZE-1;
        }
 }
 
@@ -278,7 +269,7 @@ draw_engine_dots(SDL_Surface *s) {
 
        for(i = 0; i<MAXENGINEDOTS; i++) {
                if(edot[i].active) {
-                       edot[i].x += edot[i].dx*gamerate;
+                       edot[i].x += edot[i].dx*gamerate - xscroll;
                        edot[i].y += edot[i].dy*gamerate - yscroll;
                        if((edot[i].life -= gamerate*3)<0 || edot[i].y<0 || edot[i].y>YSIZE) {
                                edot[i].active = 0;
@@ -679,7 +670,6 @@ gameloop() {
                                                // Create a new ship and start all over again
                                                state = GAMEPLAY;
                                                play_tune(1);
-                                               shipx -= 50;
                                                break;
                                        case GAME_OVER:
                                                state = HIGH_SCORE_ENTRY;
@@ -722,9 +712,9 @@ gameloop() {
                                                blast_radius = BLAST_RADIUS * (DEAD_PAUSE_LENGTH - state_timeout) / 20.0;
                                                fixonly = 0;
                                        }
-                                       blast_rocks(shipx, shipy, blast_radius, fixonly);
+                                       blast_rocks(bangx, bangy, blast_radius, fixonly);
 
-                                       if(shipx < 60) shipx = 60;
+                                       if(bangx < 60) bangx = 60;
                                }
                        }
 
@@ -736,19 +726,31 @@ gameloop() {
                                shipdy *= pow((double)0.9,(double)gamerate);
                        }
 
+                       // INERTIA
+                       shipx += shipdx*gamerate;
+                       shipy += shipdy*gamerate;
+
                        // SCROLLING
                        tmp = shipy - (YSIZE / 2);
-                       tmp += 25 * shipdy;
+                       tmp += shipdy * 25;
                        tmp /= -25;
                        tmp = ((screendy * (gamerate - 12)) + (tmp * gamerate)) / 12;
                        screendy = -tmp;
+                       tmp = shipx - (XSIZE / 3);
+                       tmp += shipdx * 25;
+                       tmp /= -25;
+                       tmp = ((screendx * (gamerate - 12)) + (tmp * gamerate)) / 12;
+                       screendx = -tmp;
+
                        xscroll = screendx * gamerate;
                        yscroll = screendy * gamerate;
+                       shipx -= xscroll;
+                       shipy -= yscroll;
+
+                       // move bang center
+                       bangx += bangdx*gamerate - xscroll;
+                       bangy += bangdy*gamerate - yscroll;
 
-                       // INERTIA
-                       shipx += shipdx*gamerate;
-                       shipy += shipdy*gamerate - yscroll;
-                       
                        move_rocks();
 
 
@@ -756,7 +758,7 @@ gameloop() {
                        if(shipx<0 || shipx>XSIZE-surf_ship->w) {
                                // BOUNCE from left and right wall
                                shipx -= shipdx*gamerate;
-                               shipdx *= -0.99;
+                               shipdx *= -99;
                        }
 
                        // BOUNCE Y
@@ -768,14 +770,15 @@ gameloop() {
 
 
                        if(draw() && state == GAMEPLAY) {
-                               // Play the explosion sound
-                               play_sound(0);
-                               makebangdots(shipx,shipy,shipdx,shipdy,surf_ship,30);
-                               shipdx = 0;
-                               shipdy = 0;
+                               // Died
+                               play_sound(0); // Play the explosion sound
+                               bangx = shipx; bangy = shipy; bangdx = shipdx; bangdy = shipdy;
+                               make_bang_dots(shipx,shipy,shipdx,shipdy,surf_ship,30);
+                               shipdx *= 0.5; shipdy *= 0.5;
                                if(--nships <= 0) {
-                                       gameover = 1;
                                        state = GAME_OVER;
+                                       gameover = 1;
+                                       shipdx = 8; shipdy = 0;
                                        state_timeout = 200.0;
                                        fadetimer = 0.0;
                                        faderate = gamerate;
@@ -800,10 +803,8 @@ gameloop() {
                                play_tune(1);
 
                                gameover = 0;
-                               shipx = 0;
-                               shipy = YSIZE/2;
-                               shipdx = -1;
-                               shipdy = 0;
+                               shipx = XSIZE/2.2; shipy = YSIZE/2;
+                               shipdx = screendx; shipdy = screendy;
                        }
 
                        maneuver = 0;
@@ -896,7 +897,6 @@ main(int argc, char **argv) {
        }
 
        reset_rocks();
-       initticks = SDL_GetTicks();
        gameloop();
 
        return 0;