aboutsummaryrefslogtreecommitdiff
path: root/debris.c
blob: cf4a526dce54804e8f9841978ade19fd8653512b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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);
			}
		}
	}
}