-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.cpp
46 lines (36 loc) · 1.29 KB
/
enemy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <SPI.h>
#include <GD.h>
#include "constants.h"
#include "level.h"
#include "enemy.h"
extern CPlayer player;
namespace
{
uint16_t getDistance(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
{
return abs(x1 - x2) + abs(y1 - y2);
}
}
void CEnemy::frame()
{
const uint16_t x = getX(), y = getY();
const EPathType path = getPxPathType(x, y);
if ((path != PATH_HORIZONTAL) && (path != PATH_VERTICAL))
{
const bool canup = ((y > 0) && (getPxPathType(x, y-1) != PATH_NONE));
const bool candown = (getPxPathType(x, y+1) != PATH_NONE);
const bool canleft = ((x > 0) && (getPxPathType(x-1, y) != PATH_NONE));
const bool canright = (getPxPathType(x+1, y) != PATH_NONE);
const uint16_t px = player.getX(), py = player.getY();
const uint16_t curdist = getDistance(x, y, px, py);
if (canup && (getDistance(x, y-1, px, py)) < curdist)
setDesiredDirection(DIR_UP);
else if (candown && (getDistance(x, y+1, px, py)) < curdist)
setDesiredDirection(DIR_DOWN);
else if (canleft && (getDistance(x-1, y, px, py)) < curdist)
setDesiredDirection(DIR_LEFT);
else if (canright && (getDistance(x+1, y, px, py)) < curdist)
setDesiredDirection(DIR_RIGHT);
}
CPlayer::frame();
}