Skip to content

Commit 18685dc

Browse files
unknownunknown
authored andcommitted
AcotrStateMachine.cpp .h
add a function AttackEnemy. it will check the angle to decide whether the attack is successful. it also will return the attack power depending the currentAttackIndex. add ULTIMATE_ATT LyubuStateMachine.cpp .h Add sound fx
1 parent c5ef63a commit 18685dc

File tree

6 files changed

+121
-11
lines changed

6 files changed

+121
-11
lines changed

ActorStateMachine.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ BOOL ActorStateMachine::CharacterSetIdle(){
129129

130130
BOOL ActorStateMachine::AppendAttackCode(ATTACK_CODE code){
131131
if (this->CanAttack() == TRUE){
132-
this->ChangeState(STATEATTACK);
132+
if (code == ULTIMATE_ATT){
133+
this->ChangeState(STATEATTACK, TRUE);
134+
this->attackDisable = TRUE;
135+
this->currentAttackIndex = 0;
136+
this->lastAttackIndex = 0;
137+
//sprintf(debug, "%s get ultimate\n", debug);
138+
}else{
139+
this->ChangeState(STATEATTACK);
140+
}
133141
this->attackKeyQueue[this->lastAttackIndex] = code;
134142
this->lastAttackIndex++;
135143
//sprintf(debug, "%s lastAttackIndex:%d\n", debug,lastAttackIndex);
@@ -248,4 +256,9 @@ BOOL ActorStateMachine::CheckEffectiveAttack(){
248256
}
249257
BOOL ActorStateMachine::UpdateEffectiveAttack(){
250258
return FALSE;
259+
}
260+
261+
int ActorStateMachine::AttackEnemy(float enemyPos[3]){
262+
// the return value is the attack power
263+
return FALSE;
251264
}

ActorStateMachine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef int ActorState;
1616

1717
#define NORMAL_ATT 0
1818
#define HEAVY_ATT 1
19-
19+
#define ULTIMATE_ATT 2
2020
typedef int ATTACK_CODE;
2121

2222
// actor free meaning it can do anything by the controller.
@@ -55,5 +55,6 @@ class ActorStateMachine
5555
BOOL AppendAttackCode(ATTACK_CODE code);
5656
virtual BOOL PlayAction(int skip);
5757
BOOL CheckEffectiveAttack();
58+
virtual int AttackEnemy(float enemyPos[3]);
5859
};
5960

BattleRoom.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ void BattleRoom::PerformAttack(){
7373
}
7474
FnActor actor;
7575
actor.Object(this->playerStateMachine->character);
76-
float pPos[3];
77-
float pDir[3];
76+
//float pPos[3];
77+
//float pDir[3];
7878
//float pUDir[3];
79-
actor.GetWorldDirection(pDir,NULL);
80-
actor.GetWorldPosition(pPos);
79+
//actor.GetWorldDirection(pDir,NULL);
80+
//actor.GetWorldPosition(pPos);
8181

8282
FnActor npc;
8383
float nPos[3];
@@ -87,20 +87,23 @@ void BattleRoom::PerformAttack(){
8787
if (playerHitMap.find(tmpid) == playerHitMap.end()){
8888
npc.Object(tmpid);
8989
npc.GetWorldPosition(nPos);
90-
if (this->AttackCheck(pDir, pPos, nPos, BATTLE_RADIUS / 1.5) == TRUE){// lyubu attack area is the half of Battle raidus
90+
if (this->playerStateMachine->AttackEnemy(nPos) > 0 ){
91+
92+
//if (this->AttackCheck(pDir, pPos, nPos, BATTLE_RADIUS / 1.5) == TRUE){// lyubu attack area is the half of Battle raidus
9193
// get a new victim;
9294
sprintf(debug, "%s new victim\n",debug);
9395
if ( this->AreanList[i]->state != STATEDIE){
9496
this->AreanList[i]->ChangeState(STATEDAMAGE,TRUE);
9597
}
98+
this->playerHitMap[tmpid] = TRUE;
9699
}
97-
this->playerHitMap[tmpid] = TRUE;
98100
}
99101
}
100102
}// should check the npc's attack;
101103
}
102104

103105
BOOL BattleRoom::AttackCheck(float attackerDir[3], float attackerPos[3], float vitimPos[3], float attackRange){
106+
// will be disable, the function will move to ActorStateMachine::AttackEnemy(float *)
104107
float dist = 0.0;
105108
for (int i = 0;i< 3;i++){
106109
dist += (attackerPos[i] - vitimPos[i]) * (attackerPos[i] - vitimPos[i]);

LyubuStateMachine.cpp

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
extern char debug[1024];
44
using namespace std;
5+
extern WORLDid gID;
56

67
LyubuStateMachine::LyubuStateMachine(void)
78
{
@@ -10,10 +11,29 @@ LyubuStateMachine::LyubuStateMachine(void)
1011

1112
LyubuStateMachine::~LyubuStateMachine(void)
1213
{
14+
FnWorld gw;
15+
gw.Object(gID);
16+
gw.DeleteAudio(audioN1);
1317
}
1418

1519
LyubuStateMachine::LyubuStateMachine(ACTORid character, char *ActionFilename):ActorStateMachine(character,ActionFilename){
1620
attackState = TRUE;
21+
FnWorld gw;
22+
gw.Object(gID);
23+
gw.SetAudioPath("Data\\Audio");
24+
audioN1 = gw.CreateAudio();
25+
FnAudio audio;
26+
audio.Object(audioN1);
27+
BOOL beA = audio.Load("lyubuN01"); // au_bullet.hit1.wav
28+
if (beA == FALSE){
29+
sprintf(debug, "%s audioN1 load failed\n", debug);
30+
}
31+
audioN2 = gw.CreateAudio();
32+
audio.Object(audioN2);
33+
beA = audio.Load("lyubuN02"); // au_bullet.hit1.wav
34+
if (beA == FALSE){
35+
sprintf(debug, "%s audioN2 load failed\n", debug);
36+
}
1737
}
1838
/*
1939
BOOL LyubuStateMachine::isNowAttackState(void) { //if now is attack state(i.e. var attackState == true), return true
@@ -40,12 +60,21 @@ BOOL LyubuStateMachine::PlayAttackAction(int skip){
4060
this->startAttack = FALSE; // reset
4161
//sprintf(debug, "%sstart attack\n", debug);
4262
if (this->attackKeyQueue[currentAttackIndex] == NORMAL_ATT ){
43-
sprintf(attackName, "N%d", currentAttackIndex + 1);
63+
sprintf(attackName, "N%d", currentAttackIndex + 1);
64+
FnAudio audio;
65+
audio.Object(audioN1);
66+
audio.Play(ONCE);
4467
// attackName should be "N1"
4568
}else if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT){
4669
sprintf(attackName, "H%d", currentAttackIndex + 1);
70+
FnAudio audio;
71+
audio.Object(audioN2);
72+
audio.Play(ONCE);
4773
// attackName should be "H1"
74+
}else if (this->attackKeyQueue[currentAttackIndex] == ULTIMATE_ATT){
75+
sprintf(attackName, "UAttack");
4876
}
77+
//sprintf(debug, "%s %s\n", debug, attackName);
4978
string systemName(attackName);
5079
this->SetNewAction(systemName);
5180
// it performs the new attack
@@ -76,6 +105,9 @@ BOOL LyubuStateMachine::PlayAttackAction(int skip){
76105
sprintf(debug, "%s next Attack fail condition\n", debug);
77106
return FALSE;
78107
}
108+
FnAudio audio;
109+
audio.Object(audioN2);
110+
audio.Play(ONCE);
79111
string systemName(attackName);
80112
this->SetNewAction(systemName);
81113
// it performs the new attack
@@ -92,7 +124,7 @@ BOOL LyubuStateMachine::UpdateEffectiveAttack(){
92124
actor.Object(this->character);
93125
float frame = actor.QueryCurrentFrame(0);
94126
//sprintf(debug, "%s frame:%lf\n", debug, frame );
95-
if (this->attackKeyQueue[currentAttackIndex]){
127+
if (this->attackKeyQueue[currentAttackIndex] == HEAVY_ATT){
96128
if (frame > 20.0){
97129
this->effectiveAttack = TRUE;
98130
}
@@ -104,4 +136,55 @@ BOOL LyubuStateMachine::UpdateEffectiveAttack(){
104136
this->effectiveAttack = TRUE;
105137
}
106138
return FALSE;
139+
}
140+
141+
int LyubuStateMachine::AttackEnemy(float enemyPos[3]){
142+
// the return value is the attack power
143+
FnActor actor;
144+
actor.Object(this->character);
145+
float attackerPos[3], attackerDir[3];
146+
actor.GetWorldPosition(attackerPos);
147+
actor.GetWorldDirection(attackerDir,NULL);
148+
149+
float dist = 0.0;
150+
for (int i = 0;i< 3;i++){
151+
dist += (attackerPos[i] - enemyPos[i]) * (attackerPos[i] - enemyPos[i]);
152+
}
153+
//sprintf(debug, "%s dist = %lf\n",debug,dist);
154+
if ( dist >= LYUBU_ATTACKRANGE ){
155+
return 0; // no attack power
156+
}
157+
float cosine,dotProduct;
158+
//float v[3];
159+
dotProduct = 0.0;
160+
for (int i = 0;i< 3;i++){
161+
dotProduct += (enemyPos[i] - attackerPos[i]) * attackerDir[i];
162+
}
163+
float length = 0.0;
164+
for (int i = 0;i< 3;i++){
165+
length += (enemyPos[i] - attackerPos[i])* (enemyPos[i] - attackerPos[i]);
166+
}
167+
cosine = dotProduct / sqrt(length);
168+
//sprintf(debug, "%s cosine = %lf\n",debug,cosine);
169+
if (this->currentAttackIndex == 0){
170+
if (this->attackKeyQueue[currentAttackIndex] == ULTIMATE_ATT){
171+
sprintf(debug, "%s attack power = %d\n",debug,10);
172+
return 10;
173+
}else if (cosine > 0.8){
174+
sprintf(debug, "%s attack power = %d\n",debug,1);
175+
return 1;
176+
}
177+
}else if (this->currentAttackIndex <= 2){
178+
if (cosine >= 0.0){
179+
sprintf(debug, "%s attack power = %d\n",debug,2);
180+
return 2;
181+
}
182+
}else if (this->currentAttackIndex <= 3){
183+
if (cosine >= -1.0){
184+
sprintf(debug, "%s attack power = %d\n",debug,2);
185+
return 3;
186+
}
187+
}
188+
return 0;
189+
107190
}

LyubuStateMachine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#pragma once
22
#include "ActorStateMachine.h"
33
#include "KeyboardControl.h"
4+
#include "FyMedia.h"
5+
#define LYUBU_ATTACKRANGE 20000.0
6+
7+
48
class LyubuStateMachine :
59
public ActorStateMachine
610
{
@@ -9,8 +13,11 @@ class LyubuStateMachine :
913
virtual ~LyubuStateMachine(void);
1014
LyubuStateMachine(ACTORid character, char *ActionFilename);
1115
virtual BOOL PlayAttackAction(int skip);
16+
int AttackEnemy(float enemyPos[3]);
1217
protected:
1318
virtual BOOL UpdateEffectiveAttack();
19+
AUDIOid audioN1;
20+
AUDIOid audioN2;
1421
/*
1522
BOOL isNowAttackState(void); //if now is attack state(i.e. var attackState == true), return true
1623
void resetAttackState(void); //reset var attackState

Main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Last Updated : 1010, 2004, C.Wang
1414
#include "AIControl.h"
1515
#include "BattleRoom.h"
1616
#include "FyFx.h"
17+
#include "FyMedia.h"
1718

1819
int oldX, oldY, oldXM, oldYM, oldXMM, oldYMM;
1920

@@ -369,6 +370,8 @@ void KeyboardAttackCommand(WORLDid gID, BYTE code, BOOL value){
369370
kc->AppendAttackCode(NORMAL_ATT);
370371
}else if(code == FY_K && FyCheckHotKeyStatus(FY_K) == TRUE){
371372
kc->AppendAttackCode(HEAVY_ATT);
373+
}else if(code == FY_L && FyCheckHotKeyStatus(FY_L) == TRUE){
374+
kc->AppendAttackCode(ULTIMATE_ATT);
372375
}
373376
}
374377

@@ -424,8 +427,8 @@ BOOL BlindKeys(){
424427
FyBindMouseFunction(RIGHT_MOUSE, InitMove, MoveCam, NULL, NULL);
425428

426429
FyDefineHotKey(FY_J, KeyboardAttackCommand, FALSE);
427-
//FyDefineHotKey(FY_J, KeyboardAttackCommand, TRUE);
428430
FyDefineHotKey(FY_K, KeyboardAttackCommand, FALSE);
431+
FyDefineHotKey(FY_L, KeyboardAttackCommand, FALSE);
429432
return TRUE;
430433
}
431434

0 commit comments

Comments
 (0)