summaryrefslogtreecommitdiff
path: root/src/entity.c
blob: 1180c1c4be0d9590688dfb3ecd3ae78e7df92ac2 (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
vec_impl(EntityP, Entity *);

void entity_method(Entity *this, const char *method) {
	if (this == nullptr) {
		pwrn("this is null");
		return;
	}
	if (this->table == nullptr) {
		pwrn("this is inactive");
		return;
	}
	if (strncasecmp(method, "draw", 4) == 0 && !this->visible)
		return;
	ETable *table = this->table;
	do {
		foreach (e, table->methods) {
			if (strcasecmp(e->name, method) == 0) {
				e->fn(this);
				goto _fn_found;
			}
		}
		if (table->parent != nullptr)
			table = etable_get(table->parent);
		else
			table = nullptr;
	} while (table != nullptr);
_fn_found:
	if (strcasecmp(method, "deinit") == 0)
		this->table = nullptr;
}

void entity_super(Entity *this, const char *method) {
	ETable *table = etable_get(this->table->parent);
	repeat(_, this->super_depth) table = etable_get(table->parent);
	while (table != nullptr) {
		foreach (e, table->methods) {
			if (strcasecmp(e->name, method) == 0) {
				this->super_depth += 1;
				e->fn(this);
				this->super_depth -= 1;
				return;
			}
		}
		if (table->parent != nullptr)
			table = etable_get(table->parent);
		else
			table = nullptr;
	}
}

typedef struct {
	vec2 tl, br;
} _Points;
static _Points _points(Entity *this, vec2 off) {
	auto p0 =
	    v2_add(v2_sub(this->pos, v2_trunc(v2_div(this->dim, 2))), off);
	auto p1 = v2_add(p0, this->dim);
	return (_Points){p0, p1};
}

bool entity_collide(Entity *this, vec2 off, u32 type) {
	const auto p = _points(this, off);
	rfor(y, p.tl.y, p.br.y) {
		rfor(x, p.tl.x, p.br.x) {
			if (map_get_px(g_map(), x, y, type))
				return true;
		}
	}
	return false;
}

bool entity_collide_solid(Entity *this, vec2 off) {
	return place_meeting(off, "solid", true);
}

bool entity_collide_spike(Entity *this, vec2 off) {
	return place_meeting(off, "spike", true);
}

bool entity_intersect(Entity *this, vec2 off, Entity *other) {
	const i32 x1 = this->pos.x + off.x;
	const i32 y1 = this->pos.y + off.y;
	const i32 w1 = this->dim.x;
	const i32 h1 = this->dim.y;
	const i32 x2 = other->pos.x;
	const i32 y2 = other->pos.y;
	const i32 w2 = other->dim.x;
	const i32 h2 = other->dim.y;
	return abs(x1 - x2) * 2 < (w1 + w2) && abs(y1 - y2) * 2 < (h1 + h2);
}

Entity *entity_place_meeting(Entity *this, vec2 off, const char *type,
                             bool visible) {
	repeat(i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table && strcasecmp(e->table->name, type) == 0 &&
		    (e->visible || !visible) && intersect(off, e))
			return e;
	}
	return nullptr;
}

VecEntityP *entity_place_meeting_list(Entity *this, vec2 off, const char *type,
                                      bool visible) {
	auto v = VecEntityP_new(0);
	repeat(i, MAX_ENTITIES) {
		const auto e = &g->entities[i];
		if (e->table && strcasecmp(e->table->name, type) == 0 &&
		    (e->visible || !visible) && intersect(off, e))
			VecEntityP_push(v, e);
	}
	return v;
}

vec2 entity_move(Entity *this, vec2 force) {
	if (collide_solid(v2_zero())) {
		REM = v2_zero();
		return v2_zero();
	}

	vec2 vel = VEL;
	repeat(a, 2) {
		const f32 sum = force.a[a] + REM.a[a];
		int spd = sum;
		REM.a[a] = sum - spd;
		const int sign = (spd > 0) - (spd < 0);
		if (sign == 0)
			continue;
		while (spd != 0) {
			POS.a[a] += sign;
			if (collide_solid(v2_zero())) {
				POS.a[a] -= sign;
				REM.a[a] = 0;
				// vel.a[a] *= 7/8.f;
				vel.a[a] = 0;
				break;
			}
			spd -= sign;
		}
	}
	return vel;
}

void entity_append_group(Entity *this, u32 gid) {
	assert(this != nullptr);
	assert(gid < MAX_GROUP_WORDS * 32);
	this->group[gid / 32] |= 1 << (gid % 32);
}

void entity_append_groups(Entity *this, const char *s) {
	assert(this != nullptr);
	if (s == nullptr)
		return;

	size_t i = 0;
	while (isdigit(s[i])) {
		entity_append_group(this, atoi(s + i) + 1);
		while (isdigit(s[i]))
			i++;
		while (s[i] != '\0' && !isdigit(s[i]))
			i++;
	}
}

void entity_copy_groups(Entity *this, Entity *other) {
	assert(this != nullptr);
	assert(other != nullptr);
	repeat(i, MAX_GROUP_WORDS) this->group[i] = other->group[i];
}

bool entity_in_group(Entity *this, u32 gid) {
	assert(this != nullptr);
	assert(gid < MAX_GROUP_WORDS * 32);
	return this->group[gid / 32] & (1 << (gid % 32));
}

static i32 _propertyi(Entity *this, const char *property, const char *type) {
	assert(this != nullptr);
	assert(this->t2c != nullptr);
	assert(property != nullptr);
	rfor(i, 0u, this->t2c->numproperties) {
		const auto e = &this->t2c->properties[i];
		if (strcasecmp(e->name, property) == 0) {
			assert(strcmp(e->type, type) == 0);
			return e->valuenumber;
		}
	}
	panic("property '%s' not found in entity %u", property,
	      (u32)this->uuid);
}

static i32 _propertyi_default(Entity *this, const char *property, i32 def,
                              const char *type) {
	assert(this != nullptr);
	assert(this->t2c != nullptr);
	assert(property != nullptr);
	rfor(i, 0u, this->t2c->numproperties) {
		const auto e = &this->t2c->properties[i];
		if (strcasecmp(e->name, property) == 0) {
			assert(strcmp(e->type, type) == 0);
			return e->valuenumber;
		}
	}
	return def;
}

i32 entity_propertyi(Entity *this, const char *property) {
	return _propertyi(this, property, "int");
}

i32 entity_propertyi_default(Entity *this, const char *property, i32 def) {
	return _propertyi_default(this, property, def, "int");
}

i32 entity_propertyo(Entity *this, const char *property) {
	return _propertyi(this, property, "object");
}

i32 entity_propertyo_default(Entity *this, const char *property, i32 def) {
	return _propertyi_default(this, property, def, "object");
}

f32 entity_propertyf(Entity *this, const char *property, f32 def) {
	assert(this != nullptr);
	assert(this->t2c != nullptr);
	assert(property != nullptr);
	rfor(i, 0u, this->t2c->numproperties) {
		const auto e = &this->t2c->properties[i];
		if (strcasecmp(e->name, property) == 0) {
			assert(strcmp(e->type, "float") == 0);
			return e->valuenumber;
		}
	}
	return def;
}

bool entity_propertyb(Entity *this, const char *property) {
	assert(this != nullptr);
	assert(this->t2c != nullptr);
	assert(property != nullptr);
	rfor(i, 0u, this->t2c->numproperties) {
		const auto e = &this->t2c->properties[i];
		if (strcasecmp(e->name, property) == 0) {
			assert(strcmp(e->type, "bool") == 0);
			return e->valuenumber;
		}
	}
	return false;
}

const char *entity_propertyc(Entity *this, const char *property) {
	assert(this != nullptr);
	assert(this->t2c != nullptr);
	assert(property != nullptr);
	rfor(i, 0u, this->t2c->numproperties) {
		const auto e = &this->t2c->properties[i];
		if (strcasecmp(e->name, property) == 0) {
			assert(strcmp(e->type, "color") == 0);
			return e->valuestring;
		}
	}
	return "#ff000000";
}