/
Hero.js
362 lines (320 loc) · 8.05 KB
/
Hero.js
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* @file 英雄
* @author yumao [zhangyu38@baidu.com]
*/
define(function (require) {
var global = require('common/global');
var config = require('level/config');
/**
* 英雄类
*
* @exports Hero
* @class
* @param {Phaser.Game} game 游戏
* @param {Object} options 参数项
* @param {number} options.index 序号
*/
var Hero = function (game, options) {
/**
* 游戏
*
* @type {Phaser.Game}
*/
this.game = game;
/**
* 序号
*
* @type {number}
*/
this.index = options.index;
/**
* 精灵图
*
* @type {?Phaser.Sprite}
*/
this.sprite = null;
/**
* 是否倒置
*
* @type {?boolean}
*/
this.upsideDown = false;
this._init();
};
/**
* 初始化
*
* @private
*/
Hero.prototype._init = function () {
this._initConfig();
this._initSprite();
};
/**
* 初始化配置
*
* @private
*/
Hero.prototype._initConfig = function () {
var heroConfig = require('common/global').herosConfig[this.index];
for (var key in heroConfig) {
if (heroConfig.hasOwnProperty(key)) {
this[key] = heroConfig[key];
}
}
};
/**
* 初始化精灵图
*
* @private
*/
Hero.prototype._initSprite = function () {
var game = this.game;
var sprite = game.add.sprite(
(game.width + this.width * this.scale) / 2,
game.height - config.initialHorizon
);
sprite.scale.set(this.scale);
sprite.anchor.set(1, 1);
this.sprite = sprite;
this.down();
};
/**
* 设置为可玩状态
*
* @public
* @param {boolean} useTween 是否使用动态过渡
* @param {Function=} cb 回调
*/
Hero.prototype.setForPlay = function (useTween, cb) {
var game = this.game;
var x = config.currEdgeX + this.paddingRight;
var y = game.height - config.horizon;
if (useTween) {
var move = game.add.tween(this.sprite)
.to({x: x, y: y}, 200, Phaser.Easing.Linear.None);
move.onComplete.add(
function () {
this.down();
cb && cb();
},
this
);
this._act('walk', true);
move.start();
}
else {
var sprite = this.sprite;
sprite.x = x;
sprite.y = y;
}
};
/**
* 行使动作
*
* @private
* @param {string} action 动作名称
* @param {boolean=} loop 是否循环, 缺省 false
*/
Hero.prototype._act = function (action, loop) {
var sprite = this.sprite;
var key = [this.name, action].join('-');
// 若已是当前 key, 则不重载贴图 (loadTexture 较耗内存)
if (sprite.key === key) {
return;
}
sprite.loadTexture(key);
// 添加/运行动画
!sprite.animations.getAnimation(action) && sprite.animations.add(action);
sprite.animations.play(action, this.actions[action].fps, !!loop);
};
/**
* 向下动作
*
* @public
*/
Hero.prototype.down = function () {
this._act('down', true);
};
/**
* 向上动作
*
* @public
*/
Hero.prototype.up = function () {
this._act('up', true);
};
/**
* 踢腿动作
*
* @public
*/
Hero.prototype.kick = function () {
this._act('kick');
};
/**
* 行走
*
* @public
* @param {number} targetX 目标位置 X 坐标
* @param {Function=} cb 回调
*/
Hero.prototype.walk = function (targetX, cb) {
var game = this.game;
var sprite = this.sprite;
// 置顶,避免被新生成的棒棒遮盖
sprite.bringToTop();
// 目标位置不越过屏宽
var maxX = game.width;
targetX += this.paddingRight;
if (targetX > maxX) {
targetX = maxX;
}
var duration = (targetX - sprite.x) * 3;
// 平移动画
var move = game.add.tween(sprite)
.to({x: targetX}, duration, Phaser.Easing.Linear.None);
move.onComplete.add(
function () {
// 动作还原为初始
this.down();
cb && cb();
},
this
);
// 装配行走动作
this._act('walk', true);
move.start();
};
/**
* 取得贴图元素
*
* @public
* @return {Phaser.Sprite} 贴图
*/
Hero.prototype.getEl = function () {
return this.sprite;
};
/**
* 落下
*
* @public
* @param {Function=} cb 回调
*/
Hero.prototype.fall = function (cb) {
var game = this.game;
var fall = game.add.tween(this.sprite)
.to(
{
// 落下距离考虑倒置因素
y: game.height + (!this.upsideDown ? this.height : 0)
},
300, Phaser.Easing.Quadratic.In, false, 100
);
cb && fall.onComplete.add(cb);
fall.start();
};
/**
* 翻转
*
* @public
*/
Hero.prototype.flip = function () {
this.sprite.scale.y *= -1;
this.upsideDown = !this.upsideDown;
};
/**
* 是否倒置
*
* @public
* @return {boolean} 是否倒置
*/
Hero.prototype.isUpsideDown = function () {
return this.upsideDown;
};
/**
* 是否倒置
*
* @public
* @return {boolean} 是否倒置
*/
Hero.prototype.getX = function () {
return this.sprite.x;
};
/**
* 取得贴图宽度
*
* @public
* @return {boolean} 贴图宽度
*/
Hero.prototype.getWidth = function () {
return this.sprite.width;
};
/**
* 更换英雄
*
* @public
* @param {number} index 序号
* @param {boolean} temporary 是否临时, 针对有形态切换的英雄
*/
Hero.prototype.change = function (index, temporary) {
// 更新当前所选序号
global.setSelected(index, !temporary);
// 更新各属性
this.index = index;
this._initConfig();
var game = this.game;
var sprite = this.sprite;
sprite.x = (game.width + this.width * this.scale) / 2;
sprite.y = game.height - config.initialHorizon;
sprite.scale.set(this.scale);
this.down();
// 根据英雄超能更新棒棒和平台的属性
var level = game.state.states.level;
var stick = level.stick;
if (stick) {
// 棒棒速度
stick.updateSpeed();
// 棒棒材质
stick.updateTexture();
// 额外棒长
stick.updateExtraLength();
}
var stage = level.stage;
// 食物出现几率
stage.updateFoodProba();
// 最小柱宽
stage.updateMinWidth();
// 奖励点乘数
stage.updateSpotMultiple();
// 奖励点宽度
stage.updateSpotWidth();
if (temporary) {
// 还原当前所选序号
global.setSelected(this.power.previousLife, false);
}
};
/**
* 续命
*
* @public
*/
Hero.prototype.sustainLife = function () {
// 切换为下一形态
this.change(this.power.nextLife, true);
this.setForPlay(false);
};
/**
* 闪烁效果
*
* @public
* @param {Function=} cb 回调
*/
Hero.prototype.twinkle = function (cb) {
var twinkle = this.game.add.tween(this.sprite)
.from({alpha: 0}, 200, Phaser.Easing.Linear.None, true, 0, 1, true);
cb && twinkle.onComplete.add(cb);
twinkle.start();
};
return Hero;
});