# Changeset 15 for libcacaTweet

Ignore:
Timestamp:
Dec 13, 2002, 8:27:16 PM (19 years ago)
Message:
• moved nuke from explosions to weapons.
• used a cool Bresenham algorithm to draw the nuke circles.
• nuke collides with aliens.
Location:
libcaca/trunk
Files:
6 edited

Unmodified
Removed
• ## libcaca/trunk/collide.c

 r11 int i; for( i = 0; i < SHOTS; i++ ) for( i = 0; i < WEAPONS; i++ ) { if( wp->y[i] >= 0 ) int i, j; for( i = 0; i < SHOTS; i++ ) for( i = 0; i < WEAPONS; i++ ) { if( wp->y[i] >= 0 ) { int ok = 0; int r; for( j = 0; j < ALIENS; j++ ) switch( wp->type[i] ) { if( wp->x[i] >= al->x[j] && wp->x[i] <= al->x[j] + 4 && wp->y[i] >= al->y[j] && wp->y[i] <= al->y[j] + 2 ) case 2: /* Big nuke */ r = (34 - wp->n[i]) * (34 - wp->n[i]) / 10; for( j = 0; j < ALIENS; j++ ) { al->life[j]--; if( al->life[j] == 0 ) if( al->x[j] < 0 ) { continue; } if( (al->x[j] - wp->x[i]) * (al->x[j] - wp->x[i]) + 4 * (al->y[j] - wp->y[i]) * (al->y[j] - wp->y[i]) <= r * r ) { /* Kill alien, not nuke */ add_explosion( g, ex, al->x[j], al->y[j], 0, 0, 1 ); al->x[j] = -1; al->y[j] = -1; add_explosion( g, ex, wp->x[i], wp->y[i], 0, 0, 1 ); } ok = 1; } else if( wp->x[i] >= al->x[j] && wp->x[i] <= al->x[j] + 4 && wp->y[i]+1 >= al->y[j] && wp->y[i]+1 <= al->y[j] + 2 ) break; case 1: default: for( j = 0; j < ALIENS; j++ ) { al->life[j]--; if( al->life[j] == 0 ) if( wp->x[i] >= al->x[j] && wp->x[i] <= al->x[j] + 4 && wp->y[i] >= al->y[j] && wp->y[i] <= al->y[j] + 2 ) { al->x[j] = -1; al->y[j] = -1; add_explosion( g, ex, wp->x[i], wp->y[i]+1, 0, 0, 1 ); al->life[j]--; if( al->life[j] == 0 ) { al->x[j] = -1; al->y[j] = -1; add_explosion( g, ex, wp->x[i], wp->y[i], 0, 0, 1 ); } ok = 1; } ok = 1; else if( wp->x[i] >= al->x[j] && wp->x[i] <= al->x[j] + 4 && wp->y[i]+1 >= al->y[j] && wp->y[i]+1 <= al->y[j] + 2 ) { al->life[j]--; if( al->life[j] == 0 ) { al->x[j] = -1; al->y[j] = -1; add_explosion( g, ex, wp->x[i], wp->y[i]+1, 0, 0, 1 ); } ok = 1; } } } if( ok ) { wp->y[i] = -1; if( ok ) { wp->y[i] = -1; } break; } }
• ## libcaca/trunk/common.h

 r11 #define STARS 50 #define SHOTS 50 #define WEAPONS 50 #define ROCKS 10 #define ALIENS 10 #   define GFX_WRITE(x) addch(x) #endif #define GFX_WRITETO(x,y,c) do{ GFX_GOTO(x,y); GFX_WRITE(c); }while(0) #define GET_RAND(p,q) ((p)+(int)((1.0*((q)-(p)))*rand()/(RAND_MAX+1.0))) typedef struct { int x[SHOTS]; int y[SHOTS]; int v[SHOTS]; int x[WEAPONS]; int y[WEAPONS]; int v[WEAPONS]; int n[WEAPONS]; int type[WEAPONS]; } weapons; int x, y; int dir; int weapon; int weapon, nuke; } player; void draw_weapons( game *g, weapons *wp ); void update_weapons( game *g, weapons *wp ); void add_weapon( game *g, weapons *wp, int x, int y ); void add_weapon( game *g, weapons *wp, int x, int y, int type ); void init_starfield( game *g, starfield *s );
• ## libcaca/trunk/explosions.c

 r11 #include #include #include "common.h" static void draw_small_explosion( int x, int y, int frame ); static void draw_big_explosion( int x, int y, int frame ); static void draw_huge_explosion( int x, int y, int frame ); void init_explosions( game *g, explosions *ex ) switch( ex->type[i] ) { case 2: draw_huge_explosion( ex->x[i], ex->y[i], ex->n[i] ); break; case 1: draw_big_explosion( ex->x[i], ex->y[i], ex->n[i] ); } static void draw_circle( int x, int y, float r ); static void draw_huge_explosion( int x, int y, int frame ) { float r = 1.5 * (30 - frame); GFX_COLOR( BLUE ); draw_circle( x, y, r ); r += 0.7; GFX_COLOR( CYAN ); draw_circle( x, y, r ); r += 0.7; GFX_COLOR( WHITE ); draw_circle( x, y, r ); } static void draw_circle( int x, int y, float r ) { #if 1 float c; for( c = 0 ; c <= 90 ; c += 1 ) { float dx = 0.5 + r * 2.0 * sin( c * M_PI / 180.0 ); float dy = 0.5 + r * cos( c * M_PI / 180.0 ); GFX_GOTO( x + dx, y + dy ); GFX_WRITE( '#' ); GFX_GOTO( x - dx, y + dy ); GFX_WRITE( '#' ); GFX_GOTO( x + dx, y - dy ); GFX_WRITE( '#' ); GFX_GOTO( x - dx, y - dy ); GFX_WRITE( '#' ); } #endif #if 0 int dx,dy,a2,b2, S, T; float a = r*8, b = r*2; a2 = a*a; b2 = b*b; dx = 0; dy = b; S = a2*(1-2*b) + 2*b2; T = b2 - 2*a2*(2*b-1); GFX_GOTO( x + dx, y + dy ); GFX_WRITE( '#' ); GFX_GOTO( x - dx, y + dy ); GFX_WRITE( '#' ); GFX_GOTO( x + dx, y - dy ); GFX_WRITE( '#' ); GFX_GOTO( x - dx, y - dy ); GFX_WRITE( '#' ); do { if (S<0) { S += 2*b2*(2*x+3); T += 4*b2*(x+1); dx++; } else if (T<0) { S += 2*b2*(2*x+3) - 4*a2*(dy-1); T += 4*b2*(x+1) - 2*a2*(2*dy-3); dx++; dy--; } else { S -= 4*a2*(dy-1); T -= 2*a2*(2*dy-3); dy--; } GFX_GOTO( x + dx, y + dy ); GFX_WRITE( '#' ); GFX_GOTO( x - dx, y + dy ); GFX_WRITE( '#' ); GFX_GOTO( x + dx, y - dy ); GFX_WRITE( '#' ); GFX_GOTO( x - dx, y - dy ); GFX_WRITE( '#' ); } while (dy>0); #endif }
• ## libcaca/trunk/main.c

 r11 break; case 'j': //if( p->y < g->h - 2 ) p->y += 1; if( p->y < g->h - 2 ) p->y += 1; break; case 'k': //if( p->y > 1 ) p->y -= 1; if( p->y > 1 ) p->y -= 1; break; case 'l': break; case '\r': add_explosion( g, ex, p->x + 2, p->y, 0, 0, 2 ); if( p->nuke == 0 ) { p->nuke = 40; add_weapon( g, wp, p->x + 2, p->y, 2 ); } break; case ' ': { p->weapon = 4; add_weapon( g, wp, p->x, p->y ); add_weapon( g, wp, p->x + 5, p->y ); add_weapon( g, wp, p->x, p->y, 1 ); add_weapon( g, wp, p->x + 5, p->y, 1 ); } break;
• ## libcaca/trunk/player.c

 r11 p->dir = 0; p->weapon = 0; p->nuke = 0; return p; } if( p->nuke ) { p->nuke--; } if( p->dir < 0 ) {
• ## libcaca/trunk/weapons.c

 r11 #include "common.h" static void draw_nuke( int x, int y, int frame ); static void draw_circle( int x, int y, int r ); void init_weapons( game *g, weapons *wp ) { int i; for( i = 0; i < SHOTS; i++ ) for( i = 0; i < WEAPONS; i++ ) { wp->x[i] = -1; wp->y[i] = -1; wp->v[i] = 0; wp->n[i] = 0; wp->type[i] = 0; } } int i; for( i = 0; i < SHOTS; i++ ) for( i = 0; i < WEAPONS; i++ ) { if( wp->x[i] >= 0 ) { GFX_COLOR( WHITE ); GFX_GOTO( wp->x[i], wp->y[i] ); GFX_WRITE( '|' ); GFX_COLOR( CYAN ); GFX_GOTO( wp->x[i], wp->y[i] + 1 ); GFX_WRITE( '|' ); switch( wp->type[i] ) { case 2: draw_nuke( wp->x[i], wp->y[i], wp->n[i] ); break; case 1: default: GFX_COLOR( WHITE ); GFX_GOTO( wp->x[i], wp->y[i] ); GFX_WRITE( '|' ); GFX_COLOR( CYAN ); GFX_GOTO( wp->x[i], wp->y[i] + 1 ); GFX_WRITE( '|' ); break; } } } int i; for( i = 0; i < SHOTS; i++ ) for( i = 0; i < WEAPONS; i++ ) { if( wp->y[i] < 0 ) else { wp->y[i] += wp->v[i]; switch( wp->type[i] ) { case 2: wp->n[i]--; if( wp->n[i]-- < 0 ) { wp->y[i] = -1; } break; case 1: default: wp->y[i] += wp->v[i]; break; } /* Check collisions */ } void add_weapon( game *g, weapons *wp, int x, int y ) void add_weapon( game *g, weapons *wp, int x, int y, int type ) { int i; for( i = 0; i < SHOTS; i++ ) for( i = 0; i < WEAPONS; i++ ) { if( wp->y[i] < 0 ) wp->x[i] = x; wp->y[i] = y; wp->type[i] = type; wp->v[i] = -2; wp->n[i] = 30; break; } } static void draw_nuke( int x, int y, int frame ) { int r = (34 - frame) * (34 - frame) / 10; /* Lots of duplicate pixels, but we don't care */ GFX_COLOR( BLUE ); draw_circle( x, y, r++ ); GFX_COLOR( CYAN ); draw_circle( x, y, r++ ); GFX_COLOR( WHITE ); draw_circle( x, y, r++ ); draw_circle( x, y, r++ ); } static void draw_circle( int x, int y, int r ) { int test, dx, dy; /* Optimized Bresenham. Kick ass. */ for( test = 0, dx = 0, dy = r ; dx <= dy ; dx++ ) { GFX_WRITETO( x + dx, y + dy / 2, '#' ); GFX_WRITETO( x - dx, y + dy / 2, '#' ); GFX_WRITETO( x + dx, y - dy / 2, '#' ); GFX_WRITETO( x - dx, y - dy / 2, '#' ); GFX_WRITETO( x + dy, y + dx / 2, '#' ); GFX_WRITETO( x - dy, y + dx / 2, '#' ); GFX_WRITETO( x + dy, y - dx / 2, '#' ); GFX_WRITETO( x - dy, y - dx / 2, '#' ); test += test > 0 ? dx - dy-- : dx; } }
Note: See TracChangeset for help on using the changeset viewer.