diff options
author | kdx <kikoodx@paranoici.org> | 2023-03-17 11:06:37 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-03-17 11:06:37 +0100 |
commit | 19983764a79acfa3f74a730a90e21cdb9813309c (patch) | |
tree | 97bbb30a47c2f5f3d78c03cf406f73e6d6745249 /src/entity.c | |
parent | 79364a7de0bd0cf7d31d1ba8a5442099ccb7b1a7 (diff) | |
download | hyperultra-19983764a79acfa3f74a730a90e21cdb9813309c.tar.gz |
collide and move
Diffstat (limited to 'src/entity.c')
-rw-r--r-- | src/entity.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/entity.c b/src/entity.c new file mode 100644 index 0000000..da7662e --- /dev/null +++ b/src/entity.c @@ -0,0 +1,44 @@ +#include "entity.h" +#include "game.h" +#include "cfg.h" + +bool +entity_collide(Entity *this, Game *g, int ox, int oy) +{ + (void)g; + const int x0 = this->pos[0] - this->width / 2 + ox; + const int y0 = this->pos[1] - this->height / 2 + oy; + const int x1 = x0 + this->width - 1; + const int y1 = y0 + this->height - 1; + return (x0 < 0 || y0 < 0 || x1 >= DISPLAY_WIDTH || y1 >= DISPLAY_HEIGHT); +} + +void +entity_move(Entity *this, Game *g) +{ + if (entity_collide(this, g, 0, 0)) { + this->vel[0] = 0.0; + this->vel[1] = 0.0; + this->rem[0] = 0.0; + this->rem[1] = 0.0; + return; + } + for (int a = 0; a < 2; a++) { + const double sum = this->vel[a] + this->rem[a]; + int spd = (int)sum; + this->rem[a] = sum - spd; + const int sign = (spd > 0) - (spd < 0); + if (sign == 0) + continue; + while (spd != 0) { + this->pos[a] += sign; + if (entity_collide(this, g, 0, 0)) { + this->pos[a] -= sign; + this->rem[a] = 0.0; + this->vel[a] = 0.0; + break; + } + spd -= sign; + } + } +} |