hyperultra/src/entity.c

47 lines
1.0 KiB
C

#include "entity.h"
#include "game.h"
#include "map.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 (map_get_px(x0, y0) == 1 || map_get_px(x0, y1) == 1 ||
map_get_px(x1, y0) == 1 || map_get_px(x1, y1) == 1);
}
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;
}
}
}