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 | 键值字串。 |