Skip to content

Commit

Permalink
添加怪物死亡时的爆炸特效。
Browse files Browse the repository at this point in the history
  • Loading branch information
oldj committed Jan 1, 2011
1 parent 4fc471a commit c03f49d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/js/td-lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ _TD.a.push(function (TD) {

return "#" + this._rndRGB2(r) + this._rndRGB2(g) + this._rndRGB2(b);
},
/**
* 将一个 rgb 色彩字符串转化为一个数组
* eg: '#ffffff' => [255, 255, 255]
* @param rgb_str {String} rgb色彩字符串,类似于“#f8c693”
*/
rgb2Arr: function (rgb_str) {
if (rgb_str.length != 7) return [0, 0, 0];

var r = rgb_str.substr(1, 2),
g = rgb_str.substr(3, 2),
b = rgb_str.substr(3, 2);

return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)];
},

/**
* 生成一个长度为 n 的随机字符串
Expand Down
64 changes: 64 additions & 0 deletions src/js/td-obj-monster.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ _TD.a.push(function (TD) {

TD.money += this.money;
building.killed ++;

TD.MonsterExplode(this.id + "-explode", {monster: this});
},
arrive: function () {
this.grid = this.next_grid;
Expand Down Expand Up @@ -307,6 +309,68 @@ _TD.a.push(function (TD) {
monster._init(cfg);

return monster;
};


/**
* 怪物死亡时的爆炸效果对象
*/
var monster_explode_obj = {
_init: function (cfg) {
cfg = cfg || {monster: null};
if (!cfg.monster) return;

var rgb = TD.lang.rgb2Arr(cfg.monster.color);
this.cx = cfg.monster.cx;
this.cy = cfg.monster.cy;
this.r = cfg.monster.r;
this.step_level = cfg.monster.step_level;
this.render_level = cfg.monster.render_level;

this.rgb_r = rgb[0];
this.rgb_g = rgb[1];
this.rgb_b = rgb[2];
this.rgb_a = 1;

this.wait = this.wait0 = TD.exp_fps_half;

cfg.monster.grid.scene.addElement(this);
},
step: function () {
if (!this.is_valid) return;

this.wait --;
this.r ++;

this.is_valid = this.wait > 0;
this.rgb_a = this.wait / this.wait0;
},
render: function () {
var ctx = TD.ctx;

ctx.fillStyle = "rgba(" + this.rgb_r + "," + this.rgb_g + ","
+ this.rgb_b + "," + this.rgb_a + ")";
ctx.beginPath();
ctx.arc(this.cx, this.cy, this.r, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fill();
}
};

/**
* @param cfg {Object} 配置对象
* 至少需要包含以下项:
* {
* monster: 怪物对象
* }
*/
TD.MonsterExplode = function (id, cfg) {
// cfg.on_events = ["enter", "out"];
var monster_explode = new TD.Element(id, cfg);
TD.lang.mix(monster_explode, monster_explode_obj);
monster_explode._init(cfg);

return monster_explode;
}

}); // _TD.a.push end
Expand Down
2 changes: 1 addition & 1 deletion src/js/td.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var _TD = {
delete this.init; // 一旦初始化运行,即删除这个入口引用,防止初始化方法被再次调用

var i, TD = {
version: "0.1.14", // 版本命名规范参考:https://semver.org/
version: "0.1.15", // 版本命名规范参考:https://semver.org/
is_debug: !!is_debug,
is_paused: true,
width: 16, // 横向多少个格子
Expand Down

0 comments on commit c03f49d

Please sign in to comment.