MapGame.vue 63 KB

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