Hook Particle to the other systems

Had to manually merge a change set for this
This commit is contained in:
Edgaru089 2024-03-26 12:17:21 +08:00
parent 8bf4d95384
commit 19895363a3
9 changed files with 87 additions and 36 deletions

3
app.c
View File

@ -3,6 +3,7 @@
#include "camera.h" #include "camera.h"
#include "entity.h" #include "entity.h"
#include "input.h" #include "input.h"
#include "particle.h"
#include "physics.h" #include "physics.h"
#include "player.h" #include "player.h"
#include "types.h" #include "types.h"
@ -18,6 +19,7 @@ App *app_NewApp() {
app->player = player_NewSystem(app); app->player = player_NewSystem(app);
app->entity = entity_NewSystem(app); app->entity = entity_NewSystem(app);
app->camera = camera_NewSystem(app); app->camera = camera_NewSystem(app);
app->particle = particle_NewSystem(app);
app->wantQuit = false; app->wantQuit = false;
@ -68,6 +70,7 @@ void app_DeleteApp(App *app) {
void app_Advance(App *app, Duration deltaTime) { void app_Advance(App *app, Duration deltaTime) {
particle_Advance(app->particle, deltaTime);
input_Advance(app->input); input_Advance(app->input);
player_Advance(app->player, deltaTime); player_Advance(app->player, deltaTime);
physics_Advance(app->physics, deltaTime); physics_Advance(app->physics, deltaTime);

2
app.h
View File

@ -5,6 +5,7 @@
#include "player.h" #include "player.h"
#include "input.h" #include "input.h"
#include "camera.h" #include "camera.h"
#include "particle.h"
#include "types.h" #include "types.h"
#include "util/vector.h" #include "util/vector.h"
@ -19,6 +20,7 @@ typedef struct _App {
System_Input *input; System_Input *input;
System_Entity *entity; System_Entity *entity;
System_Camera *camera; System_Camera *camera;
System_Particle *particle;
bool wantQuit; bool wantQuit;
} App; } App;

View File

@ -1,6 +1,7 @@
#include "app.h" #include "app.h"
#include "types.h" #include "types.h"
#include "util/tree.h"
#include "util/vector.h" #include "util/vector.h"
#include <stdio.h> #include <stdio.h>
@ -42,6 +43,10 @@ void app_DebugText(App *app, vector_Vector *vec_string) {
center.x, center.y, center.x, center.y,
size.x, size.y); size.x, size.y);
PUSH_STRING(buf); PUSH_STRING(buf);
snprintf(
buf, sizeof(buf) - 1,
"Particle count: %d\n", tree_Count(app->particle->parts));
PUSH_STRING(buf);
char zero = '\0'; char zero = '\0';
vector_Push(vec_string, &zero); vector_Push(vec_string, &zero);

View File

@ -1,6 +1,7 @@
#include "app.h" #include "app.h"
#include "camera.h" #include "camera.h"
#include "particle.h"
#include "physics.h" #include "physics.h"
#include "easyx.h" #include "easyx.h"
#include "util/tree.h" #include "util/tree.h"
@ -46,5 +47,8 @@ void app_Render(App *app) {
setbkcolor(RGB(0, 0, 0)); setbkcolor(RGB(0, 0, 0));
render_SetModes(mode_rotate, since); render_SetModes(mode_rotate, since);
render_FillCircleW(app, vec2(200, 100), 20); render_FillCircleW(app, vec2(200, 100), 20);
// Draw particles
particle_Render(app->particle);
} }
} }

View File

@ -3,7 +3,9 @@
#include "entity.h" #include "entity.h"
#include "app.h" #include "app.h"
#include "input.h" #include "input.h"
#include "particle.h"
#include "render_util.h"
#include "types.h" #include "types.h"
#include "util/assert.h" #include "util/assert.h"
#include <stdlib.h> #include <stdlib.h>
@ -51,62 +53,77 @@ static double walkSpeed = 300.0, jumpSpeed = 800.0, dashSpeed = 1500.0;
static int airjumpCount = 1, airdashCount = 1; static int airjumpCount = 1, airdashCount = 1;
static Duration dashLength = {.microseconds = 150000}, dashCooldown = {.microseconds = 400000}; static Duration dashLength = {.microseconds = 150000}, dashCooldown = {.microseconds = 400000};
static Duration emitCooldown = {.microseconds = 200000};
static TimePoint lastEmit;
void player_Advance(System_Player *sys, Duration deltaTime) { void player_Advance(System_Player *sys, Duration deltaTime) {
if (!sys->player) if (!sys->player)
return; return;
// The bulk of player logic right here // 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) if (p->faceDirection != 1 && p->faceDirection != -1)
sys->player->faceDirection = 1; // Face right by default p->faceDirection = 1; // Face right by default
double targetVecX = 0.0; double targetVecX = 0.0;
// Move left/right // Move left/right
if (input_IsPressed(input->keys[input_Key_Left])) { if (input_IsPressed(input->keys[input_Key_Left])) {
sys->player->faceDirection = -1; p->faceDirection = -1;
targetVecX += -walkSpeed; targetVecX += -walkSpeed;
} }
if (input_IsPressed(input->keys[input_Key_Right])) { if (input_IsPressed(input->keys[input_Key_Right])) {
sys->player->faceDirection = 1; p->faceDirection = 1;
targetVecX += walkSpeed; targetVecX += walkSpeed;
} }
sys->player->super->position->velocity.x = targetVecX; p->super->position->velocity.x = targetVecX;
// Jump // Jump
if (sys->player->onGround) if (p->onGround)
sys->player->jumpCount = 0; p->jumpCount = 0;
if (sys->super->input->keys[input_Key_Jump] == JustPressed || if (input->keys[input_Key_Jump] == JustPressed ||
(sys->super->input->keys[input_Key_Jump] == Pressed && dabs(sys->player->super->position->velocity.y) < 5)) { (input->keys[input_Key_Jump] == Pressed && dabs(p->super->position->velocity.y) < 5)) {
if (sys->player->onGround || sys->player->jumpCount < airjumpCount) { if (p->onGround || p->jumpCount < airjumpCount) {
sys->player->storedSpeedY = -jumpSpeed; p->storedSpeedY = -jumpSpeed;
if (!sys->player->onGround) // Took the second clause, airjumped if (!p->onGround) // Took the second clause, airjumped
sys->player->jumpCount++; p->jumpCount++;
} }
} }
// Dash // Dash
if (sys->player->onGround) if (p->onGround)
sys->player->dashCount = 0; p->dashCount = 0;
if (input_IsPressed(input->keys[input_Key_Dash]) && if (input_IsPressed(input->keys[input_Key_Dash]) &&
(sys->player->onGround || sys->player->dashCount < airdashCount) && (p->onGround || p->dashCount < airdashCount) &&
time_Since(sys->player->lastDash).microseconds > dashCooldown.microseconds) { time_Since(p->lastDash).microseconds > dashCooldown.microseconds) {
sys->player->lastDash = time_Now(); p->lastDash = time_Now();
if (!sys->player->onGround) if (!p->onGround)
sys->player->dashCount++; p->dashCount++;
} }
// Am I dashing right now? // Am I dashing right now?
if (time_Since(sys->player->lastDash).microseconds < dashLength.microseconds) { if (time_Since(p->lastDash).microseconds < dashLength.microseconds) {
sys->player->super->position->velocity.x += sys->player->faceDirection * dashSpeed; p->super->position->velocity.x += p->faceDirection * dashSpeed;
sys->player->super->position->velocity.y = 0; p->super->position->velocity.y = 0;
} else { // Release the stored Y speed } else { // Release the stored Y speed
sys->player->super->position->velocity.y += sys->player->storedSpeedY; p->super->position->velocity.y += p->storedSpeedY;
sys->player->storedSpeedY = 0; p->storedSpeedY = 0;
} }
// Check OnGround again // Check OnGround again
if (dabs(sys->player->super->position->velocity.y) > EPS) if (dabs(sys->player->super->position->velocity.y) > EPS)
sys->player->onGround = false; 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);
}
} }

View File

@ -2,6 +2,7 @@
#include "types.h" #include "types.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#ifdef WIN32_LEAN_AND_MEAN #ifdef WIN32_LEAN_AND_MEAN
#undef WIN32_LEAN_AND_MEAN #undef WIN32_LEAN_AND_MEAN
#endif #endif
@ -39,6 +40,10 @@ Vec2 vec2_Scale(Vec2 v, double scale) {
return result; 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) { bool box2_Intersects(const Box2 x, const Box2 y, Box2 *out_intersection) {
// Compute the min and max of the first rectangle on both axes // 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); const double r1MinX = dmin(x.lefttop.x, x.lefttop.x + x.size.x);

View File

@ -37,6 +37,8 @@ Vec2 vec2_Add(Vec2 x, Vec2 y);
Vec2 vec2_Minus(Vec2 pos, Vec2 neg); Vec2 vec2_Minus(Vec2 pos, Vec2 neg);
Vec2 vec2_Scale(Vec2 v, double scale); Vec2 vec2_Scale(Vec2 v, double scale);
Vec2 vec2_Random(double minX, double maxX, double minY, double maxY);
// A 2d box of double. // A 2d box of double.
typedef struct { typedef struct {

View File

@ -174,3 +174,13 @@ tree_Node *tree_Node_Previous(tree_Node *node) {
return result->father; 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;
}

View File

@ -69,6 +69,9 @@ tree_Node *tree_Node_Next(tree_Node *node);
// Node_Previous returns the previous node. Returns NULL if first. // Node_Previous returns the previous node. Returns NULL if first.
tree_Node *tree_Node_Previous(tree_Node *node); 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 #ifdef __cplusplus
} }