-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.cpp
More file actions
73 lines (59 loc) · 1.62 KB
/
ai.cpp
File metadata and controls
73 lines (59 loc) · 1.62 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <assert.h>
#include <math.h>
#include "ai.h"
namespace dragonfighting {
AI::AI(Sprite *player, Sprite *enemy) :
direction(0),
player(player),
enemy(enemy)
{
memset(&ctrlevent, 0, sizeof(ctrlevent));
ctrlevent.type = Ctrl_KEYNONE;
}
AI::~AI()
{
}
bool AI::positionXEqual(float x, float dst)
{
return fabs(dst - x) < player->speed + 0.00001f;
}
bool AI::pollEvent(struct Ctrl_KeyEvent *event)
{
assert(event != NULL);
if (ctrlevent.type == Ctrl_KEYNONE) {
return false;
}
event->type = ctrlevent.type;
event->key = ctrlevent.key;
ctrlevent.type = Ctrl_KEYNONE;
return true;
}
void AI::update(Uint32 frameStamp)
{
if (player->getState() != Character::WALK) {
if (!positionXEqual(player->x, 475)) {
if (player->getStateAllow() & Character::ALLOW_WALK) {
if (player->x > 475 && !positionXEqual(player->x, 475)) {
ctrlevent.type = Ctrl_KEYDOWN;
ctrlevent.key = CTRLKEY_LEFT;
direction = 1;
} else if (player->x < 475 && !positionXEqual(player->x, 475)) {
ctrlevent.type = Ctrl_KEYDOWN;
ctrlevent.key = CTRLKEY_RIGHT;
direction = 2;
}
}
}
} else {
if (positionXEqual(player->x, 475)) {
ctrlevent.type = Ctrl_KEYUP;
if (direction == 1) {
ctrlevent.key = CTRLKEY_LEFT;
} else if (direction == 2) {
ctrlevent.key = CTRLKEY_RIGHT;
}
direction = 0;
}
}
}
}