From 1b93095d738318b3f01e665536981d0271f19136 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 5 Mar 2024 15:20:32 +0800 Subject: [PATCH] Dashing --- input.c | 2 ++ input.h | 1 + main.cpp | 2 +- physics.c | 2 +- player.c | 46 ++++++++++++++++++++++++++++++++++++++-------- player.h | 13 +++++++++---- 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/input.c b/input.c index a17753a..b158f18 100644 --- a/input.c +++ b/input.c @@ -16,6 +16,7 @@ const char *input_KeyNames[input_Key_Count] = { "Attack", "Spell", "Use", + "Dash", "Escape"}; void input_SetDefaultKeymap(System_Input *sys) { @@ -27,6 +28,7 @@ void input_SetDefaultKeymap(System_Input *sys) { sys->systemKeymap[input_Key_Attack] = 'J'; sys->systemKeymap[input_Key_Spell] = 'K'; sys->systemKeymap[input_Key_Use] = 'L'; + sys->systemKeymap[input_Key_Dash] = VK_OEM_1; // The ;: key on the US keyboard sys->systemKeymap[input_Key_Escape] = VK_ESCAPE; } diff --git a/input.h b/input.h index cb7bc04..820c37a 100644 --- a/input.h +++ b/input.h @@ -19,6 +19,7 @@ typedef enum { input_Key_Attack, input_Key_Spell, input_Key_Use, + input_Key_Dash, input_Key_Escape, input_Key_Count } input_Key; diff --git a/main.cpp b/main.cpp index 74ac45c..f7b5bc0 100644 --- a/main.cpp +++ b/main.cpp @@ -44,7 +44,7 @@ int main() { EndBatchDraw(); - Duration toSleep = {.microseconds = 1000000 / 60 - time_Reset(&lastFrame).microseconds}; + Duration toSleep = {.microseconds = 1000000 / 300 - time_Reset(&lastFrame).microseconds}; duration_Sleep(toSleep); } diff --git a/physics.c b/physics.c index 53c4f30..ab6e970 100644 --- a/physics.c +++ b/physics.c @@ -86,7 +86,7 @@ static inline void _physics_AdvanceEntity(System_Physics *sys, Entity *e, Durati } -static double gravity = 1000; +static double gravity = 2000; void physics_Advance(System_Physics *sys, Duration deltaTime) { for (tree_Node *i = tree_FirstNode(sys->pos); diff --git a/player.c b/player.c index d1e7323..935a7b6 100644 --- a/player.c +++ b/player.c @@ -4,6 +4,7 @@ #include "app.h" #include "input.h" +#include "types.h" #include "util/assert.h" #include @@ -46,8 +47,9 @@ void player_DeleteEntity(System_Player *sys, uintptr_t id) { } -static double walkSpeed = 300.0, jumpSpeed = 500.0; -static int airjumpCount = 1; +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}; void player_Advance(System_Player *sys, Duration deltaTime) { if (!sys->player) @@ -55,27 +57,55 @@ void player_Advance(System_Player *sys, Duration deltaTime) { // The bulk of player logic right here System_Input *input = sys->super->input; + if (sys->player->faceDirection != 1 && sys->player->faceDirection != -1) + sys->player->faceDirection = 1; // Face right by default + + double targetVecX = 0.0; // Move left/right - if (input_IsPressed(input->keys[input_Key_Left])) + if (input_IsPressed(input->keys[input_Key_Left])) { + sys->player->faceDirection = -1; targetVecX += -walkSpeed; - if (input_IsPressed(input->keys[input_Key_Right])) + } + if (input_IsPressed(input->keys[input_Key_Right])) { + sys->player->faceDirection = 1; targetVecX += walkSpeed; + } sys->player->super->position->velocity.x = targetVecX; + // Jump if (sys->player->onGround) sys->player->jumpCount = 0; - - // Jump - if (sys->super->input->keys[input_Key_Jump] == JustPressed) { + if (sys->super->input->keys[input_Key_Jump] == JustPressed || + (sys->super->input->keys[input_Key_Jump] == Pressed && dabs(sys->player->super->position->velocity.y) < 5)) { if (sys->player->onGround || sys->player->jumpCount < airjumpCount) { - sys->player->super->position->velocity.y = -jumpSpeed; + sys->player->storedSpeedY = -jumpSpeed; if (!sys->player->onGround) // Took the second clause, airjumped sys->player->jumpCount++; } } + // Dash + if (sys->player->onGround) + sys->player->dashCount = 0; + if (input_IsPressed(input->keys[input_Key_Dash]) && + (sys->player->onGround || sys->player->dashCount < airdashCount) && + time_Since(sys->player->lastDash).microseconds > dashCooldown.microseconds) { + sys->player->lastDash = time_Now(); + if (!sys->player->onGround) + sys->player->dashCount++; + } + // Am I dashing right now? + if (time_Since(sys->player->lastDash).microseconds < dashLength.microseconds) { + sys->player->super->position->velocity.x += sys->player->faceDirection * dashSpeed; + sys->player->super->position->velocity.y = 0; + } else { // Release the stored Y speed + sys->player->super->position->velocity.y += sys->player->storedSpeedY; + sys->player->storedSpeedY = 0; + } + + // Check OnGround again if (dabs(sys->player->super->position->velocity.y) > EPS) sys->player->onGround = false; diff --git a/player.h b/player.h index 3650c7b..edd99ba 100644 --- a/player.h +++ b/player.h @@ -16,10 +16,15 @@ typedef struct _Entity Entity; typedef struct { Entity *super; - int jumpCount; // Number of times the player has jumped since leaving ground - // (Initial jump does not count) - bool onGround; // If the player is on the ground? - bool moveLeft, moveRight; // If the player is moving left/right? + int faceDirection; // +1 for right, -1 for left + TimePoint lastDash; + int dashCount; // Number of dashes the player did since in air + int jumpCount; // Number of times the player has jumped since leaving ground + // (Initial jump does not count) + + double storedSpeedY; // Speed stored in the middle of a dash + bool onGround; // If the player is on the ground? + bool moveLeft, moveRight; // If the player is moving left/right? } Component_Player;