Skip to content

Commit

Permalink
完成场景与编辑器同步的引擎部分
Browse files Browse the repository at this point in the history
  • Loading branch information
MakinoharaShoko committed Apr 22, 2022
1 parent 0a8fc81 commit e5abab0
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/Core/controller/gamePlay/nextSentence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const nextSentence = () => {
}
}

scriptExecutor();
scriptExecutor(-1);
return;
}

Expand Down
31 changes: 20 additions & 11 deletions src/Core/controller/gamePlay/scriptExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { commandType, ISentence } from '../../interface/coreInterface/sceneInterface';
import { runtime_currentBacklog } from '../../runtime/backlog';
import { getRef } from '../../store/storeRef';
import { runtime_currentSceneData } from '../../runtime/sceneData';
import { runScript } from './runScript';
import { logger } from '../../util/logger';
import { IStageState } from '../../interface/stateInterface/stageInterface';
import {commandType, ISentence} from '../../interface/coreInterface/sceneInterface';
import {runtime_currentBacklog} from '../../runtime/backlog';
import {getRef} from '../../store/storeRef';
import {runtime_currentSceneData} from '../../runtime/sceneData';
import {runScript} from './runScript';
import {logger} from '../../util/logger';
import {IStageState} from '../../interface/stateInterface/stageInterface';
import * as _ from 'lodash';
import { restoreScene } from '../scene/restoreScene';
import { IBacklogItem, sceneEntry } from '../../interface/coreInterface/runtimeInterface';
import {restoreScene} from '../scene/restoreScene';
import {IBacklogItem, sceneEntry} from '../../interface/coreInterface/runtimeInterface';

/**
* 语句执行器
* 执行语句,同步场景状态,并根据情况立即执行下一句或者加入backlog
* @param runToSentence 执行至 sentenceID = runToSentence, 小于0则为正常模式。
*/
export const scriptExecutor = () => {
export const scriptExecutor = (runToSentence: number) => {
// 超过总语句数量,则从场景栈拿出一个需要继续的场景,然后继续流程。若场景栈清空,则停止流程
if (runtime_currentSceneData.currentSentenceId > runtime_currentSceneData.currentScene.sentenceList.length - 1) {
if (runtime_currentSceneData.sceneStack.length !== 0) {
Expand All @@ -38,10 +39,18 @@ export const scriptExecutor = () => {

const isSaveBacklog = currentScript.command === commandType.say; // 是否在本句保存backlog(一般遇到对话保存)
let currentStageState: IStageState;

// 执行至指定 sentenceID
if (runToSentence >= 0 && runtime_currentSceneData.currentSentenceId < runToSentence) {
runtime_currentSceneData.currentSentenceId++;
scriptExecutor(runToSentence);
return;
}

// 执行“下一句”
if (isNext) {
runtime_currentSceneData.currentSentenceId++;
scriptExecutor();
scriptExecutor(-1);
return;
}

Expand Down
55 changes: 8 additions & 47 deletions src/Core/controller/gamePlay/scripts/end.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,18 @@
import { ISentence } from '../../../interface/coreInterface/sceneInterface';
import { IPerform } from '../../../interface/coreInterface/performInterface';
import {runtime_currentBacklog} from "../../../../Core/runtime/backlog";
import {initSceneData, runtime_currentSceneData} from "@/Core/runtime/sceneData";
import {runtime_gamePlay} from "../../../../Core/runtime/gamePlay";
import * as _ from "lodash";
import {initState} from "../../../../Core/store/stage";
import {getRef} from "../../../../Core/store/storeRef";
import {IStageState} from "../../../../Core/interface/stateInterface/stageInterface";
import {ISentence} from '@/Core/interface/coreInterface/sceneInterface';
import {IPerform} from '@/Core/interface/coreInterface/performInterface';
import {runtime_currentSceneData} from "@/Core/runtime/sceneData";
import {getRef} from "@/Core/store/storeRef";
import {assetSetter, fileType} from "@/Core/util/assetSetter";
import {sceneFetcher} from "@/Core/util/sceneFetcher";
import {sceneParser} from "@/Core/parser/sceneParser";
import {resetStage} from "@/Core/util/resetStage";

/**
* 结束游戏
* @param sentence
*/
export const end = (sentence: ISentence): IPerform => {
/*
清空运行时
*/
runtime_currentBacklog.splice(0, runtime_currentBacklog.length); // 清空backlog
// 清空sceneData,并重新获取
runtime_currentSceneData.currentSentenceId = 0;
runtime_currentSceneData.sceneStack = [];
runtime_currentSceneData.currentScene = initSceneData.currentScene;

// 清空所有演出和timeOut
for (const e of runtime_gamePlay.performList) {
e.stopFunction();
}
runtime_gamePlay.performList = [];
for (const e of runtime_gamePlay.timeoutList) {
clearTimeout(e);
}
runtime_gamePlay.timeoutList = [];
runtime_gamePlay.isAuto = false;
runtime_gamePlay.isFast = false;
const autoInterval = runtime_gamePlay.autoInterval;
if (autoInterval !== null) clearInterval(autoInterval);
runtime_gamePlay.autoInterval = null;
const fastInterval = runtime_gamePlay.fastInterval;
if (fastInterval !== null) clearInterval(fastInterval);
runtime_gamePlay.fastInterval = null;
const autoTimeout = runtime_gamePlay.autoTimeout;
if (autoTimeout !== null) clearInterval(autoTimeout);
runtime_gamePlay.autoTimeout = null;

// 清空舞台状态表
const initSceneDataCopy = _.cloneDeep(initState);
for (const k in initSceneDataCopy) {
if (initSceneDataCopy.hasOwnProperty(k)) {
getRef('stageRef').setStage(k as keyof IStageState, initSceneDataCopy[k as keyof IStageState]);
}
}
resetStage(true);

// 重新获取初始场景
const sceneUrl: string = assetSetter('start.txt', fileType.scene);
Expand All @@ -66,7 +26,8 @@ export const end = (sentence: ISentence): IPerform => {
duration: 0,
isOver: false,
isHoldOn: false,
stopFunction: () => {},
stopFunction: () => {
},
blockingNext: () => false,
blockingAuto: () => true,
stopTimeout: undefined, // 暂时不用,后面会交给自动清除
Expand Down
60 changes: 10 additions & 50 deletions src/Core/controller/gamePlay/startGame.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,24 @@
import { runtime_currentBacklog } from '../../runtime/backlog';
import { initSceneData, runtime_currentSceneData } from '../../runtime/sceneData';
import { runtime_gamePlay } from '../../runtime/gamePlay';
import { getRef } from '../../store/storeRef';
import { assetSetter, fileType } from '../../util/assetSetter';
import { sceneFetcher } from '../../util/sceneFetcher';
import { sceneParser } from '../../parser/sceneParser';
import { nextSentence } from './nextSentence';
import * as _ from 'lodash';
import { initState } from '../../store/stage';
import { IStageState } from '../../interface/stateInterface/stageInterface';
import {runtime_currentSceneData} from '../../runtime/sceneData';
import {getRef} from '../../store/storeRef';
import {assetSetter, fileType} from '../../util/assetSetter';
import {sceneFetcher} from '../../util/sceneFetcher';
import {sceneParser} from '../../parser/sceneParser';
import {eventSender} from "@/Core/controller/eventBus/eventSender";
import {resetStage} from "@/Core/util/resetStage";

/**
* 从头开始游戏
*/
export const startGame = () => {
/*
清空运行时
*/
runtime_currentBacklog.splice(0, runtime_currentBacklog.length); // 清空backlog
// 清空sceneData,并重新获取
runtime_currentSceneData.currentSentenceId = 0;
runtime_currentSceneData.sceneStack = [];
runtime_currentSceneData.currentScene = initSceneData.currentScene;

// 清空所有演出和timeOut
for (const e of runtime_gamePlay.performList) {
e.stopFunction();
}
runtime_gamePlay.performList = [];
for (const e of runtime_gamePlay.timeoutList) {
clearTimeout(e);
}
runtime_gamePlay.timeoutList = [];
runtime_gamePlay.isAuto = false;
runtime_gamePlay.isFast = false;
const autoInterval = runtime_gamePlay.autoInterval;
if (autoInterval !== null) clearInterval(autoInterval);
runtime_gamePlay.autoInterval = null;
const fastInterval = runtime_gamePlay.fastInterval;
if (fastInterval !== null) clearInterval(fastInterval);
runtime_gamePlay.fastInterval = null;
const autoTimeout = runtime_gamePlay.autoTimeout;
if (autoTimeout !== null) clearInterval(autoTimeout);
runtime_gamePlay.autoTimeout = null;

// 清空舞台状态表
const initSceneDataCopy = _.cloneDeep(initState);
for (const k in initSceneDataCopy) {
if (initSceneDataCopy.hasOwnProperty(k)) {
getRef('stageRef').setStage(k as keyof IStageState, initSceneDataCopy[k as keyof IStageState]);
}
}
resetStage(true);

// 重新获取初始场景
const sceneUrl: string = assetSetter('start.txt', fileType.scene);
// 场景写入到运行时
sceneFetcher(sceneUrl).then((rawScene) => {
runtime_currentSceneData.currentScene = sceneParser(rawScene, 'start.txt', sceneUrl);
// 开始第一条语句
eventSender('nextSentence_target', 0, 0);
});
getRef('GuiRef').setVisibility('showTitle', false);
// 开始第一条语句
nextSentence();
};
9 changes: 8 additions & 1 deletion src/Core/initializeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {assetSetter, fileType} from './util/assetSetter';
import {sceneFetcher} from './util/sceneFetcher';
import {runtime_currentSceneData} from './runtime/sceneData';
import {sceneParser} from './parser/sceneParser';
import {getRef} from "@/Core/store/storeRef";
import {setVolume} from "../Core/util/setVolume";
import {pixiController} from "../Core/controller/perform/pixi/pixiController";
import {bindExtraFunc} from "@/Core/util/bindExtraFunc";
import {webSocketFunc} from "@/Core/util/webSocketFunc";

/**
* 引擎初始化函数
Expand Down Expand Up @@ -51,4 +52,10 @@ export const initializeScript = (): void => {
* 启动Pixi
*/
pixiController(true);

/**
* 绑定工具函数
*/
bindExtraFunc();
webSocketFunc();
};
5 changes: 5 additions & 0 deletions src/Core/util/bindExtraFunc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {scriptExecutor} from "@/Core/controller/gamePlay/scriptExecutor";

export const bindExtraFunc = () => {
(window as any).jmpToSentence = scriptExecutor;
};
49 changes: 49 additions & 0 deletions src/Core/util/resetStage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {runtime_currentBacklog} from "@/Core/runtime/backlog";
import {initSceneData, runtime_currentSceneData} from "@/Core/runtime/sceneData";
import {runtime_gamePlay} from "@/Core/runtime/gamePlay";
import * as _ from "lodash";
import {initState} from "@/Core/store/stage";
import {getRef} from "@/Core/store/storeRef";
import {IStageState} from "@/Core/interface/stateInterface/stageInterface";

export const resetStage = (resetBacklog: boolean) => {
/**
* 清空运行时
*/
if (resetBacklog) {
runtime_currentBacklog.splice(0, runtime_currentBacklog.length); // 清空backlog
}
// 清空sceneData,并重新获取
runtime_currentSceneData.currentSentenceId = 0;
runtime_currentSceneData.sceneStack = [];
runtime_currentSceneData.currentScene = initSceneData.currentScene;

// 清空所有演出和timeOut
for (const e of runtime_gamePlay.performList) {
e.stopFunction();
}
runtime_gamePlay.performList = [];
for (const e of runtime_gamePlay.timeoutList) {
clearTimeout(e);
}
runtime_gamePlay.timeoutList = [];
runtime_gamePlay.isAuto = false;
runtime_gamePlay.isFast = false;
const autoInterval = runtime_gamePlay.autoInterval;
if (autoInterval !== null) clearInterval(autoInterval);
runtime_gamePlay.autoInterval = null;
const fastInterval = runtime_gamePlay.fastInterval;
if (fastInterval !== null) clearInterval(fastInterval);
runtime_gamePlay.fastInterval = null;
const autoTimeout = runtime_gamePlay.autoTimeout;
if (autoTimeout !== null) clearInterval(autoTimeout);
runtime_gamePlay.autoTimeout = null;

// 清空舞台状态表
const initSceneDataCopy = _.cloneDeep(initState);
for (const k in initSceneDataCopy) {
if (initSceneDataCopy.hasOwnProperty(k)) {
getRef('stageRef').setStage(k as keyof IStageState, initSceneDataCopy[k as keyof IStageState]);
}
}
};
27 changes: 27 additions & 0 deletions src/Core/util/syncWithOrigine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {getRef} from "@/Core/store/storeRef";
import {resetStage} from "@/Core/util/resetStage";
import {assetSetter, fileType} from "@/Core/util/assetSetter";
import {sceneFetcher} from "@/Core/util/sceneFetcher";
import {runtime_currentSceneData} from "@/Core/runtime/sceneData";
import {sceneParser} from "@/Core/parser/sceneParser";
import {scriptExecutor} from "@/Core/controller/gamePlay/scriptExecutor";
import {logger} from "./logger";

export const syncWithOrigine = (str: string) => {
const strLst = str.split(' ');
const scene = strLst[1].replace(/json/g, 'txt');
const sentenceID = parseInt(strLst[2], 10);
logger.warn('正在跳转到' + scene + ':' + sentenceID);
const guiRef = getRef('GuiRef');
guiRef.setVisibility('showTitle', false);
guiRef.setVisibility('showMenuPanel', false);
resetStage(true);
// 重新获取初始场景
const sceneUrl: string = assetSetter(scene, fileType.scene);
// 场景写入到运行时
sceneFetcher(sceneUrl).then((rawScene) => {
runtime_currentSceneData.currentScene = sceneParser(rawScene, 'start.txt', sceneUrl);
// 开始快进到指定语句
scriptExecutor(sentenceID);
});
};
24 changes: 24 additions & 0 deletions src/Core/util/webSocketFunc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {logger} from "./logger";
import {syncWithOrigine} from "@/Core/util/syncWithOrigine";

export const webSocketFunc = () => {
try {
const loc: string = window.location.hostname;
const wsUrl = `ws://${loc}:9999`;
logger.info('正在启动socket连接位于:' + wsUrl);
const socket = new WebSocket(wsUrl);
socket.onopen = () => {
logger.info('socket已连接');
socket.send(' WebGAL 已和 Terre 建立连接。');
};
socket.onmessage = e => {
logger.info('收到信息', e.data);
const str: string = e.data;
if (str.match('jmp')) {
syncWithOrigine(str);
}
};
} catch (e) {
logger.warn('ws连接失败');
}
};

0 comments on commit e5abab0

Please sign in to comment.