From d005a7756f6d15f67a7033a53bae23d00a58af69 Mon Sep 17 00:00:00 2001 From: zlago Date: Sun, 6 Oct 2024 19:54:24 +0200 Subject: particles and projectiles --- src/player.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 106 insertions(+), 8 deletions(-) (limited to 'src/player.c') diff --git a/src/player.c b/src/player.c index 0f0025e..a6fa86d 100644 --- a/src/player.c +++ b/src/player.c @@ -4,6 +4,7 @@ #include "input.h" #include "tilemap.h" #include +#include #define SIZE 4 #define ACCELERATION 1 @@ -44,13 +45,50 @@ struct anim player_anims[] = { {PLAYER_A_FALL2, {48, 0, 16, 16}, 15}, }; +static int slash_update(struct projectile *self) { + self->x += self->velocity.x; + self->y += self->velocity.y; + self->hp--; + #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 + if (self->hp == 0) { + return 1; + self->state = 0; + } + return 0; +} + +static int slash_draw(struct projectile *self, int camX, int camY) { + //SDL_Rect rect = self->anim.rect; + //SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16}); + //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 >= 0)? sin(self->hp - 1): -sin(self->hp - 1)) * 8, y - cos(self->hp - 1) * 6}, .color = {255, 127, 0, 255}}, + {.position = {x + ((self->facing >= 0)? sin(self->hp + 0): -sin(self->hp + 0)) * 8, y - cos(self->hp + 0) * 6}, .color = {255, 127, 0, 255}}, + {.position = {x - from_fixed(self->velocity.x) + ((self->facing >= 0)? sin(self->hp + 1): -sin(self->hp + 1)) * 8, y - cos(self->hp + 1) * 6}, .color = {255, 127, 0, 255}}, + }; + SDL_RenderGeometry(renderer, NULL, vertices, 3, NULL, 0); + return 0; +} + +static void slash_free(struct projectile *self) {} + static collision_T collide(struct entity *self) { return tilemap_area(tilemap, from_fixed(self->x) - SIZE / 2, from_fixed(self->y) - SIZE, from_fixed(self->x) + SIZE / 2, from_fixed(self->y)); } static int move(struct entity *self) { int on_ground = false; - int dx = (input_right(input_now) - input_left(input_now)) * ACCELERATION; + int dx = (input_right(input_held) - input_left(input_held)) * ACCELERATION; self->velocity.x += dx; // deaccel if (dx == 0) { @@ -114,6 +152,32 @@ static int move(struct entity *self) { return on_ground; } +static void attack(struct entity *self) { + if (!input_s(input_pressed)) { + return; + } + entities.projectile[entities.projectiles] = (struct projectile) { + .update = slash_update, + .draw = slash_draw, + .free = slash_free, + .x = self->x + to_fixed(self->facing) * 4, .y = self->y - to_fixed(3), + .velocity = (struct vec2) {self->velocity.x, 0}, + .hitbox = { + 0, 0, 0, 0, + }, + .state = PLAYER_IDLE, + .hp = 4, + .timer = 0, + .facing = self->facing, + .iframes = 0, + .texture = NULL, + .ext = NULL, + }; + //anim(entities->projectile + entities->projectiles, ENEMY_A_IDLE); + //entities->projectile[entities->projectiles].texture = res_get_texture("fywi").data; + entities.projectiles++; +} + static void anim(struct entity *self, unsigned anim) { self->anim = player_anims[anim]; } @@ -130,36 +194,47 @@ static int player_update(struct entity *self) { anim(self, PLAYER_A_FALL); self->state = PLAYER_FALL; } - if (input_right(input_now) - input_left(input_now)) { + if (input_right(input_held) - input_left(input_held)) { anim(self, PLAYER_A_WALK); self->state = PLAYER_WALK; } - if (input_a(input_now)) { + if (input_a(input_held)) { self->velocity.y = -JUMP; anim(self, PLAYER_A_JUMP); self->state = PLAYER_JUMP; } + attack(self); break; - case PLAYER_WALK: + case PLAYER_WALK:; + struct particle *part = entities.particle + entities.particles; + 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->acceleration = (struct vec2) {0, 1}; + part->hp = 5; + entities.particles++; + if (move(self) == false) { self->velocity.y = 0; anim(self, PLAYER_A_FALL); self->state = PLAYER_FALL; } - if (!(input_right(input_now) - input_left(input_now))) { + if (!(input_right(input_held) - input_left(input_held))) { anim(self, PLAYER_A_IDLE); self->state = PLAYER_IDLE; } - if (input_a(input_now)) { + if (input_a(input_held)) { self->velocity.y = -JUMP; anim(self, PLAYER_A_JUMP); self->state = PLAYER_JUMP; } + attack(self); break; case PLAYER_JUMP: - if (!input_a(input_now)) { + if (!input_a(input_held)) { self->velocity.y /= 2; anim(self, PLAYER_A_FALL); self->state = PLAYER_FALL; @@ -174,10 +249,21 @@ static int player_update(struct entity *self) { } break; - case PLAYER_FALL: + case PLAYER_FALL:; + int yy = self->velocity.y; if (move(self) == true) { anim(self, PLAYER_A_IDLE); self->state = PLAYER_IDLE; + for (int x = -2; x <= 2; x++) { + struct particle *part = entities.particle + entities.particles; + 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->acceleration = (struct vec2) {0, 1}; + part->hp = yy / 2; + entities.particles++; + } } break; } @@ -195,6 +281,18 @@ 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) {0, 0, 4, 4}; + part->acceleration = (struct vec2) {0, 1}; + part->hp = 30; + entities.particles++; + } + } } return 0; } -- cgit 1.4.1-2-gfad0