JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
we now return you to your regularly scheduled exploding ship.
[vor.git] / sprite.c
index 0a6ab87..1609368 100644 (file)
--- a/sprite.c
+++ b/sprite.c
@@ -111,10 +111,25 @@ square(int x, int y, int set)
 void
 add_sprite(Sprite *s)
 {
+       if(s->type < 0) s->type = -s->type;
        insert_sprite(square(s->x, s->y, set), s);
 }
 
 void
+reset_sprites(void)
+{
+       int i;
+
+       for(i=0; i<gw*gh; i++)
+               while(sprites[set][i]) {
+                       Sprite *s = remove_sprite(&sprites[set][i]);
+                       if(s->type < 0) s->type = -s->type;
+                       insert_sprite(&free_sprites[s->type], s);
+                       if(s->type > 0) s->type = -s->type;
+               }
+}
+
+void
 move_sprite(Sprite *s)
 {
        // move it.
@@ -128,8 +143,9 @@ sort_sprite(Sprite *s)
        // clip it, or sort it into the other set of sprites.
        if(s->x + s->w < 0 || s->x >= XSIZE
           || s->y + s->h < 0 || s->y >= YSIZE) {
+               if(s->type < 0) s->type = -s->type;
                insert_sprite(&free_sprites[s->type], s);
-               s->type = NONE;
+               if(s->type > 0) s->type = -s->type;
        } else insert_sprite(square(s->x, s->y, 1-set), s);
 }
 
@@ -139,7 +155,7 @@ move_sprites(void)
        int sq;
        Sprite **head;
 
-       // Move all the sprites (position and set)
+       // Move all the sprites
        for(sq=0; sq<gw*gh; sq++) {
                head=&sprites[set][sq];
                while(*head) {
@@ -198,6 +214,8 @@ collide(Sprite *a, Sprite *b)
 {
        int dx, dy, xov, yov;
 
+       if(a->type < 0 || b->type < 0) return false;
+
        if(b->x < a->x) { Sprite *tmp = a; a = b; b = tmp; }
 
        dx = b->x - a->x;
@@ -212,6 +230,28 @@ collide(Sprite *a, Sprite *b)
        else return mask_collide(xov, yov, a, b);
 }
 
+void
+collide_with_list(Sprite *s, Sprite *list)
+{
+       for(; list; list=list->next)
+               if(collide(s, list)) do_collision(s, list);
+}
+
+void
+collisions(void)
+{
+       int i, end = gw*gh;
+       Sprite *s;
+       for(i=0; i<end; i++) {
+               for(s=sprites[set][i]; s; s=s->next) {
+                       collide_with_list(s, s->next);
+                       if(i+1 < end) collide_with_list(s, sprites[set][i+1]);
+                       if(i+gw < end) collide_with_list(s, sprites[set][i+gw]);
+                       if(i+gw+1 < end) collide_with_list(s, sprites[set][i+gw+1]);
+               }
+       }
+}
+
 Sprite *
 hit_in_square(Sprite *r, Sprite *s)
 {
@@ -266,7 +306,7 @@ int
 pixel_hit_in_square(Sprite *r, float x, float y)
 {
        for(; r; r=r->next) {
-               if(pixel_collide(r, x, y)) return 1;
+               if(r->type >= 0 && pixel_collide(r, x, y)) return 1;
        }
        return 0;
 }
@@ -287,11 +327,11 @@ pixel_collides(float x, float y)
 }
 
 
-float
+static float
 sprite_mass(Sprite *s)
 {
-       if(s->type == SHIP_SPRITE) return s->area;
-       else if(s->type == ROCK_SPRITE) return 3*s->area;
+       if(s->type == SHIP) return s->area;
+       else if(s->type == ROCK) return 3*s->area;
        else return 0;
 }