|
|
@@ -111,7 +111,7 @@
|
|
|
|
|
|
<div class="workspace-section">
|
|
|
<div class="controls">
|
|
|
- <button id="runCode" @click="runCode" >运行代码</button>
|
|
|
+ <button id="runCode" @click="runCode" :disabled="isRunning">运行代码</button>
|
|
|
<button @click="resetPlayer" >重置玩家</button>
|
|
|
<button @click="clearWorkspace">清空工作区</button>
|
|
|
</div>
|
|
|
@@ -753,8 +753,7 @@ function registerJavaScriptGenerators() {
|
|
|
})()`;
|
|
|
|
|
|
// 获取循环体代码
|
|
|
- // const branch = javascriptGenerator.statementToCode(blockly, 'DO');
|
|
|
- const branch = ""
|
|
|
+ const branch = javascriptGenerator.statementToCode(block, 'DO');
|
|
|
|
|
|
// 生成支持异步的循环代码
|
|
|
let code = `for (let i = 0; i < ${safeRepeats}; i++) {\n`;
|
|
|
@@ -1358,10 +1357,28 @@ window.pause = async function(seconds) {
|
|
|
showCountdown.value = true;
|
|
|
countdownValue.value = seconds;
|
|
|
|
|
|
- // 倒计时循环
|
|
|
- while (countdownValue.value > 0 && !shouldStopExecution) {
|
|
|
- await new Promise(resolve => setTimeout(resolve, 1000));
|
|
|
- countdownValue.value--;
|
|
|
+ // 使用更细粒度的时间间隔,确保能快速响应重置操作
|
|
|
+ const interval = 100; // 100毫秒检查一次
|
|
|
+ const totalIterations = seconds * 10; // 总迭代次数
|
|
|
+
|
|
|
+ for (let i = 0; i < totalIterations; i++) {
|
|
|
+ // 检查是否应该停止执行
|
|
|
+ if (shouldStopExecution) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待一小段时间
|
|
|
+ await new Promise(resolve => setTimeout(resolve, interval));
|
|
|
+
|
|
|
+ // 检查是否应该停止执行
|
|
|
+ if (shouldStopExecution) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 每10次迭代(即1秒)减少倒计时值
|
|
|
+ if ((i + 1) % 10 === 0) {
|
|
|
+ countdownValue.value--;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 隐藏倒计时
|
|
|
@@ -1565,23 +1582,38 @@ window.isFinish = async function() {
|
|
|
|
|
|
if (gameState.player.position.x === endPoint.value.x && gameState.player.position.y === endPoint.value.y) {
|
|
|
|
|
|
- //检查是否有未完成的任务点
|
|
|
- const pointIndex = gameState.mapData.walkablePoints.findIndex(
|
|
|
- p => p.type === BLOCKLY_MAP_TYPE_DICT.TASK && p.status !== true
|
|
|
- );
|
|
|
- if (pointIndex !== -1) {
|
|
|
- showGameMessage(CONFIG.TIPS.UNFINISHED, 'error');
|
|
|
+ // 统计所有类型为TASK的任务点总数
|
|
|
+ const totalTasks = gameState.mapData.walkablePoints.filter(
|
|
|
+ p => p.type === BLOCKLY_MAP_TYPE_DICT.TASK
|
|
|
+ ).length;
|
|
|
+
|
|
|
+ // 统计其中status为true的完成任务数
|
|
|
+ const completedTasks = gameState.mapData.walkablePoints.filter(
|
|
|
+ p => p.type === BLOCKLY_MAP_TYPE_DICT.TASK && p.status === true
|
|
|
+ ).length;
|
|
|
+
|
|
|
+ //blockly总星星数量
|
|
|
+ const starCount = 3;
|
|
|
+
|
|
|
+ //无任务情况下直接完成
|
|
|
+ if (totalTasks === 0 || completedTasks === totalTasks) {
|
|
|
+ gameState.player.hasReachedEnd = true;
|
|
|
+ emits('saveProgress', starCount * 100)
|
|
|
+ showGameMessage(CONFIG.TIPS.FINISH, 'success' );
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- gameState.player.hasReachedEnd = true;
|
|
|
- showGameMessage(CONFIG.TIPS.FINISH, 'success' );
|
|
|
- emits('saveProgress')
|
|
|
+ //任务失败
|
|
|
+ // 计算完成百分比
|
|
|
+ const completionPercentage = totalTasks > 0 ? Math.round(completedTasks / totalTasks * starCount) : starCount;
|
|
|
+ showGameMessage(CONFIG.TIPS.UNFINISHED, 'error');
|
|
|
+ emits('saveProgress', completionPercentage * 100)
|
|
|
}
|
|
|
};
|
|
|
|
|
|
// 运行代码
|
|
|
const runCode = async () => {
|
|
|
+ await resetPlayer();
|
|
|
isRunning.value = true;
|
|
|
try {
|
|
|
await new Promise(resolve => setTimeout(resolve, CONFIG.DELAY.RESET_DELAY));
|
|
|
@@ -1651,6 +1683,8 @@ const runCode = async () => {
|
|
|
} catch (error) {
|
|
|
showGameMessage(`运行时错误: ${error.message || '未知错误'}`, 'error');
|
|
|
console.error('运行时错误:', error);
|
|
|
+ } finally {
|
|
|
+ isRunning.value = false;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -1660,6 +1694,10 @@ const resetPlayer = async () => {
|
|
|
// 设置标志强制停止所有执行
|
|
|
shouldStopExecution = true;
|
|
|
|
|
|
+ // 清除倒计时显示和重置倒计时值
|
|
|
+ showCountdown.value = false;
|
|
|
+ countdownValue.value = 0;
|
|
|
+
|
|
|
// 取消任何正在执行的代码
|
|
|
if (executionAbortController) {
|
|
|
executionAbortController.abort();
|