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;
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;
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;
}
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);