From 7fa2669277220041e53637a405ebba644450e4b7 Mon Sep 17 00:00:00 2001 From: kdx Date: Mon, 27 Mar 2023 07:54:09 +0200 Subject: particule example --- src/entity.h | 5 +++++ src/game.h | 2 +- src/part.c | 29 +++++++++++++++++++++++++++++ src/player.c | 8 +++++--- 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/part.c diff --git a/src/entity.h b/src/entity.h index c444be8..45a1a9c 100644 --- a/src/entity.h +++ b/src/entity.h @@ -15,6 +15,11 @@ struct Entity { int width; int height; bool ignore_solids; + union { + struct { + int life; + } part; + }; }; unsigned int entity_type(const char *typename); diff --git a/src/game.h b/src/game.h index 6883f76..35da7dc 100644 --- a/src/game.h +++ b/src/game.h @@ -1,7 +1,7 @@ #pragma once #include "entity.h" -enum { MAX_ENTITIES = 128 }; +enum { MAX_ENTITIES = 256 }; typedef struct Game { unsigned long uuid; diff --git a/src/part.c b/src/part.c new file mode 100644 index 0000000..c2d684d --- /dev/null +++ b/src/part.c @@ -0,0 +1,29 @@ +#include "entityimpl.h" +#include + +IMPL_INIT(part) { + this->width = this->height = 1 + rand() % 2; + this->ignore_solids = true; + this->part.life = 30 + rand() % 10; + this->vel[0] = (float)(rand() % 1024) / 1023.0; + this->vel[1] = (float)(rand() % 1024) / 1023.0; + this->vel[0] *= 1 - 2 * (rand() % 2); + this->vel[1] *= 1 - 2 * (rand() % 3); +} + +IMPL(update) { + this->part.life -= 1; + if (this->part.life <= 0) { + this->type = 0; + return; + } + this->vel[1] += 0.1; + + entity_move(this, g); +} + +IMPL(draw) { + TZR_DrawSetColor(1, 1, 1, 1); + TZR_DrawRectangle(true, this->pos[0], this->pos[1], + this->width, this->height); +} diff --git a/src/player.c b/src/player.c index 1871944..847202b 100644 --- a/src/player.c +++ b/src/player.c @@ -1,16 +1,18 @@ #include "entityimpl.h" IMPL(draw) { - TZR_DrawSetColor(1, 1, 1, 1); + TZR_DrawSetColor(1, 1, 1, 0.5); TZR_DrawRectangle(true, this->pos[0] - this->width / 2, this->pos[1] - this->height / 2, this->width, this->height); } IMPL(update) { - this->vel[0] = TZR_IsKeyDown(SDL_SCANCODE_RIGHT) - - TZR_IsKeyDown(SDL_SCANCODE_LEFT); + this->vel[0] = 2 * TZR_IsKeyDown(SDL_SCANCODE_RIGHT) + - 2 * TZR_IsKeyDown(SDL_SCANCODE_LEFT); entity_move(this, g); + entity_init(game_create_entity(g), entity_type("part"), this->pos[0], + this->pos[1], 0, 0); } IMPL_INIT(player) { -- cgit v1.2.3