MapGame.vue 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789
  1. <template>
  2. <div class="title-box">
  3. <!-- 左侧标题部分 -->
  4. <div class="left-container">
  5. <!-- 线路切换按钮 -->
  6. <div class="route-buttons">
  7. <button
  8. v-for="(route, index) in parsedRouteList"
  9. :key="index"
  10. class="game-badge"
  11. :class="{ 'active': currentRouteIndex === index }"
  12. >
  13. {{ gameSort }}{{ index + 1 }}
  14. </button>
  15. </div>
  16. </div>
  17. </div>
  18. <!-- 中间地图组件部分-->
  19. <div class="content">
  20. <!-- 地图显示区域 -->
  21. <div class="map-section">
  22. <!-- 内容简介提示 -->
  23. <div v-if="currentGameData?.info" class="info-message-container">
  24. <div class="message-item">
  25. <div class="avatar">
  26. <img src="@/assets/images/xiaozhi2.png" alt="头像" class="avatar-image" />
  27. </div>
  28. <p v-if="currentGameData?.info" v-html="currentGameData?.info"></p>
  29. </div>
  30. </div>
  31. <div class="map-container">
  32. <!-- 地图背景 -->
  33. <div class="map-background">
  34. <img :src="mapBackground" alt="地图背景" class="map-image" @load="updateMapContainerDimensions" />
  35. <!-- 可行走区域标记 -->
  36. <div
  37. v-for="(point, index) in walkablePoints"
  38. :key="index"
  39. class="walkable-point"
  40. :style="getPointStyle(point)"
  41. >
  42. </div>
  43. <!-- 玩家角色 -->
  44. <div
  45. class="player"
  46. :style="playerStyle"
  47. :class="{ 'collision': isColliding, 'success': hasReachedEnd }"
  48. >
  49. </div>
  50. <!-- 怀表倒计时容器 -->
  51. <div v-if="showCountdown" class="watch-container" :style="countdownStyle">
  52. <div class="watch-face">
  53. <div class="watch-center"></div>
  54. <div class="watch-hands">
  55. <div class="watch-hand hour-hand"></div>
  56. <div class="watch-hand minute-hand"></div>
  57. </div>
  58. <div class="watch-countdown-number">{{ countdownValue }}</div>
  59. </div>
  60. </div>
  61. <!-- 携带物品容器 -->
  62. <div class="carried-items-container" v-show="gameState.player.carriedItems.length > 0">
  63. <div
  64. v-for="(item, index) in gameState.player.carriedItems"
  65. :key="index"
  66. class="carried-item"
  67. :style="getCarriedItemStyle(index, item)"
  68. ></div>
  69. </div>
  70. </div>
  71. <!-- 游戏状态提示 -->
  72. <div v-if="gameMessage" :class="['game-message', messageType]">
  73. {{ gameMessage }}
  74. </div>
  75. </div>
  76. </div>
  77. <!-- Blockly工作区 -->
  78. <div class="blockly-section">
  79. <div class="workspace-section">
  80. <div class="controls">
  81. <button id="runCode" @click="runCode" :disabled="isRunning">运行代码</button>
  82. <!-- <button @click="generatePythonCode">生成Python代码</button>-->
  83. <button @click="resetPlayer" >重置玩家</button>
  84. <button @click="clearWorkspace">清空工作区</button>
  85. </div>
  86. <!-- 工具箱-->
  87. <div id="toolbox"></div>
  88. <!-- 工作区-->
  89. <div id="blocklyDiv"></div>
  90. </div>
  91. </div>
  92. </div>
  93. <!-- 全屏遮罩盒子 通关后弹框得奖杯 -->
  94. <div class="fullscreen-overlay" v-if="showOverlay">
  95. <div
  96. class="centered-box"
  97. :style="{
  98. backgroundImage: `url(${passConfig.passBackground})`,
  99. backgroundSize: '110%',
  100. backgroundPosition: 'center',
  101. backgroundRepeat: 'no-repeat'
  102. }">
  103. <!-- 彩带飘落效果 -->
  104. <div class="confetti-container" v-if="passConfig.passStar > 0">
  105. <div v-for="n in 50" :key="n" class="confetti" :class="`confetti-${n % 12 + 1}`"></div>
  106. </div>
  107. <!-- 关闭按钮 -->
  108. <div class="close-button" @click="closeOverlay">×</div>
  109. <!-- 题目 -->
  110. <div class="top-box">
  111. <div class="title-text">{{passConfig.title}}</div>
  112. </div>
  113. <!-- 奖杯 -->
  114. <div class="middle-box">
  115. <img :src="passConfig.passTrophy" alt="奖杯" class="gold-cup-image" />
  116. </div>
  117. <!-- 星星 -->
  118. <div class="bottom-box">
  119. <img
  120. v-for="index in passConfig.starTotal"
  121. :key="index"
  122. :src="index <= passConfig.passStar ? star01 : star02"
  123. alt="星星"
  124. class="gold-star-image"
  125. :class="index % 2 === 0 ? 'star-bottom' : 'star-top'"
  126. />
  127. </div>
  128. </div>
  129. </div>
  130. </template>
  131. <script setup>
  132. import {ref, onMounted, onUnmounted, reactive, computed, nextTick, watch, defineEmits} from 'vue';
  133. import { javascriptGenerator } from "blockly/javascript";
  134. import { pythonGenerator } from "blockly/python";
  135. import playerImage from '@/assets/images/blockly/user.png';
  136. // 导入音频文件
  137. import passMp3 from '@/assets/music/blockly/pass.MP3';
  138. import failureMp3 from '@/assets/music/blockly/failure.MP3';
  139. import errorMp3 from '@/assets/music/blockly/error.MP3'
  140. // 游戏接口数据
  141. import { registerCustomBlocks, registerJavaScriptGenerators, registerPythonGenerators, initBlockly } from '@/api/blockly/blockly.js';
  142. import {playMp3} from "@/api/blockly/music.js";
  143. import cupbg from '@/assets/blockly/cupbg.png' // 通关后奖杯底图
  144. import nocupbg from '@/assets/blockly/nocupbg.png' // 通关失败后奖杯底图
  145. import goldcup from '@/assets/blockly/goldcup.png' // 金杯
  146. import silvercup from '@/assets/blockly/silvercup.png' // 银杯
  147. import coppercup from '@/assets/blockly/coppercup.png' // 铜杯
  148. import nocup from '@/assets/blockly/nocup.png' // 无杯状态
  149. import star02 from '@/assets/blockly/star02.png' // 星星
  150. import star01 from '@/assets/blockly/star01.png' // 金星
  151. import passFailure from '@/assets/music/blockly/pass_failure.MP3' // 通关失败音效
  152. import passFireworks from '@/assets/music/blockly/pass_fireworks.MP3' // 通关小号音效
  153. import passTrump from '@/assets/music/blockly/pass_trumpet.MP3' // 通关庆祝礼炮奖杯
  154. // 星星数量,可根据需要调整
  155. const passConfig = ref({
  156. starTotal: 3,// 总星星数量
  157. title: 'YOU WIN!',// 通关标题
  158. passStar: 0,// 已通关星星数量
  159. passTrophy: "",// 通关奖杯
  160. passBackground: "",// 通关后奖杯底图
  161. });
  162. // 定义emits
  163. const emits = defineEmits(['saveProgress'])
  164. // 定义组件属性
  165. const props = defineProps({
  166. // 游戏ID
  167. gameId: {
  168. type: [String, Number],
  169. default: ''
  170. },
  171. // 地图背景
  172. mapBackground: {
  173. type: String,
  174. default: ''
  175. },
  176. // 地图瓦片大小
  177. mapTileSize: {
  178. type: [String, Object],
  179. default: ''
  180. },
  181. // 地图起点
  182. mapStartPoint: {
  183. type: [String, Object],
  184. default: ''
  185. },
  186. // 地图终点
  187. mapEndPoint: {
  188. type: [String, Object],
  189. default: ''
  190. },
  191. // 可行走点
  192. mapWalkablePoints: {
  193. type: [String, Array],
  194. default: ''
  195. },
  196. // 用户方向
  197. userDirection: {
  198. type: Number,
  199. default: 0
  200. },
  201. // 用户图片
  202. userImage: {
  203. type: String,
  204. default: ''
  205. },
  206. // 游戏信息
  207. info: {
  208. type: String,
  209. default: ''
  210. },
  211. // 路线列表
  212. routeList: {
  213. type: [String, Array],
  214. default: ''
  215. },
  216. // 课程列表
  217. courseList: {
  218. type: Array,
  219. default: () => []
  220. },
  221. // 当前课程索引
  222. currentIndex: {
  223. type: Number,
  224. default: 0
  225. },
  226. // 特殊积木方法标识集合
  227. blocklySpecialBlocks: {
  228. type: Array,
  229. default: () => []
  230. }
  231. });
  232. // 配置常量
  233. const CONFIG = {
  234. // 动画时长配置(毫秒)
  235. ANIMATION: {
  236. MOVE_DURATION: 500, // 移动动画持续时间
  237. ROTATE_DURATION: 500, // 旋转动画持续时间(左转/右转)
  238. TURN_AROUND_DURATION: 750, // 向后转动画持续时间
  239. },
  240. // 延迟配置(毫秒)
  241. DELAY: {
  242. ACTION_DELAY: 200, // 每次动作后的延迟时间
  243. COLLISION_DELAY: 300, // 碰撞后的延迟时间
  244. RESET_DELAY: 300, // 重置后的延迟时间
  245. MESSAGE_DISPLAY: 2000, // 消息显示时间
  246. ICE_MESSAGE_DISPLAY: 300, // 冰块消息显示时间
  247. COLLISION_RESET: 1000, // 碰撞状态重置时间
  248. LOOP_PREVENTION: 10, // 循环防止UI阻塞的延迟
  249. ITEMS_APPEAR: 300, // 物品出现延迟时间
  250. ITEMS_FLIGHT_ANIMATION_DELAY: 800, // 飞行动画延迟时间
  251. PALY_MP3_TIMES: 1000, // 播放MP3文件的时间间隔
  252. VIDEO_TIMEOUT: 5000, // 视频播放超时处理时间
  253. VIDEO_FADE_DURATION: 500, // 视频淡入淡出持续时间
  254. PLAYER_FADE_DURATION: 500, // 玩家淡入淡出持续时间
  255. TEMP_ITEM_FADE_DURATION: 500, // 临时物品淡入淡出持续时间
  256. COMPLETION_DISPLAY: 1000, // 任务完成后显示延迟时间
  257. },
  258. // 游戏配置
  259. GAME: {
  260. MAX_LOOP_COUNT: 100, // 最大循环次数
  261. DIRECTIONS: {
  262. UP: 0,
  263. RIGHT: 1,
  264. DOWN: 2,
  265. LEFT: 3
  266. }
  267. },
  268. // 样式配置
  269. STYLES: {
  270. DEFAULT_TILE_SIZE: 143, // 默认瓦片大小
  271. PLAYER_SIZE_RATIO: 0.8, // 玩家大小占瓦片的比例
  272. PLAYER_SIZE_MARGIN: 0.1, // 玩家大小占瓦片边距
  273. IMG_SIZE_RATIO: 0.8, // 地图素材大小占瓦片的比例
  274. IMG_SIZE_MARGIN: 0.1, // 地图素材大小占瓦片边距
  275. ITEM_CONTAINER_POSITION: { x: 30, y: 30 }, // 物品容器位置
  276. ITEM_CONTAINER_RATIO: 0.4, // 物品大小占容器的比例
  277. ITEM_CONTAINER_SPACING: 10 // 物品大小占容器的间距
  278. },
  279. //提示语
  280. TIPS: {
  281. NO_ENTRY: '当前位置无通路,无法移动',
  282. UNFINISHED: '任务未完成!',
  283. EFFORT: '再接再厉!',
  284. PASS_ROUTE: '恭喜你通过了当前路线,进入下一条路线!',
  285. FINISH: '恭喜你到达终点!',
  286. PICKUP_ITEM: '拾取物品成功!',
  287. NULL_PICKUP_ITEM: '当前位置没有可拾取的物品',
  288. USE_ITEM_SUCCESS: '使用物品成功',
  289. USE_SPECIAL_ITEM: '需要特殊物品才能使用',
  290. NO_USE_ITEM: '当前位置不需要使用物品',
  291. ERROR_ERROR: '错了错了',//任务消失时的错误提示
  292. }
  293. };
  294. // 路由和游戏状态
  295. const currentGameData = ref(null);
  296. const playerInitialDirection = ref(0); // 人物初始朝向
  297. const gameSort = ref('路线'); // 默认排序
  298. const currentRouteIndex = ref(0); // 当前线路索引
  299. // 路线通过状态:存储各路线的通过状态
  300. const routePassedStatus = ref([]);
  301. // 解析routeList
  302. const parsedRouteList = computed(() => {
  303. if (!props.routeList) return [];
  304. try {
  305. const routeData = typeof props.routeList === 'string' ? JSON.parse(props.routeList) : props.routeList;
  306. const routes = Array.isArray(routeData) ? routeData : [];
  307. // 初始化路线通过状态
  308. if (routePassedStatus.value.length === 0 && routes.length > 0) {
  309. routePassedStatus.value = routes.map((_, index) => index === 0); // 第一条路线默认可用
  310. }
  311. return routes;
  312. } catch (error) {
  313. console.error('解析routeList失败:', error);
  314. return [];
  315. }
  316. });
  317. // 运行控制标志
  318. let shouldStopExecution = false;
  319. let currentExecutionPromise = null;
  320. let executionAbortController = null;
  321. // 添加响应式的容器尺寸
  322. const mapContainerDimensions = ref({ width: 0, height: 0 });
  323. // 用于控制运行/重置按钮状态
  324. const isRunning = ref(false);
  325. // 存储生成的Python代码
  326. const generatedPythonCode = ref('');
  327. // 暂停模块-倒计时相关状态
  328. const showCountdown = ref(false);
  329. const countdownValue = ref(0);
  330. // Blockly相关状态
  331. let workspace = null;
  332. // 使用 Map 存储可行走点及其类型,提高查询效率
  333. let walkablePointsMap = new Map();
  334. // 创建游戏状态的响应式对象
  335. const gameState = reactive({
  336. // 地图配置信息
  337. mapConfig: {
  338. // 地图背景图片路径
  339. background: '',
  340. // 每个瓦片的尺寸(像素)
  341. tileSize: CONFIG.STYLES.DEFAULT_TILE_SIZE,
  342. },
  343. // 玩家相关状态
  344. player: {
  345. // 玩家当前位置坐标
  346. position: {},
  347. // 玩家当前朝向:0=上, 1=右, 2=下, 3=左
  348. direction: 1,
  349. // 是否正在发生碰撞
  350. isColliding: false,
  351. // 是否已到达终点
  352. hasReachedEnd: false,
  353. // 是否正在冰块上滑行
  354. isSliding: false,
  355. // 携带的物品数组
  356. carriedItems: [],
  357. },
  358. // 游戏状态信息
  359. status: {
  360. // 当前显示的游戏消息
  361. message: '',
  362. // 消息类型(如success、error、info等)
  363. messageType: ''
  364. },
  365. // 地图数据信息
  366. mapData: {
  367. // 游戏起点位置
  368. startPoint: {},
  369. // 游戏终点位置
  370. endPoint: {},
  371. // 地图上所有可行走的点坐标集合,添加type属性区分普通点和冰块
  372. walkablePoints: [],
  373. // 路线列表
  374. routeList: [],
  375. // 保存原始的可行走点数据,用于重置
  376. originalWalkablePoints: [],
  377. }
  378. });
  379. // 控制遮罩层显示的状态
  380. const showOverlay = ref(false);
  381. // 地图元素类型字典
  382. const BLOCKLY_MAP_TYPE_DICT = {
  383. ICE: 'ice',//冰块
  384. TASK: 'task',//钥匙
  385. ITEM: 'item',//物品
  386. TRAP: 'trap',//陷阱
  387. }
  388. // 自定义积木字典
  389. const BLOCKLY_CUSTOMIZE_DICT = {
  390. PAUSE: 'pause',//暂停
  391. PLAY_SOUND: 'play_sound',//播放音效
  392. CONSTRUCT: 'construct',//修建
  393. }
  394. // 计算属性 - 提高性能和可读性
  395. const mapBackground = computed(() => gameState.mapConfig.background);
  396. // const tileSize = computed(() => gameState.mapConfig.tileSize);
  397. const walkablePoints = computed(() => gameState.mapData.walkablePoints);
  398. const startPoint = computed(() => gameState.mapData.startPoint);
  399. const endPoint = computed(() => gameState.mapData.endPoint);
  400. const playerPosition = computed(() => gameState.player.position);
  401. const playerDirection = computed(() => gameState.player.direction);
  402. const isColliding = computed(() => gameState.player.isColliding);
  403. const hasReachedEnd = computed(() => gameState.player.hasReachedEnd);
  404. const gameMessage = computed(() => gameState.status.message);
  405. const messageType = computed(() => gameState.status.messageType);
  406. const isSliding = computed(() => gameState.player.isSliding);
  407. // 计算玩家图片路径,优先使用接口数据
  408. const playerImageSrc = computed(() => {
  409. if (currentGameData.value && currentGameData.value.userImage) {
  410. return currentGameData.value.userImage.trim();
  411. }
  412. return playerImage;
  413. });
  414. // 计算实际瓦片大小(基于容器尺寸和地图数据)
  415. const tileSize = computed(() => {
  416. if (mapContainerDimensions.value.width === 0 || mapContainerDimensions.value.height === 0) {
  417. return gameState.mapConfig.tileSize;
  418. }
  419. // 获取地图数据中的最大坐标
  420. let size = JSON.parse(gameState.mapConfig.tileSize);
  421. // 计算基于容器的瓦片大小,确保地图完全可见
  422. const tileWidth = mapContainerDimensions.value.width / size.x;
  423. const tileHeight = mapContainerDimensions.value.height / size.y;
  424. // 返回较小的值以确保地图完全可见
  425. return Math.min(tileWidth, tileHeight);
  426. });
  427. // 计算玩家样式
  428. const playerStyle = computed(() => ({
  429. left: playerPosition.value.x * tileSize.value - tileSize.value + 'px',
  430. top: playerPosition.value.y * tileSize.value - tileSize.value + 'px',
  431. transform: `rotate(${playerDirection.value * 90}deg)`,
  432. '--player-rotation': `${playerDirection.value * 90}deg`,
  433. '--player-image': `url(${playerImageSrc.value})`,
  434. width: (tileSize.value * CONFIG.STYLES.PLAYER_SIZE_RATIO) + 'px',
  435. height: (tileSize.value * CONFIG.STYLES.PLAYER_SIZE_RATIO) + 'px',
  436. margin: (tileSize.value * CONFIG.STYLES.PLAYER_SIZE_MARGIN) + 'px',
  437. }));
  438. // 暂停倒计时样式计算
  439. const countdownStyle = computed(() => {
  440. return {
  441. position: 'absolute',
  442. left: playerPosition.value.x * tileSize.value - tileSize.value - (tileSize.value * 0.2) + 'px',
  443. top: playerPosition.value.y * tileSize.value - tileSize.value - (tileSize.value * 0.2) + 'px',
  444. width: tileSize.value * 0.5 + 'px',
  445. height: tileSize.value * 0.5 + 'px',
  446. display: 'flex',
  447. justifyContent: 'center',
  448. alignItems: 'center',
  449. zIndex: 100
  450. };
  451. });
  452. // 监听props变化,当地图数据变化时更新游戏数据
  453. watch(() => [props.gameId, props.mapBackground, props.mapStartPoint, props.mapEndPoint, props.mapWalkablePoints], async () => {
  454. // 获取游戏数据
  455. await fetchGameData();
  456. // 初始化可行走点集合
  457. initWalkablePointsSet();
  458. // 重置玩家位置
  459. resetPlayer();
  460. await nextTick();
  461. // 更新容器尺寸
  462. updateMapContainerDimensions();
  463. }, { deep: true });
  464. // 生命周期钩子
  465. onMounted(async () => {
  466. // 获取游戏数据
  467. await fetchGameData();
  468. // 初始化可行走点集合
  469. initWalkablePointsSet();
  470. // 注册自定义积木
  471. registerCustomBlocks(props.blocklySpecialBlocks);
  472. // 注册JavaScript生成器
  473. registerJavaScriptGenerators(props.blocklySpecialBlocks);
  474. // 注册Python生成器
  475. registerPythonGenerators(props.blocklySpecialBlocks);
  476. // 动态生成工具箱XML
  477. const toolboxContainer = document.getElementById('toolbox');
  478. toolboxContainer.innerHTML = generateToolboxXml();
  479. // 初始化Blockly工作区
  480. workspace = initBlockly();
  481. // 重置玩家位置
  482. resetPlayer();
  483. await nextTick();
  484. // 添加对窗口大小变化的监听
  485. updateMapContainerDimensions();
  486. window.addEventListener('resize', updateMapContainerDimensions);
  487. });
  488. //================初始化=====================
  489. // 动态生成工具箱XML
  490. function generateToolboxXml() {
  491. let toolboxXml = `
  492. <category name="移动控制" colour="230">
  493. <block type="move_forward"></block>
  494. <block type="move_backward"></block>
  495. <block type="turn_left"></block>
  496. <block type="turn_right"></block>
  497. <block type="turn_around"></block>
  498. <block type="move_forward_param"></block>
  499. <block type="move_backward_param"></block>
  500. </category>
  501. <category name="功能" colour="120">
  502. <block type="pickup_item"></block>
  503. <block type="use_item"></block>
  504. `;
  505. // 确保blocklySpecialBlocks是数组
  506. const specialBlocks = Array.isArray(props.blocklySpecialBlocks) ? props.blocklySpecialBlocks : [];
  507. // 根据允许的特殊积木动态添加
  508. if (specialBlocks.includes(BLOCKLY_CUSTOMIZE_DICT.PAUSE)) {
  509. toolboxXml += '<block type="pause"></block>';
  510. }
  511. if (specialBlocks.includes(BLOCKLY_CUSTOMIZE_DICT.PLAY_SOUND)) {
  512. toolboxXml += '<block type="play_sound"></block>';
  513. }
  514. if (specialBlocks.includes(BLOCKLY_CUSTOMIZE_DICT.CONSTRUCT)) {
  515. toolboxXml += '<block type="construct"></block>';
  516. }
  517. toolboxXml += `
  518. </category>
  519. <category name="逻辑" colour="210">
  520. <block type="controls_if"></block>
  521. <block type="logic_compare"></block>
  522. <block type="logic_operation"></block>
  523. <block type="logic_boolean"></block>
  524. </category>
  525. <category name="循环" colour="160">
  526. <block type="controls_repeat_ext"></block>
  527. </category>
  528. <category name="数学" colour="60">
  529. <block type="math_number"></block>
  530. <block type="math_arithmetic"></block>
  531. </category>
  532. `;
  533. return toolboxXml;
  534. }
  535. // 获取游戏数据
  536. const fetchGameData = async () => {
  537. try {
  538. // 优先使用props传递的数据
  539. if (props.gameId && props.mapBackground) {
  540. // 使用props数据构建游戏数据对象
  541. currentGameData.value = {
  542. mapBackground: props.mapBackground,
  543. mapTileSize: props.mapTileSize,
  544. mapStartPoint: props.mapStartPoint,
  545. mapEndPoint: props.mapEndPoint,
  546. mapWalkablePoints: props.mapWalkablePoints,
  547. routeList: props.routeList,
  548. userDirection: props.userDirection,
  549. userImage: props.userImage,
  550. info: props.info
  551. };
  552. // 直接更新游戏状态
  553. await updateGameStateFromData(currentGameData.value);
  554. // 数据更新后强制刷新容器尺寸(等待DOM更新)
  555. await nextTick();
  556. updateMapContainerDimensions();
  557. } else {
  558. // 注释:如果没有props数据,则从API获取
  559. }
  560. } catch (error) {
  561. console.error('获取游戏数据失败:', error);
  562. }
  563. };
  564. // 切换线路
  565. const switchRoute = (index) => {
  566. // 检查路线是否可用
  567. if (index < 0 || index >= parsedRouteList.value.length || !isRouteAvailable(index)) return;
  568. currentRouteIndex.value = index;
  569. const route = parsedRouteList.value[index];
  570. // 更新人物朝向
  571. if (route.direction !== undefined) {
  572. playerInitialDirection.value = route.direction;
  573. gameState.player.direction = route.direction;
  574. }
  575. // 更新开始坐标
  576. if (route.startPoint) {
  577. const startPoint = typeof route.startPoint === 'string' ? JSON.parse(route.startPoint) : route.startPoint;
  578. gameState.mapData.startPoint = { x: startPoint.x, y: startPoint.y };
  579. gameState.player.position = { x: startPoint.x, y: startPoint.y };
  580. }
  581. // 更新结束坐标
  582. if (route.endPoint) {
  583. const endPoint = typeof route.endPoint === 'string' ? JSON.parse(route.endPoint) : route.endPoint;
  584. gameState.mapData.endPoint = { x: endPoint.x, y: endPoint.y };
  585. }
  586. // 重新初始化可行走点集合【目前没有单独配置路线的可行走集合,一张地图通用】
  587. // initWalkablePointsSet();
  588. };
  589. // 检查路线是否可用
  590. const isRouteAvailable = (index) => {
  591. // 第一条路线始终可用
  592. if (index === 0) return true;
  593. // 其他路线需要前一条路线通过
  594. return routePassedStatus.value[index - 1];
  595. };
  596. // 根据获取到的数据更新游戏状态
  597. const updateGameStateFromData = (gameData) => {
  598. try {
  599. // 更新地图配置
  600. gameState.mapConfig.background = gameData.mapBackground ? gameData.mapBackground.trim() : '';
  601. gameState.mapConfig.tileSize = gameData.mapTileSize || CONFIG.STYLES.DEFAULT_TILE_SIZE;
  602. // 更新玩家方向
  603. if (gameData.userDirection) {
  604. playerInitialDirection.value = gameData.userDirection;
  605. }
  606. // 更新地图数据
  607. // 地图起点
  608. if (gameData.mapStartPoint) {
  609. const startPoint = JSON.parse(gameData.mapStartPoint);
  610. gameState.mapData.startPoint = { x: startPoint.x, y: startPoint.y };
  611. gameState.player.position = { x: startPoint.x, y: startPoint.y };
  612. }
  613. // 地图终点
  614. if (gameData.mapEndPoint) {
  615. const endPoint = JSON.parse(gameData.mapEndPoint);
  616. gameState.mapData.endPoint = { x: endPoint.x, y: endPoint.y };
  617. }
  618. if (gameData.mapWalkablePoints) {
  619. gameState.mapData.walkablePoints = JSON.parse(gameData.mapWalkablePoints);
  620. }
  621. // 路线列表
  622. if (gameData.routeList) {
  623. gameState.mapData.routeList = JSON.parse(gameData.routeList);
  624. }
  625. // 初始加载时选中第一条线路
  626. if (parsedRouteList.value.length > 0) {
  627. switchRoute(0);
  628. }
  629. // 重新初始化可行走点集合
  630. initWalkablePointsSet();
  631. } catch (error) {
  632. console.error('更新游戏状态失败:', error);
  633. }
  634. };
  635. // 更新地图容器尺寸的函数
  636. function updateMapContainerDimensions() {
  637. const mapContainer = document.querySelector('.map-container');
  638. if (mapContainer) {
  639. const rect = mapContainer.getBoundingClientRect();
  640. // 确保获取到有效的尺寸值
  641. if (rect.width > 0 && rect.height > 0) {
  642. mapContainerDimensions.value = {
  643. width: rect.width,
  644. height: rect.height
  645. };
  646. } else {
  647. // 若尺寸无效,使用默认容器尺寸(可根据实际情况调整)
  648. mapContainerDimensions.value = {
  649. width: 800,
  650. height: 600
  651. };
  652. }
  653. }
  654. };
  655. // 初始化可行走点映射
  656. function initWalkablePointsSet() {
  657. walkablePointsMap.clear();
  658. gameState.mapData.walkablePoints.forEach(point => {
  659. walkablePointsMap.set(`${point.x},${point.y}`, point);
  660. });
  661. // 保存原始的可行走点数据,用于重置
  662. gameState.mapData.originalWalkablePoints = JSON.parse(JSON.stringify(gameState.mapData.walkablePoints));
  663. }
  664. // 计算点的样式
  665. function getPointStyle(point) {
  666. const style = {
  667. left: point.x * tileSize.value - tileSize.value + 'px',
  668. top: point.y * tileSize.value - tileSize.value + 'px',
  669. width: tileSize.value + 'px',
  670. height: tileSize.value + 'px',
  671. };
  672. // 如果point有img属性,则添加图标
  673. if (point.img) {
  674. const iconSize = tileSize.value * CONFIG.STYLES.IMG_SIZE_RATIO;
  675. const marginSize = tileSize.value * CONFIG.STYLES.IMG_SIZE_MARGIN;
  676. // 重置可能影响背景图显示的样式
  677. style.backgroundColor = 'transparent';
  678. style.backgroundImage = `url(${point.img})`;
  679. style.backgroundSize = 'contain';
  680. style.backgroundPosition = 'center';
  681. style.backgroundRepeat = 'no-repeat';
  682. // 设置与玩家相同的宽高和边距
  683. style.width = iconSize + 'px';
  684. style.height = iconSize + 'px';
  685. style.margin = marginSize + 'px';
  686. }
  687. return style;
  688. }
  689. // 计算携带物品样式
  690. function getCarriedItemStyle(index, item) {
  691. const baseSize = tileSize.value * CONFIG.STYLES.ITEM_CONTAINER_RATIO;
  692. return {
  693. position: 'relative',
  694. width: baseSize + 'px',
  695. height: baseSize + 'px',
  696. backgroundSize: 'contain',
  697. backgroundPosition: 'center',
  698. backgroundRepeat: 'no-repeat',
  699. backgroundImage: `url(${item.img})`,
  700. animationDelay: index * 0.1 + 's'
  701. };
  702. }
  703. //================操作按钮=====================
  704. // 运行代码
  705. const runCode = async () => {
  706. await resetPlayer();
  707. isRunning.value = true;
  708. try {
  709. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.RESET_DELAY));
  710. // 重置执行标志,允许新的执行
  711. shouldStopExecution = false;
  712. // 创建新的AbortController用于取消执行
  713. executionAbortController = new AbortController();
  714. const signal = executionAbortController.signal;
  715. // 确保生成器和工作区都存在
  716. if (!javascriptGenerator || !workspace) {
  717. throw new Error('生成器或工作区未正确初始化');
  718. }
  719. // 生成JavaScript代码
  720. const code = javascriptGenerator.workspaceToCode(workspace) + "await isFinish();";
  721. try {
  722. // 增强的安全检查
  723. const unsafePatterns = [
  724. 'eval(', 'Function(', 'document.write', 'window.location',
  725. 'document.createElement', 'XMLHttpRequest', 'fetch',
  726. 'setInterval', 'setTimeout', 'window.',
  727. 'alert(', 'confirm(', 'prompt(',
  728. 'document.cookie', 'localStorage', 'sessionStorage'
  729. ];
  730. const hasUnsafeCode = unsafePatterns.some(pattern => code.includes(pattern));
  731. if (hasUnsafeCode) {
  732. throw new Error('代码包含不安全的操作');
  733. }
  734. // 包装代码为异步函数执行,并设置超时保护
  735. currentExecutionPromise = new Promise(async (resolve, reject) => {
  736. try {
  737. // 检查信号是否已中止
  738. if (signal.aborted) {
  739. throw new Error('执行已取消');
  740. }
  741. // 添加信号监听
  742. signal.addEventListener('abort', () => {
  743. reject(new Error('执行已取消'));
  744. });
  745. const wrappedCode = `(async () => { ${code} })()`;
  746. await new Function(wrappedCode)();
  747. resolve();
  748. } catch (error) {
  749. reject(error);
  750. }
  751. });
  752. } catch (error) {
  753. // 捕获并显示执行错误
  754. if (error.message !== '执行已取消') {
  755. const errorMsg = error.message || '未知错误';
  756. showGameMessage(`代码执行错误: ${errorMsg}`, 'error');
  757. console.error('代码执行错误:', error);
  758. }
  759. } finally {
  760. // 清除当前执行的Promise引用
  761. currentExecutionPromise = null;
  762. }
  763. } catch (error) {
  764. showGameMessage(`运行时错误: ${error.message || '未知错误'}`, 'error');
  765. console.error('运行时错误:', error);
  766. } finally {
  767. isRunning.value = false;
  768. }
  769. };
  770. // 重置玩家位置和状态
  771. const resetPlayer = async () => {
  772. isRunning.value = false;
  773. // 设置标志强制停止所有执行
  774. shouldStopExecution = true;
  775. // 清除倒计时显示和重置倒计时值
  776. showCountdown.value = false;
  777. countdownValue.value = 0;
  778. // 取消任何正在执行的代码
  779. if (executionAbortController) {
  780. executionAbortController.abort();
  781. executionAbortController = null;
  782. }
  783. if (currentExecutionPromise) {
  784. currentExecutionPromise = null;
  785. }
  786. // 清除所有视频组件
  787. const mapBackground = document.querySelector('.map-background');
  788. if (mapBackground) {
  789. // 清除视频元素
  790. const videoElements = mapBackground.querySelectorAll('video');
  791. videoElements.forEach(video => {
  792. if (mapBackground.contains(video)) {
  793. mapBackground.removeChild(video);
  794. }
  795. });
  796. // 清除临时动画元素
  797. const tempElements = mapBackground.querySelectorAll('div[style*="backgroundImage"]');
  798. tempElements.forEach(temp => {
  799. if (mapBackground.contains(temp)) {
  800. mapBackground.removeChild(temp);
  801. }
  802. });
  803. }
  804. // 确保小智显形(不隐形)
  805. const playerElement = document.querySelector('.player');
  806. if (playerElement) {
  807. playerElement.style.opacity = '1';
  808. playerElement.style.transition = 'none'; // 取消过渡效果,立即显示
  809. }
  810. // 重置携带的物品回地图
  811. if (gameState.mapData.originalWalkablePoints.length > 0) {
  812. gameState.mapData.walkablePoints = JSON.parse(JSON.stringify(gameState.mapData.originalWalkablePoints));
  813. }
  814. // 清空携带物品
  815. gameState.player.carriedItems = [];
  816. // 重新初始化可行走点集合
  817. initWalkablePointsSet();
  818. gameState.player.position = { ...startPoint.value };
  819. gameState.player.direction = playerInitialDirection.value; // 重置为初始方向
  820. gameState.player.isColliding = false; //碰撞标志
  821. gameState.player.hasReachedEnd = false;
  822. gameState.player.isSliding = false; // 重置滑行状态
  823. showOverlay.value = false; // 隐藏遮罩层
  824. };
  825. // 清空工作区
  826. const clearWorkspace = () => {
  827. workspace.clear();
  828. showGameMessage('工作区已清空', 'info');
  829. };
  830. // 生成Python代码
  831. const generatePythonCode = () => {
  832. if (!workspace) {
  833. return;
  834. }
  835. try {
  836. // 生成Python代码
  837. const pythonCode = pythonGenerator.workspaceToCode(workspace);
  838. // 存储到常量中
  839. generatedPythonCode.value = pythonCode;
  840. console.log('生成的Python代码:', pythonCode);
  841. // 可以添加提示信息
  842. showGameMessage('Python代码生成成功!', 'success');
  843. } catch (error) {
  844. console.error('生成Python代码时出错:', error);
  845. showGameMessage('生成Python代码失败', 'error');
  846. }
  847. };
  848. // 统一处理撞到墙时的停止逻辑
  849. async function handleWallCollision(endMsg = CONFIG.TIPS.NO_ENTRY) {
  850. // 设置碰撞状态
  851. gameState.player.isColliding = true;
  852. // 显示错误消息
  853. showGameMessage(endMsg, 'error');
  854. // 立即中止整个代码执行
  855. if (executionAbortController) {
  856. executionAbortController.abort();
  857. }
  858. // 所有动画和移动操作立即停止
  859. shouldStopExecution = true;
  860. // 碰撞状态重置时间后取消碰撞状态
  861. setTimeout(() => {
  862. gameState.player.isColliding = false;
  863. }, CONFIG.DELAY.COLLISION_RESET);
  864. // 返回一个Promise,允许调用者等待碰撞延迟
  865. return new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.COLLISION_DELAY));
  866. }
  867. // 显示游戏消息
  868. function showGameMessage(message, type = 'info', duration = CONFIG.DELAY.MESSAGE_DISPLAY) {
  869. gameState.status.message = message;
  870. gameState.status.messageType = type;
  871. // 消息显示时间后自动清除消息
  872. setTimeout(() => {
  873. gameState.status.message = '';
  874. }, duration);
  875. }
  876. // 关闭遮罩层
  877. const closeOverlay = () => {
  878. gameState.player.hasReachedEnd = false;
  879. showOverlay.value = false;
  880. };
  881. //================积木组件方法=====================
  882. // 向前移动
  883. window.moveForward = async function(stepCount = 1) {
  884. if (shouldStopExecution || isColliding.value || isSliding.value) {
  885. return;
  886. }
  887. for (let i = 1; i <= stepCount; i++) {
  888. let newX = playerPosition.value.x;
  889. let newY = playerPosition.value.y;
  890. // 向前移动
  891. switch(playerDirection.value) {
  892. case CONFIG.GAME.DIRECTIONS.UP: newY--; break;
  893. case CONFIG.GAME.DIRECTIONS.RIGHT: newX++; break;
  894. case CONFIG.GAME.DIRECTIONS.DOWN: newY++; break;
  895. case CONFIG.GAME.DIRECTIONS.LEFT: newX--; break;
  896. }
  897. await moveStep(newX, newY);
  898. }
  899. };
  900. // 向后移动
  901. window.moveBackward = async function(stepCount = 1) {
  902. // 如果已经发生过碰撞,不再执行任何移动
  903. if (shouldStopExecution || isColliding.value || isSliding.value) {
  904. return;
  905. }
  906. for (let i = 1; i <= stepCount; i++) {
  907. let newX = playerPosition.value.x;
  908. let newY = playerPosition.value.y;
  909. // 根据当前方向计算新位置(向后移动)
  910. switch(playerDirection.value) {
  911. case CONFIG.GAME.DIRECTIONS.UP: newY++; break;
  912. case CONFIG.GAME.DIRECTIONS.RIGHT: newX--; break;
  913. case CONFIG.GAME.DIRECTIONS.DOWN: newY--; break;
  914. case CONFIG.GAME.DIRECTIONS.LEFT: newX++; break;
  915. }
  916. await moveStep(newX, newY, 1);
  917. }
  918. };
  919. //向左转(逆时针旋转90度)
  920. window.turnLeft = async function() {
  921. // 如果已经发生过碰撞,不再执行任何旋转
  922. if (shouldStopExecution || isColliding.value) {
  923. return;
  924. }
  925. // 记录起始方向和目标方向
  926. const startDirection = playerDirection.value;
  927. const targetDirection = (playerDirection.value - 1 + 4) % 4;
  928. // 实现平滑旋转
  929. const startTime = performance.now();
  930. // 使用 requestAnimationFrame 实现平滑动画
  931. await new Promise(resolve => {
  932. function animate(currentTime) {
  933. // 检查是否应该停止执行
  934. if (shouldStopExecution) {
  935. resolve();
  936. return;
  937. }
  938. const elapsedTime = currentTime - startTime;
  939. const progress = Math.min(elapsedTime / CONFIG.ANIMATION.ROTATE_DURATION, 1);
  940. // 在动画过程中更新方向
  941. gameState.player.direction = startDirection - progress;
  942. // 如果动画未完成,继续下一帧
  943. if (progress < 1) {
  944. requestAnimationFrame(animate);
  945. } else {
  946. // 动画完成后设置最终方向
  947. gameState.player.direction = targetDirection;
  948. resolve();
  949. }
  950. }
  951. // 开始动画
  952. requestAnimationFrame(animate);
  953. });
  954. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ACTION_DELAY));
  955. };
  956. //向右转(顺时针旋转90度)
  957. window.turnRight = async function() {
  958. // 如果已经发生过碰撞,不再执行任何旋转
  959. if (shouldStopExecution || isColliding.value) {
  960. return;
  961. }
  962. // 记录起始方向和目标方向
  963. const startDirection = playerDirection.value;
  964. const targetDirection = (playerDirection.value + 1) % 4;
  965. // 实现平滑旋转
  966. const startTime = performance.now();
  967. // 使用 requestAnimationFrame 实现平滑动画
  968. await new Promise(resolve => {
  969. function animate(currentTime) {
  970. // 检查是否应该停止执行
  971. if (shouldStopExecution) {
  972. resolve();
  973. return;
  974. }
  975. const elapsedTime = currentTime - startTime;
  976. const progress = Math.min(elapsedTime / CONFIG.ANIMATION.ROTATE_DURATION, 1);
  977. // 处理从3到0的边界情况,确保顺时针旋转
  978. let currentDirection;
  979. if (startDirection === 3 && targetDirection === 0) {
  980. // 对于从3到0的顺时针旋转,我们需要模拟+1的效果而不是-3
  981. currentDirection = startDirection + progress;
  982. // 当超过3.99时,设置为0(避免显示4)
  983. if (currentDirection > 3.99) {
  984. currentDirection = 0;
  985. }
  986. } else {
  987. // 正常情况下的线性插值
  988. currentDirection = startDirection + (targetDirection - startDirection) * progress;
  989. }
  990. // 在动画过程中更新方向
  991. gameState.player.direction = currentDirection;
  992. // 如果动画未完成,继续下一帧
  993. if (progress < 1) {
  994. requestAnimationFrame(animate);
  995. } else {
  996. // 动画完成后设置最终方向
  997. gameState.player.direction = targetDirection;
  998. resolve();
  999. }
  1000. }
  1001. // 开始动画
  1002. requestAnimationFrame(animate);
  1003. });
  1004. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ACTION_DELAY));
  1005. };
  1006. // 向后转(旋转180度)
  1007. window.turnAround = async function() {
  1008. // 如果已经发生过碰撞,不再执行任何旋转
  1009. if (shouldStopExecution || isColliding.value) {
  1010. return;
  1011. }
  1012. // 记录起始方向和目标方向
  1013. const startDirection = playerDirection.value;
  1014. const targetDirection = (playerDirection.value + 2) % 4;
  1015. // 实现平滑旋转
  1016. const startTime = performance.now();
  1017. // 使用 requestAnimationFrame 实现平滑动画
  1018. await new Promise(resolve => {
  1019. function animate(currentTime) {
  1020. // 检查是否应该停止执行
  1021. if (shouldStopExecution) {
  1022. resolve();
  1023. return;
  1024. }
  1025. const elapsedTime = currentTime - startTime;
  1026. const progress = Math.min(elapsedTime / CONFIG.ANIMATION.TURN_AROUND_DURATION, 1);
  1027. // 在动画过程中更新方向
  1028. gameState.player.direction = startDirection + 2 * progress;
  1029. // 如果动画未完成,继续下一帧
  1030. if (progress < 1) {
  1031. requestAnimationFrame(animate);
  1032. } else {
  1033. // 动画完成后设置最终方向
  1034. gameState.player.direction = targetDirection;
  1035. resolve();
  1036. }
  1037. }
  1038. // 开始动画
  1039. requestAnimationFrame(animate);
  1040. });
  1041. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ACTION_DELAY));
  1042. };
  1043. // 拾取物品函数
  1044. window.pickupItem = async function() {
  1045. if (shouldStopExecution || isColliding.value || isSliding.value) {
  1046. return;
  1047. }
  1048. //取人物当前位置
  1049. let x = playerPosition.value.x;
  1050. let y = playerPosition.value.y;
  1051. let tileMap = walkablePointsMap.get(`${x},${y}`);
  1052. // 判断是否是要拾取的方块类型
  1053. if (tileMap && tileMap.type === BLOCKLY_MAP_TYPE_DICT.ITEM) {
  1054. showGameMessage(tileMap.tip || CONFIG.TIPS.PICKUP_ITEM, 'warning');
  1055. // 处理携带物品逻辑
  1056. if (tileMap && tileMap.img) {
  1057. // 从地图上移除图标
  1058. const pointIndex = gameState.mapData.walkablePoints.findIndex(
  1059. p => p.x === x && p.y === y
  1060. );
  1061. if (pointIndex !== -1) {
  1062. // 保留点但移除img属性
  1063. const updatedPoint = { ...gameState.mapData.walkablePoints[pointIndex] };
  1064. delete updatedPoint.img;
  1065. updatedPoint.status = updatedPoint.must === true;
  1066. gameState.mapData.walkablePoints.splice(pointIndex, 1, updatedPoint);
  1067. // 更新映射
  1068. walkablePointsMap.set(`${x},${y}`, updatedPoint);
  1069. // 执行物品拾取动画:放大晃动两下然后移动到左上角物品容器
  1070. await animateItemPickup(tileMap, x, y);
  1071. // 将物品添加到玩家携带物品中
  1072. gameState.player.carriedItems.push({
  1073. ...tileMap,
  1074. originalX: x,
  1075. originalY: y
  1076. });
  1077. }
  1078. }
  1079. } else {
  1080. showGameMessage(CONFIG.TIPS.NULL_PICKUP_ITEM, 'info');
  1081. }
  1082. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ACTION_DELAY));
  1083. }
  1084. // 使用物品函数
  1085. window.useItem = async function() {
  1086. if (shouldStopExecution || isColliding.value || isSliding.value) {
  1087. return;
  1088. }
  1089. //取人物当前位置
  1090. let x = playerPosition.value.x;
  1091. let y = playerPosition.value.y;
  1092. let tileMap = walkablePointsMap.get(`${x},${y}`);
  1093. // 判断当前位置是否有特殊需求
  1094. if (tileMap && tileMap.type === BLOCKLY_MAP_TYPE_DICT.TASK) {
  1095. // 检查玩家是否携带了需要的物品
  1096. const requiredItems = tileMap.type;
  1097. let hasRequiredItem = false;
  1098. let itemIndex = -1;
  1099. if (gameState.player.carriedItems.length > 0) {
  1100. hasRequiredItem = true;
  1101. itemIndex = 0;
  1102. }
  1103. if (hasRequiredItem) {
  1104. // 获取要使用的物品
  1105. const itemToUse = gameState.player.carriedItems[itemIndex];
  1106. // 从携带物品中移除已使用的物品
  1107. gameState.player.carriedItems.splice(itemIndex, 1);
  1108. // 执行物品使用动画,传递finishAnimation
  1109. await animateItemUse(itemToUse, itemIndex, tileMap.finishAnimation);
  1110. // 使用物品成功
  1111. showGameMessage(tileMap.finishedTip || CONFIG.TIPS.USE_ITEM_SUCCESS, 'success');
  1112. } else {
  1113. // 提示缺少所需物品
  1114. showGameMessage(tileMap.unfinishedTip || CONFIG.TIPS.USE_SPECIAL_ITEM, 'warning');
  1115. }
  1116. } else {
  1117. showGameMessage(CONFIG.TIPS.NO_USE_ITEM, 'info');
  1118. }
  1119. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ACTION_DELAY));
  1120. }
  1121. // 暂停函数
  1122. window.pause = async function(seconds) {
  1123. if (shouldStopExecution || isSliding.value) {
  1124. return;
  1125. }
  1126. // 显示倒计时
  1127. showCountdown.value = true;
  1128. countdownValue.value = seconds;
  1129. // 使用更细粒度的时间间隔,确保能快速响应重置操作
  1130. const interval = 100; // 100毫秒检查一次
  1131. const totalIterations = seconds * 10; // 总迭代次数
  1132. //处理特殊任务消失
  1133. processingSpecialTasksDisappearing();
  1134. for (let i = 1; i <= totalIterations; i++) {
  1135. // 检查是否应该停止执行
  1136. if (shouldStopExecution) {
  1137. break;
  1138. }
  1139. // 每10次迭代(即1秒)减少倒计时值
  1140. if (i % 10 === 0) {
  1141. countdownValue.value--;
  1142. }
  1143. // 等待一小段时间
  1144. await new Promise(resolve => setTimeout(resolve, interval));
  1145. // 检查是否应该停止执行
  1146. if (shouldStopExecution) {
  1147. break;
  1148. }
  1149. }
  1150. // 隐藏倒计时
  1151. showCountdown.value = false;
  1152. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ACTION_DELAY));
  1153. }
  1154. // 声音函数
  1155. window.playSound = async function() {
  1156. if (shouldStopExecution || isColliding.value || isSliding.value) {
  1157. return;
  1158. }
  1159. if(processingSpecialTasksDisappearing()){
  1160. //延迟,确保声音播放完成
  1161. await playMp3(passMp3);
  1162. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.PALY_MP3_TIMES));
  1163. return
  1164. }
  1165. //延迟,确保声音播放完成
  1166. await playMp3(failureMp3);
  1167. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.PALY_MP3_TIMES));
  1168. };
  1169. // 修建函数
  1170. window.construct = async function() {
  1171. if (shouldStopExecution || isColliding.value || isSliding.value) {
  1172. return;
  1173. }
  1174. //处理特殊任务消失
  1175. processingSpecialTasksDisappearing()
  1176. };
  1177. //校验是否到达终点
  1178. window.isFinish = async function() {
  1179. // 如果已经发生过碰撞,不再执行任何检查
  1180. if (isColliding.value) {
  1181. return;
  1182. }
  1183. // 校验是否到达终点
  1184. if (gameState.player.position.x === endPoint.value.x && gameState.player.position.y === endPoint.value.y) {
  1185. // 如果通过了当前路线,标记为已通过并自动切换到下一个路线(true:切换下一关,false:全部通关)
  1186. if (markCurrentRouteAsPassed()){
  1187. //通关路线提示
  1188. showGameMessage(CONFIG.TIPS.PASS_ROUTE, 'success');
  1189. return;
  1190. }
  1191. // 全部通关
  1192. // 统计所有类型为TASK的任务点总数||必须完成拾取物品的人物总数
  1193. const totalTasks = gameState.mapData.walkablePoints.filter(
  1194. p => p.type === BLOCKLY_MAP_TYPE_DICT.TASK ||
  1195. p.type === BLOCKLY_MAP_TYPE_DICT.ITEM && p.must === true
  1196. ).length;
  1197. // 统计其中status为true的完成任务数||完成拾取物品的人物总数
  1198. const completedTasks = gameState.mapData.walkablePoints.filter(
  1199. p => p.type === BLOCKLY_MAP_TYPE_DICT.TASK && p.status === true
  1200. || p.type === BLOCKLY_MAP_TYPE_DICT.ITEM && p.must === true && p.status === true
  1201. ).length;
  1202. //blockly总星星数量
  1203. //无任务情况下直接完成
  1204. if (totalTasks === 0 || completedTasks === totalTasks) {
  1205. gameState.player.hasReachedEnd = true;
  1206. emits('saveProgress', 'blockly', passConfig.value.starTotal * 100)
  1207. showGameMessage(CONFIG.TIPS.FINISH, 'success' );
  1208. // 展示通过动画
  1209. await showPass(passConfig.value.starTotal);
  1210. return;
  1211. }
  1212. //任务失败
  1213. // 计算完成百分比
  1214. const completionPercentage = totalTasks > 0 ? Math.round(completedTasks / totalTasks * passConfig.value.starTotal) : passConfig.value.starTotal;
  1215. if (completionPercentage > 0){
  1216. showGameMessage(CONFIG.TIPS.EFFORT, 'warning');
  1217. }else{
  1218. showGameMessage(CONFIG.TIPS.UNFINISHED, 'error');
  1219. }
  1220. emits('saveProgress', 'blockly', completionPercentage * 100)
  1221. // 展示通过动画
  1222. await showPass(completionPercentage);
  1223. }
  1224. };
  1225. //================特殊组件积木逻辑=====================
  1226. //移动逻辑处理(前后通用)
  1227. async function moveStep(newX, newY, moveDirection = 0){
  1228. // 检查是否应该停止执行
  1229. if (shouldStopExecution) {
  1230. return;
  1231. }
  1232. // 检查是否可以移动
  1233. if (walkablePointsMap.has(`${newX},${newY}`)) {
  1234. // 移动前处理方块类型逻辑
  1235. await switchMapType(0);
  1236. // 使用平滑移动动画
  1237. await smoothMoveTo(newX, newY);
  1238. // 移动后处理方块类型逻辑
  1239. await switchMapType(1, moveDirection);
  1240. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ACTION_DELAY));
  1241. } else {
  1242. // 发生碰撞 使用统一的碰撞处理方法
  1243. await handleWallCollision();
  1244. }
  1245. }
  1246. // 处理地图类型逻辑
  1247. async function switchMapType(type, moveDirection = 0) {
  1248. //取人物当前位置
  1249. let x = playerPosition.value.x;
  1250. let y = playerPosition.value.y;
  1251. let tileMap = walkablePointsMap.get(`${x},${y}`);
  1252. //移动前置
  1253. if (type === 0) {
  1254. //判断方块类型并处理逻辑
  1255. switch (tileMap.type) {
  1256. case BLOCKLY_MAP_TYPE_DICT.TASK:
  1257. await taskLogic(tileMap);
  1258. break;
  1259. }
  1260. }else {//移动后置
  1261. //判断方块类型并处理逻辑
  1262. switch (tileMap.type) {
  1263. case BLOCKLY_MAP_TYPE_DICT.ICE:
  1264. do {
  1265. showGameMessage(tileMap.tip, 'warning',CONFIG.DELAY.ICE_MESSAGE_DISPLAY)
  1266. // 处理方块类型逻辑
  1267. await switchMapType(0);
  1268. console.log("滑行前位置:" + playerPosition.value.x + "," + playerPosition.value.y);
  1269. await slidingLogic(moveDirection);
  1270. tileMap = walkablePointsMap.get(`${playerPosition.value.x},${playerPosition.value.y}`);
  1271. }while (tileMap.type === BLOCKLY_MAP_TYPE_DICT.ICE && !isColliding.value)
  1272. break;
  1273. case BLOCKLY_MAP_TYPE_DICT.TRAP:
  1274. showGameMessage(tileMap.tip, 'error')
  1275. await handleWallCollision(tileMap.tip);
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. // 平滑移动函数
  1281. async function smoothMoveTo(targetX, targetY) {
  1282. const startX = playerPosition.value.x;
  1283. const startY = playerPosition.value.y;
  1284. const startTime = performance.now();
  1285. // 使用Promise包装动画过程,使其可以await
  1286. return new Promise(resolve => {
  1287. function animate(currentTime) {
  1288. // 检查是否应该停止执行
  1289. if (shouldStopExecution) {
  1290. resolve();
  1291. return;
  1292. }
  1293. const elapsed = currentTime - startTime;
  1294. const progress = Math.min(elapsed / CONFIG.ANIMATION.MOVE_DURATION, 1); // 计算进度,最大为1
  1295. // 线性插值计算当前位置
  1296. const currentX = startX + (targetX - startX) * progress;
  1297. const currentY = startY + (targetY - startY) * progress;
  1298. gameState.player = {
  1299. ...gameState.player,
  1300. position: { x: currentX, y: currentY },
  1301. };
  1302. // 检查是否到达终点
  1303. if (progress < 1) {
  1304. // 继续动画
  1305. requestAnimationFrame(animate);
  1306. } else {
  1307. resolve(); // 动画完成
  1308. }
  1309. }
  1310. // 启动动画
  1311. requestAnimationFrame(animate);
  1312. });
  1313. }
  1314. // 特殊任务处理消失(不需要物品)
  1315. function processingSpecialTasksDisappearing(errorTip = CONFIG.TIPS.ERROR_ERROR) {
  1316. let x = playerPosition.value.x;
  1317. let y = playerPosition.value.y;
  1318. let tileMap = walkablePointsMap.get(`${x},${y}`);
  1319. if (tileMap && tileMap.type === BLOCKLY_MAP_TYPE_DICT.TASK) {
  1320. const pointIndex = gameState.mapData.walkablePoints.findIndex(
  1321. p => p.x === x && p.y === y
  1322. );
  1323. if (pointIndex !== -1) {
  1324. const updatedPoint = { ...gameState.mapData.walkablePoints[pointIndex] };
  1325. updatedPoint.img = updatedPoint.endImg;
  1326. updatedPoint.status = true;
  1327. gameState.mapData.walkablePoints.splice(pointIndex, 1, updatedPoint);
  1328. walkablePointsMap.set(`${x},${y}`, updatedPoint);
  1329. }
  1330. showGameMessage(tileMap.finishedTip || CONFIG.TIPS.USE_ITEM_SUCCESS, 'success');
  1331. return true;
  1332. }else{
  1333. showGameMessage(errorTip, 'info');
  1334. return false;
  1335. }
  1336. }
  1337. // 处理冰块滑行逻辑
  1338. async function slidingLogic(moveDirection = 0) {
  1339. if (shouldStopExecution || isColliding.value) {
  1340. return;
  1341. }
  1342. gameState.player.isSliding = true;
  1343. try {
  1344. // 计算下一个位置
  1345. let nextX = playerPosition.value.x;
  1346. let nextY = playerPosition.value.y;
  1347. // 根据当前方向计算下一个位置
  1348. // 如果有指定方向,使用该方向滑行
  1349. if (moveDirection === 0) {
  1350. switch(playerDirection.value) {
  1351. case CONFIG.GAME.DIRECTIONS.UP: nextY--; break;
  1352. case CONFIG.GAME.DIRECTIONS.RIGHT: nextX++; break;
  1353. case CONFIG.GAME.DIRECTIONS.DOWN: nextY++; break;
  1354. case CONFIG.GAME.DIRECTIONS.LEFT: nextX--; break;
  1355. }
  1356. }else{
  1357. switch(playerDirection.value) {
  1358. case CONFIG.GAME.DIRECTIONS.UP: nextY++; break;
  1359. case CONFIG.GAME.DIRECTIONS.RIGHT: nextX--; break;
  1360. case CONFIG.GAME.DIRECTIONS.DOWN: nextY--; break;
  1361. case CONFIG.GAME.DIRECTIONS.LEFT: nextX++; break;
  1362. }
  1363. }
  1364. // 检查下一个位置是否可行走
  1365. if (walkablePointsMap.has(`${nextX},${nextY}`)) {
  1366. // 执行平滑移动到下一个位置
  1367. await smoothMoveTo(nextX, nextY);
  1368. } else {
  1369. // 使用统一的碰撞处理方法,但不等待延迟(因为slidingLogic不需要这个延迟)
  1370. await handleWallCollision();
  1371. }
  1372. } catch (error) {
  1373. console.error('滑行过程中发生错误:', error);
  1374. } finally {
  1375. // 无论如何都要确保滑行状态被重置
  1376. gameState.player.isSliding = false;
  1377. }
  1378. }
  1379. // 处理任务逻辑
  1380. async function taskLogic(tileMap) {
  1381. if (shouldStopExecution || isColliding.value) {
  1382. return;
  1383. }
  1384. try {
  1385. // 判断当前位置是否有特殊需求
  1386. if (tileMap && tileMap.type === BLOCKLY_MAP_TYPE_DICT.TASK && tileMap.noPassing === true && tileMap.status !== true) {
  1387. await handleWallCollision(tileMap.unfinishedTip);
  1388. return;
  1389. }
  1390. } catch (error) {
  1391. console.error('处理任务逻辑发生错误:', error);
  1392. }
  1393. }
  1394. // 物品拾取动画函数
  1395. async function animateItemPickup(item, itemX, itemY) {
  1396. // 创建临时动画元素
  1397. const tempItem = document.createElement('div');
  1398. const iconSize = tileSize.value * CONFIG.STYLES.IMG_SIZE_RATIO;
  1399. const marginSize = tileSize.value * CONFIG.STYLES.IMG_SIZE_MARGIN;
  1400. // 计算物品在地图上的实际位置
  1401. const itemLeft = itemX * tileSize.value - tileSize.value + marginSize;
  1402. const itemTop = itemY * tileSize.value - tileSize.value + marginSize;
  1403. // 平行移动到容器位置,同时调整大小
  1404. const finalSize = tileSize.value * CONFIG.STYLES.ITEM_CONTAINER_RATIO;
  1405. // 获取携带物品容器的位置(左上角)
  1406. // 注意:这里不需要考虑当前已携带物品数量,因为物品还没被添加
  1407. let containerLeft = CONFIG.STYLES.ITEM_CONTAINER_POSITION.x;
  1408. let containerTop = CONFIG.STYLES.ITEM_CONTAINER_POSITION.y;
  1409. // 如果已经有物品,计算新物品应该出现的位置
  1410. if (gameState.player.carriedItems.length > 0) {
  1411. let containerSize = finalSize + CONFIG.STYLES.ITEM_CONTAINER_SPACING;
  1412. containerLeft = CONFIG.STYLES.ITEM_CONTAINER_POSITION.x + gameState.player.carriedItems.length * containerSize;
  1413. }
  1414. // 设置临时元素样式
  1415. Object.assign(tempItem.style, {
  1416. position: 'absolute',
  1417. left: itemLeft + 'px',
  1418. top: itemTop + 'px',
  1419. width: iconSize + 'px',
  1420. height: iconSize + 'px',
  1421. backgroundImage: `url(${item.img})`,
  1422. backgroundSize: 'contain',
  1423. backgroundPosition: 'center',
  1424. backgroundRepeat: 'no-repeat',
  1425. zIndex: '100', // 确保在最上层
  1426. opacity: '0', // 初始完全透明
  1427. transform: 'scale(1)',
  1428. });
  1429. // 将临时元素添加到地图容器
  1430. const mapBackground = document.querySelector('.map-background');
  1431. if (!mapBackground) {
  1432. console.error('地图容器未找到');
  1433. return;
  1434. }
  1435. mapBackground.appendChild(tempItem);
  1436. // 触发淡入动画
  1437. tempItem.offsetHeight;
  1438. tempItem.style.transition = 'opacity ' + CONFIG.DELAY.ITEMS_APPEAR + 'ms ease-out';
  1439. tempItem.style.opacity = '1'; // 完全显示
  1440. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ITEMS_APPEAR));
  1441. // 移动动画
  1442. tempItem.style.transition = 'all ' + CONFIG.DELAY.ITEMS_FLIGHT_ANIMATION_DELAY + 'ms ease-out'; // 减慢动画速度
  1443. Object.assign(tempItem.style, {
  1444. left: containerLeft + 'px',
  1445. top: containerTop + 'px',
  1446. width: finalSize + 'px',
  1447. height: finalSize + 'px',
  1448. opacity: '0.8',
  1449. transform: 'scale(1)',
  1450. });
  1451. // 等待动画完成
  1452. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ITEMS_FLIGHT_ANIMATION_DELAY));
  1453. // 移除临时元素
  1454. if (mapBackground.contains(tempItem)) {
  1455. mapBackground.removeChild(tempItem);
  1456. }
  1457. }
  1458. // 物品使用动画函数
  1459. async function animateItemUse(item, itemIndex, finishAnimation) {
  1460. // 创建临时动画元素
  1461. const tempItem = document.createElement('div');
  1462. const finalSize = tileSize.value * CONFIG.STYLES.ITEM_CONTAINER_RATIO;
  1463. const iconSize = tileSize.value * CONFIG.STYLES.IMG_SIZE_RATIO;
  1464. const marginSize = tileSize.value * CONFIG.STYLES.IMG_SIZE_MARGIN;
  1465. // 计算物品在物品栏中的初始位置
  1466. let startLeft = CONFIG.STYLES.ITEM_CONTAINER_POSITION.x;
  1467. let startTop = CONFIG.STYLES.ITEM_CONTAINER_POSITION.y;
  1468. let containerSize = finalSize + CONFIG.STYLES.ITEM_CONTAINER_SPACING;
  1469. // 根据物品索引计算在物品栏中的位置
  1470. if (itemIndex > 0) {
  1471. startLeft = CONFIG.STYLES.ITEM_CONTAINER_POSITION.x + itemIndex * containerSize;
  1472. }
  1473. // 计算玩家当前位置(物品目标位置)
  1474. const playerLeft = playerPosition.value.x * tileSize.value - tileSize.value + marginSize;
  1475. const playerTop = playerPosition.value.y * tileSize.value - tileSize.value + marginSize;
  1476. // 获取玩家元素
  1477. const playerElement = document.querySelector('.player');
  1478. const originalPlayerZIndex = playerElement ? playerElement.style.zIndex : '';
  1479. const originalPlayerOpacity = playerElement ? playerElement.style.opacity : '1';
  1480. // 将临时元素添加到地图容器
  1481. const mapBackground = document.querySelector('.map-background');
  1482. if (!mapBackground) {
  1483. console.error('地图容器未找到');
  1484. return;
  1485. }
  1486. // 设置临时元素初始样式
  1487. Object.assign(tempItem.style, {
  1488. position: 'absolute',
  1489. left: startLeft + 'px',
  1490. top: startTop + 'px',
  1491. width: finalSize + 'px',
  1492. height: finalSize + 'px',
  1493. backgroundImage: `url(${item.img})`,
  1494. backgroundSize: 'contain',
  1495. backgroundPosition: 'center',
  1496. backgroundRepeat: 'no-repeat',
  1497. zIndex: '120',
  1498. transition: 'transform ' + CONFIG.DELAY.ITEMS_APPEAR + 'ms ease-out',
  1499. transform: 'scale(1)',
  1500. });
  1501. mapBackground.appendChild(tempItem);
  1502. // 1. 首先让小智逐渐消失
  1503. if (playerElement) {
  1504. playerElement.style.transition = 'opacity ' + CONFIG.DELAY.PLAYER_FADE_DURATION + 'ms ease-out';
  1505. playerElement.style.opacity = '0';
  1506. }
  1507. // 2. 准备漂移动画
  1508. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ITEMS_APPEAR));
  1509. // 3. 如果有视频,先创建视频元素但不自动播放
  1510. let videoElement = null;
  1511. if (finishAnimation) {
  1512. // 创建视频元素
  1513. videoElement = document.createElement('video');
  1514. videoElement.src = finishAnimation;
  1515. videoElement.autoplay = false; // 不自动播放
  1516. // videoElement.muted = true; // 视频默认静音
  1517. videoElement.playsinline = true;
  1518. videoElement.style.position = 'absolute';
  1519. videoElement.style.left = playerLeft + 'px';
  1520. videoElement.style.top = playerTop + 'px';
  1521. videoElement.style.width = iconSize + 'px';
  1522. videoElement.style.height = iconSize + 'px';
  1523. videoElement.style.zIndex = '2000'; // 提高z-index确保视频可见
  1524. videoElement.style.objectFit = 'contain';
  1525. // 添加渐变显示效果
  1526. videoElement.style.opacity = '0';
  1527. videoElement.style.transition = 'opacity ' + CONFIG.DELAY.VIDEO_FADE_DURATION + 'ms ease-in-out';
  1528. // 添加视频到地图容器
  1529. mapBackground.appendChild(videoElement);
  1530. // 触发重绘后设置透明度为1,实现渐变效果
  1531. setTimeout(() => {
  1532. videoElement.style.opacity = '1';
  1533. console.log('Video opacity set to 1');
  1534. }, CONFIG.DELAY.VIDEO_FADE_DURATION);
  1535. } else {
  1536. console.log('无视频');
  1537. }
  1538. // 4. 设置漂移动画样式
  1539. tempItem.style.transition = 'all ' + CONFIG.DELAY.TEMP_ITEM_FADE_DURATION + 'ms ease-out';
  1540. Object.assign(tempItem.style, {
  1541. left: playerLeft + 'px',
  1542. top: playerTop + 'px',
  1543. width: iconSize + 'px',
  1544. height: iconSize + 'px',
  1545. });
  1546. // 5. 等待漂移到玩家位置
  1547. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.ITEMS_FLIGHT_ANIMATION_DELAY));
  1548. // 6. 移除临时元素
  1549. if (mapBackground.contains(tempItem)) {
  1550. mapBackground.removeChild(tempItem);
  1551. }
  1552. let x = playerPosition.value.x;
  1553. let y = playerPosition.value.y;
  1554. // 8. 视频播放完成后替换任务图片
  1555. const pointIndex = gameState.mapData.walkablePoints.findIndex(
  1556. p => p.x === x && p.y === y
  1557. );
  1558. if (pointIndex !== -1) {
  1559. // 保留点但移除img属性并设置完成状态图标
  1560. const updatedPoint = { ...gameState.mapData.walkablePoints[pointIndex] };
  1561. updatedPoint.img = updatedPoint.endImg; // 设置完成状态图标
  1562. updatedPoint.status = true;
  1563. gameState.mapData.walkablePoints.splice(pointIndex, 1, updatedPoint);
  1564. // 更新映射
  1565. walkablePointsMap.set(`${x},${y}`, updatedPoint);
  1566. }
  1567. // 9. 处理视频播放或直接替换任务图片
  1568. if (videoElement) {
  1569. // 开始播放视频
  1570. try {
  1571. const playPromise = videoElement.play();
  1572. if (playPromise !== undefined) {
  1573. playPromise.then(_ => {
  1574. console.log('Video started playing successfully');
  1575. }).catch(error => {
  1576. console.error('Error playing video:', error);
  1577. });
  1578. }
  1579. } catch (error) {
  1580. console.error('Exception when playing video:', error);
  1581. }
  1582. // 等待视频播放完成,添加超时处理
  1583. await new Promise(resolve => {
  1584. videoElement.onended = () => {
  1585. console.log('Video ended');
  1586. resolve();
  1587. };
  1588. // 添加超时处理,防止视频卡住
  1589. setTimeout(() => {
  1590. resolve();
  1591. }, CONFIG.DELAY.VIDEO_TIMEOUT);
  1592. });
  1593. // 10. 移除视频元素 - 添加淡出效果
  1594. if (mapBackground.contains(videoElement)) {
  1595. // 设置淡出效果
  1596. videoElement.style.transition = 'opacity ' + CONFIG.DELAY.VIDEO_FADE_DURATION + 'ms ease-in-out';
  1597. videoElement.style.opacity = '0';
  1598. // 等待淡出动画完成后再移除元素
  1599. setTimeout(() => {
  1600. if (mapBackground.contains(videoElement)) {
  1601. mapBackground.removeChild(videoElement);
  1602. }
  1603. }, CONFIG.DELAY.VIDEO_FADE_DURATION); // 与动画时长一致
  1604. }
  1605. } else {
  1606. // 10.无视频
  1607. }
  1608. // 11. 视频消失后稍作停留,让用户看到完成后的图片
  1609. await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.COMPLETION_DISPLAY));
  1610. // 逐步显示玩家并将其置顶
  1611. if (playerElement) {
  1612. playerElement.style.transition = 'opacity ' + CONFIG.DELAY.PLAYER_FADE_DURATION + 'ms ease-in';
  1613. playerElement.style.opacity = originalPlayerOpacity;
  1614. playerElement.style.zIndex = originalPlayerZIndex || '100';
  1615. // 额外设置一个较高的z-index确保盖住其他人物图片
  1616. setTimeout(() => {
  1617. playerElement.style.zIndex = '150';
  1618. }, CONFIG.DELAY.PLAYER_FADE_DURATION);
  1619. }
  1620. }
  1621. // 展示通过动画
  1622. async function showPass(passStar) {
  1623. // 记录通过的星星数量
  1624. passConfig.value.passStar = passStar;
  1625. passConfig.value.title = 'YOU WIN!';
  1626. passConfig.value.passBackground = cupbg;
  1627. switch (passStar) {
  1628. case 0:
  1629. passConfig.value.title = 'YOU LOSE!';
  1630. passConfig.value.passTrophy = nocup;
  1631. passConfig.value.passBackground = nocupbg;
  1632. await playMp3(passFailure);
  1633. break;
  1634. case 1:
  1635. passConfig.value.passTrophy = coppercup;
  1636. await playMp3(passTrump);
  1637. break;
  1638. case 2:
  1639. passConfig.value.passTrophy = silvercup;
  1640. await playMp3(passTrump);
  1641. break;
  1642. case 3:
  1643. passConfig.value.passTrophy = goldcup;
  1644. await playMp3(passFireworks);
  1645. break;
  1646. }
  1647. showOverlay.value = true;
  1648. }
  1649. // 标记当前路线为已通过
  1650. const markCurrentRouteAsPassed = () => {
  1651. routePassedStatus.value[currentRouteIndex.value] = true;
  1652. // 自动切换到下一个可用路线
  1653. if (currentRouteIndex.value < parsedRouteList.value.length - 1) {
  1654. // 解锁下一个路线
  1655. routePassedStatus.value[currentRouteIndex.value + 1] = true;
  1656. // 自动切换到下一个路线
  1657. switchRoute(currentRouteIndex.value + 1);
  1658. return true;
  1659. }
  1660. return false;
  1661. };
  1662. //================卸载区=====================
  1663. // 组件卸载时清理
  1664. onUnmounted(() => {
  1665. // 清理Blockly工作区
  1666. if (workspace) {
  1667. workspace.dispose();
  1668. workspace = null;
  1669. }
  1670. // 移除事件监听器
  1671. window.removeEventListener('resize', updateMapContainerDimensions);
  1672. // 停止当前正在执行的代码
  1673. shouldStopExecution = true;
  1674. // 取消任何正在进行的执行
  1675. if (executionAbortController) {
  1676. executionAbortController.abort();
  1677. executionAbortController = null;
  1678. }
  1679. // 清除当前执行的Promise引用
  1680. currentExecutionPromise = null;
  1681. // 清理所有临时DOM元素
  1682. const mapBackground = document.querySelector('.map-background');
  1683. if (mapBackground) {
  1684. // 清除视频元素
  1685. const videoElements = mapBackground.querySelectorAll('video');
  1686. videoElements.forEach(video => {
  1687. if (mapBackground.contains(video)) {
  1688. mapBackground.removeChild(video);
  1689. }
  1690. });
  1691. // 清除临时动画元素
  1692. const tempElements = mapBackground.querySelectorAll('div[style*="backgroundImage"]');
  1693. tempElements.forEach(temp => {
  1694. if (mapBackground.contains(temp)) {
  1695. mapBackground.removeChild(temp);
  1696. }
  1697. });
  1698. }
  1699. // 清理全局函数引用
  1700. window.pickupItem = null;
  1701. window.useItem = null;
  1702. // 重置游戏状态
  1703. gameState.player.carriedItems = [];
  1704. gameState.player.position = { ...startPoint.value };
  1705. });
  1706. </script>
  1707. <style scoped lang="scss">
  1708. @use "sass:math";
  1709. @function rpx($px) {
  1710. @return math.div($px, 750) * 100vw;
  1711. }
  1712. /* 全屏遮罩盒子样式 */
  1713. .fullscreen-overlay {
  1714. position: fixed;
  1715. top: 0;
  1716. left: 0;
  1717. width: 100%;
  1718. height: 100%;
  1719. background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色 遮罩 */
  1720. z-index: 9999; /* 确保在最上层 */
  1721. // pointer-events: none; /* 允许点击穿透 */
  1722. display: flex;
  1723. justify-content: center;
  1724. align-items: center;
  1725. }
  1726. /* 垂直居中盒子样式 */
  1727. .centered-box {
  1728. border-radius: rpx(10);
  1729. z-index: 10000;
  1730. width: rpx(220);
  1731. height: rpx(255);
  1732. overflow: auto;
  1733. pointer-events: auto;
  1734. // display: flex;
  1735. flex-direction: column;
  1736. position: relative;
  1737. /* 初始状态:缩小、透明 */
  1738. transform: scale(0.7);
  1739. opacity: 0;
  1740. /* 动画效果 */
  1741. animation: popUp 0.5s ease-out forwards;
  1742. }
  1743. /* 弹出动画定义 */
  1744. @keyframes popUp {
  1745. 0% {
  1746. transform: scale(0.7);
  1747. opacity: 0;
  1748. }
  1749. 70% {
  1750. transform: scale(1.05);
  1751. opacity: 0.9;
  1752. }
  1753. 100% {
  1754. transform: scale(1);
  1755. opacity: 1;
  1756. }
  1757. }
  1758. /* 彩带容器样式 */
  1759. .confetti-container {
  1760. position: absolute;
  1761. width: 100%;
  1762. height: 100%;
  1763. overflow: hidden;
  1764. z-index: 9999;
  1765. pointer-events: none;
  1766. }
  1767. /* 彩带基本样式 */
  1768. .confetti {
  1769. position: absolute;
  1770. width: rpx(5);
  1771. height: rpx(15);
  1772. opacity: 0;
  1773. animation-timing-function: ease-in-out;
  1774. animation-iteration-count: infinite;
  1775. animation-fill-mode: forwards;
  1776. }
  1777. /* 彩带颜色和动画 */
  1778. .confetti-1 { background-color: #FF5252; animation: confetti-fall 2s 0.2s linear infinite; left: 5%; }
  1779. .confetti-2 { background-color: #536DFE; animation: confetti-fall 2.7s 0.9s linear infinite; left: 15%; }
  1780. .confetti-3 { background-color: #FFC107; animation: confetti-fall 2.3s 1.8s linear infinite; left: 25%; }
  1781. .confetti-4 { background-color: #4CAF50; animation: confetti-fall 2.1s 1.4s linear infinite; left: 35%; }
  1782. .confetti-5 { background-color: #9C27B0; animation: confetti-fall 2.5s 1.8s linear infinite; left: 45%; }
  1783. .confetti-6 { background-color: #2196F3; animation: confetti-fall 2.2s 0.2s linear infinite; left: 55%; }
  1784. .confetti-7 { background-color: #FF9800; animation: confetti-fall 2.4s 1.8s linear infinite; left: 65%; }
  1785. .confetti-8 { background-color: #795548; animation: confetti-fall 2s 1.4s linear infinite; left: 75%; }
  1786. .confetti-9 { background-color: #607D8B; animation: confetti-fall 2.6s 3.4s linear infinite; left: 85%; }
  1787. .confetti-10 { background-color: #E91E63; animation: confetti-fall 2.3s 1.8s linear infinite; left: 95%; }
  1788. /* 彩带飘落动画 */
  1789. @keyframes confetti-fall {
  1790. 0% {
  1791. top: -10%;
  1792. opacity: 0;
  1793. transform: rotate(0deg);
  1794. }
  1795. 10% {
  1796. opacity: 1;
  1797. }
  1798. 90% {
  1799. opacity: 1;
  1800. }
  1801. 100% {
  1802. top: 100%;
  1803. opacity: 0;
  1804. transform: rotate(360deg);
  1805. }
  1806. }
  1807. /* 关闭按钮样式 */
  1808. .close-button {
  1809. position: absolute;
  1810. top: rpx(0);
  1811. right: rpx(2);
  1812. font-size: rpx(20);
  1813. color: #2b8fdd;
  1814. font-weight: bold;
  1815. cursor: pointer;
  1816. width: rpx(25);
  1817. height: rpx(25);
  1818. display: flex;
  1819. justify-content: center;
  1820. align-items: center;
  1821. z-index: 10001;
  1822. text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  1823. // -webkit-text-stroke: 1px white; // 描
  1824. }
  1825. .close-button:hover {
  1826. transform: scale(1.1);
  1827. }
  1828. .top-box {
  1829. width: 100%;
  1830. height: rpx(60);
  1831. border-radius: 5px;
  1832. display: flex;
  1833. justify-content: center;
  1834. align-items: center;
  1835. }
  1836. .title-text {
  1837. font-size: rpx(20);
  1838. font-weight: bold;
  1839. color: white;
  1840. font-family: 'SourceHanSansCN-Bold_0';
  1841. text-align: center;
  1842. width: 100%;
  1843. height: 100%;
  1844. display: flex;
  1845. justify-content: center;
  1846. align-items: center;
  1847. text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  1848. }
  1849. .middle-box {
  1850. width: 100%;
  1851. height: rpx(130);
  1852. border-radius: 5px;
  1853. display: flex;
  1854. justify-content: center;
  1855. align-items: center;
  1856. }
  1857. .gold-cup-image{
  1858. height: 100%;
  1859. width: 100%;
  1860. object-fit: contain;
  1861. }
  1862. .bottom-box {
  1863. width: 100%;
  1864. height: rpx(40);
  1865. border-radius: 5px;
  1866. display: flex;
  1867. justify-content: center;
  1868. align-items: flex-start;
  1869. gap: rpx(12);
  1870. margin-top: -rpx(10); /* 向上调整位置 */
  1871. }
  1872. .gold-star-image {
  1873. width: rpx(35);
  1874. height: 100%;
  1875. object-fit: contain;
  1876. }
  1877. .star-top {
  1878. object-position: top;
  1879. }
  1880. .star-bottom {
  1881. object-position: bottom;
  1882. }
  1883. //将tileSize属性绑定到CSS变量上
  1884. :root {
  1885. --tile-size: v-bind('tileSize + "px"');
  1886. }
  1887. /* 游戏简介样式 */
  1888. .info-message-container {
  1889. display: flex;
  1890. flex-direction: column;
  1891. align-items: flex-start;
  1892. }
  1893. .message-item {
  1894. display: flex;
  1895. align-items: flex-start;
  1896. width: 100%;
  1897. margin-bottom: rpx(5);
  1898. }
  1899. /* 头像样式 */
  1900. .avatar {
  1901. margin-right: rpx(4);
  1902. flex-shrink: 0;
  1903. }
  1904. .avatar-image {
  1905. width: rpx(30);
  1906. height: rpx(30);
  1907. object-fit: cover;
  1908. }
  1909. /* 消息内容样式 */
  1910. .message-item {
  1911. flex: 1;
  1912. }
  1913. .message-item p {
  1914. margin: rpx(4) 0;
  1915. line-height: 0;
  1916. font-size: rpx(10);
  1917. text-align: left;
  1918. color: black;
  1919. background-color: #e6faff;
  1920. opacity: 0.8;
  1921. border-radius: rpx(4);
  1922. padding: rpx(2) rpx(5);
  1923. max-width: 100%;
  1924. }
  1925. .message-item p:first-child {
  1926. margin-top: 0;
  1927. font-weight: 500;
  1928. }
  1929. .message-item p:last-child {
  1930. margin-bottom: 0;
  1931. }
  1932. .title-box {
  1933. position: relative;
  1934. top: rpx(5);
  1935. padding-left: 15px;
  1936. z-index: 10;
  1937. display: flex;
  1938. justify-content: space-between;
  1939. align-items: center;
  1940. }
  1941. /* 左侧容器样式 */
  1942. .left-container {
  1943. display: flex;
  1944. flex-direction: column;
  1945. align-items: flex-start;
  1946. gap: 10px;
  1947. }
  1948. /* 线路按钮容器样式 */
  1949. .route-buttons {
  1950. display: flex;
  1951. gap: rpx(5);
  1952. margin-left: rpx(3);
  1953. }
  1954. /* 右侧两个角为圆角的长方形格子样式 */
  1955. .game-badge {
  1956. width: rpx(70);
  1957. height: rpx(20);
  1958. background-color: #5fb5dc;
  1959. color: #fff;
  1960. border-radius: 0 rpx(20) rpx(20) 0;
  1961. display: flex;
  1962. align-items: center;
  1963. justify-content: center;
  1964. font-size: rpx(15);
  1965. font-weight: bold;
  1966. border: none;
  1967. cursor: pointer;
  1968. transition: all 0.3s ease;
  1969. }
  1970. .game-badge:hover {
  1971. background-color: #3498db;
  1972. transform: translateY(-2px);
  1973. box-shadow: 0 rpx(5) rpx(10) rgba(0, 0, 0, 0.1);
  1974. }
  1975. .game-badge.active {
  1976. background-color: #e74c3c;
  1977. color: #fff;
  1978. font-weight: bold;
  1979. }
  1980. /* 右侧容器样式 */
  1981. .right-container {
  1982. display: flex;
  1983. gap: 10px;
  1984. padding-right: 20px;
  1985. }
  1986. /* 上下节按钮样式 */
  1987. .section-button {
  1988. padding: rpx(5) rpx(12);
  1989. border: none;
  1990. border-radius: rpx(10);
  1991. background: #3498db;
  1992. color: #fff;
  1993. font-weight: 500;
  1994. cursor: pointer;
  1995. transition: all 0.3s ease;
  1996. font-size: rpx(7);
  1997. }
  1998. .section-button:hover {
  1999. background: #2980b9;
  2000. transform: translateY(-2px);
  2001. box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
  2002. }
  2003. .previous-btn {
  2004. background-color: rgba(255, 255, 255, 0.8);
  2005. color: #333;
  2006. }
  2007. .previous-btn:hover {
  2008. background-color: rgba(255, 255, 255, 0.9);
  2009. }
  2010. .next-btn {
  2011. background: #5fb5dc;
  2012. }
  2013. .next-btn:hover {
  2014. background: #2980b9;
  2015. }
  2016. .box-icon {
  2017. display: flex;
  2018. align-items: center;
  2019. margin-top: rpx(5);
  2020. gap: 10px;
  2021. padding: 10px 20px;
  2022. background-color: rgba(255, 255, 255, 0.8);
  2023. border-radius: 30px;
  2024. backdrop-filter: blur(10px);
  2025. cursor: pointer;
  2026. transition: all 0.3s ease;
  2027. font-size: 16px;
  2028. color: #333;
  2029. font-weight: 500;
  2030. width: fit-content;
  2031. }
  2032. .box-icon:hover {
  2033. background-color: rgba(255, 255, 255, 0.9);
  2034. transform: translate(-3px);
  2035. }
  2036. .left-icon {
  2037. font-size: 18px;
  2038. }
  2039. .content {
  2040. display: flex;
  2041. flex-wrap: nowrap;
  2042. gap: 20px;
  2043. padding: 20px;
  2044. min-width: 1160px;
  2045. }
  2046. /* 地图区域样式 */
  2047. .map-section {
  2048. flex: 1;
  2049. min-width: 500px;
  2050. background: rgba(248, 249, 250, 0.82);
  2051. padding: 15px;
  2052. border-radius: 15px;
  2053. }
  2054. .map-container {
  2055. position: relative;
  2056. width: 100%;
  2057. height: 100%;
  2058. overflow: hidden; // 防止内容溢出
  2059. }
  2060. .map-background {
  2061. position: relative;
  2062. width: 100%;
  2063. height: 100%;
  2064. background-size: cover;
  2065. background-position: center;
  2066. background-repeat: no-repeat;
  2067. }
  2068. .map-image {
  2069. width: 100%;
  2070. height: 100%;
  2071. object-fit: cover;
  2072. }
  2073. /* 可行走区域样式 */
  2074. .walkable-point {
  2075. position: absolute;
  2076. //background-color: rgba(52, 152, 219, 0.2);
  2077. //border: 1px solid rgba(52, 152, 219, 0.5);
  2078. //box-sizing: border-box;
  2079. //是否显示可行路线
  2080. opacity: 1;
  2081. }
  2082. /* 玩家样式 */
  2083. .player {
  2084. position: absolute;
  2085. background-image: var(--player-image);
  2086. background-size: contain;
  2087. background-repeat: no-repeat;
  2088. background-position: center;
  2089. border-radius: 5px;
  2090. z-index: 10;
  2091. }
  2092. /* 碰撞动画 */
  2093. .player.collision {
  2094. animation: collision 0.5s ease-in-out;
  2095. }
  2096. @keyframes collision {
  2097. 0% { transform: rotate(var(--player-rotation)) translateX(0) translateY(0) scale(1); }
  2098. 25% { transform: rotate(var(--player-rotation)) translateX(-3px) translateY(-2px) scale(1.2); }
  2099. 50% { transform: rotate(var(--player-rotation)) translateX(3px) translateY(2px) scale(1.2); }
  2100. 75% { transform: rotate(var(--player-rotation)) translateX(-3px) translateY(-2px) scale(1.2); }
  2101. 100% { transform: rotate(var(--player-rotation)) translateX(0) translateY(0) scale(1); }
  2102. }
  2103. /* 滑行动画 */
  2104. @keyframes sliding {
  2105. 0% { transform: rotate(var(--player-rotation)) translateX(0) translateY(0); }
  2106. 25% { transform: rotate(var(--player-rotation)) translateX(2px) translateY(0); }
  2107. 75% { transform: rotate(var(--player-rotation)) translateX(-2px) translateY(0); }
  2108. 100% { transform: rotate(var(--player-rotation)) translateX(0) translateY(0); }
  2109. }
  2110. /* 携带物品容器 */
  2111. .carried-items-container {
  2112. position: absolute;
  2113. top: 20px;
  2114. left: 20px;
  2115. background: rgba(255, 255, 255, 0.8);
  2116. border: 2px solid #3498db;
  2117. border-radius: 10px;
  2118. padding: 10px;
  2119. display: flex;
  2120. gap: 10px;
  2121. z-index: 15;
  2122. backdrop-filter: blur(10px);
  2123. animation: fadeInScale 0.5s ease-out;
  2124. }
  2125. /* 淡入缩放动画 */
  2126. @keyframes fadeInScale {
  2127. 0% {
  2128. opacity: 0;
  2129. transform: scale(0.5) translateY(-10px);
  2130. }
  2131. 100% {
  2132. opacity: 1;
  2133. transform: scale(1) translateY(0);
  2134. }
  2135. }
  2136. /* 携带物品样式 */
  2137. .carried-item {
  2138. animation: bounceIn 0.3s ease-out forwards;
  2139. }
  2140. /* 【暂停】怀表容器样式 */
  2141. .watch-container {
  2142. animation: fadeInCountdown 0.3s ease-out;
  2143. position: relative;
  2144. }
  2145. /* 【暂停】怀表表盘样式 */
  2146. .watch-face {
  2147. width: 100%;
  2148. height: 100%;
  2149. border-radius: 50%;
  2150. background: linear-gradient(145deg, rgba(240, 240, 240, 0.8), rgba(200, 200, 200, 0.6));
  2151. border: 4px solid rgba(100, 100, 100, 0.8);
  2152. box-shadow:
  2153. 0 0 20px rgba(0, 0, 0, 0.3),
  2154. inset 0 0 20px rgba(0, 0, 0, 0.1);
  2155. position: relative;
  2156. display: flex;
  2157. justify-content: center;
  2158. align-items: center;
  2159. backdrop-filter: blur(5px);
  2160. }
  2161. /* 【暂停】怀表中心圆点 */
  2162. .watch-center {
  2163. width: 10%;
  2164. height: 10%;
  2165. background-color: rgba(100, 50, 20, 0.8);
  2166. border-radius: 50%;
  2167. position: absolute;
  2168. z-index: 10;
  2169. }
  2170. /* 【暂停】表针容器 */
  2171. .watch-hands {
  2172. position: absolute;
  2173. width: 100%;
  2174. height: 100%;
  2175. display: flex;
  2176. justify-content: center;
  2177. align-items: center;
  2178. }
  2179. /* 【暂停】表针基础样式 */
  2180. .watch-hand {
  2181. position: absolute;
  2182. background-color: rgba(100, 50, 20, 0.8);
  2183. transform-origin: bottom center;
  2184. border-radius: 2px;
  2185. }
  2186. /* 【暂停】时针样式 */
  2187. .hour-hand {
  2188. width: 4%;
  2189. height: 30%;
  2190. top: 20%;
  2191. animation: rotateHourHand 12s linear infinite;
  2192. }
  2193. /* 【暂停】分针样式 */
  2194. .minute-hand {
  2195. width: 3%;
  2196. height: 40%;
  2197. top: 10%;
  2198. animation: rotateMinuteHand 1s linear infinite;
  2199. }
  2200. /* 【暂停】怀表中心倒计时数字 */
  2201. .watch-countdown-number {
  2202. font-size: calc(var(--tile-size, 143px) * 0.3);
  2203. font-weight: bold;
  2204. color: rgba(100, 50, 20, 0.9);
  2205. text-shadow: 1px 1px 2px rgba(255, 255, 255, 0.8);
  2206. z-index: 5;
  2207. animation: pulseCountdown 1s infinite;
  2208. }
  2209. /* 【暂停】怀表淡入动画 */
  2210. @keyframes fadeInCountdown {
  2211. 0% {
  2212. opacity: 0;
  2213. transform: scale(0.8) rotate(-30deg);
  2214. }
  2215. 100% {
  2216. opacity: 1;
  2217. transform: scale(1) rotate(0deg);
  2218. }
  2219. }
  2220. /* 【暂停】倒计时数字脉冲动画 */
  2221. @keyframes pulseCountdown {
  2222. 0%, 100% {
  2223. opacity: 0.9;
  2224. transform: scale(1);
  2225. }
  2226. 50% {
  2227. opacity: 0.7;
  2228. transform: scale(1.05);
  2229. }
  2230. }
  2231. /* 【暂停】时针旋转动画 */
  2232. @keyframes rotateHourHand {
  2233. from { transform: rotate(0deg); }
  2234. to { transform: rotate(360deg); }
  2235. }
  2236. /* 【暂停】分针旋转动画 */
  2237. @keyframes rotateMinuteHand {
  2238. from { transform: rotate(0deg); }
  2239. to { transform: rotate(360deg); }
  2240. }
  2241. /* 弹入动画 */
  2242. @keyframes bounceIn {
  2243. 0% {
  2244. opacity: 0;
  2245. transform: scale(0.3) translateY(-20px);
  2246. }
  2247. 50% {
  2248. opacity: 0.7;
  2249. transform: scale(1.1) translateY(5px);
  2250. }
  2251. 80% {
  2252. opacity: 0.9;
  2253. transform: scale(0.95) translateY(-2px);
  2254. }
  2255. 100% {
  2256. opacity: 1;
  2257. transform: scale(1) translateY(0);
  2258. }
  2259. }
  2260. /* 成功到达终点动画 */
  2261. .player.success {
  2262. animation: success 1s ease-in-out;
  2263. }
  2264. @keyframes success {
  2265. 0% { transform: rotate(var(--player-rotation)) scale(1); }
  2266. 10% { transform: rotate(var(--player-rotation)) scale(1.2) translateX(-5px) translateY(-5px); }
  2267. 20% { transform: rotate(var(--player-rotation)) scale(1.3) translateX(5px) translateY(5px); }
  2268. 30% { transform: rotate(var(--player-rotation)) scale(1.2) translateX(-5px) translateY(-5px); }
  2269. 40% { transform: rotate(var(--player-rotation)) scale(1.3) translateX(5px) translateY(5px); }
  2270. 50% { transform: rotate(var(--player-rotation)) scale(1.4) translateX(0) translateY(0); }
  2271. 60% { transform: rotate(var(--player-rotation)) scale(1.3) translateX(-3px) translateY(-3px); }
  2272. 70% { transform: rotate(var(--player-rotation)) scale(1.2) translateX(3px) translateY(3px); }
  2273. 80% { transform: rotate(var(--player-rotation)) scale(1.3) translateX(-3px) translateY(-3px); }
  2274. 90% { transform: rotate(var(--player-rotation)) scale(1.2) translateX(3px) translateY(3px); }
  2275. 100% { transform: rotate(var(--player-rotation)) scale(1); }
  2276. }
  2277. /* 游戏消息样式 */
  2278. .game-message {
  2279. position: absolute;
  2280. top: 20px;
  2281. left: 50%;
  2282. transform: translateX(-50%);
  2283. padding: 10px 20px;
  2284. border-radius: 5px;
  2285. font-weight: bold;
  2286. z-index: 20;
  2287. min-width: 200px;
  2288. text-align: center;
  2289. }
  2290. .game-message.success {
  2291. background-color: #d4edda;
  2292. color: #155724;
  2293. border: 1px solid #c3e6cb;
  2294. }
  2295. .game-message.error {
  2296. background-color: #f8d7da;
  2297. color: #721c24;
  2298. border: 1px solid #f5c6cb;
  2299. }
  2300. .game-message.info {
  2301. background-color: #d1ecf1;
  2302. color: #0c5460;
  2303. border: 1px solid #bee5eb;
  2304. }
  2305. .game-message.warning {
  2306. background-color: #baeff8;
  2307. color: #035767;
  2308. border: 1px solid #9be9f6;
  2309. }
  2310. /* Blockly区域样式 */
  2311. .blockly-section {
  2312. flex: 1;
  2313. min-width: 600px;
  2314. display: flex;
  2315. flex-direction: column;
  2316. gap: 20px;
  2317. }
  2318. // 统一区块样式
  2319. .map-section, .workspace-section {
  2320. background: rgba(248, 249, 250, 0.82);
  2321. padding: 15px;
  2322. border-radius: 15px;
  2323. height: 100%;
  2324. }
  2325. .map-section h2, .workspace-section h2 {
  2326. margin-bottom: 15px;
  2327. color: #2c3e50;
  2328. border-bottom: 2px solid #3498db;
  2329. padding-bottom: 8px;
  2330. }
  2331. #blocklyDiv {
  2332. height: 87%;
  2333. min-height: rpx(250);
  2334. width: 100%;
  2335. background: #fff;
  2336. border: 1px solid #ddd;
  2337. border-radius: 8px;
  2338. }
  2339. /* 优化Blockly积木样式 */
  2340. /* 积木高度 */
  2341. .blocklyBlockCanvas .blocklyBlock {
  2342. height: 45px; /* 默认高度 */
  2343. min-height: 45px;
  2344. }
  2345. /* 积木内部元素的行高和间距 */
  2346. .blocklyText {
  2347. font-size: 16px;
  2348. line-height: 20px;
  2349. font-weight: 500;
  2350. }
  2351. /* 输入字段的高度 */
  2352. .blocklyHtmlInput {
  2353. height: 30px;
  2354. font-size: 14px;
  2355. padding: 5px;
  2356. }
  2357. /* 下拉菜单的高度 */
  2358. .blocklyDropdownMenu {
  2359. line-height: 28px;
  2360. font-size: 14px;
  2361. }
  2362. /* 优化积木圆角和阴影效果 */
  2363. .blocklyBlock {
  2364. border-radius: 8px;
  2365. filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
  2366. }
  2367. /* 积木之间的连接点间距 */
  2368. .blocklyConnection {
  2369. height: 20px;
  2370. width: 20px;
  2371. }
  2372. /* 工具箱中积木的高度 */
  2373. .blocklyTreeRow {
  2374. height: 40px;
  2375. line-height: 40px;
  2376. }
  2377. .controls {
  2378. display: flex;
  2379. gap: 10px;
  2380. margin: 15px 0px;
  2381. flex-wrap: wrap;
  2382. }
  2383. button {
  2384. padding: 10px 20px;
  2385. border: none;
  2386. border-radius: 5px;
  2387. background: #3498db;
  2388. color: #fff;
  2389. font-weight: 700;
  2390. cursor: pointer;
  2391. transition: all 0.3s ease;
  2392. }
  2393. .game-badge:hover {
  2394. background-color: #3498db;
  2395. transform: translateY(-2px);
  2396. box-shadow: 0 rpx(5) rpx(10) rgba(0, 0, 0, 0.1);
  2397. }
  2398. .game-badge.active {
  2399. background-color: #e74c3c;
  2400. color: #fff;
  2401. font-weight: bold;
  2402. }
  2403. .game-badge.passed {
  2404. background-color: #27ae60;
  2405. color: #fff;
  2406. }
  2407. .game-badge.disabled {
  2408. background-color: #bdc3c7;
  2409. color: #7f8c8d;
  2410. cursor: not-allowed;
  2411. pointer-events: none;
  2412. }
  2413. .game-badge.disabled:hover {
  2414. background-color: #bdc3c7;
  2415. transform: none;
  2416. box-shadow: none;
  2417. }
  2418. #runCode {
  2419. background: #e74c3c;
  2420. }
  2421. #runCode:hover {
  2422. background: #c0392b;
  2423. }
  2424. /* 响应式布局 */
  2425. @media (max-width: 1200px) {
  2426. .map-section,
  2427. .blockly-section {
  2428. flex: 1;
  2429. min-width: 45%;
  2430. }
  2431. .map-background {
  2432. width: 100%;
  2433. height: 400px;
  2434. }
  2435. }
  2436. </style>