From 753cdae8c9656f2f5c8380868dca1c5e9be9b437 Mon Sep 17 00:00:00 2001 From: zlago Date: Wed, 9 Oct 2024 06:09:43 +0200 Subject: work on combat --- src/entity.h | 23 +++++++++++++++++------ src/player.c | 49 ++++++++++++++++++++++++++++++++----------------- src/res/particles.ase | Bin 288 -> 317 bytes 3 files changed, 49 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/entity.h b/src/entity.h index 0166bdc..72fccb2 100644 --- a/src/entity.h +++ b/src/entity.h @@ -2,23 +2,32 @@ #include -#define from_fixed(a) (a / 16) -#define to_fixed(a) (a * 16) - -struct vec2 { - signed x, y; -}; +#define from_fixed(a) ((a) / 16) +#define to_fixed(a) ((a) * 16) struct hitbox { unsigned left, right, top, bottom; }; +static inline int hitbox_overlap(struct hitbox const a, struct hitbox const b) { + return a.left <= b.right && a.right >= b.left && a.top <= b.bottom && a.bottom >= b.top; +} + +struct vec2 { + signed x, y; +}; + struct anim { unsigned frame; SDL_Rect rect; unsigned length; }; +enum factions { + FACTION_PLAYER, + FACTION_ENEMY, +}; + struct entity { int (*update)(struct entity *self); int (*hurt)(struct entity *self, int damage); @@ -33,6 +42,7 @@ struct entity { int timer; int iframes; signed facing; + enum factions faction; void *texture; void *ext; }; @@ -60,6 +70,7 @@ struct projectile { int timer; int iframes; signed facing; + enum factions faction; void *texture; void *ext; }; diff --git a/src/player.c b/src/player.c index f36ae1a..72aedac 100644 --- a/src/player.c +++ b/src/player.c @@ -52,19 +52,32 @@ static int slash_update(struct projectile *self) { self->x += self->velocity.x; self->y += self->velocity.y; self->hp--; - int x = from_fixed(self->x) + self->facing * sin(self->hp + 0) * SLASH_REACH_X; - int y = from_fixed(self->y) - cos(self->hp + 0) * SLASH_REACH_Y; - self->hitbox = (struct hitbox) {.left = x, .right = x, .top = y, .bottom = y}; - #if 0 - struct particle *part = entities.particle + entities.particles; - part->x = self->x + to_fixed(self->facing >= 0? sin(self->hp): -sin(self->hp)) * 6; - part->y = self->y - to_fixed(cos(self->hp)) * 4; - part->velocity = (struct vec2) {0, 0}; - part->rect = (SDL_Rect) {0, 0, 4, 4}; - part->acceleration = (struct vec2) {0, 0}; - part->hp = 5; - entities.particles++; - #endif + int x = from_fixed(self->x) + self->facing * sin(self->hp / 2) * SLASH_REACH_X; + int y = from_fixed(self->y) - cos(self->hp / 2) * SLASH_REACH_Y; + int xx = from_fixed(self->x) + self->facing * sin(self->hp / 2 + 1) * SLASH_REACH_X; + int yy = from_fixed(self->y) - cos(self->hp / 2 + 1) * SLASH_REACH_Y; + self->hitbox = (struct hitbox) {.left = x - 2, .right = x + 2, .top = y - 2, .bottom = y + 2}; + for (int i = 0, e = entities.enemies; i < 64 && e; i++) { + if (entities.enemy[i].state) { + e--; + } else { + continue; + } + if (entities.enemy[i].faction == self->faction) { + continue; + } + if (hitbox_overlap(self->hitbox, entities.enemy[i].hitbox)) { + entities.enemy[i].hurt(entities.enemy + i, 1); + } + } + 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->acceleration = (struct vec2) {0, 0}; + part->hp = 10 - self->hp; + entities.particles++; if (self->hp == 0) { return 1; self->state = 0; @@ -78,9 +91,9 @@ static int slash_draw(struct projectile *self, int camX, int camY) { //SDL_RenderFillRect(renderer, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16}); int const x = from_fixed(self->x) - camX, y = from_fixed(self->y) - camY; SDL_Vertex vertices[3] = { - {.position = {x + from_fixed(self->velocity.x) + self->facing * sin(self->hp - 1) * SLASH_REACH_X, y - cos(self->hp - 1) * SLASH_REACH_Y}, .color = {255, 127, 0, 255}}, - {.position = {x + self->facing * sin(self->hp + 0) * SLASH_REACH_X, y - cos(self->hp + 0) * SLASH_REACH_Y}, .color = {255, 127, 0, 255}}, - {.position = {x - from_fixed(self->velocity.x) + self->facing * sin(self->hp + 1) * SLASH_REACH_X, y - cos(self->hp + 1) * SLASH_REACH_Y}, .color = {255, 127, 0, 255}}, + {.position = {x + from_fixed(self->velocity.x) + self->facing * sin(self->hp / 2 - 1) * SLASH_REACH_X, y - cos(self->hp / 2 - 1) * SLASH_REACH_Y}, .color = {255, 191, 63, 255}}, + {.position = {x + self->facing * sin(self->hp / 2 + 0) * SLASH_REACH_X, y - cos(self->hp / 2 + 0) * SLASH_REACH_Y}, .color = {255, 127, 0, 255}}, + {.position = {x - from_fixed(self->velocity.x) + self->facing * sin(self->hp / 2 + 1) * SLASH_REACH_X, y - cos(self->hp / 2 + 1) * SLASH_REACH_Y}, .color = {255, 127, 0, 255}}, }; SDL_RenderGeometry(renderer, NULL, vertices, 3, NULL, 0); return 0; @@ -172,9 +185,10 @@ static void attack(struct entity *self) { 0, 0, 0, 0, }, .state = PLAYER_IDLE, - .hp = 4, + .hp = 8, .timer = 0, .facing = self->facing, + .faction = FACTION_PLAYER, .iframes = 0, .texture = NULL, .ext = NULL, @@ -328,6 +342,7 @@ struct entity *player_new(struct entities *entities) { .hp = 3, .timer = 0, .facing = 1, + .faction = FACTION_PLAYER, .iframes = 0, .texture = NULL, .ext = NULL, diff --git a/src/res/particles.ase b/src/res/particles.ase index 5e1762d..09dbb2e 100644 Binary files a/src/res/particles.ase and b/src/res/particles.ase differ -- cgit 1.4.1-2-gfad0