-
Notifications
You must be signed in to change notification settings - Fork 1
/
BattleRoom.cpp
163 lines (145 loc) · 4.57 KB
/
BattleRoom.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "BattleRoom.h"
using namespace std;
extern char debug[1024];
BattleRoom::BattleRoom(void)
{
this->hurt = TRUE;
}
BattleRoom::~BattleRoom(void)
{
}
BattleRoom::BattleRoom(ActorStateMachine *playerStateMachine, vector<ActorStateMachine *> npcStateMachineList){
this->npcStateMachineList = npcStateMachineList;
this->playerStateMachine = playerStateMachine;
this->playerHitMap.clear();
this->npcHitMap.clear();
}
void BattleRoom::RefreshArena(){
FnActor actor;
FnActor npc;
actor.Object(this->playerStateMachine->character);
float playerPos[3];
float npcPos[3];
actor.GetWorldPosition(playerPos);
this->AreanList.clear();
for (unsigned int i =0;i< this->npcStateMachineList.size();i++){
if (this->npcStateMachineList[i]->state == STATEVANISH){
continue;
}
npc.Object(this->npcStateMachineList[i]->character);
npc.GetWorldPosition(npcPos);
if (this->CheckDistanceAndState(playerPos, npcPos,
this->playerStateMachine->state, this->npcStateMachineList[i]->state ) == TRUE){
this->JoinArena( this->npcStateMachineList[i] );
}
}
//sprintf(debug, "%s npc state machine list size = %d\n",debug,this->npcStateMachineList.size());
this->PerformAttack();
}
BOOL BattleRoom::CheckDistanceAndState(float playerPos[3], float npcPos[3], ActorState playerState,ActorState npcState ){
if (playerState == STATEATTACK || npcState == STATEATTACK){
float dist = 0.0;
for (int i = 0;i< 3;i++){
dist += (playerPos[i] - npcPos[i]) * (playerPos[i] - npcPos[i]);
}
if (dist < BATTLE_RADIUS){
//sprintf(debug, "%s dist = %f\n",debug,dist);
return TRUE;
}
}
return FALSE;
}
BOOL BattleRoom::JoinArena(ActorStateMachine * npcStm){
this->AreanList.push_back(npcStm);
return TRUE;
}
void BattleRoom::PerformAttack(){
if (this->AreanList.empty() == true){// no npc in the arean means that no attack exists.
return;
}
FnActor actor;
actor.Object(this->playerStateMachine->character);
float pPos[3];
FnActor npc;
float nPos[3];
if (playerStateMachine->state == STATEATTACK && playerStateMachine->effectiveAttack == TRUE ){
if (this->playerStateMachine->newAttack == TRUE){
this->playerStateMachine->newAttack = FALSE;
this->playerHitMap.clear();
}
actor.GetWorldPosition(pPos);
int beOutShot;
for (unsigned int i= 0;i< this->AreanList.size();i++){
ACTORid tmpid = AreanList[i]->character;
if (playerHitMap.find(tmpid) == playerHitMap.end()){
npc.Object(tmpid);
npc.GetWorldPosition(nPos);
int attackPower = this->playerStateMachine->AttackEnemy(nPos, &beOutShot);
if (attackPower > 0 ){
// get a new victim;
//sprintf(debug, "%s new victim\n",debug);
if ( this->AreanList[i]->state != STATEDIE && this->AreanList[i]->state != STATEVANISH){
this->AreanList[i]->TakeDamage(attackPower, beOutShot, pPos);
}
this->playerHitMap[tmpid] = TRUE;
}
}
}
}
if (this->hurt == FALSE){
return; // player won't get damage.
}
actor.GetWorldPosition(pPos);
//this->npcHitMap.clear();
for (int i= 0;i< this->AreanList.size();i++){
ACTORid tmpid = AreanList[i]->character;
if (this->AreanList[i]->newAttack == TRUE){
//sprintf(debug, "%s erase\n",debug);
this->AreanList[i]->newAttack = FALSE;
npcHitMap.erase(tmpid);
}
if (AreanList[i]->state == STATEATTACK && AreanList[i]->effectiveAttack == TRUE ){
if (npcHitMap.find(tmpid) == npcHitMap.end()){ // find nothing
npc.Object(tmpid);
npc.GetWorldPosition(nPos);
int beOutShot;
int attackPower = this->AreanList[i]->AttackEnemy(pPos, &beOutShot);
if (attackPower > 0 ){
//sprintf(debug, "%s player damage\n",debug);
if ( this->playerStateMachine->state != STATEDIE){
this->playerStateMachine->TakeDamage(attackPower, beOutShot, nPos);
}
npcHitMap[tmpid] = TRUE;
}
}
}
}
}
BOOL BattleRoom::AttackCheck(float attackerDir[3], float attackerPos[3], float vitimPos[3], float attackRange){
// will be disable, the function will move to ActorStateMachine::AttackEnemy(float *)
float dist = 0.0;
for (int i = 0;i< 3;i++){
dist += (attackerPos[i] - vitimPos[i]) * (attackerPos[i] - vitimPos[i]);
}
//sprintf(debug, "%s dist = %lf\n",debug,dist);
if ( dist >= attackRange){
return FALSE;
}
float cosine,dotProduct;
//float v[3];
dotProduct = 0.0;
for (int i = 0;i< 3;i++){
dotProduct += (vitimPos[i] - attackerPos[i]) * attackerDir[i];
}
float length = 0.0;
for (int i = 0;i< 3;i++){
length += (vitimPos[i] - attackerPos[i])* (vitimPos[i] - attackerPos[i]);
}
cosine = dotProduct / length;
//sprintf(debug, "%s cosine = %lf\n",debug,cosine);
if (cosine >= 0){
return TRUE;
}else{
return FALSE;
}
}