From 8db8e52819f446f7ece15ff54bb90f03ee1326f5 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 26 Mar 2024 13:54:32 +0800 Subject: [PATCH] Particles when dashing --- particle.c | 2 +- player.c | 42 +++++++++++++++++++++++++++++------------- util/rand.c | 5 +++++ util/rand.h | 2 ++ 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/particle.c b/particle.c index 93f06cc..d739118 100644 --- a/particle.c +++ b/particle.c @@ -35,7 +35,7 @@ static void _particle_AdvancePart(System_Particle *sys, Particle *p, Duration de Vec2 delta = vec2_Scale(p->vec, duration_Seconds(deltaTime)); p->pos = vec2_Add(p->pos, delta); // Move slower - Vec2 delta_vec = vec2_Scale(p->vec, -p->vec_friction); + Vec2 delta_vec = vec2_Scale(p->vec, -p->vec_friction * duration_Seconds(deltaTime)); p->vec = vec2_Add(p->vec, delta_vec); } diff --git a/player.c b/player.c index 11c4566..6dcbaa2 100644 --- a/player.c +++ b/player.c @@ -8,6 +8,7 @@ #include "render_util.h" #include "types.h" #include "util/assert.h" +#include "util/rand.h" #include @@ -53,8 +54,6 @@ static double walkSpeed = 300.0, jumpSpeed = 800.0, dashSpeed = 1500.0; static int airjumpCount = 1, airdashCount = 1; static Duration dashLength = {.microseconds = 150000}, dashCooldown = {.microseconds = 400000}; -static Duration emitCooldown = {.microseconds = 200000}; -static TimePoint lastEmit; void player_Advance(System_Player *sys, Duration deltaTime) { if (!sys->player) @@ -66,6 +65,34 @@ void player_Advance(System_Player *sys, Duration deltaTime) { if (p->faceDirection != 1 && p->faceDirection != -1) p->faceDirection = 1; // Face right by default + // Particles + static Duration emitCooldown = {.microseconds = 150000}; + static TimePoint lastEmit = {}; + if (time_Since(lastEmit).microseconds > emitCooldown.microseconds) { + lastEmit = time_Now(); + Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y)); + particle_Emit( + sys->super->particle, + vec2_Add(vec2_Random(-20, 20, -10, 10), to_pos), + vec2(0, -100), 2, 6, 6, + duration_FromSeconds(0), &render_ModeDefault); + } + // Particles when dashing + if (time_Since(p->lastDash).microseconds < dashLength.microseconds && dabs(p->super->position->velocity.x) > EPS) { + static TimePoint lastDashEmit = {}; + static Duration cooldown = {.microseconds = 10000}; + if (time_Since(lastDashEmit).microseconds > cooldown.microseconds) { + lastDashEmit = time_Now(); + Vec2 to_pos = vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y / 2.0)); + particle_Emit( + sys->super->particle, + vec2_Add(vec2_Random(-5, 5, -30, 30), to_pos), + vec2(rand_DoubleRange(650, 700) * -p->faceDirection, rand_DoubleRange(-100, 100)), + 7, rand_DoubleRange(10, 16), 3, + duration_FromSeconds(rand_DoubleRange(1.5, 2.0)), &render_ModeInverse); + } + } + double targetVecX = 0.0; // Move left/right @@ -115,15 +142,4 @@ void player_Advance(System_Player *sys, Duration deltaTime) { // Check OnGround again if (dabs(sys->player->super->position->velocity.y) > EPS) sys->player->onGround = false; - - - // Particles - if (time_Since(lastEmit).microseconds > emitCooldown.microseconds) { - lastEmit = time_Now(); - particle_Emit( - sys->super->particle, - vec2_Add(p->super->position->position, vec2(0, -p->super->hitbox->box.size.y)), - vec2(0, -100), 2, 6, 6, - duration_FromSeconds(0), &render_ModeDefault); - } } diff --git a/util/rand.c b/util/rand.c index ebd4819..19e7a6d 100644 --- a/util/rand.c +++ b/util/rand.c @@ -7,3 +7,8 @@ double rand_Double01() { // Very bad implementation. return ((double)rand()) / (double)RAND_MAX; } + + +double rand_DoubleRange(double min, double max) { + return min + rand_Double01() * (max - min); +} diff --git a/util/rand.h b/util/rand.h index c7e3ebd..09357ae 100644 --- a/util/rand.h +++ b/util/rand.h @@ -7,6 +7,8 @@ extern "C" { // returns a double in the [0,1) range. double rand_Double01(); +double rand_DoubleRange(double min, double max); + #ifdef __cplusplus } #endif