]> git.seodisparate.com - jumpartifact.com_demo_0/commitdiff
Rework electricity effect "line" generation
authorStephen Seo <seo.disparate@gmail.com>
Thu, 24 Aug 2023 06:49:22 +0000 (15:49 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 24 Aug 2023 06:49:22 +0000 (15:49 +0900)
src/electricity_effect.cc
src/electricity_effect.h
src/screen_trunner.cc
src/screen_trunner.h

index aee501481711f5c65a8fb3a3d2c8422cbdd9567d..aae424774940001216ffd647c3b1b26acc61baf2 100644 (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);
-
-      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;
+  std::queue<Vector3> positions;
+  Vector3 next, dir;
+  for (unsigned int idx = 0; idx < CYLINDER_SPLIT_COUNT; ++idx) {
+    positions.push(center);
+  }
+  while (line_count-- > 0 && !positions.empty()) {
+    next = positions.front();
+    positions.pop();
 
-        dir = Vector3Normalize(dir);
+    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 = pos, .direction = dir}, center, radius);
+    auto coll = GetRayCollisionSphere(Ray{.position = next, .direction = dir},
+                                      center, radius);
 
-        cylinders.push_back(Cylinder{.start = pos, .end = coll.point});
+    cylinders.push_back(Cylinder{.start = next, .end = coll.point});
 
-        if (idx == CYLINDER_SPLIT_COUNT - 1) {
-          pos = coll.point;
-        }
-      }
-    }
-    if (++sub_count >= CYLINDER_SUB_COUNT_MAX) {
-      sub_count = 0;
+    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);
     }
   }
 }
index ca5e8c7b212d423b930f7c8fea58c6a2b584e80e..1ffcf763b1e8a5d2f6a9be87e8cd7d2d0189facb 100644 (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;
index 967e7d882e0a8f8f7655131c8563a24526a36ce0..1b9e472ca7abe58ac114ab48584edd71938bc7f2 100644 (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();
index 7a47347290fdec6751fcee10527e5cb4b7301347..c1cbdceb753ee1b355c5a6dccb2b0cf9f05dd6df 100644 (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 {