This commit is contained in:
Edgaru089 2024-03-05 15:20:32 +08:00
parent c5a230647e
commit 1b93095d73
6 changed files with 52 additions and 14 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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);

View File

@ -4,6 +4,7 @@
#include "app.h"
#include "input.h"
#include "types.h"
#include "util/assert.h"
#include <stdlib.h>
@ -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;

View File

@ -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;