Skip to content

Commit 9b49b80

Browse files
unknownunknown
authored andcommitted
npc => attack player
1 parent f69102f commit 9b49b80

File tree

6 files changed

+112
-27
lines changed

6 files changed

+112
-27
lines changed

AIControl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#define MOVE_DISTANCE 5
77
#define KEEP_TRACK_DISTANCE 1500
8-
#define ATTACK_DISTANCE 50
8+
#define ATTACK_DISTANCE 100
99

1010

1111
//class AIControl :

ActorStateMachine.cpp

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ int ActorStateMachine::ChangeState(ActorState s, BOOL forceSet){
123123
}else if (s == STATEDAMAGE){
124124
FnActor actor;
125125
actor.Object(this->character);
126-
actor.MoveForward(-MOVE_LENGTH,TRUE, FALSE, 0.0, TRUE);
127-
this->SetNewAction("LightDamage");
126+
//actor.MoveForward(-MOVE_LENGTH,TRUE, FALSE, 0.0, TRUE);
127+
//this->SetNewAction("LightDamage");
128128
this->currentAttackIndex = 0;
129129
this->lastAttackIndex = 0;
130-
//this->SetNewAction("HeavyDamage");
130+
this->SetNewAction("HeavyDamage");
131131
}else if (s == STATEDIE){
132132
this->SetNewAction("Die");
133133
}
@@ -275,6 +275,28 @@ BOOL ActorStateMachine::CheckEffectiveAttack(){
275275
return this->effectiveAttack;
276276
}
277277
BOOL ActorStateMachine::UpdateEffectiveAttack(){
278+
FnActor actor;
279+
actor.Object(this->character);
280+
float frame = actor.QueryCurrentFrame(0);
281+
//sprintf(debug, "%s frame:%lf\n", debug, frame );
282+
if (frame > 10.0){
283+
this->effectiveAttack = TRUE;
284+
}
285+
/*
286+
if (this->attackKeyQueue[currentAttackIndex] == ULTIMATE_ATT){
287+
if (frame > 20.0){
288+
this->newAttack = TRUE;
289+
this->effectiveAttack = TRUE;
290+
}
291+
}else if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT){
292+
if (frame > 20.0){
293+
this->effectiveAttack = TRUE;
294+
}
295+
}else if (this->currentAttackIndex > 0){
296+
if (frame > 10.0){
297+
this->effectiveAttack = TRUE;
298+
}
299+
}*/
278300
return FALSE;
279301
}
280302

@@ -284,7 +306,52 @@ int ActorStateMachine::AttackEnemy(float enemyPos[3], BOOL *beOutShot){
284306
*beOutShot = FALSE;
285307
}
286308
// the return value is the attack power
287-
return FALSE;
309+
FnActor actor;
310+
actor.Object(this->character);
311+
float attackerPos[3], attackerDir[3];
312+
actor.GetWorldPosition(attackerPos);
313+
actor.GetWorldDirection(attackerDir,NULL);
314+
float frame = actor.QueryCurrentFrame(0);
315+
316+
float dist = 0.0;
317+
for (int i = 0;i< 3;i++){
318+
dist += (attackerPos[i] - enemyPos[i]) * (attackerPos[i] - enemyPos[i]);
319+
}
320+
//sprintf(debug, "%s dist = %lf\n",debug,dist);
321+
if ( dist >= ROBOT_ATTACKRANGE ){
322+
return 0; // no attack power
323+
}
324+
float cosine,dotProduct;
325+
//float v[3];
326+
dotProduct = 0.0;
327+
for (int i = 0;i< 3;i++){
328+
dotProduct += (enemyPos[i] - attackerPos[i]) * attackerDir[i];
329+
}
330+
float length = 0.0;
331+
for (int i = 0;i< 3;i++){
332+
length += (enemyPos[i] - attackerPos[i])* (enemyPos[i] - attackerPos[i]);
333+
}
334+
cosine = dotProduct / sqrt(length);
335+
//sprintf(debug, "%s cosine = %lf\n",debug,cosine);
336+
337+
if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT || currentAttackIndex == MAXATTACK -1){
338+
*beOutShot = TRUE;
339+
}else {
340+
*beOutShot = FALSE;
341+
}
342+
343+
if (this->currentAttackIndex == 0){
344+
if (cosine > 0.8){ // normal or heavy attack, only attack the front side enemy
345+
sprintf(debug, "%s attack power = %d\n",debug,1);
346+
return 1;
347+
}
348+
}else if (this->currentAttackIndex <= 3){
349+
if (cosine >= 0.0){
350+
sprintf(debug, "%s attack power = %d\n",debug,2);
351+
return 3;
352+
}
353+
}
354+
return 0;
288355
}
289356

290357
void ActorStateMachine::TakeDamage(float damage, BOOL beShot, float *attackerPos ){
@@ -301,7 +368,7 @@ void ActorStateMachine::TakeDamage(float damage, BOOL beShot, float *attackerPos
301368
newDir[2] = 0.0f;
302369
actor.SetWorldDirection(newDir,NULL);
303370
//if (beShot == TRUE){
304-
//actor.MoveForward(-OUTSHOT_DIS,TRUE, FALSE, 0.0, TRUE);
371+
actor.MoveForward(-OUTSHOT_DIS,TRUE, FALSE, 0.0, TRUE);
305372
//}
306373
actor.SetWorldDirection(dir,NULL);
307374
}

ActorStateMachine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ typedef int ActorState;
1919
#define ULTIMATE_ATT 2
2020
typedef int ATTACK_CODE;
2121

22-
#define OUTSHOT_DIS 200.0
22+
#define OUTSHOT_DIS 100.0
2323
#define MAX_LIFE 1000.0
24+
#define ROBOT_ATTACKRANGE 18000.0
2425

2526
// actor free meaning it can do anything by the controller.
2627
// actor stay meaning that it can't be move beacuse of being attacked.

BattleRoom.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ BattleRoom::BattleRoom(ActorStateMachine *playerStateMachine, vector<ActorStateM
1616
this->npcStateMachineList = npcStateMachineList;
1717
this->playerStateMachine = playerStateMachine;
1818
this->playerHitMap.clear();
19+
this->npcHitMap.clear();
1920
}
2021

2122
void BattleRoom::RefreshArena(){
@@ -66,21 +67,20 @@ void BattleRoom::PerformAttack(){
6667
if (this->AreanList.empty() == true){// no npc in the arean means that no attack exists.
6768
return;
6869
}
70+
FnActor actor;
71+
actor.Object(this->playerStateMachine->character);
72+
float pPos[3];
73+
74+
FnActor npc;
75+
float nPos[3];
76+
6977
if (playerStateMachine->state == STATEATTACK && playerStateMachine->effectiveAttack == TRUE ){
7078
if (this->playerStateMachine->newAttack == TRUE){
7179
this->playerStateMachine->newAttack = FALSE;
7280
this->playerHitMap.clear();
7381
}
74-
FnActor actor;
75-
actor.Object(this->playerStateMachine->character);
76-
float pPos[3];
77-
//float pDir[3];
78-
//float pUDir[3];
79-
//actor.GetWorldDirection(pDir,NULL);
8082
actor.GetWorldPosition(pPos);
8183

82-
FnActor npc;
83-
float nPos[3];
8484
int beOutShot;
8585
for (int i= 0;i< this->AreanList.size();i++){
8686
ACTORid tmpid = AreanList[i]->character;
@@ -99,7 +99,34 @@ void BattleRoom::PerformAttack(){
9999
}
100100
}
101101
}
102-
}// should check the npc's attack;
102+
}
103+
104+
actor.GetWorldPosition(pPos);
105+
//this->npcHitMap.clear();
106+
for (int i= 0;i< this->AreanList.size();i++){
107+
ACTORid tmpid = AreanList[i]->character;
108+
if (this->AreanList[i]->newAttack == TRUE){
109+
//sprintf(debug, "%s erase\n",debug);
110+
this->AreanList[i]->newAttack = FALSE;
111+
npcHitMap.erase(tmpid);
112+
}
113+
if (AreanList[i]->state == STATEATTACK && AreanList[i]->effectiveAttack == TRUE ){
114+
if (npcHitMap.find(tmpid) == npcHitMap.end()){ // find nothing
115+
npc.Object(tmpid);
116+
npc.GetWorldPosition(nPos);
117+
BOOL beOutShot;
118+
119+
int attackPower = this->AreanList[i]->AttackEnemy(pPos, &beOutShot);
120+
if (attackPower > 0 ){
121+
sprintf(debug, "%s player damage\n",debug);
122+
if ( this->playerStateMachine->state != STATEDIE){
123+
this->playerStateMachine->TakeDamage(attackPower, beOutShot, pPos);
124+
}
125+
npcHitMap[tmpid] = TRUE;
126+
}
127+
}
128+
}
129+
}
103130
}
104131

105132
BOOL BattleRoom::AttackCheck(float attackerDir[3], float attackerPos[3], float vitimPos[3], float attackRange){

BattleRoom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class BattleRoom
1515
std::vector<ActorStateMachine *> npcStateMachineList; // they are candidates, not always in the arean.
1616
std::vector<ActorStateMachine *> AreanList;
1717
std::map<ACTORid, BOOL> playerHitMap;
18+
std::map<ACTORid, BOOL> npcHitMap;
1819
public:
1920
BattleRoom(void);
2021
virtual ~BattleRoom(void);

LyubuStateMachine.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,6 @@ LyubuStateMachine::LyubuStateMachine(ACTORid character, char *ActionFilename):Ac
3838
sprintf(debug, "%s audioN2 load failed\n", debug);
3939
}
4040
}
41-
/*
42-
BOOL LyubuStateMachine::isNowAttackState(void) { //if now is attack state(i.e. var attackState == true), return true
43-
return attackState;
44-
}
45-
void LyubuStateMachine::resetAttackState(void) { //reset var attackState
46-
attackState = FALSE;
47-
}
48-
void LyubuStateMachine::setAttackState(void) { //set var attackState
49-
attackState = TRUE;
50-
}
51-
*/
5241

5342
BOOL LyubuStateMachine::PlayAttackAction(int skip){
5443
// the difference from parent function is that this function contain the sound fx

0 commit comments

Comments
 (0)