aboutsummaryrefslogtreecommitdiff
path: root/world.c
blob: a84aac64fe77cbd4de6d94fd753231c0a52085f3 (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
#include "world.h"
#include "platform.h"
#include "error.h"

void init_world(World* world) {
	int i, s = sizeof *world;
	for (i = 0; i < s; i++) {
		((char*)world)[i] = 0;
	}
}

Entity new_entity(World* world) {
	Entity e;

#if DEBUG
	if (
		world->entity_count >= max_entities
		&& world->recycle_bin_count == 0
	) {
		platform_err("Too many entities.\n");
		platform_abort(error_out_of_memory);
	}
#endif

	e = 
		world->recycle_bin_count > 0 ?
		world->recycle_bin[--world->recycle_bin_count] :
		world->entity_count++;

	world->bitmask[e] = 0;

	world->gmemory--;

	return e;
}

void destroy_entity(World* world, Entity e) {
	world->recycle_bin[world->recycle_bin_count++] = e;
	if (world->bitmask[e] & ctype_enemy) {
		world->enemy_count--;
	}
	world->bitmask[e] = 0;
	world->gmemory++;
}

void add_components(World* world, Entity e, CType bits) {
	world->bitmask[e] |= bits;
}

void remove_components(World* world, Entity e, CType bits) {
	world->bitmask[e] &= ~bits;
}