Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 游戏结束会回到首页 #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions demo/starter/calc.adv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[开始]
```yaml
type: background
url: /img/bg/night.jpg
```
@旁白
这是个手搓的计算器,用来测试
先来选一个数字吧!

- [ ] 5

```ts
$adv.store.userData.first = 5
$adv.nav.next()
```

- [ ] 7

```ts
$adv.store.userData.first = 7
$adv.nav.next()
```
然后选操作:

- [ ] +

```ts
$adv.store.userData.opt = 'add'
$adv.nav.next()
```

- [ ] -

```ts
$adv.store.userData.opt = 'sub'
$adv.nav.next()
```

然后选被操作数:
- [ ] 2

```ts
$adv.store.userData.second = 2
$adv.nav.next()
```

- [ ] 3

```ts
$adv.store.userData.second = 3
$adv.nav.next()
```
这样就行了!
你的结果是:
```ts
let res = 0
if ($adv.store.userData.opt === 'add')
res = $adv.store.userData.first + $adv.store.userData.second

else
res = $adv.store.userData.first - $adv.store.userData.second

if (res % 2 === 0)
$adv.nav.go('双数')

```
[单数]

你的结果是单数!
```ts
$adv.store.cur.order = Number.MAX_VALUE
$adv.nav.next()
```
[双数]

你的结果是双数!
2 changes: 1 addition & 1 deletion demo/starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"scripts": {
"build": "adv build ./wlwz.adv.md",
"dev": "nodemon -w '../../packages/**/dist/*.js' --exec \"adv ./wlwz.adv.md --open=false --log=info\"",
"dev": "nodemon -w '../../packages/**/dist/*.js' --exec \"adv ./calc.adv.md --open=false --log=info\"",
"preview": "vite preview --host",
"preview-https": "serve dist"
},
Expand Down
10 changes: 8 additions & 2 deletions packages/client/components/internals/AdvGame.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { AdvAst, AdvConfig, Tachie } from '@advjs/types'

import { useBeforeUnload } from '@advjs/client/composables'
import { computed, ref } from 'vue'
import { computed, onUnmounted, ref } from 'vue'
import { getCharacter } from '@advjs/core'
import { useAppStore } from '~/stores/app'

Expand All @@ -22,6 +22,9 @@ const curNode = computed(() => $adv.store.curNode)
// 添加提示,防止意外退出
if (!__DEV__)
useBeforeUnload()
// 初始化需要执行next,否则如果场景或脚本开头会黑屏
if ($adv.store.cur.order === 0 && curNode.value?.type !== 'dialog')
$adv.nav.next()

const app = useAppStore()

Expand All @@ -41,6 +44,9 @@ const tachies = computed(() => {

return tachiesMap
})
onUnmounted(() => {
$adv.store.reset()
})
</script>

<template>
Expand All @@ -62,7 +68,7 @@ const tachies = computed(() => {
</transition>

<transition enter-active-class="animate__fadeInUp" leave-active-class="animate__fadeOutDown">
<AdvChoice v-show="curNode?.type === 'choices'" :node="curNode" class="z-2" />
<AdvChoice v-if="curNode?.type === 'choices'" :node="curNode" class="z-2" />
</transition>

<transition enter-active-class="animate__fadeInUp" leave-active-class="animate__fadeOutDown">
Expand Down
13 changes: 12 additions & 1 deletion packages/client/components/internals/dialog/AdvDialogBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
import { speak } from '@advjs/shared/speech'
import { computed, ref, watch } from 'vue'
import type { AdvAst } from '@advjs/types'
import { useMagicKeys } from '@vueuse/core'
import { useAdvCtx } from '~/setup'
import { useSettingsStore } from '~/stores/settings'
import { useAppStore } from '~/stores/app'

const props = defineProps<{
node: AdvAst.Child
}>()

const $adv = useAdvCtx()
const keys = useMagicKeys()
const { space } = keys
const app = useAppStore()

const settings = useSettingsStore()

Expand All @@ -33,8 +38,9 @@ const end = ref(false)
const animation = ref(true)

const next = async () => {
// 该回首页了
if ($adv.store.status.isEnd)
return
return $adv.nav.next()

if (!end.value && animation.value) {
animation.value = false
Expand All @@ -59,6 +65,11 @@ const next = async () => {
}
}

watch(space, (v) => {
if (v && !app.showHistory && !app.showSaveMenu && !app.showLoadMenu && $adv.store.curNode?.type !== 'choices')
next()
})

const curCharacter = computed(() => curDialog.value.character)

const characterAvatar = computed(() => {
Expand Down
9 changes: 0 additions & 9 deletions packages/client/composables/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ import { useAppStore } from '@advjs/client/stores/app'
import { watch } from 'vue'
import { useMagicKeys } from '@vueuse/core'

import { useAdvCtx } from '~/setup'

/**
* register adv magic keys
* - `space`: adv next
* - `T+C`: toggle Canvas
*/
export function useAdvKeys() {
const app = useAppStore()
const $adv = useAdvCtx()

const keys = useMagicKeys()
const { space } = keys

const advKeys = [
{
Expand All @@ -29,11 +25,6 @@ export function useAdvKeys() {
},
]

watch(space, (v) => {
if (v && !app.showHistory && !app.showSaveMenu && !app.showLoadMenu)
$adv.nav.next()
})

advKeys.forEach((item) => {
watch(keys[item.keys], (v) => {
if (v) {
Expand Down
16 changes: 10 additions & 6 deletions packages/client/setup/adv/logic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export function useLogic(ctx: {

const nodeLen = store.ast.children.length
const curOrder = store.cur.order
if (curOrder >= nodeLen)
if (curOrder >= nodeLen) {
location.replace('#/start')
return
}

store.cur.order++

Expand Down Expand Up @@ -130,10 +132,11 @@ export function useLogic(ctx: {
return

if (Array.isArray(node.value)) {
for (const advNode of node.value) {
if (await handleCodeOperation(advNode))
return true
}
let res = false
for (const advNode of node.value)
res = res || await handleCodeOperation(advNode)

return res
}
}
}
Expand All @@ -144,7 +147,7 @@ export function useLogic(ctx: {
* run predefined
* @param node Adv Node
*/
async function handleCodeOperation(node: AdvAst.CodeOperation): Promise<boolean | void> {
async function handleCodeOperation(node: AdvAst.CodeOperation): Promise<boolean> {
switch (node.type) {
case 'tachie': {
return tachies.handle(node)
Expand All @@ -166,6 +169,7 @@ export function useLogic(ctx: {
default:
break
}
return false
}

return {
Expand Down
56 changes: 33 additions & 23 deletions packages/client/setup/adv/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export interface CurStateType {
/**
* 游戏存档类型格式
*/
export type AdvGameRecord = CurStateType
export interface AdvGameRecord {
cur: CurStateType
userData: any
}

export interface AdvGameRecordMeta extends StorageMeta {
/**
Expand All @@ -35,6 +38,29 @@ export interface AdvGameRecordMeta extends StorageMeta {
memo?: string
}

const defaultState: CurStateType = {
order: 0,
/**
* 当前对话
*/
dialog: {
type: 'dialog',
character: {
type: 'character',
name: '',
status: '',
},
children: [
{
type: 'text',
value: '',
},
],
},
tachies: new Map(),
background: '',
}

export const useAdvStore = defineStore('adv', () => {
/**
* 语法树
Expand All @@ -51,28 +77,7 @@ export const useAdvStore = defineStore('adv', () => {
],
})

const curState = ref<CurStateType>({
order: 0,
/**
* 当前对话
*/
dialog: {
type: 'dialog',
character: {
type: 'character',
name: '',
status: '',
},
children: [
{
type: 'text',
value: '',
},
],
},
tachies: new Map(),
background: '',
})
const curState = ref<CurStateType>(Object.assign({}, defaultState))

const curNode = computed((): AdvAst.Item | undefined => {
if (ast.value && ast.value.children.length > 0 && curState.value.order < ast.value.children.length)
Expand All @@ -86,6 +91,9 @@ export const useAdvStore = defineStore('adv', () => {
if (curNode.value?.type === 'dialog')
curState.value.dialog = curNode.value
})
const reset = () => {
curState.value = Object.assign({}, defaultState)
}

return {
ast,
Expand All @@ -103,6 +111,8 @@ export const useAdvStore = defineStore('adv', () => {
status: {
isEnd: computed(() => curState.value.order >= ast.value.children.length - 1),
},
reset,
userData: {},
}
})

Expand Down
7 changes: 3 additions & 4 deletions packages/parser/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ export function convertMdToAdv(mdAst: MdAst.Root): AdvAst.Root {
const advItem = parseChild(child)
if (advItem) {
ast.children.push(advItem)
if (advItem.type === 'scene')
ast.scene[advItem.place] = ast.children.length - 1
if (advItem.type === 'scene') { ast.scene[advItem.place] = ast.children.length - 1 }
// mount code function
if (isScript(advItem)) {
else if (isScript(advItem)) {
const name = `advFunc${i}`
ast.functions[`advFunc${i}`] = advItem.value
advItem.value = name
i += 1
}
if (advItem.type === 'choices') {
else if (advItem.type === 'choices') {
advItem.choices.forEach((choice) => {
if (isScript(choice.do)) {
const name = `advFunc${i}`
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/syntax/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AdvAst } from '@advjs/types'
export function parseScene(text: string) {
// 匹配场景
// 以 【】开头结尾,且至少存在一个字段
const re = /^(.+)$/
const re = /^[【\[](.+)[】\]]$/
const separator = ','
if (re.test(text)) {
const metaInfo: AdvAst.SceneInfo = {
Expand Down
9 changes: 5 additions & 4 deletions packages/theme-default/components/save/SavedCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const saveCardMeta = async () => {
*/
const saveToCard = async () => {
const dataUrl = await screenshotGameThumb()
const curRecord = $adv.store.cur
const curRecord = { cur: $adv.store.cur, userData: $adv.store.userData }
try {
await game.saveRecord(props.no, curRecord)
record.value = curRecord
Expand All @@ -74,7 +74,8 @@ const router = useRouter()
const loadFromCard = () => {
if (!record.value)
return
$adv.store.cur = record.value
$adv.store.cur = record.value.cur
$adv.store.userData = record.value.userData

// 关闭加载菜单
app.toggleShowLoadMenu()
Expand Down Expand Up @@ -103,8 +104,8 @@ const onCardClick = () => {
</h3>
<p class="preview-narration" flex="~ col" h="full" bg="white" @click="onCardClick">
<template v-if="record">
<span class="flex truncate" text="xs" m="1">{{ record.dialog?.character.name }}</span>
<span class="truncate" m="x-1">{{ record.dialog?.children[0].value }}</span>
<span class="flex truncate" text="xs" m="1">{{ record.cur.dialog?.character.name }}</span>
<span class="truncate" m="x-1">{{ record.cur.dialog?.children[0].value }}</span>
</template>
</p>
<h3 text="base" class="flex justify-between items-center" bg="white">
Expand Down