JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
engine dots push rocks
authorJason Woofenden <jason@jasonwoof.com>
Sat, 25 Mar 2006 20:22:53 +0000 (20:22 +0000)
committerJason Woofenden <jason@jasonwoof.com>
Sat, 25 Mar 2006 20:22:53 +0000 (20:22 +0000)
pixel collision functions return what sprite was hit
new config var: ENGINE_DOT_WEIGHT

config.h
main.c
sprite.c
sprite.h

index 23d697e..f857291 100644 (file)
--- 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 (file)
--- 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; i<MAXENGINEDOTS; i++) {
                if(!edot[i].active) continue;
@@ -268,7 +269,15 @@ draw_engine_dots(SDL_Surface *s) {
                        edot[i].active = 0;
                        continue;
                }
-               if(pixel_collides(edot[i].x, edot[i].y)) { edot[i].active = 0; continue; }
+               // check collisions
+               if((hit = pixel_collides(edot[i].x, edot[i].y))) {
+                       if(hit->type != 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;
index 809547b..abe9fb3 100644 (file)
--- 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;
 }
 
index 75d7730..3cb2c0e 100644 (file)
--- 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);