Lingara Lingara 說明文件 學習指南 API 程式庫 App 建立 嵌入 網頁版
語言: 粵語

JVM 同 Android

呢頁係由英文翻譯過嚟嘅。如果兩個版本有出入,以英文頁為準。 睇英文頁

Kotlin 玩家套件喺 JVM 上面行,桌面同 Android 都用得。Java 遊戲就透過佢嘅 Java 外觀層用同一個套件。範例跟住一個 libGDX 遊戲嚟做,佢嘅繪製迴圈天生就啱用嚟清空事件,而同樣嘅呼叫亦都適用於任何遊戲迴圈。

加入套件同佢嘅原生程式庫

套件同佢嘅原生程式庫係 Maven Central 上面各自獨立嘅構件,所以兩樣都要加。桌面遊戲加 lingara-embed-natives-desktop,入面有 Windows、macOS 同 Linux 用嘅程式庫。Android 模組就改為加 lingara-embed-natives-android,佢係一個 AAR。喺 libGDX 專案入面,桌面原生程式庫放喺桌面模組,Android 原生程式庫放喺 Android 模組。

Kotlin 遊戲依賴 lingara-embed-kotlin;Java 遊戲就依賴外觀層 lingara-embed-java。

由你部伺服器攞權杖

套件永遠唔會持有密鑰。將你自己部伺服器嘅權杖路由,同埋帶住玩家工作階段嘅標頭交畀佢,每當需要權杖,套件就會向嗰條路由傳送 POST 請求。你部伺服器用伺服器套件簽發權杖,再回覆權杖同佢嘅到期時間。

喺繪製迴圈入面清空事件

核心喺佢自己嘅執行緒上面做網絡 I/O,永遠唔會呼叫你嘅程式碼。喺你嘅繪製迴圈入面、喺遊戲執行緒上面,清空已經送到嘅嘢,每次最多 256 個事件,然後逐個處理:練習更新、串流嘅 NPC 回覆、錯誤。喺 Java 入面,每個事件會交畀你嘅訪問者(visitor)入面對應嘅方法。喺遊戲嘅 dispose 步驟度關閉套件,咁樣會停止核心同埋釋放佢。

睇 NPC 對話同練習結果,了解呢啲事件帶住啲乜嘢。

程式碼例子嘅語言

Kotlin

// The kit, and the natives for the platform: natives-desktop in a libGDX lwjgl3 module, natives-android in its android module.
implementation("com.getlingara:lingara-embed-kotlin:0.1.0-alpha.6")
implementation("com.getlingara:lingara-embed-natives-desktop:0.1.0-alpha.6")

Java

<!-- The kit, and the natives for the platform (lingara-embed-natives-android in an Android module). -->
<dependency>
  <groupId>com.getlingara</groupId>
  <artifactId>lingara-embed-java</artifactId>
  <version>0.1.0-alpha.6</version>
</dependency>
<dependency>
  <groupId>com.getlingara</groupId>
  <artifactId>lingara-embed-natives-desktop</artifactId>
  <version>0.1.0-alpha.6</version>
</dependency>
程式碼例子嘅語言

Kotlin

// Your server mints the embed token; the kit POSTs {} here whenever it needs one.
private val lingara =
    LingaraEmbed.start(
        EmbedConfig(
            tokenSource =
                TokenSourceConfig.Http(
                    url = "https://my-game.example/lingara/token",
                    headers = listOf(Header("Authorization", Secret("Bearer $session"))),
                ),
            userAgentSuffix = "my-game/1.4",
        ),
    )

Java

// Your server mints the embed token; the kit POSTs {} here whenever it needs one.
lingara =
    LingaraEmbedJava.builder()
        .httpTokenSource(
            "https://my-game.example/lingara/token",
            Map.of("Authorization", "Bearer " + session))
        .userAgentSuffix("my-game/1.4")
        .build();
程式碼例子嘅語言

Kotlin

// In render(), on the game thread: one crossing, at most 256 events.
fun render() {
    lingara.drain(256) { event ->
        when (event) {
            is EmbedEvent.Practice -> showPractice(event.snapshot.question?.prompt)
            is EmbedEvent.NpcTurn -> onNpcTurn(event.update)
            is EmbedEvent.Error -> println("Lingara error: ${event.error.message}")
            else -> Unit
        }
    }
}

// In dispose(): stops the core and frees it.
fun dispose() {
    lingara.close()
}

Java

// In render(), on the game thread: one crossing, at most 256 events, each to its visitor method.
void render() {
  lingara.drain(256, event -> LingaraEmbedJava.visit(event, visitor));
}

// In dispose(), or let try-with-resources do it.
@Override
public void close() {
  lingara.close();
}