Rework electricity effect "line" generation
All checks were successful
Build and Publish WASM version of demo / Build-And-Deploy (push) Successful in 23s

This commit is contained in:
Stephen Seo 2023-08-24 15:49:22 +09:00
parent d7351f9e3b
commit bfa1e31305
4 changed files with 25 additions and 47 deletions

View file

@ -1,5 +1,8 @@
#include "electricity_effect.h"
// standard library includes
#include <queue>
// third party includes
#include <raylib.h>
#include <raymath.h>
@ -18,54 +21,28 @@ ElectricityEffect::ElectricityEffect(Vector3 center, float radius,
cylinders.reserve(line_count);
// Generate cylinders.
int sub_count = 0;
Vector3 dir, pos;
for (; line_count > 0; --line_count) {
if (sub_count == 0) {
dir.x = call_js_get_random() * 2.0F - 1.0F;
dir.y = call_js_get_random() * 2.0F - 1.0F;
dir.z = call_js_get_random() * 2.0F - 1.0F;
dir = Vector3Normalize(dir);
pos = center + dir * (radius * 0.7F);
dir.x = call_js_get_random() * 2.0F - 1.0F;
dir.y = call_js_get_random() * 2.0F - 1.0F;
dir.z = call_js_get_random() * 2.0F - 1.0F;
dir = Vector3Normalize(dir);
auto coll = GetRayCollisionSphere(
Ray{.position = center, .direction = dir}, center, radius);
cylinders.push_back(Cylinder{.start = pos, .end = coll.point});
pos = coll.point;
} else {
dir = Vector3Normalize(center - pos);
pos = pos + dir * (radius * CYLINDER_EDGE_OFFSET);
std::queue<Vector3> positions;
Vector3 next, dir;
for (unsigned int idx = 0; idx < CYLINDER_SPLIT_COUNT; ++idx) {
dir.x = call_js_get_random() * 2.0F - 1.0F;
dir.y = call_js_get_random() * 2.0F - 1.0F;
dir.z = call_js_get_random() * 2.0F - 1.0F;
dir = Vector3Normalize(dir);
auto coll = GetRayCollisionSphere(
Ray{.position = pos, .direction = dir}, center, radius);
cylinders.push_back(Cylinder{.start = pos, .end = coll.point});
if (idx == CYLINDER_SPLIT_COUNT - 1) {
pos = coll.point;
positions.push(center);
}
}
}
if (++sub_count >= CYLINDER_SUB_COUNT_MAX) {
sub_count = 0;
while (line_count-- > 0 && !positions.empty()) {
next = positions.front();
positions.pop();
dir = Vector3Normalize(Vector3{call_js_get_random() * 2.0F - 1.0F,
call_js_get_random() * 2.0F - 1.0F,
call_js_get_random() * 2.0F - 1.0F});
auto coll = GetRayCollisionSphere(Ray{.position = next, .direction = dir},
center, radius);
cylinders.push_back(Cylinder{.start = next, .end = coll.point});
dir = Vector3Normalize(center - coll.point);
coll.point = coll.point + dir * (radius * CYLINDER_EDGE_OFFSET);
for (unsigned int idx = 0; idx < CYLINDER_SPLIT_COUNT; ++idx) {
positions.push(coll.point);
}
}
}

View file

@ -7,7 +7,6 @@
// third party includes
#include <raylib.h>
constexpr int CYLINDER_SUB_COUNT_MAX = 3;
constexpr int CYLINDER_SPLIT_COUNT = 3;
constexpr int CYLINDER_SIDES = 3;
constexpr float CYLINDER_MAX_RADIUS = 0.03F;

View file

@ -101,8 +101,8 @@ bool TRunnerScreen::update(float dt, bool is_resized) {
if (walker_hack_success && controlled_walker_idx.has_value()) {
walkers[controlled_walker_idx.value()].set_player_controlled(true);
electricityEffects.push_back(ElectricityEffect(
walkers[controlled_walker_idx.value()].get_body_pos(), 1.7F, 15,
1.0F));
walkers[controlled_walker_idx.value()].get_body_pos(), 1.7F,
ELECTRICITY_EFFECT_LINE_COUNT, 1.0F));
} else {
controlled_walker_idx.reset();

View file

@ -29,6 +29,8 @@ constexpr float SURFACE_RESET_TIME = 4.0F;
constexpr float SURFACE_RESET_TIME_TRI_DRAW = 3.0F;
constexpr float SURFACE_RESET_Y_OFFSET = 40.0F;
constexpr int ELECTRICITY_EFFECT_LINE_COUNT = 50;
class TRunnerScreen : public Screen {
public:
struct SurfaceUnit {