diff options
Diffstat (limited to 'debris.c')
-rw-r--r-- | debris.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/debris.c b/debris.c new file mode 100644 index 0000000..cf4a526 --- /dev/null +++ b/debris.c @@ -0,0 +1,96 @@ +#include "components.h" +#include "map.h" +#include "sprite.h" +#include "standard.h" +#include "world.h" + +Entity new_debris( + struct World* world, + int x, + int y, + int vx, + int vy, + Sprite_ID spr +) { + Entity e; + CPosition* pos; + CDebris* debris; + CCollider* col; + CSprite* sprite; + + e = new_entity(world); + add_components( + world, + e, + ctype_moveable | + ctype_collider | + ctype_debris | + ctype_position | + ctype_sprite + ); + + pos = &world->positions[e]; + debris = &world->debris[e]; + col = &world->colliders[e]; + sprite = &world->sprites[e]; + + init_csprite(sprite, spr); + pos->x = x; + pos->y = y; + debris->vx = vx; + debris->vy = vy; + col->x = 0; + col->y = 0; + col->w = sprite->rect.w; + col->h = sprite->rect.h; + + return e; +} + +void debris_system(World* world) { + int i; + unsigned bits; + CPosition* pos; + CDebris* debris; + CSprite* sprite; + Bitmap map_bitmap; + const Bitmap* bmp; + + get_map_bitmap(&world->map, &map_bitmap); + + for (i = 0; i < world->entity_count; i++) { + bits = world->bitmask[i]; + if ( + (bits & ctype_debris) && + (bits & ctype_position) && + (bits & ctype_sprite) + ) { + pos = &world->positions[i]; + debris = &world->debris[i]; + sprite = &world->sprites[i]; + + pos->x += debris->vx; + pos->y += debris->vy; + debris->vx = (debris->vx << fbits) / 600; + debris->vy = (debris->vy << fbits) / 600; + + if (debris->vx == 0 && debris->vy == 0) { + /* Blit the motherfucker straight to the map + * so that we don't have to keep updating it + * when it's just sitting still. We can have + * INFINITE debris now. */ + + bmp = get_bitmap(sprite->id); + blit( + &map_bitmap, + bmp, + pos->x >> fbits, + pos->y >> fbits, + &sprite->rect + ); + + destroy_entity(world, i); + } + } + } +} |