diff options
Diffstat (limited to 'src/entity.c')
-rw-r--r-- | src/entity.c | 161 |
1 files changed, 68 insertions, 93 deletions
diff --git a/src/entity.c b/src/entity.c index 54e6137..5f81cda 100644 --- a/src/entity.c +++ b/src/entity.c @@ -1,11 +1,9 @@ -vec_impl(EntityP, Entity*); +vec_impl(EntityP, Entity *); -void -entity_method(Entity *this, const char *method) -{ +void entity_method(Entity *this, const char *method) { if (this == nullptr) { pwrn("this is null"); - return; \ + return; } if (this->table == nullptr) { pwrn("this is inactive"); @@ -25,18 +23,15 @@ entity_method(Entity *this, const char *method) table = etable_get(table->parent); else table = nullptr; - } while(table != nullptr); + } while (table != nullptr); _fn_found: if (strcasecmp(method, "deinit") == 0) this->table = nullptr; } -void -entity_super(Entity *this, const char *method) -{ +void entity_super(Entity *this, const char *method) { ETable *table = etable_get(this->table->parent); - repeat (_, this->super_depth) - table = etable_get(table->parent); + repeat(_, this->super_depth) table = etable_get(table->parent); while (table != nullptr) { foreach (e, table->methods) { if (strcasecmp(e->name, method) == 0) { @@ -53,21 +48,20 @@ entity_super(Entity *this, const char *method) } } -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); +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 }; + return (_Points){p0, p1}; } -bool -entity_collide(Entity *this, vec2 off, u32 type) -{ +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) { + 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; } @@ -75,21 +69,15 @@ entity_collide(Entity *this, vec2 off, u32 type) return false; } -bool -entity_collide_solid(Entity *this, vec2 off) -{ +bool entity_collide_solid(Entity *this, vec2 off) { return place_meeting(off, "solid", true); } -bool -entity_collide_spike(Entity *this, vec2 off) -{ +bool entity_collide_spike(Entity *this, vec2 off) { return place_meeting(off, "spike", true); } -bool -entity_intersect(Entity *this, vec2 off, Entity *other) -{ +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; @@ -101,56 +89,49 @@ entity_intersect(Entity *this, vec2 off, Entity *other) 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) { +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)) + 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) -{ +VecEntityP *entity_place_meeting_list(Entity *this, vec2 off, const char *type, + bool visible) { auto v = VecEntityP_new(0); - repeat (i, MAX_ENTITIES) { + 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)) + 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) -{ +vec2 entity_move(Entity *this, vec2 force) { if (collide_solid(v2_zero())) { REM = v2_zero(); return v2_zero(); } vec2 vel = VEL; - repeat (a, 2) { + 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; + 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] *= 7/8.f; vel.a[a] = 0; break; } @@ -160,19 +141,16 @@ entity_move(Entity *this, vec2 force) return vel; } -void -entity_append_group(Entity *this, u32 gid) -{ +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) -{ +void entity_append_groups(Entity *this, const char *s) { assert(this != nullptr); - if (s == nullptr) return; + if (s == nullptr) + return; size_t i = 0; while (isdigit(s[i])) { @@ -184,46 +162,39 @@ entity_append_groups(Entity *this, const char *s) } } -void -entity_copy_groups(Entity *this, Entity *other) -{ +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]; + repeat(i, MAX_GROUP_WORDS) this->group[i] = other->group[i]; } -bool -entity_in_group(Entity *this, u32 gid) -{ +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) -{ +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) { + 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); + 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) -{ +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) { + 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); @@ -233,37 +204,27 @@ _propertyi_default(Entity *this, const char *property, i32 def, const char *type return def; } -i32 -entity_propertyi(Entity *this, const char *property) -{ +i32 entity_propertyi(Entity *this, const char *property) { return _propertyi(this, property, "int"); } -i32 -entity_propertyi_default(Entity *this, const char *property, i32 def) -{ +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) -{ +i32 entity_propertyo(Entity *this, const char *property) { return _propertyi(this, property, "object"); } -i32 -entity_propertyo_default(Entity *this, const char *property, i32 def) -{ +i32 entity_propertyo_default(Entity *this, const char *property, i32 def) { return _propertyi_default(this, property, def, "object"); } -bool -entity_propertyb(Entity *this, const char *property) -{ +bool entity_propertyb(Entity *this, const char *property) { assert(this != nullptr); assert(this->t2c != nullptr); assert(property != nullptr); - rfor (i, 0u, this->t2c->numproperties) { + 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); @@ -272,3 +233,17 @@ entity_propertyb(Entity *this, const char *property) } 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 "#000000ff"; +} |