summaryrefslogtreecommitdiff
path: root/src/game.c
blob: c4ff9fff175259ed4795d1d8deaf161fd68bd240 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
Game *g = nullptr;

void g_init() {
	g = alloc(sizeof(*g));
	g->maps = VecMap_new(0);
}

void g_push_map(const Tiled2cMap *t2c) {
	VecMap_push(g->maps, map_new(t2c));
}

void g_map_next() {
	g->map_id = (g->map_id + 1) % g->maps->size;
	g_map_restart();
}

void g_map_restart() {
	g_spawn_entities();
}

Map *g_map() {
	return VecMap_get(g->maps, g->map_id);
}

#define PROCESS(X) \
	repeat (i, MAX_ENTITIES) { \
		const auto e = &g->entities[i]; \
		if (e->table != nullptr) \
			entity_method(e, #X); \
	}

void g_update() {
	g->shake -= (g->shake > 0);
	tileset_update();
	PROCESS(update);
	PROCESS(update_post);
	memset(&g->ev, 0, sizeof(g->ev));
	repeat (i, MAX_ENTITIES) {
		auto e = &g->entities[i];
		e->visible = e->visible_next;
	}
	if (g->room_next) {
		if (--g->room_next == 0) {
			g_map_next();
			g->room_restart = 0;
		}
	}
	if (g->room_restart) {
		if (--g->room_restart == 0) {
			g_map_restart();
			g->room_next = 0;
		}
	}
}

void g_draw() {
	int x = g_map()->t2c->width * cfg.tile_width/2 - cfg.display_width/2;
	int y = g_map()->t2c->height * cfg.tile_height/2 - cfg.display_height/2;
	if (g->shake) {
		x += g->shake - rand() % (g->shake * 2);
		y += g->shake - rand() % (g->shake * 2);
	}
	TZR_SetCamera(x, y);
	TZR_DrawSetColor(1, 1, 1);
	map_draw(g_map(), v2_zero());
	PROCESS(draw_begin);
	PROCESS(draw);
	PROCESS(draw_end);
}

Entity *g_new_entity(const char *type, bool init) {
	assert(type != nullptr);
	Entity *e = nullptr;
	repeat (i, MAX_ENTITIES) {
		if (g->entities[i].table == nullptr) {
			e = &g->entities[i];
			break;
		}
	}
	if (e == nullptr) {
		pwrn("entities array is full");
		return nullptr;
	}

	const auto table = etable_get(type);
	if (table == nullptr) {
		pwrn("type '%s' hard cringe", type);
		return nullptr;
	}
	if (table->abstract) {
		pwrn("type '%s' is abstract and can't be instantiated", type);
		return nullptr;
	}

	memset(e, 0, sizeof(*e));
	e->table = table;
	e->uuid = ++g->uuid;
	e->visible = true;
	e->visible_next = true;
	if (init)
		entity_method(e, "init");
	return e;
}

Entity *g_new_entity_t2c(const Tiled2cObject *t2c) {
	Entity groups = {};
	entity_append_groups(&groups, t2c->name);

	i32 x = t2c->x;
	i32 y = t2c->y;
	if (t2c->tile) {
		x += t2c->width / 2;
		y -= t2c->height / 2;
	} else {
		x += t2c->width / 2;
		y += t2c->height / 2;
	}

	auto type = t2c->type;
	if ((!type || !type[0]) && t2c->tile) {
		const auto e_t2c = tile_t2c(t2c->tile);
		if (e_t2c != nullptr) {
			type = e_t2c->type;
			if (!type || !type[0])
				type = "tile";
			if (e_t2c->numobjects) {
				Entity *e = nullptr;
				rfor (i, 0u, e_t2c->numobjects) {
					e = g_new_entity_t2c(&e_t2c->objects[i]);
					if (e == nullptr)
						break;
					e->pos.x += x - t2c->width / 2;
					e->pos.y += y - t2c->height / 2;
					entity_copy_groups(e, &groups);
				}
			}
		}
	}
	auto e = g_new_entity(type, false);
	if (e == nullptr) {
		pwrn("g_new_entity failed");
		return nullptr;
	}

	e->t2c = t2c;
	e->tile = t2c->tile;
	repeat (i, MAX_GROUP_WORDS)
		e->group[i] = groups.group[i];
	e->pos.x = x;
	e->pos.y = y;
	e->dim.x = (i32)t2c->width;
	e->dim.y = (i32)t2c->height;
	e->visible = t2c->visible;
	e->visible_next = e->visible;
	entity_method(e, "init");
	return e;
}

Entity *g_get_entity(const char *type) {
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table != nullptr && !strcasecmp(e->table->name, type))
			return e;
	}
	return nullptr;
}

Entity *g_get_entity_t2c(u32 id) {
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table != nullptr && e->t2c && e->t2c->id == id)
			return e;
	}
	return nullptr;
}

void g_clear_entities() {
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table != nullptr)
			entity_method(e, "deinit");
	}
}

void g_spawn_entities() {
	g_clear_entities();
	const auto map = g_map();
	rfor (i, 0u, map->t2c->numobjects) {
		const auto obj = &map->t2c->objects[i];
		g_new_entity_t2c(obj);
	}
	rfor (i, 0u, map->t2c->numlayers) {
		const auto lay = &map->t2c->layers[i];
		if (!map_object_layer(lay))
			continue;
		Entity groups = {};
		entity_append_groups(&groups, lay->class);
		const auto visible = lay->visible && lay->opacity > .8;
		rfor (y, 0u, map->t2c->height) {
			rfor (x, 0u, map->t2c->width) {
				const auto tile =
				    lay->tilelayer.data[x + y*map->t2c->width];
				if (!tile)
					continue;
				with (e, g_new_entity(lay->class, false)) {
					e->tile = visible ? tile : 0;
					e->pos.x = x * map->t2c->tilewidth +
					           map->t2c->tilewidth / 2.;
					e->pos.y = y * map->t2c->tileheight +
					           map->t2c->tileheight / 2.;
					e->dim.x = map->t2c->tilewidth;
					e->dim.y = map->t2c->tileheight;
					e->visible = true;
					e->visible_next = true;
					entity_copy_groups(e, &groups);
					entity_method(e, "init");
				}
			}
		}
	}
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table != nullptr)
			entity_method(e, "init_post");
	}
	plog("free groups");
	rfor (i, 1, MAX_GROUP_WORDS * 32 - 1) {
		if (!g_group_count(i)) {
			u32 r = 0;
			while (i + r < MAX_GROUP_WORDS * 32 - 1 &&
			       g_group_count(i + r + 1) == 0)
				r += 1;
			if (r == 0)
				plog("%d", i - 1);
			else
				plog("%d -> %d", i - 1, i + r - 1);
			i += r;
		}
	}
}

u32 g_count(const char *type) {
	u32 c = 0;
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table != nullptr && strcmp(e->table->name, type) == 0)
			c += 1;
	}
	return c;
}

void g_shake(i32 duration) {
	g->shake = max(g->shake, duration);
}

u32 g_group_count(u32 gid) {
	u32 c = 0;
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table && entity_in_group(e, gid))
			c += 1;
	}
	return c;
}

void g_group_set_visibility(u32 gid, bool visible) {
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table && entity_in_group(e, gid))
			e->visible_next = visible;
	}
}

void g_group_set_flip(u32 gid, bool flip_x, bool flip_y) {
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table && entity_in_group(e, gid)) {
			e->flip_x ^= flip_x;
			e->flip_y ^= flip_y;
		}
	}
}

VecEntityP *g_group(u32 gid) {
	VecEntityP *v = VecEntityP_new(0);
	repeat (i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table && entity_in_group(e, gid))
			VecEntityP_push(v, e);
	}
	return v;
}