From fd0760ebf4a5950b62b8149b4e5c55825240c5f1 Mon Sep 17 00:00:00 2001 From: zlago Date: Tue, 1 Oct 2024 21:02:07 +0200 Subject: player damage? --- src/player.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'src/player.c') diff --git a/src/player.c b/src/player.c index ed7dcbb..9efd0ac 100644 --- a/src/player.c +++ b/src/player.c @@ -51,6 +51,7 @@ static int move(struct entity *self) { int on_ground = false; int dx = (input_right(input_now) - input_left(input_now)) * ACCELERATION; self->velocity.x += dx; + // deaccel if (dx == 0) { if (self->velocity.x < -FRICTION) { self->velocity.x += FRICTION; @@ -60,13 +61,16 @@ static int move(struct entity *self) { self->velocity.x = 0; } } + // speed cap if (self->velocity.x > MAX_SPEED) { self->velocity.x = MAX_SPEED; } else if (self->velocity.x < -MAX_SPEED) { self->velocity.x = -MAX_SPEED; } + // x collision self->x += self->velocity.x; - if (collision_solid(collide(self))) { + collision_T const cx = collide(self); + if (collision_solid(cx)) { if (self->velocity.x < 0) { self->x += to_fixed(8) - ((self->x - to_fixed(SIZE / 2)) % to_fixed(8)); // left } else if (self->velocity.x == 0) { @@ -82,9 +86,10 @@ static int move(struct entity *self) { self->facing = +1; } self->velocity.y += GRAVITY; + // y collision self->y += self->velocity.y; - //self->y += (input_down(input_now) - input_up(input_now)) * 8; - if (collision_solid(collide(self))) { + collision_T const cy = collide(self); + if (collision_solid(cy)) { if (self->velocity.y < 0) { self->y += to_fixed(8) - ((self->y - to_fixed(SIZE)) % to_fixed(8)); // up self->velocity.y = 0; @@ -96,10 +101,15 @@ static int move(struct entity *self) { on_ground = true; } } - self->hitbox.left = self->x; - self->hitbox.right = self->x + SIZE; - self->hitbox.top = self->y; - self->hitbox.bottom = self->y + SIZE; + + if (collision_hazard(cx | cy)) { + self->hurt(self, 1); + } + + self->hitbox.left = from_fixed(self->x) - SIZE / 2; + self->hitbox.right = from_fixed(self->x) + SIZE / 2; + self->hitbox.top = from_fixed(self->y) - SIZE; + self->hitbox.bottom = from_fixed(self->y); return on_ground; } @@ -108,6 +118,9 @@ void anim(struct entity *self, unsigned anim) { } static int player_update(struct entity *self) { + if (self->hp <= 0) { + return 1; + } switch (self->state) { case PLAYER_IDLE: if (move(self) == false) { @@ -166,6 +179,9 @@ static int player_update(struct entity *self) { } break; } + if (self->iframes > 0) { + self->iframes--; + } self->anim.length--; if (self->anim.length == 0) { anim(self, self->anim.frame); @@ -174,16 +190,21 @@ static int player_update(struct entity *self) { } static int player_hurt(struct entity *self, int damage) { + if (self->iframes == 0) { + self->hp -= damage; + self->iframes = 60; + } return 0; } static int player_draw(struct entity *self, int camX, int camY) { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_Rect rect = self->anim.rect; - if (self->facing == -1) { - rect.y += 16; + if (!(self->iframes & 0x4)) { + SDL_Rect rect = self->anim.rect; + if (self->facing == -1) { + rect.y += 16; + } + SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16}); } - SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16}); return 0; } -- cgit 1.4.1-2-gfad0