diff options
| author | esquizo <esquizo+noreply@esquizo.net> | 2026-05-04 07:29:36 -0300 |
|---|---|---|
| committer | esquizo <esquizo+noreply@esquizo.net> | 2026-05-04 07:29:36 -0300 |
| commit | 38287a5a739ed80857c97bba8e193a765d974529 (patch) | |
| tree | 40d6ed6beb24dca1d104eda0db56fe9eacb7f8fc | |
| parent | 140b6d591f1d0d73b4781a9fe7f1c03e70cad66e (diff) | |
sistema de vida
| -rw-r--r-- | dat.h | 2 | ||||
| -rw-r--r-- | game.c | 73 | ||||
| -rw-r--r-- | sdl3_main.c | 41 |
3 files changed, 67 insertions, 49 deletions
@@ -18,6 +18,8 @@ typedef struct { bool active; Body body; float time; + float life; + bool alive; } Entity; typedef struct { @@ -9,12 +9,14 @@ #define SPEED 250 static EntityCollisionProc bulletcollproc; +static EntityCollisionProc ghostcollproc; static EntityUpdateProc playerproc; static EntityUpdateProc ghostproc; static EntityUpdateProc bulletproc; static EntityCollisionProc *collisionProcs[LASTEntity] = { [EBULLET] = bulletcollproc, + [EGHOST] = ghostcollproc, }; static EntityUpdateProc *updateprocs[LASTEntity] = { @@ -37,6 +39,8 @@ entinit() player->body.pos[1] = player_x; player->body.size[0] = ENTITY_SIZE; player->body.size[1] = ENTITY_SIZE; + player->life = 1; + player->alive = true; for(int i = 0; i < ghostcount; i++) { Entity *e = allocentity(); @@ -45,6 +49,8 @@ entinit() e->body.pos[1] = ghostpositions[i][1] * ENTITY_SIZE * 2; e->body.size[0] = ENTITY_SIZE; e->body.size[1] = ENTITY_SIZE; + e->life = 2; + e->alive = true; } } @@ -76,9 +82,25 @@ entupdate(float delta) void bulletcollproc(Entity *self, Entity *target) { - if(target->type == EGHOST) { + if(target->type == EGHOST && target->alive) { + target->life -= 1; + if(target->life <= 0) + target->alive = false; + self->active = false; + } +} + +void +ghostcollproc(Entity *self, Entity *target) +{ + if(!self->alive) + return; + + if(target->type == EPLAYER && target->alive) { + target->life -= 1; + if(target->life <= 0) + target->alive = false; self->active = false; - target->active = false; } } @@ -90,6 +112,9 @@ playerproc(Entity *e, float delta) e->body.vel[0] = 0; e->body.vel[1] = 0; + if(!e->alive) + return; + if(moveup) e->body.vel[1] -= SPEED; if(moveleft) @@ -132,33 +157,35 @@ ghostproc(Entity *e, float delta) vx = 0; vy = 0; - float dx = player->body.pos[0] - e->body.pos[0]; - float dy = player->body.pos[1] - e->body.pos[1]; - float dd = sqrtf(dx * dx + dy * dy); + if(e->alive) { + float dx = player->body.pos[0] - e->body.pos[0]; + float dy = player->body.pos[1] - e->body.pos[1]; + float dd = sqrtf(dx * dx + dy * dy); - if(dd < 400.0) { - dx /= dd; - dy /= dd; + if(dd < 400.0) { + dx /= dd; + dy /= dd; - vx += dx * SPEED; - vy += dy * SPEED; - } + vx += dx * SPEED; + vy += dy * SPEED; + } - for(int j = 0; j < 1024; j++) { - other = entitybuffer + j; - if(!other->active || other == e || other->type != EGHOST) - continue; + for(int j = 0; j < 1024; j++) { + other = entitybuffer + j; + if(!other->active || other == e || other->type != EGHOST || !other->alive) + continue; - float dx = other->body.pos[0] - e->body.pos[0]; - float dy = other->body.pos[1] - e->body.pos[1]; - float dd = sqrtf(dx * dx + dy * dy); + float dx = other->body.pos[0] - e->body.pos[0]; + float dy = other->body.pos[1] - e->body.pos[1]; + float dd = sqrtf(dx * dx + dy * dy); - dx /= dd; - dy /= dd; + dx /= dd; + dy /= dd; - if(dd < 40) { - vx -= dx * SPEED * 2; - vy -= dy * SPEED * 2; + if(dd < 40) { + vx -= dx * SPEED * 2; + vy -= dy * SPEED * 2; + } } } diff --git a/sdl3_main.c b/sdl3_main.c index 701dfe4..93df535 100644 --- a/sdl3_main.c +++ b/sdl3_main.c @@ -15,6 +15,7 @@ static bool running; static Uint64 old_ticks, new_ticks; static float camera_x, camera_y; +static float camera_vx, camera_vy; bool shot; bool moveup, movedown, moveleft, moveright; @@ -58,30 +59,6 @@ main() void render(void) { - { - float plx, ply, minx, miny, maxx, maxy; - - plx = player->body.pos[0] - camera_x; - ply = player->body.pos[1] - camera_y; - - minx = plx - 80; - miny = ply - 80; - maxx = plx + 80; - maxy = ply + 80; - - if(minx < 0) - camera_x += minx; - - if(miny < 0) - camera_y += miny; - - if(maxx > 800) - camera_x += (maxx - 800); - - if(maxy > 600) - camera_y += (maxy - 600); - } - SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); SDL_RenderClear(renderer); @@ -106,7 +83,10 @@ render(void) switch (e->type) { case EPLAYER: - SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); + if(e->alive) + SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); + else + SDL_SetRenderDrawColor(renderer, 0x88, 0x00, 0x00, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ .x = e->body.pos[0] - ENTITY_SIZE - camera_x, .y = e->body.pos[1] - ENTITY_SIZE - camera_y, @@ -116,7 +96,10 @@ render(void) break; case EGHOST: - SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF); + if(e->alive) + SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF); + else + SDL_SetRenderDrawColor(renderer, 0x88, 0x00, 0x00, 0xFF); SDL_RenderRect(renderer, &(SDL_FRect){ .x = e->body.pos[0] - ENTITY_SIZE - camera_x, .y = e->body.pos[1] - ENTITY_SIZE - camera_y, @@ -143,6 +126,12 @@ update(float delta) { entphysics(delta); entupdate(delta); + + camera_vx = ((player->body.pos[0] - 400) - camera_x) * 2.0; + camera_vy = ((player->body.pos[1] - 300) - camera_y) * 2.0; + + camera_x += delta * camera_vx; + camera_y += delta * camera_vy; } void |
