diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-19 16:33:09 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-19 16:33:09 (EDT) |
commit | e236632ceea7a7cbee7c4139b0bf86710770077b (patch) | |
tree | d055629b89f37f7dd81aa81b9186fce4978048c5 | |
parent | b8d1885498c09893898aad1de1e6098e34091665 (diff) | |
download | mazefight-e236632ceea7a7cbee7c4139b0bf86710770077b.zip mazefight-e236632ceea7a7cbee7c4139b0bf86710770077b.tar.gz mazefight-e236632ceea7a7cbee7c4139b0bf86710770077b.tar.bz2 |
char/enemy: Fix movement upon collision after turn
-rw-r--r-- | src/char/enemy.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/char/enemy.c b/src/char/enemy.c index 6b1440f..eeda38d 100644 --- a/src/char/enemy.c +++ b/src/char/enemy.c @@ -112,6 +112,36 @@ _mf_enemy_step(struct mf_char *c) static int _mf_enemy_turn(struct mf_char *c) { + struct mf_enemy *e = (struct mf_enemy *) c; + int dx; + int dy; + int i; + + switch (c->cur_dir) { + case MF_CHAR_DIR_U_: dx = 0; dy = -1; break; + case MF_CHAR_DIR_D_: dx = 0; dy = 1; break; + case MF_CHAR_DIR_L_: dx = -1; dy = 0; break; + case MF_CHAR_DIR_R_: dx = 1; dy = 0; break; + default: dx = 0; dy = 0; break; + } + + for (i = 0; i < e->num_allies; ++i) { + if (e->allies[i] == c) { + /* Found self */ + continue; + } + if (e->allies[i]->cur_x == c->cur_x + dx && + e->allies[i]->cur_y == c->cur_y + dy) { + /* Another enemy currently in this direction */ + return 0; + } + if (e->allies[i]->new_x == c->cur_x + dx && + e->allies[i]->new_y == c->cur_y + dy) { + /* Another enemy moving in this direction */ + return 0; + } + } + /* Go straight */ c->new_dir = c->cur_dir; |