summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-10 02:08:41 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-10 02:21:48 (EDT)
commit3e619979dafa64c6b2576420ff1ce59462256d5e (patch)
tree27249a38387828b922dad3b88a8ee55f1949ec83
parent85494ad6865c9087b0d0f5a547b17042e7122662 (diff)
downloadmazefight-3e619979dafa64c6b2576420ff1ce59462256d5e.zip
mazefight-3e619979dafa64c6b2576420ff1ce59462256d5e.tar.gz
mazefight-3e619979dafa64c6b2576420ff1ce59462256d5e.tar.bz2
char: Add collision detection/handling
-rw-r--r--src/char.h3
-rw-r--r--src/char/char.c18
-rw-r--r--src/char/char.h2
-rw-r--r--src/char/enemy.c14
-rw-r--r--src/char/player.c5
-rw-r--r--src/game.c7
6 files changed, 49 insertions, 0 deletions
diff --git a/src/char.h b/src/char.h
index 3e0e0ab..2c31ca2 100644
--- a/src/char.h
+++ b/src/char.h
@@ -39,6 +39,9 @@ int
mf_char_update(struct mf_char *c);
int
+mf_char_collision(struct mf_char *c1, struct mf_char *c2, int handle);
+
+int
mf_char_render(struct mf_char *c, SDL_Renderer *renderer);
void
diff --git a/src/char/char.c b/src/char/char.c
index 0ab7145..4a4a4e7 100644
--- a/src/char/char.c
+++ b/src/char/char.c
@@ -126,6 +126,24 @@ mf_char_update(struct mf_char *c)
}
int
+mf_char_collision(struct mf_char *c1, struct mf_char *c2, int handle)
+{
+ if (c1->cur_x == c2->cur_x && c1->cur_y == c2->cur_y) goto hit;
+ if (c1->cur_x == c2->new_x && c1->cur_y == c2->new_y) goto hit;
+ if (c1->new_x == c2->cur_x && c1->new_y == c2->cur_y) goto hit;
+ if (c1->new_x == c2->new_x && c1->new_y == c2->new_y) goto hit;
+
+ return 0;
+
+ hit:
+ if (handle == SDL_TRUE) {
+ c1->collide(c1);
+ c2->collide(c2);
+ }
+ return 1;
+}
+
+int
mf_char_render(struct mf_char *c, SDL_Renderer *renderer)
{
int e = 0;
diff --git a/src/char/char.h b/src/char/char.h
index f5054d5..02bddaa 100644
--- a/src/char/char.h
+++ b/src/char/char.h
@@ -54,6 +54,7 @@ struct mf_char {
int (*update)(struct mf_char *);
int (*step)(struct mf_char *);
int (*turn)(struct mf_char *);
+ void (*collide)(struct mf_char *);
int (*render)(struct mf_char *, SDL_Renderer *);
void (*destroy)(struct mf_char *);
};
@@ -70,6 +71,7 @@ mf_char_new(size_t size);
c->update = &_mf_##name##_update; \
c->step = &_mf_##name##_step; \
c->turn = &_mf_##name##_turn; \
+ c->collide = &_mf_##name##_collide; \
c->render = &_mf_##name##_render; \
c->destroy = &_mf_##name##_destroy; \
t_c = (struct mf_##name *) c; \
diff --git a/src/char/enemy.c b/src/char/enemy.c
index 82cc16b..fb259a2 100644
--- a/src/char/enemy.c
+++ b/src/char/enemy.c
@@ -97,6 +97,20 @@ _mf_enemy_turn(struct mf_char *c)
return 0;
}
+static void
+_mf_enemy_collide(struct mf_char *c)
+{
+ /* Don't go forward unless necessary */
+ switch (c->cur_dir) {
+ case MF_CHAR_DIR_U_: c->cur_dir = MF_CHAR_DIR_D_; break;
+ case MF_CHAR_DIR_D_: c->cur_dir = MF_CHAR_DIR_U_; break;
+ case MF_CHAR_DIR_L_: c->cur_dir = MF_CHAR_DIR_R_; break;
+ case MF_CHAR_DIR_R_: c->cur_dir = MF_CHAR_DIR_L_; break;
+ default: c->cur_dir = MF_CHAR_DIR_N_; break;
+ }
+ _mf_enemy_step(c);
+}
+
static int
_mf_enemy_render(struct mf_char *c __attribute__((__unused__)),
SDL_Renderer *renderer __attribute__((__unused__)))
diff --git a/src/char/player.c b/src/char/player.c
index eff0dbd..fdeac5c 100644
--- a/src/char/player.c
+++ b/src/char/player.c
@@ -74,6 +74,11 @@ _mf_player_render(struct mf_char *c __attribute__((__unused__)),
}
static void
+_mf_player_collide(struct mf_char *c __attribute__((__unused__)))
+{
+}
+
+static void
_mf_player_destroy(struct mf_char *c __attribute__((__unused__)))
{
}
diff --git a/src/game.c b/src/game.c
index 5daea66..0c94202 100644
--- a/src/game.c
+++ b/src/game.c
@@ -156,6 +156,7 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
int fr;
int won;
Uint32 beg;
+ int j;
int secs;
Uint32 end;
Uint32 delay;
@@ -246,6 +247,12 @@ mf_game(long seed, int size, int fow, int reveal, int enemies,
for (i = 0; i < 1 + enemies; ++i) {
mf_char_update(chars[i]);
}
+ for (i = 1; i < 1 + enemies; ++i) {
+ for (j = i + 1; j < 1 + enemies; ++j) {
+ mf_char_collision(chars[i], chars[j],
+ SDL_TRUE);
+ }
+ }
mf_char_get_vector(chars[0], &game.player_cx,
&game.player_cy, &game.player_travel,
&game.player_dx, &game.player_dy);