Aggressive monster now path a lot better

This commit is contained in:
Linus Probert 2018-03-03 12:56:53 +01:00
parent 0b98cad726
commit e41e560219
3 changed files with 61 additions and 16 deletions

View File

@ -59,4 +59,8 @@
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
typedef enum Direction_t {
UP, DOWN, LEFT, RIGHT
} Direction;
#endif // DEFINES_H_ #endif // DEFINES_H_

View File

@ -21,10 +21,7 @@
#include <SDL.h> #include <SDL.h>
#include <stdbool.h> #include <stdbool.h>
#include "defines.h"
typedef enum Direction_t {
UP, DOWN, LEFT, RIGHT
} Direction;
bool bool
keyboard_direction_press(Direction, SDL_Event*); keyboard_direction_press(Direction, SDL_Event*);

View File

@ -29,6 +29,7 @@
#include "item_builder.h" #include "item_builder.h"
#include "map.h" #include "map.h"
#include "particle_engine.h" #include "particle_engine.h"
#include "defines.h"
static void static void
monster_load_texts(Monster *m, SDL_Renderer *renderer) monster_load_texts(Monster *m, SDL_Renderer *renderer)
@ -186,20 +187,63 @@ monster_agressive_walk(Monster *m, RoomMatrix *rm)
mPos = position_to_matrix_coords(&m->sprite->pos); mPos = position_to_matrix_coords(&m->sprite->pos);
x_dist = mPos.x - rm->playerRoomPos.x; unsigned int currentScore = 100;
y_dist = mPos.y - rm->playerRoomPos.y; unsigned int chosenDirection = UP;
for (unsigned int i = 0; i < 4; ++i) {
Position next = mPos;
unsigned int nextScore = 0;
if (abs(x_dist) > abs(y_dist)) { switch (i) {
if (x_dist > 0) case UP:
move_left(m, rm); next.y -= 1;
else break;
move_right(m, rm); case DOWN:
} else { next.y += 1;
if (y_dist > 0) break;
case LEFT:
next.x -= 1;
break;
case RIGHT:
next.x += 1;
break;
}
if (position_equals(&next, &rm->playerRoomPos)) {
chosenDirection = (Direction) i;
break;
}
if (!position_in_roommatrix(&next))
continue;
x_dist = next.x - rm->playerRoomPos.x;
y_dist = next.y - rm->playerRoomPos.y;
if (rm->spaces[next.x][next.y].occupied) {
nextScore += 50;
}
nextScore += (max(abs(x_dist), abs(y_dist)));
if (nextScore < currentScore) {
currentScore = nextScore;
chosenDirection = (Direction)i;
}
}
switch (chosenDirection) {
case UP:
move_up(m, rm); move_up(m, rm);
else break;
case DOWN:
move_down(m, rm); move_down(m, rm);
break;
case LEFT:
move_left(m, rm);
break;
case RIGHT:
move_right(m, rm);
break;
} }
} }