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.isEscapeKeyPressedEsc 鍵狀態。
Engine.isReturnKeyPressedEnter 鍵狀態。
Engine.isSpaceKeyPressed空白鍵狀態。
Engine.isTabKeyPressedTab 鍵狀態。
Engine.isBackspaceKeyPressedBackspace 鍵狀態。
Engine.isDeleteKeyPressedDelete 鍵狀態。
Engine.isHomeKeyPressedHome 鍵狀態。
Engine.isEndKeyPressedEnd 鍵狀態。
Engine.isPageupKeyPressedPageUp 鍵狀態。
Engine.isPagedownKeyPressedPageDown 鍵狀態。
Engine.isShiftKeyPressedShift 鍵狀態。
Engine.isControlKeyPressedCtrl 鍵狀態。
Engine.isAltKeyPressedAlt 鍵狀態。
Engine.isUpKeyPressed上方向鍵狀態。
Engine.isDownKeyPressed下方向鍵狀態。
Engine.isRightKeyPressed右方向鍵狀態。
Engine.isLeftKeyPressed左方向鍵狀態。
Engine.isAKeyPressedA 鍵狀態。
Engine.isBKeyPressedB 鍵狀態。
Engine.isCKeyPressedC 鍵狀態。
Engine.isDKeyPressedD 鍵狀態。
Engine.isEKeyPressedE 鍵狀態。
Engine.isFKeyPressedF 鍵狀態。
Engine.isGKeyPressedG 鍵狀態。
Engine.isHKeyPressedH 鍵狀態。
Engine.isIKeyPressedI 鍵狀態。
Engine.isJKeyPressedJ 鍵狀態。
Engine.isKKeyPressedK 鍵狀態。
Engine.isLKeyPressedL 鍵狀態。
Engine.isMKeyPressedM 鍵狀態。
Engine.isNKeyPressedN 鍵狀態。
Engine.isOKeyPressedO 鍵狀態。
Engine.isPKeyPressedP 鍵狀態。
Engine.isQKeyPressedQ 鍵狀態。
Engine.isRKeyPressedR 鍵狀態。
Engine.isSKeyPressedS 鍵狀態。
Engine.isTKeyPressedT 鍵狀態。
Engine.isUKeyPressedU 鍵狀態。
Engine.isVKeyPressedV 鍵狀態。
Engine.isWKeyPressedW 鍵狀態。
Engine.isXKeyPressedX 鍵狀態。
Engine.isYKeyPressedY 鍵狀態。
Engine.isZKeyPressedZ 鍵狀態。
Engine.is1KeyPressed1 鍵狀態。
Engine.is2KeyPressed2 鍵狀態。
Engine.is3KeyPressed3 鍵狀態。
Engine.is4KeyPressed4 鍵狀態。
Engine.is5KeyPressed5 鍵狀態。
Engine.is6KeyPressed6 鍵狀態。
Engine.is7KeyPressed7 鍵狀態。
Engine.is8KeyPressed8 鍵狀態。
Engine.is9KeyPressed9 鍵狀態。
Engine.is0KeyPressed� 鍵狀態。
Engine.isF1KeyPressedF1 鍵狀態。
Engine.isF2KeyPressedF2 鍵狀態。
Engine.isF3KeyPressedF3 鍵狀態。
Engine.isF4KeyPressedF4 鍵狀態。
Engine.isF5KeyPressedF5 鍵狀態。
Engine.isF6KeyPressedF6 鍵狀態。
Engine.isF7KeyPressedF7 鍵狀態。
Engine.isF8KeyPressedF8 鍵狀態。
Engine.isF9KeyPressedF9 鍵狀態。
Engine.isF10KeyPressedF10 鍵狀態。
Engine.isF11KeyPressedF11 鍵狀態。
Engine.isF12KeyPressedF12 鍵狀態。
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 NameDescription
width貼圖寬度。
height貼圖高度。
r紅色。
g綠色。
b藍色。
aAlpha 值。

func createBlockTexture() { blockTex = Engine.createColorTexture({ width: 16, height: 16, r: 255, g: 255, b: 255, a: 255 }); }

Engine.loadTexture()

此 API 會從資產載入貼圖並回傳。

Argument NameDescription
file要載入的檔案名稱。

` func loadPlayerTexture() { playerTex = Engine.loadTexture({ file: "player.png" });

var width = playerTex.width; var height = playerTex.height; } `

Engine.destroyTexture()

此 API 會銷毀貼圖。

Argument NameDescription
texture貼圖。

func destroyPlayerTexture() { Engine.loadTexture({ texture: playerTex }); }

Engine.renderTexture()

此 API 會將貼圖渲染到畫面上。

Argument NameDescription
dstLeft螢幕座標 X。
dstTop螢幕座標 Y。
dstWidth螢幕上的寬度。
dstHeight螢幕上的高度。
texture貼圖。
srcLeft貼圖左上角 X。
srcTop貼圖左上角 Y。
srcWidth貼圖矩形寬度。
srcHeight貼圖矩形高度。
alphaAlpha 值(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 NameDescription
texture貼圖。
x螢幕座標 X。
y螢幕座標 Y。

func renderPlayer() { Engine.draw({ texture: playerTex, x: playerPos.x, y: playerPos.y }); }

Engine.renderTexture3D()

此 API 會將貼圖渲染到畫面上。

Argument NameDescription
x1螢幕座標 X1。
y1螢幕座標 Y1。
x2螢幕座標 X2。
y2螢幕座標 Y2。
x3螢幕座標 X3。
y3螢幕座標 Y3。
x4螢幕座標 X4。
y4螢幕座標 Y4。
texture貼圖。
srcLeft貼圖左上角 X。
srcTop貼圖左上角 Y。
srcWidth貼圖矩形寬度。
srcHeight貼圖矩形高度。
alphaAlpha 值(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 NameDescription
slot字型槽索引。(0-3)
file要載入的檔案名稱。

func loadNotoSansFont() { Engine.loadFont({ slot: 0, file: "NotoSans.ttf" }); }

Engine.createTextTexture()

此 API 會建立貼圖並在其上繪製文字。

Argument NameDescription
slot字型槽索引。(0-3)
text要繪製的文字。
size字型大小。
r紅色。
g綠色。
b藍色。
aAlpha 值。

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 NameDescription
stream音軌索引。(0-3)
file要播放的檔案。

func playJumpSound() { Engine.playSound({ stream: 0, file: "jump.ogg" }); }

Engine.playSoundLoop()

此 API 會開始播放指定音效資產檔。

Argument NameDescription
stream音軌索引。(0-3)
file要播放的檔案。

func playBGM() { Engine.playSoundLoop({ stream: 0, file: "jump.ogg" }); }

Engine.stopSound()

此 API 會停止指定音軌上的音效播放。

Argument NameDescription
stream音軌索引。(0-3)

func playJumpSound() { Engine.stopSound({ stream: 0 }); }

Engine.setSoundVolume()

此 API 會設定指定音軌的音量。

Argument NameDescription
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 NameDescription
key鍵值字串。
value值。(整數、浮點數、陣列或字典)

Engine.readSaveData()

此 API 會讀取與 key 字串對應的存檔資料值。 回傳值會是一個代表存檔資料值的物件。 當指定的 key 不可用時,此 API 會失敗。

Argument NameDescription
key鍵值字串。

Engine.checkSaveData()

此 API 會檢查指定 key 字串是否存在存檔資料。 回傳值為布林值。

Argument NameDescription
key鍵值字串。