87,979
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
// 经验系统
var Experience = function(param) {
this.name = param.name;
// 经验值
this.expVal = param.expVal;
this.players = [];
}
// 发放经验
Experience.prototype.giveExp = function(player) {
console.log("系统送给[" + player.name + "]" + this.expVal + "点经验.");
}
// 玩家
var Player = function(param) {
this.name = param.name;
}
// 申请经验模式
Player.prototype.apply = function(experience) {
var players = experience.players;
var isExist = false;
for(var i=0,len=players.length;i<len;i++) {
if(players[i] == this) {
isExist = true;
break;
}
}
if(!isExist) {
players.push(this);
}
return this;
}
// 领取经验
Player.prototype.getExp = function(experience) {
var players = experience.players;
for(var i=0,len=players.length;i<len;i++) {
if(players[i] == this) {
experience.giveExp(this);
break;
}
}
return this;
}
// 测试
// 申明玩家
var player1 = new Player({"name":"张三"}); // 玩家1,等级4
var player2 = new Player({"name":"李四"}); // 玩家2,等级12
// 申明经验系统
var exp2000 = new Experience({name:"2000点经验",expVal:2000});
var exp3000 = new Experience({name:"3000点经验",expVal:3000});
// 等级1~10能申请3000点经验
player1.apply(exp3000);
// 等级11以上能申请2000点经验
player2.apply(exp2000);
// 去NPC处领取经验:
player1.getExp(exp3000);
player2.getExp(exp2000);
/*
输出:
系统送给[张三]3000点经验.
系统送给[李四]2000点经验.
*/
</script>
if(players[i] == this) {
experience.giveExp(this);
break;
}
// 定时发放经验
Experience.prototype.giveExperience = function() {
for(var i=0,len=this.players.length;i<len;i++) {
var player = this.players[i];
console.log("系统送给[" + player.name + "]" + this.expVal + "点经验.");
}
}
// 定时发放经验:
exp3000.giveExperience();
exp2000.giveExperience();
/*
输出:
系统送给[张三]3000点经验.
系统送给[李四]2000点经验.
*/