GameApi.getTickRate()、getBaseTickRate()
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/1215
以下節錄自 @chronodivide/game-api
之型別宣告,可看到 GameApi
提供 getTickRate()
及 getBaseTickRate()
兩個方法:
366 */
367 generateRandom(): number;
368 /** Current game speed in ticks per real-time second. IMPORTANT: The game speed can change during a game */
369 getTickRate(): number;
370 /** Game speed in ticks per in-game second (at game speed 4) */
371 getBaseTickRate(): number;
372 getCurrentTick(): number;
在壓縮後的實作檔中,兩個方法分別回傳目前遊戲速度(依實際秒數)及基礎速度常數:
getTickRate() { return Mp(this, Lp, "f").speed.value * Us.BASE_TICKS_PER_SECOND }
getBaseTickRate() { return Us.BASE_TICKS_PER_SECOND }
BASE_TICKS_PER_SECOND
本身在同檔案中定義為 15:
...60/(6-e),t/Us.BASE_TICKS_PER_SECOND}}Us.BASE_TICKS_PER_SECOND=15;class Ws...
作用
getTickRate()
回傳「每秒鐘執行多少個 tick」。此值會隨遊戲速度調整而改變,例如快速或慢速模式。
getBaseTickRate()
回傳在速度 4(標準速度)下,每個「遊戲秒」包含的 tick 數;根據上述常數,其值固定為 15。
這兩個方法可用於將 tick 計數轉換成真實時間或遊戲內時間。例如在 src/bot/bot.ts
中,Bot
依 getTickRate()
取得當前速度後計算自身更新頻率:
36 const gameRate = game.getTickRate();
37 const botApm = 300;
38 const botRate = botApm / 60;
39 this.tickRatio = Math.ceil(gameRate / botRate);
在 awareness.ts
裡也藉由 getTickRate()
計算過去 60 秒更新過的區塊比例:
153 let updateRatio = sectorCache?.getSectorUpdateRatio(game.getCurrentTick() - game.getTickRate() * 60);
簡單示例
以下程式展示如何利用這兩個方法取得遊戲速度並換算時間:
import { cdapi, Bot, GameApi } from "@chronodivide/game-api";
class SpeedInfoBot extends Bot {
onGameStart(game: GameApi) {
const tickRate = game.getTickRate();
const baseRate = game.getBaseTickRate();
console.log(`Current tick rate: ${tickRate} ticks/sec`);
console.log(`Base tick rate: ${baseRate} ticks/game-sec`);
const currentSeconds = game.getCurrentTick() / tickRate;
console.log(`Elapsed real seconds: ${currentSeconds.toFixed(2)}`);
}
}
async function main() {
await cdapi.init(process.env.MIX_DIR!);
await cdapi.createGame({
agents: [new SpeedInfoBot("SpeedBot", "Americans")],
mapName: "mp03t4.map",
shortGame: true,
online: false,
});
}
main().catch(console.error);
此程式在遊戲開始時輸出目前每秒 tick 數與基準 tick 數,並換算已經過的「真實秒」。
藉由 getTickRate()
配合 getBaseTickRate()
,Bot
能夠根據遊戲速度調整自身更新頻率或計時邏輯。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at