diff options
author | zlago | 2024-10-09 06:09:43 +0200 |
---|---|---|
committer | zlago | 2024-10-09 06:09:43 +0200 |
commit | 753cdae8c9656f2f5c8380868dca1c5e9be9b437 (patch) | |
tree | fbf799f78e4c36ac2e3daad77e7a48087f1114d5 | |
parent | c851581465b9bae8ac679415368d792cefd76de2 (diff) |
work on combat
-rw-r--r-- | src/entity.h | 23 | ||||
-rw-r--r-- | src/player.c | 49 | ||||
-rw-r--r-- | src/res/particles.ase | bin | 288 -> 317 bytes |
3 files changed, 49 insertions, 23 deletions
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 <SDL2/SDL.h> -#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 --- a/src/res/particles.ase +++ b/src/res/particles.ase Binary files differ |