From 254d0d2f3d720e32b36a50aae14ae4829687b1a7 Mon Sep 17 00:00:00 2001 From: soeren Date: Thu, 21 Jan 2010 18:32:17 +0000 Subject: [PATCH] :w --- games/breakout/ball.c | 53 ++++++++++++++++++++++++++-------------- games/breakout/config.h | 24 +++++++++++++++--- games/breakout/rebound.c | 13 +++++++++- games/breakout/rebound.h | 1 + 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/games/breakout/ball.c b/games/breakout/ball.c index 5cdddf0..1a79cd0 100644 --- a/games/breakout/ball.c +++ b/games/breakout/ball.c @@ -37,9 +37,10 @@ void bounce_rand_vector (ball_t *in_b, uint8_t in_bouncetype) case BOUNCE_REBOUND: /* the rebound is rather percise */ in_b->dir_x ^= (rval & 0x03); in_b->dir_y ^= (rval & 0x03); + if (JOYISRIGHT || JOYISLEFT) { - /* a moving rebond accelerates the ball by 1/8th */ + /* a moving rebond accelerates the ball 12,5% */ in_b->dir_y += (in_b->dir_y / 8); in_b->dir_x += (in_b->dir_x / 8); } @@ -78,12 +79,7 @@ void ball_think (ball_t *b) bounce = (BOUNCE_X | bounce) & (BOUNCE_X | BOUNCE_Y); bounce |= check_bounce (b->x / 256, proj_y); - if (bounce & BOUNCE_UNDEF) - bounce = (BOUNCE_Y | bounce) & (BOUNCE_X | BOUNCE_Y); - bounce |= check_bounce (proj_x, proj_y); - if (bounce & BOUNCE_UNDEF) - bounce = BOUNCE_X | BOUNCE_Y; bounce_rand_vector (b, bounce); @@ -91,34 +87,53 @@ void ball_think (ball_t *b) if (bounce & (BOUNCE_X | BOUNCE_BRICK)) { b->dir_x *= -1; /* invert x vector */ - -#if BOUNCE_SLOWDOWN - if (b->dir_x < 0) - { - b->dir_x += BOUNCE_SLOWDOWN; - } else - { - b->dir_x -= BOUNCE_SLOWDOWN; - } -#endif } /* bounce in y direction */ if (bounce & (BOUNCE_Y | BOUNCE_BRICK)) { b->dir_y *= -1; /* invert y vector */ - + } + #if BOUNCE_SLOWDOWN - if (b->dir_y < 0) + if (bounce & BOUNCE_BRICK) + { + if (b->dir_y < -BALL_MINSPEED) { b->dir_y += BOUNCE_SLOWDOWN; - } else + } else if (b->dir_y > BALL_MINSPEED) { b->dir_y -= BOUNCE_SLOWDOWN; } + + if (b->dir_x < -BALL_MINSPEED) + { + b->dir_x += BOUNCE_SLOWDOWN; + } else if (b->dir_y > BALL_MINSPEED) + { + b->dir_x -= BOUNCE_SLOWDOWN; + } + } #endif + + if (bounce & BOUNCE_REBOUND) + { + rebound_reflect(b, proj_x); } + if (b->dir_x > BALL_MAXSPEED) + b->dir_x = BALL_MAXSPEED; + + if (b->dir_x < - BALL_MAXSPEED) + b->dir_x = - BALL_MAXSPEED; + + if (b->dir_y > BALL_MAXSPEED) + b->dir_y = BALL_MAXSPEED; + + if (b->dir_y < - BALL_MAXSPEED) + b->dir_y = - BALL_MAXSPEED; + + b->y += b->dir_y; b->x += b->dir_x; diff --git a/games/breakout/config.h b/games/breakout/config.h index 85c54b0..f30b7cf 100644 --- a/games/breakout/config.h +++ b/games/breakout/config.h @@ -1,14 +1,32 @@ +#ifndef CONFIG_H +#define CONFIG_H /* amount of speed to slow down on bounce */ -#define BOUNCE_SLOWDOWN 0 +#define BOUNCE_SLOWDOWN 8 /* minimum speed of the ball */ -#define BALL_MINSPEED 0x0010 +#define BALL_MINSPEED 64 +#define BALL_MAXSPEED 224 /* initial amount of lifes */ #define START_LIFES 3 /* rebound size */ -#define REBOUND_SIZE 5 +#define REBOUND_SIZE 4 + +/* rebound reflection: values to add to the vector at rebound field n + * note: directions are inverted + */ +static int8_t rebound_reflection[6][2] = +{ + {-54,-20}, /* offside */ + {-32,-12}, + {-16, -8}, /* side ... middle */ + {16, -8}, + {32, -12}, + {54, -20} +}; /* "color" of the rebound */ #define REBOUND_COLOR 2 + +#endif /* CONFIG_H */ diff --git a/games/breakout/rebound.c b/games/breakout/rebound.c index 676b03c..365aac7 100644 --- a/games/breakout/rebound.c +++ b/games/breakout/rebound.c @@ -17,9 +17,20 @@ */ #include "rebound.h" -#include static uint8_t rbpos; +void rebound_reflect (ball_t *b, int8_t in_x) +{ + uint8_t tmpidx; + + tmpidx = (in_x - rbpos) +1; + + printf("bounce idx %i\n", tmpidx); + + b->dir_x += rebound_reflection[tmpidx][0]; + b->dir_y += rebound_reflection[tmpidx][1]; +} + uint8_t rebound_getpos () { return (rbpos + (REBOUND_SIZE / 2)); diff --git a/games/breakout/rebound.h b/games/breakout/rebound.h index e3b83b7..627085a 100644 --- a/games/breakout/rebound.h +++ b/games/breakout/rebound.h @@ -21,4 +21,5 @@ void rebound_init(); void rebound_tick(); uint8_t rebound_getpos (); +void rebound_reflect (ball_t *b, int8_t in_x); #endif