GameApi.isPlayerDefeated(playerName)
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/1201
以下界面檔案中可以看到 GameApi
的方法列表,其中 isPlayerDefeated
會依玩家名稱回傳 boolean 值,判斷該玩家是否已被擊敗:
320 #private;
321 mapApi: MapApi;
322 rulesApi: RulesApi;
323 isPlayerDefeated(playerName: string): boolean;
324 areAlliedPlayers(p1Name: string, p2Name: string): boolean;
325 canPlaceBuilding(playerName: string, buildingName: string, tile: Tile): boolean;
326 getBuildingPlacementData(objName: string): BuildingPlacementData;
327 getPlayers(): string[];
328 getPlayerData(playerName: string): PlayerData;
329 getAllTerrainObjects(): number[];
isPlayerDefeated
的實作在打包後的 index.js
中可以找到,核心邏輯只是回傳對應玩家物件的 defeated
屬性:
pi=new Pp(e.rules)}isPlayerDefeated(e){return Mp(this,Lp,"f").getPlayerByName(e).defeated}areAlliedPlayers(e,t){var i=Mp(this,Lp,"f").getPlayerByName(e);if(!i)t
而 PlayerStats
介面中確實有 defeated
這個布林值欄位:
974 export declare interface PlayerStats {
975 name: string;
976 country: Country;
977 ai: boolean;
978 defeated: boolean;
979 credits: number;
980 startLocation: number;
981 }
使用範例
以下是一段簡化的 TypeScript 範例,展示如何在自訂 bot 中利用 isPlayerDefeated
檢查每位玩家狀態:
import { cdapi, Bot, GameApi } from "@chronodivide/game-api";
class MonitorBot extends Bot {
onGameStart(game: GameApi) {
// 遊戲開始時列出所有玩家
for (const name of game.getPlayers()) {
const defeated = game.isPlayerDefeated(name);
console.log(`${name} defeated: ${defeated}`);
}
}
onGameTick(game: GameApi) {
// 每回合檢查存活的玩家
const survivors = game.getPlayers()
.filter(p => !game.isPlayerDefeated(p));
if (survivors.length <= 1) {
console.log("剩餘玩家:", survivors.join(", "));
}
}
}
async function main() {
await cdapi.init(process.env.MIX_DIR!);
const game = await cdapi.createGame({
agents: [new MonitorBot("BotA", "Americans"), new MonitorBot("BotB", "French")],
mapName: "mp03t4.map",
shortGame: true
});
while (!game.isFinished()) {
await game.update();
}
game.saveReplay();
game.dispose();
}
main().catch(console.error);
此範例透過 game.isPlayerDefeated(name)
判斷各玩家是否已經被擊敗,並在遊戲迴圈中持續監視剩下的玩家。
結合 GameInstanceApi.isFinished()
等其他方法,即可在所有敵手被消滅時結束或統計結果。
總結來說,GameApi.isPlayerDefeated(playerName)
只是直接讀取內部的「是否被擊敗」標記,讓 bot 或其他工具隨時瞭解某位玩家是否已經無法再參與比賽。使用時只需傳入玩家名稱並取得返回值即可。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at