/**
*
* Author:
*oldj
*链接已屏蔽
*
* File: td-obj-monster.js
* @save-up: [td.js, ../td.html]
*
* Create Date: 2010-11-20 12:34:41
* Last Update: 2010-11-22 15:01:45
*
*/
// _TD.a.push begin
_TD.a.push(function (TD) {
// monster 瀵硅薄鐨勫睘鎬с€佹柟娉曘€傛敞鎰忓睘鎬т腑涓嶈鏈夋暟缁勩€佸璞$瓑
// 寮曠敤灞炴€э紝鍚﹀垯澶氫釜瀹炰緥鐨勭浉鍏冲睘鎬т細鍙戠敓鍐茬獊
var monster_obj = {
_init: function (cfg) {
cfg = cfg || {};
this.idx = cfg.idx || 1;
this.difficulty = cfg.difficulty || 1.0;
var attr = TD.getDefaultMonsterAttributes(this.idx);
this.speed = Math.floor(
(attr.speed + this.difficulty - 1) * (Math.random() * 0.5 + 0.75));
if (this.speed < 1) this.speed = 1;
if (this.speed > cfg.max_speed) this.speed = cfg.max_speed;
this.life = this.life0 = Math.floor(attr.life * this.difficulty * (Math.random() + 0.5));
if (this.life < 1) this.life = this.life0 = 1;
this.shield = Math.floor(attr.shield + Math.sqrt(this.difficulty) - 1);
if (this.shield < 0) this.shield = 0;
this.damage = Math.floor((attr.damage || 1) * (0.5 + Math.random()));
if (this.damage < 1) this.damage = 1;
this.money = attr.money || Math.floor(Math.sqrt((this.speed + this.life) * (this.shield + 1) * this.damage));
if (this.money < 1) this.money = 1;
this.color = attr.color || TD.lang.rndRGB();
this.r = this.damage;
if (this.r < 4) this.r = 4;
if (this.r > TD.grid_size / 2 - 4) this.r = TD.grid_size / 2 - 4;
this.render = attr.render;
this.grid = null; // 褰撳墠鏍煎瓙
this.map = null;
this.next_grid = null;
this.way = [];
this.toward = 2; // 榛樿闈㈡湞涓嬫柟
this._dx = 0;
this._dy = 0;
},
caculatePos: function () {
if (!this.map) return;
},
beHit: function (building, damage) {
if (!this.is_valid) return;
damage -= this.shield;
if (damage <= 0) damage = 1;
this.life -= damage;
TD.score += Math.min(
Math.floor(Math.sqrt(damage)), 1
);
if (this.life < 0) {
this.beKilled(building);
}
},
beKilled: function (building) {
if (!this.is_valid) return;
this.life = 0;
this.is_valid = false;
TD.money += this.money;
building.killed ++;
},
arrive: function () {
this.grid = this.next_grid;
this.next_grid = null;
this.checkFinish();
},
findWay: function () {
var _this = this;
var fw = new TD.FindWay(
this.map.grid_x, this.map.grid_y,
this.grid.mx, this.grid.my,
this.map.exit.mx, this.map.exit.my,
function (x, y) {
return _this.map.checkPassable(x, y);
}
);
this.way = fw.way;
delete fw;
},
/**
* 妫€鏌ユ槸鍚﹀凡鍒拌揪缁堢偣
*/
checkFinish: function () {
if (this.grid && this.map && this.grid == this.map.exit) {
TD.life -= this.damage;
TD.wave_damage += this.damage;
if (TD.life <= 0) {
TD.life = 0;
TD.stage.gameover();
} else {
this.pause();
this.del();
}
}
},
beAddToGrid: function (grid) {
this.grid = grid;
this.map = grid.map
this.cx = grid.cx;
this.cy = grid.cy;
this.grid.scene.addElement(this);
},
/**
* 鍙栧緱鏈濆悜
* 鍗充笅涓€涓牸瀛愬湪褰撳墠鏍煎瓙鐨勫摢杈? * 0锛氫笂锛?锛氬彸锛?锛氫笅锛?锛氬乏
*/
getToward: function () {
if (!this.grid || !this.next_grid) return;
if (this.grid.my < this.next_grid.my) {
this.toward = 0;
} else if (this.grid.mx < this.next_grid.mx) {
this.toward = 1;
} else if (this.grid.my > this.next_grid.my) {
this.toward = 2;
} else if (this.grid.mx > this.next_grid.mx) {
this.toward = 3;
}
},
getNextGrid: function () {
if (this.way.length == 0 ||
Math.random() < 0.1 // 鏈?1/10 鐨勬鐜囪嚜鍔ㄩ噸鏂板璺?) {
this.findWay();
}
var next_grid = this.way.shift();
if (next_grid && !this.map.checkPassable(next_grid[0], next_grid[1])) {
this.findWay();
next_grid = this.way.shift();
}
if (!next_grid) {
return;
}
this.next_grid = this.map.getGrid(next_grid[0], next_grid[1]);
this.getToward();
},
step: function () {
if (!this.is_valid || this.is_paused || !this.grid) return;
if (!this.next_grid) {
this.getNextGrid();
}
if (this.cx == this.next_grid.cx && this.cy == this.next_grid.cy) {
this.arrive();
} else {
// 绉诲姩鍒?next grid
var dpx = this.next_grid.cx - this.cx,
dpy = this.next_grid.cy - this.cy,
sx = dpx < 0 ? -1 : 1,
sy = dpy < 0 ? -1 : 1,
speed = this.speed * TD.global_speed;
if (Math.abs(dpx) < speed && Math.abs(dpy) < speed) {
this.cx = this.next_grid.cx;
this.cy = this.next_grid.cy;
this._dx = speed - Math.abs(dpx);
this._dy = speed - Math.abs(dpy);
} else {
this.cx += dpx == 0 ? 0 : sx * (speed + this._dx);
this.cy += dpy == 0 ? 0 : sy * (speed + this._dy);
this._dx = 0;
this._dy = 0;
}
}
},
onClick: function () {
}
};
/**
* @param cfg 閰嶇疆瀵硅薄
* 鑷冲皯闇€瑕佸寘鍚互涓嬮」锛? * {
* life: 鎬墿鐨勭敓鍛藉€? * shield: 鎬墿鐨勯槻寰″€? * speed: 鎬墿鐨勯€熷害
* }
*/
TD.Monster = function (id, cfg) {
//cfg.on_events = ["enter", "out", "click"];
//monster 鏆傛椂涓嶇洃鍚簨浠?var monster = new TD.Element(id, cfg);
TD.lang.mix(monster, monster_obj);
monster._init(cfg);
return monster;
}
}); // _TD.a.push end
更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明
Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有