Skip to content

Commit f1cea64

Browse files
unknownunknown
unknown
authored and
unknown
committed
ActorStateMachine.cpp .h
add blood bar
1 parent 18685dc commit f1cea64

File tree

4 files changed

+84
-19
lines changed

4 files changed

+84
-19
lines changed

ActorStateMachine.cpp

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "ActorStateMachine.h"
22

33
extern char debug[1024];
4-
4+
extern SCENEid sID;
55
using namespace std;
66
#define MOVE_LENGTH 20.0
77

@@ -12,6 +12,9 @@ ActorStateMachine::ActorStateMachine(void)
1212

1313
ActorStateMachine::~ActorStateMachine(void)
1414
{
15+
FnScene scene;
16+
scene.Object(sID);
17+
scene.DeleteObject(this->bloodID);
1518
}
1619

1720
ActorStateMachine::ActorStateMachine(ACTORid character, char * ActionFilename){
@@ -22,10 +25,35 @@ ActorStateMachine::ActorStateMachine(ACTORid character, char * ActionFilename){
2225
this->lastAttackIndex = 0;
2326
this->newAttack = FALSE;
2427
this->effectiveAttack = FALSE;
25-
this->life = 3;
2628
this->initActionIDMap(ActionFilename);
29+
this->initLife();
2730
}
2831

32+
BOOL ActorStateMachine::initLife(){
33+
this->life = 100;
34+
35+
FnScene scene;
36+
scene.Object(sID);
37+
bloodID = scene.CreateObject(ROOT);
38+
FnObject blood;
39+
blood.Object(bloodID);
40+
//FnBillBoard blood;
41+
//blood.Object(bloodID, 0);
42+
FnActor actor;
43+
actor.Object(this->character);
44+
OBJECTid baseID = actor.GetBaseObject();
45+
46+
float pos[3], size[2], color[3];
47+
pos[0] = 0.0f;
48+
pos[1] = 0.0f;
49+
pos[2] = 80.0f;
50+
size[0] = 50.0f;
51+
size[1] = 2.0f;
52+
color[0] = 1.0f; color[1] = color[2] = 0.0f;
53+
blood.Billboard(pos, size, NULL, 0, color);
54+
blood.SetParent(baseID);
55+
return TRUE;
56+
}
2957
BOOL ActorStateMachine::initActionIDMap(char *ActionFilename){
3058
FILE *fp = fopen(ActionFilename,"r");
3159
if (fp == NULL){
@@ -100,14 +128,6 @@ int ActorStateMachine::ChangeState(ActorState s, BOOL forceSet){
100128
this->currentAttackIndex = 0;
101129
this->lastAttackIndex = 0;
102130
this->SetNewAction("HeavyDamage");
103-
this->life --;
104-
sprintf(debug, "%s life=%d\n", debug, this->life);
105-
if (this->life <= 0) {
106-
// it will make a recursive call.
107-
this->ChangeState(STATEDIE, TRUE);
108-
// prevent the damage actionID will replace the die actionID
109-
return 0;
110-
}
111131
}else if (s == STATEDIE){
112132
this->SetNewAction("Die");
113133
}
@@ -176,7 +196,7 @@ BOOL ActorStateMachine::PlayAction(int skip){
176196
BOOL ActorStateMachine::PlayAttackAction(int skip){
177197
FnActor actor;
178198
actor.Object(this->character);
179-
ACTIONid actionID;
199+
//ACTIONid actionID;
180200

181201
char attackName[20] = "\0";
182202
if (this->startAttack == TRUE){// first attack
@@ -261,4 +281,40 @@ BOOL ActorStateMachine::UpdateEffectiveAttack(){
261281
int ActorStateMachine::AttackEnemy(float enemyPos[3]){
262282
// the return value is the attack power
263283
return FALSE;
284+
}
285+
286+
void ActorStateMachine::TakeDamage(float damage, BOOL beShot, float *attackerPos ){
287+
FnActor actor;
288+
actor.Object(character);
289+
float pos[3];
290+
float dir[3];
291+
actor.GetWorldPosition(pos);
292+
actor.GetWorldDirection(dir, NULL);
293+
if (attackerPos !=NULL){
294+
float newDir[3];
295+
newDir[0] = attackerPos[0] - pos[0];
296+
newDir[1] = attackerPos[1] - pos[1];
297+
newDir[2] = 0.0f;
298+
actor.SetWorldDirection(newDir,NULL);
299+
if (beShot == TRUE){
300+
actor.MoveForward(-OUTSHOT_DIS,TRUE, FALSE, 0.0, TRUE);
301+
}else{
302+
actor.MoveForward(-1.0f,TRUE, FALSE, 0.0, TRUE);
303+
}
304+
actor.SetWorldDirection(dir,NULL);
305+
}
306+
this->life -= damage;
307+
sprintf(debug, "%s life=%d\n", debug, this->life);
308+
if (this->life <= 0) {
309+
this->ChangeState(STATEDIE, TRUE);
310+
}else {
311+
this->ChangeState(STATEDAMAGE, TRUE);
312+
}
313+
314+
FnBillBoard blood;
315+
blood.Object(bloodID, 0);
316+
float size[2];
317+
blood.GetSize(size);
318+
size[0] = life / MAX_LIFE * 50.0f;
319+
blood.SetSize(size);
264320
}

ActorStateMachine.h

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

22+
#define OUTSHOT_DIS 100.0
23+
#define MAX_LIFE 100.0
24+
2225
// actor free meaning it can do anything by the controller.
2326
// actor stay meaning that it can't be move beacuse of being attacked.
2427

@@ -41,6 +44,8 @@ class ActorStateMachine
4144
virtual BOOL PlayAttackAction(int skip);
4245
BOOL SetNewAction(std::string systemName);
4346
virtual BOOL UpdateEffectiveAttack();
47+
OBJECTid bloodID;
48+
virtual BOOL initLife();
4449
public:
4550
ActorStateMachine(void);
4651
virtual ~ActorStateMachine(void);
@@ -56,5 +61,6 @@ class ActorStateMachine
5661
virtual BOOL PlayAction(int skip);
5762
BOOL CheckEffectiveAttack();
5863
virtual int AttackEnemy(float enemyPos[3]);
64+
virtual void TakeDamage(float damage, BOOL beShot, float* attackerPos);
5965
};
6066

BattleRoom.cpp

Lines changed: 6 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];
76+
float pPos[3];
7777
//float pDir[3];
7878
//float pUDir[3];
7979
//actor.GetWorldDirection(pDir,NULL);
80-
//actor.GetWorldPosition(pPos);
80+
actor.GetWorldPosition(pPos);
8181

8282
FnActor npc;
8383
float nPos[3];
@@ -87,13 +87,13 @@ void BattleRoom::PerformAttack(){
8787
if (playerHitMap.find(tmpid) == playerHitMap.end()){
8888
npc.Object(tmpid);
8989
npc.GetWorldPosition(nPos);
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
90+
int attackPower = this->playerStateMachine->AttackEnemy(nPos);
91+
if (attackPower > 0 ){
9392
// get a new victim;
9493
sprintf(debug, "%s new victim\n",debug);
9594
if ( this->AreanList[i]->state != STATEDIE){
96-
this->AreanList[i]->ChangeState(STATEDAMAGE,TRUE);
95+
this->AreanList[i]->TakeDamage(attackPower, TRUE, pPos);
96+
//this->AreanList[i]->ChangeState(STATEDAMAGE,TRUE);
9797
}
9898
this->playerHitMap[tmpid] = TRUE;
9999
}

LyubuStateMachine.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
extern char debug[1024];
44
using namespace std;
55
extern WORLDid gID;
6+
extern SCENEid sID;
67

78
LyubuStateMachine::LyubuStateMachine(void)
89
{
910
}
1011

1112

12-
LyubuStateMachine::~LyubuStateMachine(void)
13-
{
13+
LyubuStateMachine::~LyubuStateMachine(void){
1414
FnWorld gw;
1515
gw.Object(gID);
1616
gw.DeleteAudio(audioN1);
17+
FnScene scene;
18+
scene.Object(sID);
19+
scene.DeleteObject(this->bloodID);
1720
}
1821

1922
LyubuStateMachine::LyubuStateMachine(ACTORid character, char *ActionFilename):ActorStateMachine(character,ActionFilename){

0 commit comments

Comments
 (0)