summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-03-17 11:06:37 +0100
committerkdx <kikoodx@paranoici.org>2023-03-17 11:06:37 +0100
commit19983764a79acfa3f74a730a90e21cdb9813309c (patch)
tree97bbb30a47c2f5f3d78c03cf406f73e6d6745249
parent79364a7de0bd0cf7d31d1ba8a5442099ccb7b1a7 (diff)
downloadhyperultra-19983764a79acfa3f74a730a90e21cdb9813309c.tar.gz
collide and move
-rw-r--r--src/entity.c44
-rw-r--r--src/entity.h3
-rw-r--r--src/player.c7
3 files changed, 52 insertions, 2 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;
+ }
+ }
+}
diff --git a/src/entity.h b/src/entity.h
index 9f5861c..a1594e7 100644
--- a/src/entity.h
+++ b/src/entity.h
@@ -26,3 +26,6 @@ struct Entity {
Player player;
};
};
+
+bool entity_collide(Entity *this, struct Game *g, int ox, int oy);
+void entity_move(Entity *this, struct Game *g);
diff --git a/src/player.c b/src/player.c
index 49dddde..9778746 100644
--- a/src/player.c
+++ b/src/player.c
@@ -7,8 +7,9 @@
static void
player_update(Entity *this, Game *g)
{
- (void)g;
- this->pos[1] = this->pos[0] += 1;
+ this->vel[0] = 1.2;
+ this->vel[1] += 0.1;
+ entity_move(this, g);
}
static void
@@ -25,6 +26,8 @@ void
player_init(Entity *this)
{
memset(this, 0, sizeof(*this));
+ this->pos[0] = 16;
+ this->pos[1] = 16;
this->type = ET_PLAYER;
this->update = player_update;
this->draw = player_draw;