From ee316a07cdfb01e52694edef2cc998e672e2885b Mon Sep 17 00:00:00 2001 From: zlago Date: Sat, 26 Oct 2024 20:21:15 +0200 Subject: redo the damage system --- src/disk.c | 2 +- src/entity.h | 6 +++++- src/flier.c | 23 ++++++++++---------- src/particles.c | 6 ++++++ src/particles.h | 7 ++++++ src/player.c | 66 ++++++++++++++++++++++++++++++++------------------------- src/walker.c | 23 ++++++++++---------- 7 files changed, 80 insertions(+), 53 deletions(-) create mode 100644 src/particles.c create mode 100644 src/particles.h (limited to 'src') diff --git a/src/disk.c b/src/disk.c index a6e3d80..5231953 100644 --- a/src/disk.c +++ b/src/disk.c @@ -64,7 +64,7 @@ static int save_update(struct entity *self) { return 0; } -static int save_hurt(struct entity *self, int damage) { +static int save_hurt(struct entity *self, struct damage damage) { return 0; } diff --git a/src/entity.h b/src/entity.h index 33aca68..038b62a 100644 --- a/src/entity.h +++ b/src/entity.h @@ -28,9 +28,13 @@ enum factions { FACTION_ENEMY, }; +struct damage { + int damage, iframes; +}; + struct entity { int (*update)(struct entity *self); - int (*hurt)(struct entity *self, int damage); + int (*hurt)(struct entity *self, struct damage damage); int (*draw)(struct entity *self, int camx, int camy); void (*free)(struct entity *self); int x, y; // unsigned results in a bunch of weird edge cases diff --git a/src/flier.c b/src/flier.c index f5d33e4..81596d0 100644 --- a/src/flier.c +++ b/src/flier.c @@ -5,6 +5,7 @@ #include #include #include +#include "particles.h" #define SIZE 4 #define ACCELERATION 1 @@ -79,7 +80,7 @@ static int bullet_update(struct projectile *self) { int y = from_fixed(self->y); self->hitbox = (struct hitbox) {.left = x, .right = x, .top = y, .bottom = y}; if (hitbox_overlap(self->hitbox, entities.player[0].hitbox)) { - entities.player[0].hurt(entities.player + 0, 1); + entities.player[0].hurt(entities.player + 0, (struct damage) {1, 60}); return 1; } if (self->hp == 0) { @@ -90,14 +91,14 @@ static int bullet_update(struct projectile *self) { } static int bullet_draw(struct projectile *self, int camX, int camY) { - SDL_Rect rect; + SDL_Rect const *rect; if (self->hp & 0x2) { - rect = (SDL_Rect) {4, 0, 4, 4}; + rect = &particle_red; } else { - rect = (SDL_Rect) {12, 0, 4, 4}; + rect = &particle_white; } - SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 1, from_fixed(self->y) - camY - 1, 4, 4}); - SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x - self->velocity.x) - camX - 1, from_fixed(self->y - self->velocity.y) - camY - 1, 4, 4}); + SDL_RenderCopy(renderer, self->texture, rect, &(SDL_Rect) {from_fixed(self->x) - camX - 1, from_fixed(self->y) - camY - 1, 4, 4}); + SDL_RenderCopy(renderer, self->texture, rect, &(SDL_Rect) {from_fixed(self->x - self->velocity.x) - camX - 1, from_fixed(self->y - self->velocity.y) - camY - 1, 4, 4}); return 0; } @@ -177,7 +178,7 @@ static void move(struct entity *self, signed direction_x, signed direction_y, bo } if (collision_hazard(cx | cy)) { - self->hurt(self, 1); + self->hurt(self, (struct damage) {1, 60}); } self->hitbox.left = from_fixed(self->x) - SIZE / 2; @@ -357,17 +358,17 @@ static int flier_init(struct entity *self) { return flier_update(self); } -static int flier_hurt(struct entity *self, int damage) { +static int flier_hurt(struct entity *self, struct damage damage) { if (self->iframes == 0) { - self->hp -= damage; - self->iframes = 60; + self->hp -= damage.damage; + self->iframes = damage.iframes; for (int x = -1; x <= 1; x += 2) { for (int y = -1; y <= 1; y += 2) { struct particle *part = entities.particle + entities.particles; part->x = self->x; part->y = self->y; part->velocity = (struct vec2) {to_fixed(x), to_fixed(y) - 8}; - part->rect = (SDL_Rect) {4, 0, 4, 4}; + part->rect = particle_red; part->acceleration = (struct vec2) {0, 1}; part->hp = 30; entities.particles++; diff --git a/src/particles.c b/src/particles.c new file mode 100644 index 0000000..3655f23 --- /dev/null +++ b/src/particles.c @@ -0,0 +1,6 @@ +#include + +SDL_Rect const particle_gray = {0, 0, 4, 4}; +SDL_Rect const particle_red = {4, 0, 4, 4}; +SDL_Rect const particle_orange = {8, 0, 4, 4}; +SDL_Rect const particle_white = {12, 0, 4, 4}; diff --git a/src/particles.h b/src/particles.h new file mode 100644 index 0000000..c841817 --- /dev/null +++ b/src/particles.h @@ -0,0 +1,7 @@ +#pragma once +#include + +extern SDL_Rect const particle_gray; +extern SDL_Rect const particle_red; +extern SDL_Rect const particle_orange; +extern SDL_Rect const particle_white; diff --git a/src/player.c b/src/player.c index 4844b96..e7dfd98 100644 --- a/src/player.c +++ b/src/player.c @@ -5,6 +5,7 @@ #include "tilemap.h" #include #include +#include "particles.h" #define SIZE 4 #define ACCELERATION 1 @@ -51,6 +52,12 @@ struct anim player_anims[] = { {PLAYER_A_FALL2, {48, 0, 16, 16}, 15}, }; +enum slash_type { + SLASH_NORMAL, + SLASH_STRONG, + SLASH_WEAK, +}; + static int slash_update(struct projectile *self) { self->x += self->velocity.x; self->y += self->velocity.y; @@ -74,14 +81,14 @@ static int slash_update(struct projectile *self) { continue; } if (hitbox_overlap(self->hitbox, entities.enemy[i].hitbox)) { - entities.enemy[i].hurt(entities.enemy + i, 1); + entities.enemy[i].hurt(entities.enemy + i, (struct damage) {self->ext == SLASH_STRONG? 2: 1, self->ext == SLASH_NORMAL? 10: 60}); } } struct particle *part = entities.particle + entities.particles; part->x = to_fixed(x - 1); part->y = to_fixed(y - 1); part->velocity = (struct vec2) {self->velocity.x + x - xx, self->velocity.y + y - yy}; - part->rect = (SDL_Rect) {8, 0, 4, 4}; + part->rect = particle_orange; part->acceleration = (struct vec2) {0, 0}; part->hp = 10 - (self->hp >= 0? self->hp: -self->hp - 8); entities.particles++; @@ -179,7 +186,7 @@ static int move(struct entity *self) { } if (collision_hazard(cx | cy)) { - self->hurt(self, 1); + self->hurt(self, (struct damage) {1, 60}); } self->hitbox.left = from_fixed(self->x) - SIZE / 2; @@ -189,7 +196,7 @@ static int move(struct entity *self) { return on_ground; } -static void attack(struct entity *self, int a) { +static void attack(struct entity *self, int a, enum slash_type type) { if (!input_s(input_pressed)) { return; } @@ -209,7 +216,7 @@ static void attack(struct entity *self, int a) { .faction = FACTION_PLAYER, .iframes = 0, .texture = NULL, - .ext = NULL, + .ext = (void *) type, }; //anim(entities->projectile + entities->projectiles, ENEMY_A_IDLE); //entities->projectile[entities->projectiles].texture = res_get_texture("fywi").data; @@ -244,7 +251,7 @@ static int player_update(struct entity *self) { part->x = self->x - to_fixed(-3) - to_fixed(1); part->y = self->y - 2; part->velocity = (struct vec2) {12, -8}; - part->rect = (SDL_Rect) {0, 0, 4, 4}; + part->rect = particle_gray; part->acceleration = (struct vec2) {0, 1}; part->hp = 5; entities.particles++; @@ -252,12 +259,12 @@ static int player_update(struct entity *self) { part->x = self->x - to_fixed(3) - to_fixed(1); part->y = self->y - 2; part->velocity = (struct vec2) {-12, -8}; - part->rect = (SDL_Rect) {0, 0, 4, 4}; + part->rect = particle_gray; part->acceleration = (struct vec2) {0, 1}; part->hp = 5; entities.particles++; } - attack(self, 1); + attack(self, 1, SLASH_NORMAL); if (input_s(input_pressed)) { self->state = PLAYER_ATTACK1; self->timer = 30; @@ -279,7 +286,7 @@ static int player_update(struct entity *self) { anim(self, PLAYER_A_JUMP); self->state = PLAYER_JUMP; } - attack(self, 0); + attack(self, 0, SLASH_NORMAL); if (input_s(input_pressed)) { self->state = PLAYER_ATTACK2; self->timer = 30; @@ -304,7 +311,7 @@ static int player_update(struct entity *self) { anim(self, PLAYER_A_JUMP); self->state = PLAYER_JUMP; } - attack(self, 1); + attack(self, 1, SLASH_STRONG); if (input_s(input_pressed)) { self->state = PLAYER_ATTACK3; self->timer = 60; @@ -339,7 +346,7 @@ static int player_update(struct entity *self) { part->x = self->x - to_fixed(self->facing) * 3 - to_fixed(1); part->y = self->y - 2; part->velocity = (struct vec2) {-self->velocity.x, -8}; - part->rect = (SDL_Rect) {0, 0, 4, 4}; + part->rect = particle_gray; part->acceleration = (struct vec2) {0, 1}; part->hp = 5; entities.particles++; @@ -358,7 +365,7 @@ static int player_update(struct entity *self) { anim(self, PLAYER_A_JUMP); self->state = PLAYER_JUMP; } - attack(self, 1); + attack(self, 1, SLASH_WEAK); break; case PLAYER_JUMP: @@ -387,7 +394,7 @@ static int player_update(struct entity *self) { part->x = self->x; part->y = self->y; part->velocity = (struct vec2) {x * 5, -yy / 4 - 8}; - part->rect = (SDL_Rect) {0, 0, 4, 4}; + part->rect = particle_gray; part->acceleration = (struct vec2) {0, 1}; part->hp = yy / 2; entities.particles++; @@ -405,24 +412,25 @@ static int player_update(struct entity *self) { return 0; } -static int player_hurt(struct entity *self, int damage) { - if (self->iframes == 0) { - self->hp -= damage; - self->iframes = 60; - for (int x = -1; x <= 1; x += 2) { - for (int y = -1; y <= 1; y += 2) { - struct particle *part = entities.particle + entities.particles; - part->x = self->x; - part->y = self->y; - part->velocity = (struct vec2) {to_fixed(x), to_fixed(y) - 8}; - part->rect = (SDL_Rect) {4, 0, 4, 4}; - part->acceleration = (struct vec2) {0, 1}; - part->hp = 30; - entities.particles++; - } +static int player_hurt(struct entity *self, struct damage damage) { + if (self->iframes > 0) { + return 0; + } + self->hp -= damage.damage; + self->iframes = damage.iframes; + for (int x = -1; x <= 1; x += 2) { + for (int y = -1; y <= 1; y += 2) { + struct particle *part = entities.particle + entities.particles; + part->x = self->x; + part->y = self->y; + part->velocity = (struct vec2) {to_fixed(x), to_fixed(y) - 8}; + part->rect = particle_red; + part->acceleration = (struct vec2) {0, 1}; + part->hp = 30; + entities.particles++; } } - return 0; + return 1; } static int player_draw(struct entity *self, int camX, int camY) { diff --git a/src/walker.c b/src/walker.c index 49595ae..6441909 100644 --- a/src/walker.c +++ b/src/walker.c @@ -4,6 +4,7 @@ #include "tilemap.h" #include #include +#include "particles.h" #define SIZE 4 #define ACCELERATION 1 @@ -83,7 +84,7 @@ static int bullet_update(struct projectile *self) { int y = from_fixed(self->y); self->hitbox = (struct hitbox) {.left = x, .right = x, .top = y, .bottom = y}; if (hitbox_overlap(self->hitbox, entities.player[0].hitbox)) { - entities.player[0].hurt(entities.player + 0, 1); + entities.player[0].hurt(entities.player + 0, (struct damage) {1, 60}); return 1; } if (self->hp == 0) { @@ -94,14 +95,14 @@ static int bullet_update(struct projectile *self) { } static int bullet_draw(struct projectile *self, int camX, int camY) { - SDL_Rect rect; + SDL_Rect const *rect; if (self->hp & 0x2) { - rect = (SDL_Rect) {4, 0, 4, 4}; + rect = &particle_red; } else { - rect = (SDL_Rect) {12, 0, 4, 4}; + rect = &particle_white; } - SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 1, from_fixed(self->y) - camY - 1, 4, 4}); - SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x - self->velocity.x) - camX - 1, from_fixed(self->y - self->velocity.y) - camY - 1, 4, 4}); + SDL_RenderCopy(renderer, self->texture, rect, &(SDL_Rect) {from_fixed(self->x) - camX - 1, from_fixed(self->y) - camY - 1, 4, 4}); + SDL_RenderCopy(renderer, self->texture, rect, &(SDL_Rect) {from_fixed(self->x - self->velocity.x) - camX - 1, from_fixed(self->y - self->velocity.y) - camY - 1, 4, 4}); //SDL_RenderFillRect(renderer, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16}); return 0; } @@ -168,7 +169,7 @@ static int move(struct entity *self, signed direction) { } if (collision_hazard(cx | cy)) { - self->hurt(self, 1); + self->hurt(self, (struct damage) {1, 60}); } self->hitbox.left = from_fixed(self->x) - SIZE / 2; @@ -359,17 +360,17 @@ static int walker_update(struct entity *self) { return 0; } -static int walker_hurt(struct entity *self, int damage) { +static int walker_hurt(struct entity *self, struct damage damage) { if (self->iframes == 0) { - self->hp -= damage; - self->iframes = 60; + self->hp -= damage.damage; + self->iframes = damage.iframes; for (int x = -1; x <= 1; x += 2) { for (int y = -1; y <= 1; y += 2) { struct particle *part = entities.particle + entities.particles; part->x = self->x; part->y = self->y; part->velocity = (struct vec2) {to_fixed(x), to_fixed(y) - 8}; - part->rect = (SDL_Rect) {4, 0, 4, 4}; + part->rect = particle_red; part->acceleration = (struct vec2) {0, 1}; part->hp = 30; entities.particles++; -- cgit 1.4.1-2-gfad0