|
| 1 | +const GameManager = ( function() { |
| 2 | + // 存储所有玩家 |
| 3 | + const players = [] |
| 4 | + // 操作实体 |
| 5 | + const operations = {} |
| 6 | + // 新增玩家 |
| 7 | + operations.addPlayer = function (player) { |
| 8 | + let teamColor = player.teamColor |
| 9 | + players[teamColor] = players[teamColor] || []; // 如果该颜色的玩家还没有成立队伍,则新成立一个队伍 |
| 10 | + players[teamColor].push(player); // 添加玩家进队伍 |
| 11 | + } |
| 12 | + // 玩家掉线 |
| 13 | + operations.playerDisconnect = function (player) { |
| 14 | + // 玩家队伍颜色 |
| 15 | + let teamColor = player.teamColor |
| 16 | + let teamPlayer = players[teamColor] |
| 17 | + for(let i in teamPlayer) { |
| 18 | + if (teamPlayer[i].name = player.name) { |
| 19 | + teamPlayer.splice(i, 1) |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + // 玩家死亡 |
| 25 | + operations.playerDead = function (player) { |
| 26 | + let teamColor = player.teamColor |
| 27 | + teamPlayers = players[teamColor] |
| 28 | + // 团灭标志 |
| 29 | + let ace_flag = true |
| 30 | + // 设置玩家状态为死亡 |
| 31 | + this.state = 'dead' |
| 32 | + // 遍历队友列表,若没有团灭,则还未失败 |
| 33 | + for(let i in teamPlayers) { |
| 34 | + if (teamPlayers[i].state !== 'dead') { |
| 35 | + ace_flag = false |
| 36 | + break |
| 37 | + } |
| 38 | + } |
| 39 | + // 如果已被团灭 |
| 40 | + if (ace_flag === true) { |
| 41 | + // 己方失败 |
| 42 | + for(let i in teamPlayers) { |
| 43 | + teamPlayers[i].lose() |
| 44 | + } |
| 45 | + // 敌方胜利 |
| 46 | + for(let color in players) { |
| 47 | + if (color !== teamColor) { |
| 48 | + let teamPlayers = players[color] |
| 49 | + teamPlayers.map(player => { |
| 50 | + player.win() |
| 51 | + }) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + function reciveMessage (message, player) { |
| 58 | + operations[message](player) |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + reciveMessage: reciveMessage |
| 63 | + } |
| 64 | +})() |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +class Player { |
| 70 | + constructor(name, teamColor) { |
| 71 | + this.name = name // 英雄名称 |
| 72 | + this.teamColor = teamColor // 队伍颜色 |
| 73 | + this.state = 'alive' // 存活状态 |
| 74 | + } |
| 75 | + // 获胜 |
| 76 | + win() { |
| 77 | + console.log(`Vicotry! ${this.name}`) |
| 78 | + } |
| 79 | + // 失败 |
| 80 | + lose() { |
| 81 | + console.log(`Defeat! ${this.name}`) |
| 82 | + } |
| 83 | + // 死亡方法 |
| 84 | + die() { |
| 85 | + // 设置玩家状态为死亡 |
| 86 | + this.state = 'dead' |
| 87 | + // 向中介者发送死亡的宣告 |
| 88 | + GameManager.reciveMessage('playerDead', this) |
| 89 | + } |
| 90 | + // 玩家掉线 |
| 91 | + disconnect() { |
| 92 | + GameManager.reciveMessage('playerDisconnect', this) |
| 93 | + } |
| 94 | +} |
| 95 | +// 玩家列表 |
| 96 | +const Players = [] |
| 97 | + |
| 98 | +// 定义一个工厂函数来生成玩家 |
| 99 | + |
| 100 | +function playerFactory (name, teamColor) { |
| 101 | + let newPlayer = new Player(name, teamColor) |
| 102 | + // 通知中介者新增玩家 |
| 103 | + GameManager.reciveMessage('addPlayer', newPlayer) |
| 104 | + return newPlayer |
| 105 | +} |
| 106 | + |
| 107 | +// 开始比赛 |
| 108 | +// 蓝色方 |
| 109 | +let hero1 = playerFactory('盖伦', 'Blue') |
| 110 | +let hero2 = playerFactory('皇子', 'Blue') |
| 111 | +let hero3 = playerFactory('拉克丝', 'Blue') |
| 112 | +let hero4 = playerFactory('剑姬', 'Blue') |
| 113 | +let hero5 = playerFactory('赵信', 'Blue') |
| 114 | + |
| 115 | +// 红色方 |
| 116 | +let hero6 = playerFactory('诺手', 'Red') |
| 117 | +let hero7 = playerFactory('德莱文', 'Red') |
| 118 | +let hero8 = playerFactory('卡特琳娜', 'Red') |
| 119 | +let hero9 = playerFactory('乌鸦', 'Red') |
| 120 | +let hero10 = playerFactory('赛恩', 'Red') |
| 121 | + |
| 122 | + |
| 123 | +// 红色方被团灭 |
| 124 | +hero6.die() |
| 125 | +hero7.die() |
| 126 | +hero8.die() |
| 127 | +hero9.die() |
| 128 | +hero10.die() |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +/* 运行结果: |
| 133 | +Defeat! 赛恩 |
| 134 | +Defeat! 诺手 |
| 135 | +Defeat! 德莱文 |
| 136 | +Defeat! 卡特琳娜 |
| 137 | +Defeat! 乌鸦 |
| 138 | +Vicotry! 盖伦 |
| 139 | +Vicotry! 皇子 |
| 140 | +Vicotry! 拉克丝 |
| 141 | +Vicotry! 剑姬 |
| 142 | +Vicotry! 赵信 |
| 143 | +*/ |
0 commit comments