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/player.c | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'src/player.c') 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, -- cgit 1.4.1-2-gfad0