diff --git a/games/breakout/ball.c b/games/breakout/ball.c index 6846493..692d196 100644 --- a/games/breakout/ball.c +++ b/games/breakout/ball.c @@ -59,12 +59,7 @@ void bounce_rand_vector (ball_t *in_b, uint8_t in_bouncetype) void ball_think (ball_t *b) { - int8_t proj_x, proj_y, bounce; - - /* - if (!b->strength) - return; - */ + int8_t proj_x, proj_y, bounce, tmp; /* projection of the new coordinates */ proj_x = (b->x + (b->dir_x)) / 256; @@ -76,22 +71,30 @@ void ball_think (ball_t *b) bounce = check_bounce (proj_x, b->y / 256); - if (bounce & BOUNCE_UNDEF) - bounce = (BOUNCE_X | bounce) & (BOUNCE_X | BOUNCE_Y); + /* bouncing on bricks needs special handling */ + if (bounce & (BOUNCE_BRICK)) + bounce |= BOUNCE_X; - bounce |= check_bounce (b->x / 256, proj_y); - bounce |= check_bounce (proj_x, proj_y); + tmp = check_bounce (b->x / 256, proj_y); + if (tmp & (BOUNCE_BRICK)) + bounce |= BOUNCE_Y; + bounce |= tmp; + + tmp = check_bounce (proj_x, proj_y); + if (tmp & (BOUNCE_BRICK)) + bounce |= BOUNCE_X | BOUNCE_Y; + bounce |= tmp; bounce_rand_vector (b, bounce); /* bounce in x direction */ - if (bounce & (BOUNCE_X | BOUNCE_BRICK)) + if (bounce & BOUNCE_X) { b->dir_x *= -1; /* invert x vector */ } /* bounce in y direction */ - if (bounce & (BOUNCE_Y | BOUNCE_BRICK)) + if (bounce & BOUNCE_Y) { b->dir_y *= -1; /* invert y vector */ } diff --git a/games/breakout/breakout.c b/games/breakout/breakout.c index aa9405c..13f18dc 100644 --- a/games/breakout/breakout.c +++ b/games/breakout/breakout.c @@ -61,6 +61,7 @@ void borg_breakout() ball_spawn_default (&(balls[0])); balls[0].strength++; level_init(level); + rebound_init(); } } } diff --git a/games/breakout/config.h b/games/breakout/config.h index a14076e..d375a6f 100644 --- a/games/breakout/config.h +++ b/games/breakout/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG_H #define CONFIG_H /* amount of speed to slow down on bounce */ -#define BOUNCE_SLOWDOWN 8 +#define BOUNCE_SLOWDOWN 4 /* minimum speed of the ball */ #define BALL_MINSPEED 64 @@ -17,4 +17,18 @@ /* "color" of the rebound */ #define REBOUND_COLOR 2 +/* rebound reflection: values to add to the vector at rebound field n + * the size of this array must be REBOUND_SIZE +2 + */ +static const int8_t rebound_reflection[6][2] = +{ + {-72, -20}, /* offside left */ + {-40, -12}, /* left */ + {-16, -8}, /* center left */ + { 16, -8}, /* center right */ + { 40, -12}, /* right */ + { 72, -20} /* offside right */ +}; + #endif /* CONFIG_H */ + diff --git a/games/breakout/rebound.c b/games/breakout/rebound.c index 654090b..213e760 100644 --- a/games/breakout/rebound.c +++ b/games/breakout/rebound.c @@ -17,25 +17,13 @@ */ #include "rebound.h" -/* rebound reflection: values to add to the vector at rebound field n - */ -const int8_t rebound_reflection[6][2] = -{ - {-54, -20}, /* offside */ - {-32, -12}, /* left */ - {-16, -8}, /* center */ - { 16, -8}, - { 32, -12}, - { 54, -20} -}; - static uint8_t rbpos; void rebound_reflect (ball_t *b, int8_t in_x) { uint8_t tmpidx; - tmpidx = (in_x - rbpos) +1; + tmpidx = ((in_x - rbpos) +1) % (REBOUND_SIZE +2); b->dir_x += rebound_reflection[tmpidx][0]; b->dir_y += rebound_reflection[tmpidx][1]; @@ -56,10 +44,14 @@ void rebound_draw () { uint8_t i; - for (i=rbpos;i= rbpos && i < rbpos + REBOUND_SIZE) + playfield_set (i, NUM_ROWS-1, rb); /* set rebound pixel */ + else + playfield_set (i, NUM_ROWS-1, sp); /* space */ } + printf("rpos: %i\n", rbpos); } void rebound_tick() @@ -67,16 +59,13 @@ void rebound_tick() /* directions are inverted (JOYISLEFT means RIGHT) */ if (JOYISRIGHT && rbpos) { - playfield_set (rbpos + REBOUND_SIZE, NUM_ROWS-1, sp); /* clear rebound pixel */ rbpos--; - playfield_set (rbpos, NUM_ROWS-1, rb); /* set rebound pixel */ + rebound_draw(); } - if (JOYISLEFT && rbpos < (NUM_COLS - (REBOUND_SIZE+1))) + if (JOYISLEFT && rbpos < (NUM_COLS - (REBOUND_SIZE))) { - playfield_set (rbpos, NUM_ROWS-1, sp); /* clear rebound pixel */ rbpos++; - playfield_set (rbpos + REBOUND_SIZE, NUM_ROWS-1, rb); /* set rebound pixel */ + rebound_draw(); } - rebound_draw(); }