summaryrefslogtreecommitdiff
path: root/game.c
diff options
context:
space:
mode:
authoresquizo <esquizo+noreply@esquizo.net>2026-05-04 07:29:36 -0300
committeresquizo <esquizo+noreply@esquizo.net>2026-05-04 07:29:36 -0300
commit38287a5a739ed80857c97bba8e193a765d974529 (patch)
tree40d6ed6beb24dca1d104eda0db56fe9eacb7f8fc /game.c
parent140b6d591f1d0d73b4781a9fe7f1c03e70cad66e (diff)
sistema de vida
Diffstat (limited to 'game.c')
-rw-r--r--game.c73
1 files changed, 50 insertions, 23 deletions
diff --git a/game.c b/game.c
index b41c501..cc96428 100644
--- a/game.c
+++ b/game.c
@@ -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;
+ }
}
}