From f1cd2ff38dd9f3c5c4925d8a5f9588129b3bf343 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sat, 25 Mar 2006 20:22:53 +0000 Subject: [PATCH] engine dots push rocks pixel collision functions return what sprite was hit new config var: ENGINE_DOT_WEIGHT --- config.h | 3 +++ main.c | 11 ++++++++++- sprite.c | 21 +++++++++++---------- sprite.h | 3 ++- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/config.h b/config.h index 23d697e..f857291 100644 --- a/config.h +++ b/config.h @@ -34,6 +34,9 @@ #define W 100 #define M 255 +// determines how hard they push the rocks. Set to 0 to disable pushing rocks +#define ENGINE_DOT_WEIGHT 0.1 + // radius^2 (pixels) which will be cleared of rocks when you die #define BLAST_RADIUS 300 diff --git a/main.c b/main.c index 43d7bda..b8b03c3 100644 --- a/main.c +++ b/main.c @@ -256,6 +256,7 @@ draw_engine_dots(SDL_Surface *s) { uint16_t *pixels = (uint16_t *) s->pixels; int row_inc = s->pitch/sizeof(uint16_t); int heatindex; + Sprite *hit; for(i = 0; itype != SHIP) { // they shouldn't hit the ship, but they do + edot[i].active = 0; + hit->dx += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dx / sprite_mass(hit); + hit->dy += ENGINE_DOT_WEIGHT * edot[i].life * edot[i].dy / sprite_mass(hit); + continue; + } + } heatindex = edot[i].life * 6; c = heatindex>3*W ? heatcolor[3*W-1] : heatcolor[heatindex]; pixels[row_inc*(int)(edot[i].y) + (int)(edot[i].x)] = c; diff --git a/sprite.c b/sprite.c index 809547b..abe9fb3 100644 --- a/sprite.c +++ b/sprite.c @@ -302,36 +302,37 @@ pixel_collide(Sprite *s, int x, int y) return s->mask[(y*s->mask_w) + (x>>5)] & pmask; } -int +Sprite * pixel_hit_in_square(Sprite *r, float x, float y) { for(; r; r=r->next) { - if(COLLIDES(r) && pixel_collide(r, x, y)) return 1; + if(COLLIDES(r) && pixel_collide(r, x, y)) return r; } return 0; } -int +Sprite * pixel_collides(float x, float y) { int l, t; Sprite **sq; + Sprite *ret; l = (x + grid_size) / grid_size; t = (y + grid_size) / grid_size; sq = &sprites[set][l + t*gw]; - if(pixel_hit_in_square(*sq, x, y)) return true; - if(l > 0 && pixel_hit_in_square(*(sq-1), x, y)) return true; - if(t > 0 && pixel_hit_in_square(*(sq-gw), x, y)) return true; - if(l > 0 && t > 0 && pixel_hit_in_square(*(sq-1-gw), x, y)) return true; - return false; + if((ret = pixel_hit_in_square(*sq, x, y))) return ret; + if(l > 0 && (ret = pixel_hit_in_square(*(sq-1), x, y))) return ret; + if(t > 0 && (ret = pixel_hit_in_square(*(sq-gw), x, y))) return ret; + if(l > 0 && t > 0 && (ret = pixel_hit_in_square(*(sq-1-gw), x, y))) return ret; + return 0; } -static float +float sprite_mass(Sprite *s) { if(s->type == SHIP) return s->area; - else if(s->type == ROCK) return 3*s->area; + else if(s->type == ROCK) return 3 * s->area; else return 0; } diff --git a/sprite.h b/sprite.h index 75d7730..3cb2c0e 100644 --- a/sprite.h +++ b/sprite.h @@ -44,9 +44,10 @@ void move_sprite(Sprite *s); void move_sprites(void); Sprite *collides(Sprite *s); -int pixel_collides(float x, float y); +Sprite * pixel_collides(float x, float y); void load_sprite(Sprite *sprite, char *filename); +float sprite_mass(Sprite *s); void bounce(Sprite *a, Sprite *b); -- 1.7.10.4