Lingara Lingara 說明文件 學習指南 API 函式庫 應用程式 起造 嵌入 網頁版
語言: 閩南語

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。

對你的伺服器提權杖

套件從來袂保管密鑰。共你家己伺服器的權杖路由,佮紮玩家工作階段的標頭交予伊,套件需要權杖的時就會送請求去遐。你的伺服器用一个伺服器套件簽發,閣用權杖佮伊的到期時間來回應。

佇你的繪圖迴圈內底清事件

核心佇伊家己的執行緒頂懸行網路 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();
}