// 创建8个玩家对象 var player1 = playerFactory('a', 'red'); var player2 = playerFactory('b', 'red'); var player3 = playerFactory('c', 'red'); var player4 = playerFactory('d', 'red');
var player5 = playerFactory('e', 'blue'); var player6 = playerFactory('f', 'blue'); var player7 = playerFactory('g', 'blue'); var player8 = playerFactory('h', 'blue');
1. 使用中介者模式改造
```javascript
// 先定义玩家构造函数
function Player(name, teamColor){
this.name = name; // 角色名字
this.teamColor = teamColor; // 队伍颜色
this.state = 'alive'; // 玩家生存状态
}
Player.prototype.win = function(){
console.log('winner:' + this.name);
};
Player.prototype.lose = function(){
console.log('loser:' + this.name);
};
Player.prototype.die = function(){
this.state = 'dead';
playerDirector.ReceiveMessage('playerDead', this); // 给中介者发送消息,玩家死亡
};
Player.prototype.remove = function(){
playerDirector.ReceiveMessage('removePlayer', this); // 给中介者发送消息,移除一个玩家
};
Player.prototype.changeTeam = function(){
playerDirector.ReceiveMessage('changeTeam', this); // 给中介者发送消息,玩家换队
};
// 实现playerDirector对象
var playDirector = (function(){
var players = {}; // 保存所有玩家
var operations = {}; // 中介者可以执行的操作
// 新增一个玩家
operations.add = function(player){
var teamColor = player.teamColor;
players[teamColor] = players[teamColor] || [];
players[teamColor].push(player);
};
// 移除一个玩家
operations.removePlayer = function(player){
var teamColor = player.teamColor;
var teamPlayers = players[teamColor] || [];
for(var i=teamPlayers.length - 1; i >= 0 ;i --){
if(teamPlayers[i] === player){
teamPlayers.splice(i, 1);
}
}
};
// 玩家换队
operations.changeTeam = function(player, newTeamColor){
operations.removePlayer(player); // 从原队伍中删除
player.teamColor = newTeamColor; // 换颜色
operations.addPlayer(player); // 新增玩家到新的队伍
}
operations.playerDead = function(player){
var teamColor = player.teamColor;
var teamPlayer = players[teamColor];
var all_dead = true;
// 遍历队友列表
for(var i=0, player; player = teamPlayer[i++];){
if(player.state !== 'dead'){
all_dead = false;
break;
}
}
// 如果队友全部死亡
if(all_dead === true){
this.lose();
// 通知所有队友玩家游戏失败
for(var i=0, player; player = teamPlayer[i++];){
player.lose();
}
// 通知所有敌人游戏胜利
for(var color in players){
if(color !== teamColor){
var teamPlayers = players[color];
for(var i=0, player; player = teamPlayers[i++];){
player.win();
}
}
}
}
}
var ReceiveMessage = function(){
var message = Array.prototype.shift.call(arguments);
operations[message].apply(this, arguments);
};
return {
ReciveMessage: ReceiveMessage
}
})();
var playerFactory = function(name, teamColor){
var newPlayer = new Player(name, teamColor);
// 给中介者发送消息,新增玩家(工厂只通过中介playerDirector发送消息和参数,完全不需要关心Player类是怎样操作)
playerDirector.ReceiveMessage('addPlayer', newPlayer);
return newPlayer;
};
// 创建8个玩家对象
var player1 = playerFactory('a', 'red');
var player2 = playerFactory('b', 'red');
var player3 = playerFactory('c', 'red');
var player4 = playerFactory('d', 'red');
var player5 = playerFactory('e', 'blue');
var player6 = playerFactory('f', 'blue');
var player7 = playerFactory('g', 'blue');
var player8 = playerFactory('h', 'blue');