视觉小说
使用 Pixi’VN 创建视觉小说的分步指南,涵盖项目设置、叙事、素材和交互性。
本教程将指导你完成创建第一部视觉小说的过程。
视觉小说(VN)是一种数字互动小说形式。视觉小说通常与电子游戏这一媒介相关联,但它们本身并不总是被标记为电子游戏。它们将文本叙事与静态或动态插图相结合,并具有不同程度的互动性。这种形式较少被称为"novel game",这是日式英语术语 noberu gēmu(ノベルゲーム)的转写,该术语在日语中更常用。
为了测试的目的,在本指南中我们将使用 Pixi’VN 重新创建视觉小说 Breakdown。 Breakdown 是一个短篇故事,具备了视觉小说应有的所有特性。 Breakdown 的创作者 Josh Powlison 已经允许我们出于教育目的使用他的叙事内容❤️。
由于 Pixi’VN 允许你通过选择一种或多种可用的叙事语言来编写自己的叙事内容,因此在每个开发步骤中都会为当前每种可用的语言提供示例。
创建新项目
在开始之前,请确保你已经安装了前置条件。
第一步是创建一个新项目。你可以通过运行以下命令来完成:
npm create pixi-vn@latest然后选择 Visual Novel 模板。
VS Code
如果你使用 VS Code 打开项目,系统会要求你同意运行一些工作区任务,并安装项目推荐的扩展。强烈建议你接受这两项。
你可以在这里找到有关模板、任务和扩展的更多信息。
如果你不想使用 VS Code 任务,可以通过在项目根目录下运行以下命令来实现相同的效果:
npm install
npm start创建角色
现在我们将定义这个故事中的角色。为此,我们将在 /content/characters.ts 文件中定义我们将要使用的角色。
mc 是"Main Character"(主角)的常见缩写。在视觉小说中,使用 mc 作为主角的名字是一种常见做法。
下面使用的 Character 模型定义在 models/Character.ts 中。你可以根据自己的喜好进行修改,例如添加更多与角色相关的属性(例如 surname、age 等)。你可以在这里找到有关此文件的更多信息。
有关如何创建和使用角色的更多信息,请参阅:角色
import Character from "@/models/Character";
import { RegisteredCharacters } from "@drincs/pixi-vn";
export const mc = new Character("mc", {
name: "Me",
});
export const james = new Character("james", {
name: "James",
color: "#0084ac",
});
export const steph = new Character("steph", {
name: "Steph",
color: "#ac5900",
});
export const sly = new Character("sly", {
name: "Sly",
color: "#6d00ac",
});
RegisteredCharacters.add([mc, james, steph, sly]);叙事初稿
Markup
所有模板都支持Markdown和Tailwind CSS,因此我们将在叙事中使用它。
现在我们可以开始编写视觉小说叙事的"初稿"了。我们将创建第一个名为 start 的叙事节点(label),它将是游戏的开始。之后我们可以编写视觉小说中接下来的对话。
这是示例:
=== start ===
james: You're my roommate's replacement, huh?
james: Don't worry, you don't have much to live up to. Just don't use heroin like the last guy, and you' fine!
mc: ...
He thrusts out his hand.
james: James!
mc: ...Peter.
I take his hand and shake.
james: Ooh, Peter! Nice, firm handshake! The last quy always gave me the dead fish. I already think we'r gonna get along fine.
james: Come on in and...
james: ...
james: I know you're both watching, come on out already!
sly: I just wanted to see what the new guy was like. Hey, you, Peter- be nice to our little brother, or you'll have to deal with *us*.
mc: ...
james: Peter, this is Sly. Yes, that is her real name.
I put out my hand.
sly: I'm not shakin' your hand until I decide you're an all-right dude. Sorry, policy.
mc: Fair enough, I'm a pretty scary guy, or so l've been told.
james: The redhead behind her is Stephanie.
// Example of using Tailwind CSS
steph: <span class="inline-block motion-translate-y-loop-25">Hey</span>! Everyone calls me Steph. I'll shake your hand.
// ...
-> DONE在视觉小说中,将文本粘贴到当前对话中通常很有用。例如,暂停一段对话,并在后续的叙事步骤(step)中继续。为此,我们可以使用胶水(glue)功能。
这是示例:
=== start ===
// ...
james: Ooh, [mc]! Nice, firm handshake!
<>The last guy always gave me the dead fish.
<>I already think we're gonna get along fine.
james: Come on in and...
// ...
-> DONE也可以创建一个"暂停"效果,暂时隐藏对话卡片。在 ink 中,你可以使用# pause,而在 JS/TS 中,你可以将 narration.dialogue 设置为 undefined。
=== start ===
// ...
# pause
// ...
-> DONE将叙事拆分为多个叙事节点(labels)
不建议创建非常长的叙事节点(labels)(即使对于线性视觉小说也是如此),而建议创建多个小的叙事节点(labels),并在需要时使用叙事流程控制功能来"调用"它们。
因此,尽管在我们的案例中故事是线性的,它仍将被分为两个叙事节点(labels),第一个是我们刚刚创建的那个(start),第二个将被称为 second_part。
这是示例:
=== start ===
james: You're my roommate's replacement, huh?
james: Don't worry, you don't have much to live up to. Just don't use heroin like the last guy, and you' fine!
mc: ...
He thrusts out his hand.
james: James!
mc: ...Peter.
// ...
-> second_part
=== second_part ===
She enters my room before I'VE even had a chance to. \\n\\n...I could've just come back and gotten the platter later...
She sets it on a desk. I throw my two paper bags down beside the empty bed.
steph: They got you a new mattress, right? That last guy was a druggie, did James tell you that?
sly: *We're* the reason he got expelled!
steph: Sly! If word gets out about that... well, actually, it wouldn't matter, *he's* the one who shot himself up.
I'm fumbling for a new subject.
// ...
-> DONE选择菜单
现在我们将询问玩家是否想继续视觉小说的第二部分。
为此,我们将把 narration.choices 设置为一个由 newChoiceOption(用于跳转或调用某个叙事节点(label))和 newCloseChoiceOption(用于关闭选择菜单而不跳转到任何地方)创建的选择项数组。
你可以在这里找到有关选择菜单的更多信息。
这是示例:
=== start ===
// ...
You want continue to the next part?
* Yes, I want to continue
-> second_part
* No, I want to stop here
-> END
=== second_part ===
// ...
-> DONE编辑角色信息并将其用作变量
现在我将赋予玩家更改 mc 名字的能力。为此,我将要求玩家使用 Pixi’VN 的功能填写一个输入框。
获取输入值后,你可以使用获得的值来设置角色名称。
在 ink 中,_input_value_ 是一个特殊变量,用于保存玩家输入的值。你可以在这里阅读更多相关信息。
这是示例:
=== start ===
// ...
He thrusts out his hand.
# request input type string default Peter
What is your name?
# rename mc { _input_value_ }
// ...
-> DONE现在我们可以在对话中使用角色名称。
这是示例:
VAR steph_fullname = "Stephanie"
=== start ===
// ...
sly: I just wanted to see what the new guy was like. Hey, you, [mc]- be nice to our little brother, or you'll have to deal with *us*.
mc: ...
james: [mc], this is [sly]. Yes, that is her real name.
I put out my hand.
sly: I'm not shakin' your hand until I decide you're an all-right dude. Sorry, policy.
mc: Fair enough, I'm a pretty scary guy, or so l've been told.
james: The redhead behind her is [steph_fullname].
steph: Hey! Everyone calls me [steph]. I'll shake your hand.
She puts out her hand, and I take it.
mc: Thanks, good to meet you, [steph_fullname].
steph: WOW, that is, like, the most perfect handshake I've ever had! Firm, but also gentle. [sly], you *gotta* shake his hand!
// ...
-> DONE定义并加载素材
要加载和操作素材(图片、gif、视频……),,您需要使用 Assets。 Assets 是一个功能丰富的类,来自 PixiJS 库,如果你想了解更多信息,请阅读这里。
最初的步骤之一是选择在哪里保存你的视觉小说素材:你可以使用在线托管服务,也可以将它们保存在项目本地。你可以在这里找到有关这两种选项的更多信息。
如果你选择将素材保存在本地,只需将它们放置在 src/assets 中:模板会自动使用 AssetPack 来分析文件,并将它们定义在 src/assets/manifest.gen.json 中。如果你选择将它们保存在线上,则需要手动编辑 assets/index.ts 来注册它们。
无论哪种情况,每个素材都会被分配一个或多个别名(alias),之后你可以在代码中使用它来引用该素材。
{
"bundles": [
{
"name": "images",
"assets": [
{
"alias": [
"images_icon",
"icon"
],
"src": [
"images/[email protected]",
"images/icon-VA-NtA.webp",
"images/[email protected]",
"images/icon-E_ZC2g.png"
],
"data": {
"tags": {}
}
}
]
}
]
}在我们的案例中,我们将游戏图片保存在托管服务上。因此素材的加载并不是即时的。
为了不让玩家感觉到太多的加载过程,我们应该在游戏的某些阶段将它们分组。在我的案例中,我将在叙事节点(label)开始时加载最常用的图片。
你可以在这里找到有关如何管理加载的更多信息。
这是示例:
=== start ===
# lazyload bundle m01 fm01 fm02
# show image bg bg01-hallway
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-neutral01] xAlign 0.5 yAlign 1 with movein direction right speed 300
james: You're my roommate's replacement, huh?
# show imagecontainer james [m01-body m01-eyes-grin m01-mouth-smile01]
james: Don't worry, you don't have much to live up to. Just don't use heroin like the last guy, and you'll be fine!
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-grin00]
mc: ...
// ...
-> DONE添加背景和角色图片
现在也该考虑视觉部分了。我们将把背景和角色图片添加到视觉小说的画布中。
在计算机图形学中,sprite 是一种整合到更大场景中的二维位图,最常见于 2D 电子游戏中。在 Pixi’VN 中,画布上显示的单个图片(例如背景,或角色的身体/眼睛/嘴巴)就是一个 Sprite。
container 是一种将其他组件(sprite,甚至其他 container)分组的组件,使它们可以作为一个整体进行定位、移动和操作,同时仍然可以访问和编辑每个单独的子元素。
在我们的案例中,每个角色由 3 个 sprite 组成:身体、眼睛和嘴巴。我们使用ImageContainer——一个将 Sprite 子元素分组的 Container——将它们组合在一起以构成角色。你可以在这份文档中找到有关如何添加画布组件的更多信息。
这是示例:
=== start ===
# show image bg bg01-hallway
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-neutral01] xAlign 0.5 yAlign 1
james: You're my roommate's replacement, huh?
# show imagecontainer james [m01-body m01-eyes-grin m01-mouth-smile01]
james: Don't worry, you don't have much to live up to. Just don't use heroin like the last guy, and you'll be fine!
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-grin00]
mc: ...
// ...
-> DONE为了让视觉小说更加生动,你可以使用过渡效果,以动画效果显示或移除画布组件(图片、container……),而不是让它们突然出现或消失。 Pixi’VN 提供了多种内置过渡效果——例如溶解(dissolve)、淡入淡出(fade)、移入/移出(move in/out)、推入/推出(push in/out)和放大/缩小(zoom in/out)——同时也允许你创建自己的自定义过渡效果。
在这个示例中,我们使用移入/移出(move in/out)过渡效果,让 james 从画布外部进入场景。在 ink 中,可以在 # show 命令后添加 with movein(及其参数)来实现;而在 JS/TS 中,可以使用 transitions.moveIn/transitions.moveOut 函数。
你可以在这里找到有关使用过渡效果的更多信息。
这是示例:
=== start ===
// ...
# show image bg bg01-hallway
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-neutral01] xAlign 0.5 yAlign 1 with movein direction right speed 300
james: You're my roommate's replacement, huh?
# show imagecontainer james [m01-body m01-eyes-grin m01-mouth-smile01]
james: Don't worry, you don't have much to live up to. Just don't use heroin like the last guy, and you'll be fine!
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-grin00]
mc: ...
// ...
-> DONE为了让视觉小说更加生动,你可以使用动画。如果你需要设置许多属性,我建议使用 TypeScript,因为这可以给你更多的控制、更多的功能和类型反馈。
在这个示例中,我的动画会将 steph 移出场景,并在下一个叙事步骤(step)中将她重新插入。我还会在 x 轴上镜像她,以确保她面朝正确的方向。
为了让 steph 离开场景后再返回,我将使用 transitions.moveOut 和 transitions.moveIn 函数。对于镜像效果,我将使用 canvas.animate 函数。
首先,我使用 canvas.animate 函数创建一个动画,将 scaleX 属性从 -1(镜像)设置为 1,并使用 autoplay: false,使其不会立即开始。
仅为了说明目的,以下是在 ink 中等效的写法:
# animate steph scaleX 1 options autoplay false completeOnContinue true接着,我使用 transitions.moveIn 函数让 steph 进入场景,并传入动画的 tickerId,以便在过渡效果完成后恢复动画。
我使用 completeOnContinue: true 选项来确保动画在下一个叙事步骤(step)执行之前完成。由于 transitions.moveIn 是一种过渡效果,因此 completeOnContinue 默认值为 true。
有关如何使用动画的更多信息,请参阅这里。
由于我在这个动画中使用了 TypeScript,我为它创建了一个叙事节点(label),这样它也可以从其他语言中被调用。
import { canvas, ImageContainer, newLabel, transitions } from "@drincs/pixi-vn";
export const animation01 = newLabel("animation_01", [
async () => {
let tickerId = canvas.animate<ImageContainer>(
"steph",
{
scaleX: 1,
},
{ autoplay: false, completeOnContinue: true },
);
await transitions.moveIn(
"steph",
{
value: ["fm02-body", "fm02-eyes-joy", "fm02-mouth-smile01"],
options: {
xAlign: 0.8,
yAlign: 1,
scale: { y: 1, x: -1 },
anchor: 0.5,
},
},
{
direction: "right",
ease: "easeInOut",
tickerIdToResume: tickerId,
},
);
},
]);ink
正如这里所解释的,从 ink 中你可以调用用 JS/TS 编写的叙事节点(labels),反之亦然。
现在你可以从主叙事节点(label)(start)调用这个叙事节点(label)(animation_01)。
=== start ===
// ...
# show imagecontainer james [m01-body m01-eyes-grin m01-mouth-grin00]
# show imagecontainer sly [fm01-body fm01-eyes-smile fm01-mouth-smile00]
# show imagecontainer steph [fm02-body fm02-eyes-upset fm02-mouth-nervous00]
# remove image steph with moveout direction left speed 300
[steph_fullname] goes through the opposite door,
-> animation_01 ->
<> and returns with a HUGE tinfoil-covered platter.
// ...
-> DONE声音和音乐
要为你的视觉小说添加声音和音乐,你可以使用 sound 工具。
你可以定义声音频道来管理不同类型的声音(例如 BGM、SFX),并控制它们的音量、暂停、恢复等。建议在游戏开始时定义这些频道,例如在 Game.init 函数的 then 回调中。
在这个示例中,我定义了两个频道:一个用于背景音乐(BGM),一个用于音效(SFX)。我还将 defaultChannelAlias 设置为 SFX 频道,这样如果我在播放声音时没有指定频道,它将默认在 SFX 频道中播放。为了定义背景频道,我将 background 属性设置为 true,这样与其他频道不同,声音不会在每个叙事步骤(step)结束时停止,而是会持续播放,直到被暂停或停止。
import { Game, sound } from "@drincs/pixi-vn";
import { BGM_CHANNEL_NAME, SFX_CHANNEL_NAME } from "@/constans";
Game.init(body, {
// ...
}).then(() => {
sound.channels.add(BGM_CHANNEL_NAME, { background: true });
sound.channels.add(SFX_CHANNEL_NAME);
sound.defaultChannelAlias = SFX_CHANNEL_NAME;
});最后,你可以在叙事节点(labels)中使用 sound.play 函数播放声音和音乐。你还可以使用频道来管理不同类型的声音(例如 BGM、SFX),并控制它们的音量、暂停、恢复等。
你可以在这里找到有关如何使用它的更多信息。
=== start ===
# show image bg bg01-hallway
# play sound sfx_whoosh delay 0.1
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-neutral01] xAlign 0.5 yAlign 1 with movein direction right ease circInOut type spring
james: You're my roommate's replacement, huh?
# play sound sfx_whoosh channel bgm loop true
# show imagecontainer james [m01-body m01-eyes-grin m01-mouth-smile01]
james: Don't worry, you don't have much to live up to. Just don't use heroin like the last guy, and you'll be fine!
# show imagecontainer james [m01-body m01-eyes-smile m01-mouth-grin00]
mc: ...
// ...
# pause all sounds
# show imagecontainer steph [fm02-body fm02-eyes-smile fm02-mouth-smile00]
# play sound sfx_whoosh delay 0.1
# remove image james with moveout direction right ease circInOut type spring duration 0.5 delay 0.05
# remove image sly with moveout direction right ease anticipate duration 0.5
# remove image steph with moveout direction left ease easeInOut duration 0.5 delay 0.1
You want continue to the next part?<># continue
* Yes, I want to continue
-> second_part
* No, I want to stop here
-> END
=== second_part ===
# show text bg "(A few minutes later...)" style { fontFamily: "Arial", dropShadow: { alpha: 0.8, angle: 2.1, blur: 4, color: "0x111111", distance: 10, }, fill: "\#ffffff", stroke: { color: "\#004620", width: 12, join: "round" }, fontSize: 60, fontWeight: "lighter" } with fade
# edit text bg align 0.5
# pause
# resume all sounds
# show image bg bg02-dorm align 0 with fade
# play sound sfx_whoosh delay 0.4
// ...
She enters my room before I'VE even had a chance to.
// ...
-> DONE
结语
好了,现在你已经知道如何使用 Pixi’VN 创建视觉小说了。能力越大,责任越大,所以请明智地使用它,创作一个精彩的故事吧! 🚀