Ray 低階 API 參考
Low Level API (Engine.*) 是為多樣化的 2D 遊戲製作而設計的。
每個 API 函式都只接受一個引數。 該引數必須是字典,而引數必須以鍵值對方式存放。
骨架
// 請勿在函式外定義變數,這會造成語法錯誤。
// 視窗建立時呼叫。
func setup() {
// 回傳視窗設定。
return {
width: 1280,
height: 720,
title: "My First Game"
};
}
// 遊戲開始時只呼叫一次。
func start() {
// 全域變數應該在這裡定義。
posX = 0;
posY = 0;
// 建立一張白色的 100x100 紋理。
tex = Engine.createColorTexture({
width: 100,
height: 100,
r: 255, g: 255, b: 255, a: 255
});
}
// 每一幀在渲染前呼叫。
func update() {
posX = Engine.mousePosX;
posY = Engine.mousePosY;
}
// 每一幀用來繪製圖形。
func render() {
Engine.draw({ texture: tex, x: posX, y: posY });
}
除錯
print()
這個 API 會列印字串或轉儲物件。 只接受非字典引數。
func dumpEnemies() {
if (Engine.isGamepadXPressed) {
print("[Current emenies]");
print(enemies);
}
}
時間
絕對時間
| 變數 | 說明 |
|---|---|
| Engine.millisec | 毫秒時間。 |
func update() {
// 取得距離上次呼叫經過的秒數。
var curTime = Engine.millisec;
var dt = (curTime - lastTime) * 0.001;
lastTime = curTime;
updateCharacters(dt);
}
Engine.getDate()
回傳日期字典。
func frame() {
var date = Engine.getDate({});
var year = date.year;
var month = date.month;
var day = date.day;
var hour = date.hour;
var min = date.minute;
var sec = date.second;
}
輸入
以下是變數,不是函式。
| 變數 | 說明 |
|---|---|
| Engine.mousePosX | 滑鼠 X 座標。 |
| Engine.mousePosY | 滑鼠 Y 座標。 |
| Engine.isMouseLeftPressed | 滑鼠左鍵狀態。 |
| Engine.isMouseRightPressed | 滑鼠右鍵狀態。 |
| Engine.isEscapeKeyPressed | Esc 鍵狀態。 |
| Engine.isReturnKeyPressed | Enter 鍵狀態。 |
| Engine.isSpaceKeyPressed | 空白鍵狀態。 |
| Engine.isTabKeyPressed | Tab 鍵狀態。 |
| Engine.isBackspaceKeyPressed | Backspace 鍵狀態。 |
| Engine.isDeleteKeyPressed | Delete 鍵狀態。 |
| Engine.isHomeKeyPressed | Home 鍵狀態。 |
| Engine.isEndKeyPressed | End 鍵狀態。 |
| Engine.isPageupKeyPressed | PageUp 鍵狀態。 |
| Engine.isPagedownKeyPressed | PageDown 鍵狀態。 |
| Engine.isShiftKeyPressed | Shift 鍵狀態。 |
| Engine.isControlKeyPressed | Ctrl 鍵狀態。 |
| Engine.isAltKeyPressed | Alt 鍵狀態。 |
| Engine.isUpKeyPressed | 上方向鍵狀態。 |
| Engine.isDownKeyPressed | 下方向鍵狀態。 |
| Engine.isRightKeyPressed | 右方向鍵狀態。 |
| Engine.isLeftKeyPressed | 左方向鍵狀態。 |
| Engine.isAKeyPressed | A 鍵狀態。 |
| Engine.isBKeyPressed | B 鍵狀態。 |
| Engine.isCKeyPressed | C 鍵狀態。 |
| Engine.isDKeyPressed | D 鍵狀態。 |
| Engine.isEKeyPressed | E 鍵狀態。 |
| Engine.isFKeyPressed | F 鍵狀態。 |
| Engine.isGKeyPressed | G 鍵狀態。 |
| Engine.isHKeyPressed | H 鍵狀態。 |
| Engine.isIKeyPressed | I 鍵狀態。 |
| Engine.isJKeyPressed | J 鍵狀態。 |
| Engine.isKKeyPressed | K 鍵狀態。 |
| Engine.isLKeyPressed | L 鍵狀態。 |
| Engine.isMKeyPressed | M 鍵狀態。 |
| Engine.isNKeyPressed | N 鍵狀態。 |
| Engine.isOKeyPressed | O 鍵狀態。 |
| Engine.isPKeyPressed | P 鍵狀態。 |
| Engine.isQKeyPressed | Q 鍵狀態。 |
| Engine.isRKeyPressed | R 鍵狀態。 |
| Engine.isSKeyPressed | S 鍵狀態。 |
| Engine.isTKeyPressed | T 鍵狀態。 |
| Engine.isUKeyPressed | U 鍵狀態。 |
| Engine.isVKeyPressed | V 鍵狀態。 |
| Engine.isWKeyPressed | W 鍵狀態。 |
| Engine.isXKeyPressed | X 鍵狀態。 |
| Engine.isYKeyPressed | Y 鍵狀態。 |
| Engine.isZKeyPressed | Z 鍵狀態。 |
| Engine.is1KeyPressed | 1 鍵狀態。 |
| Engine.is2KeyPressed | 2 鍵狀態。 |
| Engine.is3KeyPressed | 3 鍵狀態。 |
| Engine.is4KeyPressed | 4 鍵狀態。 |
| Engine.is5KeyPressed | 5 鍵狀態。 |
| Engine.is6KeyPressed | 6 鍵狀態。 |
| Engine.is7KeyPressed | 7 鍵狀態。 |
| Engine.is8KeyPressed | 8 鍵狀態。 |
| Engine.is9KeyPressed | 9 鍵狀態。 |
| Engine.is0KeyPressed | � 鍵狀態。 |
| Engine.isF1KeyPressed | F1 鍵狀態。 |
| Engine.isF2KeyPressed | F2 鍵狀態。 |
| Engine.isF3KeyPressed | F3 鍵狀態。 |
| Engine.isF4KeyPressed | F4 鍵狀態。 |
| Engine.isF5KeyPressed | F5 鍵狀態。 |
| Engine.isF6KeyPressed | F6 鍵狀態。 |
| Engine.isF7KeyPressed | F7 鍵狀態。 |
| Engine.isF8KeyPressed | F8 鍵狀態。 |
| Engine.isF9KeyPressed | F9 鍵狀態。 |
| Engine.isF10KeyPressed | F10 鍵狀態。 |
| Engine.isF11KeyPressed | F11 鍵狀態。 |
| Engine.isF12KeyPressed | F12 鍵狀態。 |
| Engine.isGamepadUpPressed | 手把上方向鍵狀態。 |
| Engine.isGamepadDownPressed | 手把下方向鍵狀態。 |
| Engine.isGamepadLeftPressed | 手把左方向鍵狀態。 |
| Engine.isGamepadRightPressed | 手把右方向鍵狀態。 |
| Engine.isGamepadAPressed | 手把 A 按鈕狀態。 |
| Engine.isGamepadBPressed | 手把 B 按鈕狀態。 |
| Engine.isGamepadXPressed | 手把 X 按鈕狀態。 |
| Engine.isGamepadYPressed | 手把 Y 按鈕狀態。 |
| Engine.isGamepadLPressed | 手把 L 按鈕狀態。 |
| Engine.isGamepadRPressed | 手把 R 按鈕狀態。 |
| Engine.gamepadAnalogX1 | 手把類比 1 X 軸(-32768, 32767) |
| Engine.gamepadAnalogY1 | 手把類比 1 Y 軸(-32768, 32767) |
| Engine.gamepadAnalogX2 | 手把類比 2 X 軸(-32768, 32767) |
| Engine.gamepadAnalogY2 | 手把類比 2 Y 軸(-32768, 32767) |
| Engine.gamepadAnalogL | 手把類比 L(-32768, 32767) |
| Engine.gamepadAnalogR | 手把類比 R(-32768, 32767) |
func update() { if (Engine.isMouseLeftPressed) { player.x = player.x + 100; } }
Rendering
Engine.createColorTexture()
此 API 會以指定顏色建立一個材質貼圖並回傳。
| Argument Name | Description |
|---|---|
| width | 貼圖寬度。 |
| height | 貼圖高度。 |
| r | 紅色。 |
| g | 綠色。 |
| b | 藍色。 |
| a | Alpha 值。 |
func createBlockTexture() { blockTex = Engine.createColorTexture({ width: 16, height: 16, r: 255, g: 255, b: 255, a: 255 }); }
Engine.loadTexture()
此 API 會從資產載入貼圖並回傳。
| Argument Name | Description |
|---|---|
| file | 要載入的檔案名稱。 |
` func loadPlayerTexture() { playerTex = Engine.loadTexture({ file: "player.png" });
var width = playerTex.width; var height = playerTex.height; } `
Engine.destroyTexture()
此 API 會銷毀貼圖。
| Argument Name | Description |
|---|---|
| texture | 貼圖。 |
func destroyPlayerTexture() { Engine.loadTexture({ texture: playerTex }); }
Engine.renderTexture()
此 API 會將貼圖渲染到畫面上。
| Argument Name | Description |
|---|---|
| dstLeft | 螢幕座標 X。 |
| dstTop | 螢幕座標 Y。 |
| dstWidth | 螢幕上的寬度。 |
| dstHeight | 螢幕上的高度。 |
| texture | 貼圖。 |
| srcLeft | 貼圖左上角 X。 |
| srcTop | 貼圖左上角 Y。 |
| srcWidth | 貼圖矩形寬度。 |
| srcHeight | 貼圖矩形高度。 |
| alpha | Alpha 值(0-255)。 |
func renderPlayer() { Engine.renderTexture({ dstLeft: playerPos.x, dstTop: playerPos.y, dstWidth: playerTex.width, dstHeight: playerTex.height, texture: playerTex, srcLeft: 0, srcTop: 0, srcWidth: playerTex.width, srcHeight: playerTex.height, alpha: 255 }); }
Engine.draw()
此 API 會將貼圖渲染到畫面上(比 Engine.renderTexture() 更簡單的版本)。
| Argument Name | Description |
|---|---|
| texture | 貼圖。 |
| x | 螢幕座標 X。 |
| y | 螢幕座標 Y。 |
func renderPlayer() { Engine.draw({ texture: playerTex, x: playerPos.x, y: playerPos.y }); }
Engine.renderTexture3D()
此 API 會將貼圖渲染到畫面上。
| Argument Name | Description |
|---|---|
| x1 | 螢幕座標 X1。 |
| y1 | 螢幕座標 Y1。 |
| x2 | 螢幕座標 X2。 |
| y2 | 螢幕座標 Y2。 |
| x3 | 螢幕座標 X3。 |
| y3 | 螢幕座標 Y3。 |
| x4 | 螢幕座標 X4。 |
| y4 | 螢幕座標 Y4。 |
| texture | 貼圖。 |
| srcLeft | 貼圖左上角 X。 |
| srcTop | 貼圖左上角 Y。 |
| srcWidth | 貼圖矩形寬度。 |
| srcHeight | 貼圖矩形高度。 |
| alpha | Alpha 值(0-255)。 |
func renderPlayer3D() { Engine.renderTexture3D({ x1: playerPos.x, y1: playerPos.y, x2: playerPos.x + playerTex.width, y2: playerPos.y, x3: playerPos.x + playerTex.width, y3: playerPos.y + playerTex.height, x4: playerPos.x, y4: playerPos.y + playerTex.height, texture: playerTex, srcLeft: 0, srcTop: 0, srcWidth: playerTex.width, srcHeight: playerTex.height, alpha: 255 }); }
Engine.loadFont()
此 API 會將資產字型檔載入到指定字型槽。
| Argument Name | Description |
|---|---|
| slot | 字型槽索引。(0-3) |
| file | 要載入的檔案名稱。 |
func loadNotoSansFont() { Engine.loadFont({ slot: 0, file: "NotoSans.ttf" }); }
Engine.createTextTexture()
此 API 會建立貼圖並在其上繪製文字。
| Argument Name | Description |
|---|---|
| slot | 字型槽索引。(0-3) |
| text | 要繪製的文字。 |
| size | 字型大小。 |
| r | 紅色。 |
| g | 綠色。 |
| b | 藍色。 |
| a | Alpha 值。 |
func createScoreTexture() { scoreTex = Engine.createTextTexture({ slot: 0, text: "Score: " + score, size: 32, r: 255, g: 255, b: 255, a: 255 }); }
Sound
Engine.playSound()
此 API 會開始播放指定音效資產檔。
| Argument Name | Description |
|---|---|
| stream | 音軌索引。(0-3) |
| file | 要播放的檔案。 |
func playJumpSound() { Engine.playSound({ stream: 0, file: "jump.ogg" }); }
Engine.playSoundLoop()
此 API 會開始播放指定音效資產檔。
| Argument Name | Description |
|---|---|
| stream | 音軌索引。(0-3) |
| file | 要播放的檔案。 |
func playBGM() { Engine.playSoundLoop({ stream: 0, file: "jump.ogg" }); }
Engine.stopSound()
此 API 會停止指定音軌上的音效播放。
| Argument Name | Description |
|---|---|
| stream | 音軌索引。(0-3) |
func playJumpSound() { Engine.stopSound({ stream: 0 }); }
Engine.setSoundVolume()
此 API 會設定指定音軌的音量。
| Argument Name | Description |
|---|---|
| stream | 音軌索引。(0-3,-1 代表主音量) |
| volume | 音量值。(0-1.0) |
func playJumpSound() { Engine.setSoundVolume({ stream: 0, volume: 1.0 }); }
Save Data
Engine.writeSaveData()
此 API 會寫入與 key 字串對應的存檔資料值。 如果存檔資料值太大,這個 API 會失敗。
| Argument Name | Description |
|---|---|
| key | 鍵值字串。 |
| value | 值。(整數、浮點數、陣列或字典) |
Engine.readSaveData()
此 API 會讀取與 key 字串對應的存檔資料值。 回傳值會是一個代表存檔資料值的物件。 當指定的 key 不可用時,此 API 會失敗。
| Argument Name | Description |
|---|---|
| key | 鍵值字串。 |
Engine.checkSaveData()
此 API 會檢查指定 key 字串是否存在存檔資料。 回傳值為布林值。
| Argument Name | Description |
|---|---|
| key | 鍵值字串。 |