-
Notifications
You must be signed in to change notification settings - Fork 1
/
LyubuStateMachine.cpp
213 lines (199 loc) · 6.24 KB
/
LyubuStateMachine.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "LyubuStateMachine.h"
extern char debug[1024];
using namespace std;
extern WORLDid gID;
extern SCENEid sID;
LyubuStateMachine::LyubuStateMachine(void)
{
}
LyubuStateMachine::~LyubuStateMachine(void){
FnWorld gw;
gw.Object(gID);
gw.DeleteAudio(audioN1);
FnScene scene;
scene.Object(sID);
scene.DeleteObject(this->bloodID);
}
LyubuStateMachine::LyubuStateMachine(ACTORid character, char *ActionFilename):ActorStateMachine(character,ActionFilename){
attackState = TRUE;
FnWorld gw;
gw.Object(gID);
gw.SetAudioPath("Data\\Audio");
audioN1 = gw.CreateAudio();
FnAudio audio;
audio.Object(audioN1);
BOOL beA = audio.Load("lyubuN01"); // au_bullet.hit1.wav
if (beA == FALSE){
sprintf(debug, "%s audioN1 load failed\n", debug);
}
audioN2 = gw.CreateAudio();
audio.Object(audioN2);
beA = audio.Load("lyubuN02"); // au_bullet.hit1.wav
if (beA == FALSE){
sprintf(debug, "%s audioN2 load failed\n", debug);
}
}
BOOL LyubuStateMachine::PlayAttackAction(int skip){
// the difference from parent function is that this function contain the sound fx
FnActor actor;
actor.Object(this->character);
//this->setAttackState();
char attackName[20] = "\0";
if (this->startAttack == TRUE){// first attack
this->lastAttackFrame = 0.0f;
this->startAttack = FALSE; // reset
//sprintf(debug, "%sstart attack\n", debug);
if (this->attackKeyQueue[currentAttackIndex] == NORMAL_ATT ){
sprintf(attackName, "N%d", currentAttackIndex + 1);
FnAudio audio;
audio.Object(audioN1);
audio.Play(ONCE);
// attackName should be "N1"
}else if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT){
sprintf(attackName, "H%d", currentAttackIndex + 1);
FnAudio audio;
audio.Object(audioN2);
audio.Play(ONCE);
// attackName should be "H1"
}else if (this->attackKeyQueue[currentAttackIndex] == ULTIMATE_ATT){
sprintf(attackName, "UAttack");
}
//sprintf(debug, "%s %s\n", debug, attackName);
string systemName(attackName);
this->SetNewAction(systemName);
// it performs the new attack
// set the flag here and BattleRoom will check it.
this->newAttack = TRUE;
}else{
// the attack name should be refine from reading the file
BOOL ret = actor.Play(0,ONCE, (float)skip, TRUE,TRUE);
this->UpdateEffectiveAttack();
if (ret == FALSE){
this->lastAttackFrame = 0.0f;
// play the next one
this->effectiveAttack = FALSE;
currentAttackIndex++;
if (currentAttackIndex >= lastAttackIndex){
// finish attacking
this->ChangeState(STATEIDLE);// should be change into combatidle;
this->attackDisable = FALSE;
currentAttackIndex = 0;
lastAttackIndex = 0;
return FALSE;
}else if (this->attackKeyQueue[currentAttackIndex] == NORMAL_ATT){
// get the next one attacking pos
sprintf(attackName, "N%d", currentAttackIndex + 1);
}else if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT){
// get the next one attacking pos
sprintf(attackName, "H%d", currentAttackIndex);
}else{
sprintf(debug, "%s next Attack fail condition\n", debug);
return FALSE;
}
FnAudio audio;
audio.Object(audioN2);
audio.Play(ONCE);
string systemName(attackName);
this->SetNewAction(systemName);
// it performs the new attack
// set the flag here and BattleRoom will check it and set the flag to FALSE after checking.
this->newAttack = TRUE;
}
}
//sprintf(debug, "%s lastAttack%d\n", debug, lastAttackIndex );
return TRUE;
}
BOOL LyubuStateMachine::UpdateEffectiveAttack(){
FnActor actor;
actor.Object(this->character);
float frame = actor.QueryCurrentFrame(0);
//sprintf(debug, "%s frame:%lf\n", debug, frame );
if (this->attackKeyQueue[currentAttackIndex] == ULTIMATE_ATT){
if (frame > 20.0){
if (frame - this->lastAttackFrame > 10.0f){
this->lastAttackFrame = frame;
this->newAttack = TRUE;
}
this->effectiveAttack = TRUE;
}
}else if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT){
if (frame > 60.0f){
this->effectiveAttack = FALSE;
}else if (frame > 35.0f && frame <= 60.0f){
if (frame - this->lastAttackFrame > 30.0f){// trick
this->lastAttackFrame = frame;
this->newAttack = TRUE; // another attack
}
this->effectiveAttack = TRUE;
}else if (frame > 20.0 && frame <= 30.0f){
this->effectiveAttack = TRUE;
}
}else if (this->currentAttackIndex > 0){
if (frame > 10.0){
this->effectiveAttack = TRUE;
}
}else{
this->effectiveAttack = TRUE;
}
return FALSE;
}
int LyubuStateMachine::AttackEnemy(float enemyPos[3], SHOT_CODE *shot_code){
// the return value is the attack power
FnActor actor;
actor.Object(this->character);
float attackerPos[3], attackerDir[3];
actor.GetWorldPosition(attackerPos);
actor.GetWorldDirection(attackerDir,NULL);
float frame = actor.QueryCurrentFrame(0);
float dist = 0.0;
for (int i = 0;i< 3;i++){
dist += (attackerPos[i] - enemyPos[i]) * (attackerPos[i] - enemyPos[i]);
}
//sprintf(debug, "%s dist = %lf\n",debug,dist);
if ( dist >= LYUBU_ATTACKRANGE ){
return 0; // no attack power
}
float cosine,dotProduct;
//float v[3];
dotProduct = 0.0;
for (int i = 0;i< 3;i++){
dotProduct += (enemyPos[i] - attackerPos[i]) * attackerDir[i];
}
float length = 0.0;
for (int i = 0;i< 3;i++){
length += (enemyPos[i] - attackerPos[i])* (enemyPos[i] - attackerPos[i]);
}
cosine = dotProduct / sqrt(length);
//sprintf(debug, "%s cosine = %lf\n",debug,cosine);
if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT || currentAttackIndex == MAXATTACK -1){
*shot_code = BIG_SHOT;
}else {
*shot_code = SMALL_SHOT;
}
if (this->currentAttackIndex == 0){
if (this->attackKeyQueue[currentAttackIndex] == ULTIMATE_ATT){
if (frame > 100){ // almost finish attack
*shot_code = BIG_SHOT;
//sprintf(debug, "%s attack power = %d\n",debug,10);
return 10;
}
*shot_code = STUCK_SHOT;
//sprintf(debug, "%s attack power = %d\n",debug,1);
return 1;
}else if (cosine > 0.8){ // normal or heavy attack, only attack the front side enemy
sprintf(debug, "%s attack power = %d\n",debug,1);
return 1;
}
}else if (this->currentAttackIndex <= 2){
if (cosine >= 0.0){
//sprintf(debug, "%s attack power = %d\n",debug,2);
return 2;
}
}else if (this->currentAttackIndex <= 3){
if (cosine >= -1.0){
//sprintf(debug, "%s attack power = %d\n",debug,2);
return 3;
}
}
return 0;
}