|
|
@@ -139,35 +139,43 @@ import { javascriptGenerator } from "blockly/javascript";
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
-// 整合游戏状态
|
|
|
+// 创建游戏状态的响应式对象
|
|
|
const gameState = reactive({
|
|
|
- // 地图配置
|
|
|
+ // 地图配置信息
|
|
|
mapConfig: {
|
|
|
+ // 地图背景图片路径
|
|
|
background: '@/assets/images/room.png',
|
|
|
+ // 每个瓦片的尺寸(像素)
|
|
|
tileSize: 50,
|
|
|
- width: 10,
|
|
|
- height: 8
|
|
|
},
|
|
|
|
|
|
- // 玩家状态
|
|
|
+ // 玩家相关状态
|
|
|
player: {
|
|
|
+ // 玩家当前位置坐标
|
|
|
position: { x: 1, y: 1 },
|
|
|
- direction: 1, // 0=上, 1=右, 2=下, 3=左
|
|
|
+ // 玩家当前朝向:0=上, 1=右, 2=下, 3=左
|
|
|
+ direction: 1,
|
|
|
+ // 是否正在发生碰撞
|
|
|
isColliding: false,
|
|
|
+ // 是否已到达终点
|
|
|
hasReachedEnd: false,
|
|
|
- collisionHappened: false // 标记是否已发生碰撞
|
|
|
},
|
|
|
|
|
|
- // 游戏状态
|
|
|
+ // 游戏状态信息
|
|
|
status: {
|
|
|
+ // 当前显示的游戏消息
|
|
|
message: '',
|
|
|
+ // 消息类型(如success、error、info等)
|
|
|
messageType: ''
|
|
|
},
|
|
|
|
|
|
- // 地图数据
|
|
|
+ // 地图数据信息
|
|
|
mapData: {
|
|
|
+ // 游戏起点位置
|
|
|
startPoint: { x: 1, y: 1 },
|
|
|
- endPoint: { x: 2, y: 1 },
|
|
|
+ // 游戏终点位置
|
|
|
+ endPoint: { x: 3, y: 1 },
|
|
|
+ // 地图上所有可行走的点坐标集合
|
|
|
walkablePoints: [
|
|
|
{ x: 1, y: 1 }, { x: 2, y: 1 }, { x: 3, y: 1 }, { x: 4, y: 1 }, { x: 5, y: 1 },
|
|
|
{ x: 1, y: 2 }, { x: 5, y: 2 },
|
|
|
@@ -464,7 +472,7 @@ function getDirectionText() {
|
|
|
// 创建通用的移动函数
|
|
|
async function move(direction) {
|
|
|
// direction: 1 表示向前,-1 表示向后
|
|
|
- if (isColliding.value || gameState.player.collisionHappened) {
|
|
|
+ if (isColliding.value) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -508,7 +516,6 @@ async function move(direction) {
|
|
|
} else {
|
|
|
// 发生碰撞
|
|
|
gameState.player.isColliding = true;
|
|
|
- gameState.player.collisionHappened = true;
|
|
|
showGameMessage('哎呀,撞到墙了!', 'error');
|
|
|
output.value += '碰撞!不能移动到这个位置';
|
|
|
|
|
|
@@ -539,7 +546,7 @@ window.moveBackward = async function() {
|
|
|
|
|
|
window.turnLeft = async function() {
|
|
|
// 如果已经发生过碰撞,不再执行任何旋转
|
|
|
- if (isColliding.value || gameState.player.collisionHappened) {
|
|
|
+ if (isColliding.value) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -553,7 +560,7 @@ window.turnLeft = async function() {
|
|
|
|
|
|
window.turnRight = async function() {
|
|
|
// 如果已经发生过碰撞,不再执行任何旋转
|
|
|
- if (isColliding.value || gameState.player.collisionHappened) {
|
|
|
+ if (isColliding.value) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -567,7 +574,7 @@ window.turnRight = async function() {
|
|
|
|
|
|
window.turnAround = async function() {
|
|
|
// 如果已经发生过碰撞,不再执行任何旋转
|
|
|
- if (isColliding.value || gameState.player.collisionHappened) {
|
|
|
+ if (isColliding.value) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -586,20 +593,13 @@ let executionAbortController = null;
|
|
|
// 运行代码
|
|
|
const runCode = async () => {
|
|
|
try {
|
|
|
- // 取消任何正在执行的代码
|
|
|
- if (executionAbortController) {
|
|
|
- executionAbortController.abort();
|
|
|
- }
|
|
|
+
|
|
|
+ await resetPlayer();
|
|
|
|
|
|
// 创建新的AbortController用于取消执行
|
|
|
executionAbortController = new AbortController();
|
|
|
const signal = executionAbortController.signal;
|
|
|
|
|
|
- // 重置玩家状态
|
|
|
- gameState.player.isColliding = false;
|
|
|
- gameState.player.hasReachedEnd = false;
|
|
|
- gameState.player.collisionHappened = false; // 重置碰撞标志
|
|
|
-
|
|
|
// 初始化输出
|
|
|
output.value = '// 执行结果:\n';
|
|
|
|
|
|
@@ -649,17 +649,6 @@ const runCode = async () => {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- // 设置执行超时(例如10秒)
|
|
|
- await Promise.race([
|
|
|
- currentExecutionPromise,
|
|
|
- new Promise((_, reject) => setTimeout(() => reject(new Error('代码执行超时')), 10000))
|
|
|
- ]);
|
|
|
-
|
|
|
- // 检查是否到达终点
|
|
|
- if (!gameState.player.hasReachedEnd) {
|
|
|
- showGameMessage('程序执行完毕,但未到达终点', 'info');
|
|
|
- output.value += '程序执行完毕,但未到达终点\n';
|
|
|
- }
|
|
|
} catch (error) {
|
|
|
// 捕获并显示执行错误
|
|
|
if (error.message !== '执行已取消') {
|
|
|
@@ -692,7 +681,7 @@ const clearOutput = () => {
|
|
|
|
|
|
// 重置玩家位置和状态
|
|
|
const resetPlayer = () => {
|
|
|
-// 取消任何正在执行的代码
|
|
|
+ // 取消任何正在执行的代码
|
|
|
if (executionAbortController) {
|
|
|
executionAbortController.abort();
|
|
|
executionAbortController = null;
|
|
|
@@ -704,9 +693,8 @@ const resetPlayer = () => {
|
|
|
|
|
|
gameState.player.position = { ...startPoint.value };
|
|
|
gameState.player.direction = 1; // 重置为向上方向
|
|
|
- gameState.player.isColliding = false;
|
|
|
+ gameState.player.isColliding = false; //碰撞标志
|
|
|
gameState.player.hasReachedEnd = false;
|
|
|
- gameState.player.collisionHappened = false; // 重置碰撞标志
|
|
|
showGameMessage('玩家已重置到起点', 'info');
|
|
|
output.value += '玩家已重置到起点\n';
|
|
|
};
|