Browse Source

游戏进度存储工具函数

任一存 2 năm trước cách đây
mục cha
commit
906d32b61b
1 tập tin đã thay đổi với 49 bổ sung0 xóa
  1. 49 0
      src/store/index.js

+ 49 - 0
src/store/index.js

@@ -1,9 +1,30 @@
 import { createStore } from 'vuex'
 
+function resetGameProgress() {
+  const ret = {
+    jagsawProgress: config.sceneTree.map((item) => {
+      return {
+        name: item.name,
+        isJagsawDone: false,
+        children: item.children.map((innerItem) => {
+          return {
+            name: innerItem.name,
+            hasGotJagsaw: false,
+          }
+        })
+      }
+    }),
+    buildProgress: 0,
+  }
+  localStorage.setItem('HuiZhouGuJianZhuProgress', JSON.stringify(ret))
+  return ret
+}
+
 export default createStore({
   state: {
     hasPlayedStartupVideo: process.env.VUE_APP_CLI_MODE === 'dev' ? true : false,
     hasPlayedGameRule: false,
+    gameProgress: localStorage.getItem('HuiZhouGuJianZhuProgress') ? JSON.parse(localStorage.getItem('HuiZhouGuJianZhuProgress')) : resetGameProgress()
   },
   getters: {
   },
@@ -13,6 +34,34 @@ export default createStore({
     },
     recordGameRulePlayed(state) {
       state.hasPlayedGameRule = true
+    },
+    // 获得拼图碎片
+    recordJagsawGot(state, sceneL2Id, sceneL3Id) {
+      try {
+        state.gameProgress.jagsawProgress[sceneL2Id].children[sceneL3Id].hasGotJagsaw = true
+        localStorage.setItem('HuiZhouGuJianZhuProgress', JSON.stringify(state.gameProgress))
+      } catch (error) {
+        console.error(error)
+        state.gameProgress = resetGameProgress()
+      }
+    },
+    recordJagsawDone(state, sceneL2Id) {
+      try {
+        state.gameProgress.jagsawProgress[sceneL2Id].isJagsawDone = true
+        localStorage.setItem('HuiZhouGuJianZhuProgress', JSON.stringify(state.gameProgress))
+      } catch (error) {
+        console.error(error)
+        state.gameProgress = resetGameProgress()
+      }
+    },
+    recordBuildProgress(state, progress) {
+      try {
+        state.jagsawProgress.gameProgress.buildProgress = progress
+        localStorage.setItem('HuiZhouGuJianZhuProgress', JSON.stringify(state.gameProgress))
+      } catch (error) {
+        console.error(error)
+        state.gameProgress = resetGameProgress()
+      }
     }
   },
   actions: {