From 3c944d6f8e4f06b51ef995e3e474f2566f767b57 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 31 Aug 2023 15:26:24 +0900 Subject: [PATCH] Impl. randomized effect colors on walker hack Also, minor fixes/refactorings. --- Makefile | 6 ++++-- src/3d_helpers.cc | 8 ++++++-- src/3d_helpers.h | 6 ++++-- src/common_constants.cc | 41 +++++++++++++++++++++++++++++++++++++++ src/common_constants.h | 16 +++++++++++++++ src/electricity_effect.cc | 6 ++++-- src/electricity_effect.h | 5 +++-- src/screen_trunner.cc | 10 ++++++---- src/spark_effect.cc | 6 +++--- src/spark_effect.h | 5 +++-- wasm_build/Makefile | 6 ++++-- 11 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 src/common_constants.cc diff --git a/Makefile b/Makefile index ea69839..4cc8afc 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,8 @@ SOURCES = \ src/surface_triangle.cc \ src/screen_walker_hack.cc \ src/electricity_effect.cc \ - src/spark_effect.cc + src/spark_effect.cc \ + src/common_constants.cc HEADERS = \ src/game.h \ @@ -35,7 +36,8 @@ HEADERS = \ src/surface_triangle.h \ src/screen_walker_hack.h \ src/electricity_effect.h \ - src/spark_effect.h + src/spark_effect.h \ + src/common_constants.h OBJECTS = $(addprefix ${OBJDIR}/,$(subst .cc,.cc.o,${SOURCES})) diff --git a/src/3d_helpers.cc b/src/3d_helpers.cc index 4057a95..14d0b56 100644 --- a/src/3d_helpers.cc +++ b/src/3d_helpers.cc @@ -196,11 +196,11 @@ std::array get_quad_from_start_end(Vector3 start, Vector3 end, return quad; } -Vector3 operator+(const Vector3 &a, const Vector3 &b) { +Vector3 operator+(Vector3 a, Vector3 b) { return Vector3{a.x + b.x, a.y + b.y, a.z + b.z}; } -Vector3 operator-(const Vector3 &a, const Vector3 &b) { +Vector3 operator-(Vector3 a, Vector3 b) { return Vector3{a.x - b.x, a.y - b.y, a.z - b.z}; } @@ -211,3 +211,7 @@ Vector3 operator*(Vector3 vec3, float factor) { Vector3 operator*(Matrix mat, Vector3 vec3) { return Vector3Transform(vec3, mat); } + +Vector4 operator*(Vector4 a, Vector4 b) { + return Vector4{a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; +} diff --git a/src/3d_helpers.h b/src/3d_helpers.h index e0f0190..1a6be94 100644 --- a/src/3d_helpers.h +++ b/src/3d_helpers.h @@ -50,11 +50,13 @@ extern std::array get_quad_from_start_end(Vector3 start, // weirdness regarding column-major matrices. // extern Vector4 operator*(const Matrix &m, const Vector4 &v); -extern Vector3 operator+(const Vector3 &a, const Vector3 &b); -extern Vector3 operator-(const Vector3 &a, const Vector3 &b); +extern Vector3 operator+(Vector3 a, Vector3 b); +extern Vector3 operator-(Vector3 a, Vector3 b); extern Vector3 operator*(Vector3 vec3, float factor); extern Vector3 operator*(Matrix mat, Vector3 vec3); +extern Vector4 operator*(Vector4 a, Vector4 b); + #endif diff --git a/src/common_constants.cc b/src/common_constants.cc new file mode 100644 index 0000000..16d2f3a --- /dev/null +++ b/src/common_constants.cc @@ -0,0 +1,41 @@ +#include "common_constants.h" + +// third-party includes. +#include + +// local includes. +#include "ems.h" + +NeonColor get_random_neon_color() { + return (NeonColor)(call_js_get_random() * (float)NeonColor::NEON_COLOR_SIZE); +} + +void set_color_from_neon_color(NeonColor nc, Color *c_out) { + if (!c_out) { + return; + } + + switch (nc) { + case NeonColor::NEON_COLOR_RED: + *c_out = Color{255, 0, 0, 255}; + break; + case NeonColor::NEON_COLOR_GREEN: + *c_out = Color{0, 255, 0, 255}; + break; + case NeonColor::NEON_COLOR_BLUE: + *c_out = Color{0, 0, 255, 255}; + break; + case NeonColor::NEON_COLOR_CYAN: + *c_out = Color{0, 255, 255, 255}; + break; + case NeonColor::NEON_COLOR_YELLOW: + *c_out = Color{255, 255, 0, 255}; + break; + case NeonColor::NEON_COLOR_MAGENTA: + *c_out = Color{255, 0, 255, 255}; + break; + default: + *c_out = Color{255, 255, 255, 255}; + break; + } +} diff --git a/src/common_constants.h b/src/common_constants.h index 47e0c0f..574a1e4 100644 --- a/src/common_constants.h +++ b/src/common_constants.h @@ -1,10 +1,26 @@ #ifndef JUMPARTIFACT_DOT_COM_DEMO_0_COMMON_CONSTANTS_H_ #define JUMPARTIFACT_DOT_COM_DEMO_0_COMMON_CONSTANTS_H_ +// Forward declaration. +struct Color; + constexpr unsigned int SURFACE_UNIT_WIDTH = 51; constexpr unsigned int SURFACE_UNIT_HEIGHT = 51; constexpr float SURFACE_X_OFFSET = (float)SURFACE_UNIT_WIDTH / 2.0F - 0.5F; constexpr float SURFACE_Y_OFFSET = (float)SURFACE_UNIT_HEIGHT / 2.0F - 0.5F; +enum class NeonColor { + NEON_COLOR_RED = 0, + NEON_COLOR_GREEN, + NEON_COLOR_BLUE, + NEON_COLOR_CYAN, + NEON_COLOR_YELLOW, + NEON_COLOR_MAGENTA, + NEON_COLOR_SIZE +}; + +extern NeonColor get_random_neon_color(); +extern void set_color_from_neon_color(NeonColor nc, Color *c_out); + #endif diff --git a/src/electricity_effect.cc b/src/electricity_effect.cc index c03daa2..5b0ed59 100644 --- a/src/electricity_effect.cc +++ b/src/electricity_effect.cc @@ -33,9 +33,11 @@ std::optional ElectricityEffect::shader = std::nullopt; ElectricityEffect::ElectricityEffect(Vector3 center, float radius, - int line_count, float lifetime) + int line_count, float lifetime, + Color color) : end_points(), center(center), + color(color), radius(radius), lifetime(lifetime), timer(0.0F) { @@ -113,7 +115,7 @@ bool ElectricityEffect::update(float dt) { return timer >= lifetime; } -void ElectricityEffect::draw(Color color, Camera *camera) { +void ElectricityEffect::draw(Camera *camera) { float ratio = timer < lifetime ? (1.0F - timer / lifetime) : 0.0F; for (const auto &end_point : end_points) { diff --git a/src/electricity_effect.h b/src/electricity_effect.h index 2f3b6de..aecc322 100644 --- a/src/electricity_effect.h +++ b/src/electricity_effect.h @@ -17,12 +17,12 @@ constexpr float QUAD_MOVE_RATE = 0.05F; class ElectricityEffect { public: ElectricityEffect(Vector3 center, float radius, int line_count, - float lifetime); + float lifetime, Color color); /// Returns true if lifetime ended. bool update(float dt); /// Assumes draw mode is active. - void draw(Color color, Camera *camera); + void draw(Camera *camera); static Shader get_shader(); static void cleanup_shader(); @@ -39,6 +39,7 @@ class ElectricityEffect { static std::optional shader; std::vector end_points; Vector3 center; + Color color; float radius; float lifetime; float timer; diff --git a/src/screen_trunner.cc b/src/screen_trunner.cc index bb6e625..975d36b 100644 --- a/src/screen_trunner.cc +++ b/src/screen_trunner.cc @@ -108,15 +108,17 @@ bool TRunnerScreen::update(float dt, bool is_resized) { if (flags.test(1)) { if (walker_hack_success && controlled_walker_idx.has_value()) { walkers[controlled_walker_idx.value()].set_player_controlled(true); + Color color; + set_color_from_neon_color(get_random_neon_color(), &color); electricityEffects.push_back(ElectricityEffect( walkers[controlled_walker_idx.value()].get_body_pos(), ELECTRICITY_EFFECT_RADIUS, ELECTRICITY_EFFECT_LINE_COUNT, - ELECTRICITY_EFFECT_LIFETIME)); + ELECTRICITY_EFFECT_LIFETIME, color)); sparkEffects.push_back( SparkEffect(SPARK_EFFECT_SPARK_COUNT, SPARK_EFFECT_LIFETIME, walkers[controlled_walker_idx.value()].get_body_pos(), - SPARK_EFFECT_XZ_VARIANCE, SPARK_EFFECT_RADIUS)); + SPARK_EFFECT_XZ_VARIANCE, SPARK_EFFECT_RADIUS, color)); } else { controlled_walker_idx.reset(); } @@ -372,11 +374,11 @@ bool TRunnerScreen::draw(RenderTexture *render_texture) { } for (auto &ee : electricityEffects) { - ee.draw(GREEN, &camera); + ee.draw(&camera); } for (auto &se : sparkEffects) { - se.draw(GREEN); + se.draw(); } // TODO DEBUG diff --git a/src/spark_effect.cc b/src/spark_effect.cc index b747e3b..d60133d 100644 --- a/src/spark_effect.cc +++ b/src/spark_effect.cc @@ -5,8 +5,8 @@ #include "ems.h" SparkEffect::SparkEffect(int count, float lifetime, Vector3 pos, - float pos_xz_variance, float radius) - : sparks(), lifetime(lifetime), timer(0.0F) { + float pos_xz_variance, float radius, Color color) + : sparks(), color(color), lifetime(lifetime), timer(0.0F) { sparks.reserve(count); Vector3 above_pos = pos; @@ -36,7 +36,7 @@ bool SparkEffect::update(float dt) { return timer > lifetime; } -void SparkEffect::draw(Color color) { +void SparkEffect::draw() { float ratio = timer < lifetime ? (1.0F - timer / lifetime) : 0.0F; for (const auto &spark : sparks) { diff --git a/src/spark_effect.h b/src/spark_effect.h index 0dde24a..430cf7a 100644 --- a/src/spark_effect.h +++ b/src/spark_effect.h @@ -15,13 +15,13 @@ constexpr float SPARK_ACC_RATE = 8.0F; class SparkEffect { public: SparkEffect(int count, float lifetime, Vector3 pos, float pos_xz_variance, - float radius); + float radius, Color color); /// Returns true if end of lifetime. bool update(float dt); /// Assumes draw mode is active when called. - void draw(Color color); + void draw(); private: struct Spark { @@ -29,6 +29,7 @@ class SparkEffect { }; std::vector sparks; + Color color; float lifetime; float timer; }; diff --git a/wasm_build/Makefile b/wasm_build/Makefile index 0af63c6..37f6cfa 100644 --- a/wasm_build/Makefile +++ b/wasm_build/Makefile @@ -19,7 +19,8 @@ SOURCES = \ ../src/surface_triangle.cc \ ../src/screen_walker_hack.cc \ ../src/electricity_effect.cc \ - ../src/spark_effect.cc + ../src/spark_effect.cc \ + ../src/common_constants.cc HEADERS = \ ../src/ems.h \ @@ -32,7 +33,8 @@ HEADERS = \ ../src/surface_triangle.h \ ../src/screen_walker_hack.h \ ../src/electricity_effect.h \ - ../src/spark_effect.h + ../src/spark_effect.h \ + ../src/common_constants.h OBJECTS = $(addprefix ${OBJDIR}/,$(subst ..,PREVDIR,$(subst .cc,.cc.o,${SOURCES}))) -- 2.49.0