MapApi.isPassableTile(tile, speedType, onBridge, subCell)
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/1230
isPassableTile
是 MapApi
中判斷地塊是否可供單位通行的函式。在型別宣告中可看到兩個重載:其中一個需要明確傳入 subCell
參數
getTile(rx: number, ry: number): Tile | undefined;
getTilesInRect(rectangle: Rectangle): Tile[];
getTilesInRect(baseTile: Tile, size: Size): Tile[];
getObjectsOnTile(tile: Tile): number[];
hasBridgeOnTile(tile: Tile): boolean;
hasHighBridgeOnTile(tile: Tile): boolean;
isPassableTile(tile: Tile, speedType: SpeedType, onBridge: boolean, subCell: boolean): boolean;
/** @deprecated Use overload with explicit subCell instead */
isPassableTile(tile: Tile, speedType: SpeedType, onBridge: boolean): boolean;
在壓縮後的實作中,isPassableTile
的主要邏輯如下【F:node_modules/@chronodivide/game-api/dist/index.js†byte787330-787550】
isPassableTile(e,t,i,s){
return s = s ?? t === ti.Foot,
0 < Op(this,Cp,"f").terrain.getPassableSpeed(e, t, s, i)
}
-
tile
:要檢查的Tile
物件。 -
speedType
:單位的移動類型(步行、履帶車等)。 -
onBridge
:是否視為在橋面上移動。 -
subCell
:是否計算單位在 tile 內的次格(若省略,預設在speedType
為Foot
時才啟用)。
方法內部透過 terrain.getPassableSpeed(...)
取得對應地塊的可通行速度,只要結果大於 0 即表示該格可通行。
範例:檢查指定位置的通行性
import { cdapi, Bot, GameApi, SpeedType } from "@chronodivide/game-api";
class PassableCheckBot extends Bot {
onGameStart(game: GameApi) {
const tile = game.mapApi.getTile(50, 50);
if (!tile) {
console.log("Tile out of bounds");
return;
}
const isOnBridge = tile.onBridgeLandType !== undefined;
// 判斷步行單位在此格是否能移動 (以 subCell 預設邏輯)
const walkable = game.mapApi.isPassableTile(
tile,
SpeedType.Foot,
isOnBridge,
true
);
console.log(`Tile (50,50) walkable: ${walkable}`);
}
}
async function main() {
await cdapi.init(process.env.MIX_DIR!);
await cdapi.createGame({
agents: [new PassableCheckBot("Scout", "Americans")],
mapName: "mp03t4.map",
shortGame: true,
online: false,
});
}
main().catch(console.error);
以上程式在遊戲開始時取出 (50,50) 的 Tile
,並以 SpeedType.Foot
判定該格是否能讓步兵通過。
藉由 isPassableTile
,Bot
能根據單位類型與橋面狀態精準掌握地形通行限制,方便在路徑規劃或建築擺放時避免走入無法通過的格子。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at