1
1
#include " AIControl.h"
2
-
2
+ # include " ActorStateMachine.h "
3
3
using namespace std ;
4
4
extern char debug[1024 ];
5
5
@@ -27,7 +27,7 @@ void AIControl::PlayAction(int skip){
27
27
npcStateMachineList[i]->PlayAction (skip);
28
28
}
29
29
}
30
- float AIControl::distanceBetweenLyubu () {
30
+ void AIControl::moveTowardLyubu () {
31
31
for (int i = 0 ;i< this ->npcStateMachineList .size (); i++){
32
32
int npcId = npcStateMachineList[i]->character ;
33
33
@@ -47,9 +47,40 @@ float AIControl::distanceBetweenLyubu() {
47
47
+ (npcPos[0 ] - lyubuPos[0 ]) * (npcPos[0 ] - lyubuPos[0 ])
48
48
+ (npcPos[0 ] - lyubuPos[0 ]) * (npcPos[0 ] - lyubuPos[0 ]));
49
49
50
- sprintf (debug, " %s distance = %f\n " ,debug,distance);
51
-
52
- return distance;
50
+ // sprintf(debug, "%s distance = %f\n",debug,distance);
51
+
52
+ if (distance > ATTACK_DISTANCE && distance < KEEP_TRACK_DISTANCE) {
53
+ // turn toward lyubu
54
+ float newFDir[3 ], normalize;
55
+ newFDir[0 ] = lyubuPos[0 ] - npcPos[0 ];
56
+ newFDir[1 ] = lyubuPos[1 ] - npcPos[1 ];
57
+ newFDir[2 ] = lyubuPos[2 ] - npcPos[2 ];
58
+ normalize = sqrt (newFDir[0 ] * newFDir[0 ] + newFDir[1 ] * newFDir[1 ] + newFDir[2 ] * newFDir[2 ]);
59
+ newFDir[0 ] /= normalize;
60
+ newFDir[1 ] /= normalize;
61
+ newFDir[2 ] /= normalize;
53
62
63
+ float npcFDir[3 ], npcUDir[3 ];
64
+ npc.GetWorldDirection (npcFDir, npcUDir);
65
+ npc.SetWorldDirection (newFDir, npcUDir);
66
+
67
+ // move forward
68
+ int block = npc.MoveForward (MOVE_DISTANCE,TRUE , FALSE , 0.0 , TRUE );
69
+ if (block) {
70
+ // sprintf(debug, "%s npc is blocked\n",debug);
71
+ while (npc.MoveForward (MOVE_DISTANCE,TRUE , FALSE , 0.0 , TRUE )) {
72
+ sprintf (debug, " %s npc turn right\n " ,debug);
73
+ npc.TurnRight (300 );
74
+ npc.MoveForward (MOVE_DISTANCE,TRUE , FALSE , 0.0 , TRUE );
75
+ }
76
+ }
77
+ npcStateMachineList[i]->ChangeState (STATERUN, FALSE );
78
+ }
79
+ else if (distance <= ATTACK_DISTANCE) {
80
+ npcStateMachineList[i]->AppendAttackCode (NORMAL_ATT);
81
+ }
82
+ else {
83
+ npcStateMachineList[i]->ChangeState (STATEIDLE, TRUE );
84
+ }
54
85
}
55
- }
86
+ }
0 commit comments