forked from SuooL/KungfuBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hero.cpp
254 lines (227 loc) · 6.36 KB
/
Hero.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*!
* \class Hero
*
* \ingroup GroupName
*
* \brief 英雄实体类
*
* TODO: long description
*
* \note
*
* \author SuooL
*
* \version 1.0
*
* \date 五月 2015
*
* Contact: [email protected]
*
*/
#include "Hero.h"
#include "ActionTool.h"
USING_NS_CC;
// 构造性方法
void Hero::InitHeroSprite(char *hero_name, int m_iLevel)
{
// 属性初始化
m_HeroSprite = NULL;
m_bCanCrazy = false;
m_bIsAction = false;
m_bIsJumping = false;
HeroDirecton = false; // 向右运动
Hero_name = NULL;
IsRunning = false;
IsAttack = false;
IsHurt = false;
IsDead = false;
m_iCurrentMp = 0.0f;
m_iTotleMp = 100.0f;
m_iSpeed = 5;
Hero_name = hero_name;
m_iCurrentHp = m_iTotleHp = 300.0f*m_iLevel;
percentage = 100.0f;
this->m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(hero_name));
this->addChild(m_HeroSprite);
}
Sprite* Hero::GetSprite()
{
return m_HeroSprite;
}
// 动作方法
// 设置动作
void Hero::SetAnimation(const char *frameName, float delay, bool run_directon)
{
// 调整方向
if (HeroDirecton != run_directon)
{
HeroDirecton = run_directon;
m_HeroSprite->setFlippedX(run_directon);
}
if (IsRunning || IsHurt || IsAttack)
return;
/* 创建动画动作 */
Animate* action = ActionTool::animationWithFrameName(frameName, -1, delay);
m_HeroSprite->runAction(action);
IsRunning = true;
}
// 停止动作
void Hero::StopAnimation()
{
if (!IsRunning)
return;
m_HeroSprite->stopAllActions();//当前精灵停止所有动画
//恢复精灵原来的初始化贴图
this->removeChild(m_HeroSprite, true);//把原来的精灵删除掉
this->m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(Hero_name));//恢复精灵原来的贴图样子
m_HeroSprite->setFlippedX(HeroDirecton);
this->addChild(m_HeroSprite);
IsRunning = false;
}
// 跳起
void Hero::JumpUpAnimation(const char *name_each, float delay, bool run_directon)
{
// 调整方向
if (HeroDirecton != run_directon)
{
HeroDirecton = run_directon;
m_HeroSprite->setFlippedX(run_directon);
}
if (IsHurt || IsAttack || IsDead)
return;
// 创建动画动作
Animate* action = ActionTool::animationWithFrameName(name_each, -1, delay);
m_HeroSprite->runAction(action);
m_bIsJumping = true;
}
// 跳落
void Hero::JumpDownAnimation(const char *name_each, float delay, bool run_directon)
{
// 调整方向
if (HeroDirecton != run_directon)
{
HeroDirecton = run_directon;
m_HeroSprite->setFlippedX(run_directon);
}
if (IsHurt || IsAttack)
return;
/* 创建动画动作 */
Animate* action = ActionTool::animationWithFrameName(name_each, -1, delay);
m_HeroSprite->runAction(action);
m_bIsJumping = true;
}
// 跳完
void Hero::JumpEnd()
{
m_HeroSprite->stopAllActions();//当前精灵停止所有动画
// 恢复精灵原来的初始化贴图
this->removeChild(m_HeroSprite, true);//把原来的精灵删除掉
this->m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(Hero_name));//恢复精灵原来的贴图样子
m_HeroSprite->setFlippedX(HeroDirecton);
this->addChild(m_HeroSprite);
m_bIsJumping = false;
}
// 攻击
void Hero::AttackAnimation(const char *name_each, float delay, bool run_directon)
{
// 调整方向
if (HeroDirecton != run_directon)
{
HeroDirecton = run_directon;
m_HeroSprite->setFlippedX(run_directon);
}
if (IsAttack || m_bIsJumping)
return;
// 创建动作
Animate* act = ActionTool::animationWithFrameName(name_each, 1, delay);
// 创建回调动作,攻击结束后调用AttackEnd()
CallFunc* callFunc = CallFunc::create(this, callfunc_selector(Hero::AttackEnd));
// 创建连续动作
ActionInterval* attackact = Sequence::create(act, callFunc, NULL);
IsAttack = true;
m_HeroSprite->runAction(attackact);
}
// 攻击结束
void Hero::AttackEnd()
{
m_HeroSprite->setFlippedX(HeroDirecton);
IsAttack = false;
if (m_bCanCrazy == true)
{
m_bCanCrazy = false;
m_iCurrentMp = 0;
}
}
// 受伤
void Hero::HurtByMonsterAnimation(const char *name_each, float delay, bool run_directon)
{
if (IsHurt || IsDead)
return;
//受伤优先
if (IsRunning || IsAttack)
{
m_HeroSprite->stopAllActions();//当前精灵停止所有动画
//恢复精灵原来的初始化贴图
this->removeChild(m_HeroSprite, true);//把原来的精灵删除掉
m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(Hero_name));//恢复精灵原来的贴图样子
m_HeroSprite->setFlippedX(HeroDirecton);
this->addChild(m_HeroSprite);
IsRunning = false;
IsAttack = false;
}
Animate* action = ActionTool::animationWithFrameName(name_each, 1, delay);
//创建回调动作,受伤动画结束调用HurtEnd()
CallFunc* callFunc = CallFunc::create(this, callfunc_selector(Hero::HurtByMonsterEnd));
//创建连续动作
ActionInterval* hurtackact = Sequence::create(action, callFunc, NULL);
m_HeroSprite->runAction(hurtackact);
IsHurt = true;
}
// 受伤结束
void Hero::HurtByMonsterEnd()
{
m_iCurrentHp -= 20.0f;
IsHurt = false;
percentage = m_iCurrentHp / m_iTotleHp * 100.0f;
if (m_iCurrentHp < 0.0f)
{
DeadAnimation("dead", 0, HeroDirecton);
}
}
// 死亡
void Hero::DeadAnimation(const char *name_each, float delay, bool run_directon)
{
m_HeroSprite->stopAllActions();
// 调整方向
if (HeroDirecton != run_directon)
{
HeroDirecton = run_directon;
m_HeroSprite->setFlippedX(run_directon);
}
// 创建动作
Animate* act = ActionTool::animationWithFrameName(name_each, 1, delay);
//创建回调动作,攻击结束后调用AttackEnd()
CallFunc* callFunc = CallFunc::create(this, callfunc_selector(Hero::DeadEnd));
//创建连续动作
ActionInterval* attackact = Sequence::create(act, callFunc, NULL);
m_HeroSprite->runAction(attackact);
Director::getInstance()->getScheduler()->setTimeScale(0.5);
}
// 死亡结束
void Hero::DeadEnd()
{
IsDead = true;
//恢复死亡的样子
this->removeChild(m_HeroSprite, true); //把原来的精灵删除掉
m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("monsterDie6.png")); //恢复死亡的样子
m_HeroSprite->setFlippedX(HeroDirecton);
this->addChild(m_HeroSprite);
}
// 判断位置
bool Hero::JudgePosition(Size visibleSize)
{
if (this->getPositionX() > (visibleSize.width / 2.0 + 2.0) || (this->getPositionX() < visibleSize.width / 2.0 - 2.0)) // 精灵到达mid?
return false;
else
return true;//到达中间位置
}