diff --git a/app.c b/app.c index 8e8ef1d..52166d2 100644 --- a/app.c +++ b/app.c @@ -3,6 +3,7 @@ #include "camera.h" #include "entity.h" #include "input.h" +#include "particle.h" #include "physics.h" #include "player.h" #include "types.h" @@ -13,11 +14,12 @@ App *app_NewApp() { App *app = malloc(sizeof(App)); - app->input = input_NewSystem(app); - app->physics = physics_NewSystem(app); - app->player = player_NewSystem(app); - app->entity = entity_NewSystem(app); - app->camera = camera_NewSystem(app); + app->input = input_NewSystem(app); + app->physics = physics_NewSystem(app); + app->player = player_NewSystem(app); + app->entity = entity_NewSystem(app); + app->camera = camera_NewSystem(app); + app->particle = particle_NewSystem(app); app->wantQuit = false; @@ -68,6 +70,7 @@ void app_DeleteApp(App *app) { void app_Advance(App *app, Duration deltaTime) { + particle_Advance(app->particle, deltaTime); input_Advance(app->input); player_Advance(app->player, deltaTime); physics_Advance(app->physics, deltaTime); diff --git a/app.h b/app.h index f678ad4..f1de50b 100644 --- a/app.h +++ b/app.h @@ -5,6 +5,7 @@ #include "player.h" #include "input.h" #include "camera.h" +#include "particle.h" #include "types.h" #include "util/vector.h" @@ -14,11 +15,12 @@ extern "C" { typedef struct _App { - System_Physics *physics; - System_Player *player; - System_Input *input; - System_Entity *entity; - System_Camera *camera; + System_Physics *physics; + System_Player *player; + System_Input *input; + System_Entity *entity; + System_Camera *camera; + System_Particle *particle; bool wantQuit; } App; diff --git a/app_debug.c b/app_debug.c index 7be3570..0b4e6b7 100644 --- a/app_debug.c +++ b/app_debug.c @@ -1,6 +1,7 @@ #include "app.h" #include "types.h" +#include "util/tree.h" #include "util/vector.h" #include @@ -42,6 +43,10 @@ void app_DebugText(App *app, vector_Vector *vec_string) { center.x, center.y, size.x, size.y); PUSH_STRING(buf); + snprintf( + buf, sizeof(buf) - 1, + "Particle count: %d\n", tree_Count(app->particle->parts)); + PUSH_STRING(buf); char zero = '\0'; vector_Push(vec_string, &zero); diff --git a/app_render.cpp b/app_render.cpp index 6fe8e85..d7f3357 100644 --- a/app_render.cpp +++ b/app_render.cpp @@ -1,6 +1,7 @@ #include "app.h" #include "camera.h" +#include "particle.h" #include "physics.h" #include "easyx.h" #include "util/tree.h" @@ -46,5 +47,8 @@ void app_Render(App *app) { setbkcolor(RGB(0, 0, 0)); render_SetModes(mode_rotate, since); render_FillCircleW(app, vec2(200, 100), 20); + + // Draw particles + particle_Render(app->particle); } } diff --git a/player.c b/player.c index 935a7b6..11c4566 100644 --- a/player.c +++ b/player.c @@ -3,7 +3,9 @@ #include "entity.h" #include "app.h" #include "input.h" +#include "particle.h" +#include "render_util.h" #include "types.h" #include "util/assert.h" #include @@ -51,62 +53,77 @@ 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) return; // The bulk of player logic right here - System_Input *input = sys->super->input; + System_Input *input = sys->super->input; + Component_Player *p = sys->player; - if (sys->player->faceDirection != 1 && sys->player->faceDirection != -1) - sys->player->faceDirection = 1; // Face right by default + if (p->faceDirection != 1 && p->faceDirection != -1) + p->faceDirection = 1; // Face right by default double targetVecX = 0.0; // Move left/right if (input_IsPressed(input->keys[input_Key_Left])) { - sys->player->faceDirection = -1; + p->faceDirection = -1; targetVecX += -walkSpeed; } if (input_IsPressed(input->keys[input_Key_Right])) { - sys->player->faceDirection = 1; + p->faceDirection = 1; targetVecX += walkSpeed; } - sys->player->super->position->velocity.x = targetVecX; + p->super->position->velocity.x = targetVecX; // Jump - if (sys->player->onGround) - sys->player->jumpCount = 0; - 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->storedSpeedY = -jumpSpeed; - if (!sys->player->onGround) // Took the second clause, airjumped - sys->player->jumpCount++; + if (p->onGround) + p->jumpCount = 0; + if (input->keys[input_Key_Jump] == JustPressed || + (input->keys[input_Key_Jump] == Pressed && dabs(p->super->position->velocity.y) < 5)) { + if (p->onGround || p->jumpCount < airjumpCount) { + p->storedSpeedY = -jumpSpeed; + if (!p->onGround) // Took the second clause, airjumped + p->jumpCount++; } } // Dash - if (sys->player->onGround) - sys->player->dashCount = 0; + if (p->onGround) + p->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++; + (p->onGround || p->dashCount < airdashCount) && + time_Since(p->lastDash).microseconds > dashCooldown.microseconds) { + p->lastDash = time_Now(); + if (!p->onGround) + p->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; + if (time_Since(p->lastDash).microseconds < dashLength.microseconds) { + p->super->position->velocity.x += p->faceDirection * dashSpeed; + p->super->position->velocity.y = 0; } else { // Release the stored Y speed - sys->player->super->position->velocity.y += sys->player->storedSpeedY; - sys->player->storedSpeedY = 0; + p->super->position->velocity.y += p->storedSpeedY; + p->storedSpeedY = 0; } // 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/types.c b/types.c index 52b49c6..4f03a4e 100644 --- a/types.c +++ b/types.c @@ -2,6 +2,7 @@ #include "types.h" #include #include +#include #ifdef WIN32_LEAN_AND_MEAN #undef WIN32_LEAN_AND_MEAN #endif @@ -39,6 +40,10 @@ Vec2 vec2_Scale(Vec2 v, double scale) { return result; } +Vec2 vec2_Random(double minX, double maxX, double minY, double maxY) { +} + + bool box2_Intersects(const Box2 x, const Box2 y, Box2 *out_intersection) { // Compute the min and max of the first rectangle on both axes const double r1MinX = dmin(x.lefttop.x, x.lefttop.x + x.size.x); diff --git a/types.h b/types.h index 21816e7..662c9d3 100644 --- a/types.h +++ b/types.h @@ -37,6 +37,8 @@ Vec2 vec2_Add(Vec2 x, Vec2 y); Vec2 vec2_Minus(Vec2 pos, Vec2 neg); Vec2 vec2_Scale(Vec2 v, double scale); +Vec2 vec2_Random(double minX, double maxX, double minY, double maxY); + // A 2d box of double. typedef struct { diff --git a/util/tree.c b/util/tree.c index 9235193..dedf1d0 100644 --- a/util/tree.c +++ b/util/tree.c @@ -174,3 +174,13 @@ tree_Node *tree_Node_Previous(tree_Node *node) { return result->father; } } + + +size_t tree_Count(tree_Tree *tree) { + size_t cnt = 0; + for (tree_Node *i = tree_FirstNode(tree); + i != NULL; + i = tree_Node_Next(i)) + cnt++; + return cnt; +} diff --git a/util/tree.h b/util/tree.h index b8679f7..add8048 100644 --- a/util/tree.h +++ b/util/tree.h @@ -69,6 +69,9 @@ tree_Node *tree_Node_Next(tree_Node *node); // Node_Previous returns the previous node. Returns NULL if first. tree_Node *tree_Node_Previous(tree_Node *node); +// Count walks the tree and counts how many nodes are there in the tree. +size_t tree_Count(tree_Tree *tree); + #ifdef __cplusplus }