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();
}