Refactor duplicate code into lambda fn
All checks were successful
Build and Publish WASM version of demo / Build-And-Deploy (push) Successful in 16s

This commit is contained in:
Stephen Seo 2023-08-09 13:26:15 +09:00
parent 2dc30ce252
commit cbcf0da5ce

View file

@ -201,51 +201,38 @@ bool TRunnerScreen::update(float dt) {
Vector3 sw{xf - 0.5F, current.sw, zf + 0.5F}; Vector3 sw{xf - 0.5F, current.sw, zf + 0.5F};
Vector3 se{xf + 0.5F, current.se, zf + 0.5F}; Vector3 se{xf + 0.5F, current.se, zf + 0.5F};
if (auto collision = ray_collision_triangle(ray, nw, sw, ne); const auto on_collide_fn = [this, idx, xf, zf,
collision.has_value()) { &current](const auto &collision) {
idx_hit = idx; this->idx_hit = idx;
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "first: idx_hit set to " << idx_hit << std::endl; std::cout << "idx_hit set to " << idx_hit << std::endl;
#endif #endif
mouse_hit = collision.value(); this->mouse_hit = collision.value();
camera_target.x = xf;
camera_target.y = this->camera_target.x = xf;
this->camera_target.y =
(current.nw + current.ne + current.sw + current.se) / 4.0F; (current.nw + current.ne + current.sw + current.se) / 4.0F;
camera_target.z = zf; this->camera_target.z = zf;
if (idx != SURFACE_UNIT_WIDTH / 2 + if (idx != SURFACE_UNIT_WIDTH / 2 +
(SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) { (SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) {
camera_pos = this->camera_pos = Vector3Add(
Vector3Add(Vector3Scale(Vector3Normalize(camera_target), 4.0F), Vector3Scale(Vector3Normalize(this->camera_target), 4.0F),
camera_target); this->camera_target);
camera_pos.y += 4.0F; this->camera_pos.y += 4.0F;
} else { } else {
camera_pos.x = 0.0F; this->camera_pos.x = 0.0F;
camera_pos.y = camera_target.y + 4.0F; this->camera_pos.y = this->camera_target.y + 4.0F;
camera_pos.z = 0.0F; this->camera_pos.z = 0.0F;
} }
};
if (auto collision = ray_collision_triangle(ray, nw, sw, ne);
collision.has_value()) {
on_collide_fn(collision);
break; break;
} else if (auto collision = ray_collision_triangle(ray, ne, sw, se); } else if (auto collision = ray_collision_triangle(ray, ne, sw, se);
collision.has_value()) { collision.has_value()) {
idx_hit = idx; on_collide_fn(collision);
#ifndef NDEBUG
std::cout << "second: idx_hit set to " << idx_hit << std::endl;
#endif
mouse_hit = collision.value();
camera_target.x = xf;
camera_target.y =
(current.nw + current.ne + current.sw + current.se) / 4.0F;
camera_target.z = zf;
if (idx != SURFACE_UNIT_WIDTH / 2 +
(SURFACE_UNIT_HEIGHT / 2) * SURFACE_UNIT_WIDTH) {
camera_pos =
Vector3Add(Vector3Scale(Vector3Normalize(camera_target), 4.0F),
camera_target);
camera_pos.y += 4.0F;
} else {
camera_pos.x = 0.0F;
camera_pos.y = camera_target.y + 4.0F;
camera_pos.z = 0.0F;
}
break; break;
} }
} }