@@ -6,7 +6,6 @@ import { SoundObject } from '../resource/soundObject'
66import { sleep } from '../utils/waitUtil'
77import { getDefaultDialogTemplate } from '../utils/fallbackTemplate'
88import { generateStore , Store } from '../utils/store'
9- import { logError } from '../utils/logger'
109
1110// eslint-disable-next-line @typescript-eslint/no-var-requires
1211let engineConfig : any = require ( '../../engineConfig.json' )
@@ -84,53 +83,45 @@ export class Core {
8483 }
8584
8685 async start ( initScene : string ) : Promise < void > {
87- try {
88- // TODO: ブラウザ用のビルドの場合は、最初にクリックしてもらう
89- // titleタグの内容を書き換える
90- document . title = engineConfig . title
91- // sceneファイルを読み込む
92- await this . loadScene ( initScene || 'title' )
93- // 画面を表示する
94- await this . loadScreen ( this . sceneConfig )
95- // 入力イベントを設定する
96- document . querySelector ( '#gameContainer' ) ! . addEventListener ( 'keydown' , ( e : Event ) => {
97- const ke = e as KeyboardEvent
98- if ( ke . key === 'Enter' ) {
99- if ( this . onNextHandler ) this . onNextHandler ( )
100- } else if ( ke . key === 'Control' ) {
101- this . drawer . isSkip = true
102- this . isNext = true
103- }
104- } )
105- document . querySelector ( '#gameContainer' ) ! . addEventListener ( 'keyup' , ( e : Event ) => {
106- const ke = e as KeyboardEvent
107- if ( ke . key === 'Control' ) {
108- this . drawer . isSkip = true
109- this . isNext = false
110- }
111- } )
112- document . querySelector ( '#gameContainer' ) ! . addEventListener ( 'click' , ( ) => {
86+ // TODO: ブラウザ用のビルドの場合は、最初にクリックしてもらう
87+ // titleタグの内容を書き換える
88+ document . title = engineConfig . title
89+ // sceneファイルを読み込む
90+ await this . loadScene ( initScene || 'title' )
91+ // 画面を表示する
92+ await this . loadScreen ( this . sceneConfig )
93+ // 入力イベントを設定する
94+ document . querySelector ( '#gameContainer' ) ! . addEventListener ( 'keydown' , ( e : Event ) => {
95+ const ke = e as KeyboardEvent
96+ if ( ke . key === 'Enter' ) {
11397 if ( this . onNextHandler ) this . onNextHandler ( )
114- } )
115-
116- await this . textHandler ( 'タップでスタート' )
117- // BGMを再生する
118- await this . soundHandler ( {
119- mode : 'bgm' ,
120- src : this . sceneConfig . bgm ,
121- loop : true ,
122- play : true ,
123- } )
124- // シナリオを実行する
125- while ( this . scenarioManager . hasNext ( ) ) {
126- await this . runScenario ( )
98+ } else if ( ke . key === 'Control' ) {
99+ this . drawer . isSkip = true
100+ this . isNext = true
127101 }
128- } catch ( error : any ) {
129- // エラーをログに記録(スタックトレース付き)
130- await logError ( error , 'Error in runScenario' )
131- // エラーをアラートで表示
132- alert ( `システムエラーが発生しました。\n詳細はコンソールで確認してください。:\n${ error . message } ` )
133- throw error
102+ } )
103+ document . querySelector ( '#gameContainer' ) ! . addEventListener ( 'keyup' , ( e : Event ) => {
104+ const ke = e as KeyboardEvent
105+ if ( ke . key === 'Control' ) {
106+ this . drawer . isSkip = true
107+ this . isNext = false
108+ }
109+ } )
110+ document . querySelector ( '#gameContainer' ) ! . addEventListener ( 'click' , ( ) => {
111+ if ( this . onNextHandler ) this . onNextHandler ( )
112+ } )
113+
114+ await this . textHandler ( 'タップでスタート' )
115+ // BGMを再生する
116+ this . soundHandler ( {
117+ mode : 'bgm' ,
118+ src : this . sceneConfig . bgm ,
119+ loop : true ,
120+ play : true ,
121+ } )
122+ // シナリオを実行する
123+ while ( this . scenarioManager . hasNext ( ) ) {
124+ await this . runScenario ( )
134125 }
135126 }
136127
@@ -207,25 +198,12 @@ export class Core {
207198 }
208199 this . gameContainer . innerHTML = mainDiv . innerHTML
209200 this . drawer . setScreen ( this . gameContainer , engineConfig . resolution )
210- // Styleタグを取り出して、headタグに追加する
211- const styleElement = doc . head . getElementsByTagName ( 'style' ) [ 0 ]
212- if ( styleElement ) {
213- document . head . appendChild ( styleElement )
214- }
215201 } else {
216- // ダイアログの場合、古いダイアログ用のスタイルを削除する
217- const oldDialogStyles = document . head . querySelectorAll ( 'style[data-dialog-style]' )
218- oldDialogStyles . forEach ( ( styleTag ) => {
219- document . head . removeChild ( styleTag )
220- } )
221202 this . gameContainer . appendChild ( mainDiv )
222- // ダイアログ用のStyleタグを取り出して、マークを付けてheadタグに追加する
223- const styleElement = doc . head . getElementsByTagName ( 'style' ) [ 0 ]
224- if ( styleElement ) {
225- styleElement . setAttribute ( 'data-dialog-style' , 'true' )
226- document . head . appendChild ( styleElement )
227- }
228203 }
204+ // Styleタグを取り出して、headタグに追加する
205+ const styleElement = doc . head . getElementsByTagName ( 'style' ) [ 0 ]
206+ document . head . appendChild ( styleElement )
229207
230208 if ( ! skipBackground ) {
231209 console . info ( `background: ${ await this . checkResourceExists ( sceneConfig . background ) } ` )
@@ -248,7 +226,6 @@ export class Core {
248226 }
249227
250228 async runScenario ( ) : Promise < void > {
251-
252229 let scenarioObject = this . scenarioManager . next ( )
253230 if ( ! scenarioObject ) {
254231 return
@@ -384,7 +361,6 @@ export class Core {
384361 }
385362 this . scenarioManager . setHistory ( { line, selectId } as any )
386363 ; ( document . querySelector ( '#interactiveView' ) as HTMLElement ) . style . visibility = 'hidden'
387- this . isNext = false
388364 }
389365
390366 jumpHandler ( line : any ) : void {
@@ -401,10 +377,8 @@ export class Core {
401377 const scenarioList = scenario . slice ( line . index , this . scenarioManager . getIndex ( ) )
402378 // sub=falseの行だけを取得
403379 const subFalseScenario = scenarioList . filter ( ( item : any ) => ! item . sub )
404- // after に残っている sub=true の要素を除去(前回の選択肢の残骸を除去する)
405- const filteredAfter = noEditScenarioList . after . filter ( ( item : any ) => ! item . sub )
406380 // scenarioManagerに追加
407- this . scenarioManager . setScenario ( [ ...noEditScenarioList . before , ...subFalseScenario , ...filteredAfter ] )
381+ this . scenarioManager . setScenario ( [ ...noEditScenarioList . before , ...subFalseScenario , ...noEditScenarioList . after ] )
408382 }
409383 this . newpageHandler ( )
410384 this . scenarioManager . setIndex ( Number ( line . index ) )
@@ -518,7 +492,12 @@ export class Core {
518492
519493 // ファイルの存在確認
520494 if ( ! ( await this . checkResourceExists ( line . src ) ) ) {
521- throw new Error ( `Image file not found: ${ line . src } ` )
495+ console . error ( `Image file not found: ${ line . src } ` )
496+
497+ // エラーメッセージを表示
498+ await this . textHandler ( `エラー: 画像ファイルが見つかりません: ${ line . src } ` )
499+ // 空の画像オブジェクトを返す
500+ return new ImageObject ( )
522501 }
523502
524503 // 既にインスタンスがある場合は、それを使う
@@ -568,7 +547,12 @@ export class Core {
568547 // ファイルの存在確認
569548 if ( line . src ) {
570549 if ( ! ( await this . checkResourceExists ( line . src ) ) ) {
571- throw new Error ( `Sound file not found: ${ line . src } ` )
550+ console . error ( `Sound file not found: ${ line . src } ` )
551+
552+ // エラーメッセージを表示
553+ await this . textHandler ( `エラー: 音声ファイルが見つかりません: ${ line . src } ` )
554+ // 空のサウンドオブジェクトを返す
555+ return new SoundObject ( )
572556 }
573557 }
574558
@@ -779,8 +763,8 @@ export class Core {
779763 const context = { ...this . sceneFile }
780764 const func = new Function ( ...Object . keys ( context ) , code )
781765 return func . apply ( null , Object . values ( context ) )
782- } catch ( error : any ) {
783- throw new Error ( ` Error executing code: ${ error . message } ` )
766+ } catch ( error ) {
767+ console . error ( ' Error executing code:' , error )
784768 }
785769 }
786770
@@ -913,62 +897,75 @@ export class Core {
913897
914898 const saveDataRaw = ( this . store as any ) . get ? ( this . store as any ) . get ( `save_${ slot } ` ) : this . store [ `save_${ slot } ` ]
915899 if ( ! saveDataRaw ) {
916- throw new Error ( `セーブデータが見つかりません: スロット${ slot } ` )
900+ const errorMsg = `セーブデータが見つかりません: スロット${ slot } `
901+
902+ if ( line . message !== false ) {
903+ await this . textHandler ( errorMsg )
904+ }
905+ return
917906 }
918907
919908 // ディープコピーで循環参照を回避
920909 const saveData = JSON . parse ( JSON . stringify ( saveDataRaw ) )
921910
922- const sceneName = saveData . scenarioManager . sceneName || saveData . sceneConfig . name
923- if ( ! sceneName ) {
924- throw new Error ( 'Scene name not found in save data' )
925- }
911+ try {
912+ const sceneName = saveData . scenarioManager . sceneName || saveData . sceneConfig . name
913+ if ( ! sceneName ) {
914+ throw new Error ( 'Scene name not found in save data' )
915+ }
926916
927- // シーンとプログレスを復元
928- await this . loadScene ( sceneName )
929- await this . loadScreen ( saveData . sceneConfig , { skipBackground : true , skipBgm : true } )
917+ // シーンとプログレスを復元
918+ await this . loadScene ( sceneName )
919+ await this . loadScreen ( saveData . sceneConfig , { skipBackground : true } )
930920
931- // 読んだところまで復元
932- this . scenarioManager . setSceneName ( saveData . scenarioManager . sceneName )
933- this . scenarioManager . setIndex ( saveData . scenarioManager . currentIndex )
934- this . scenarioManager . setHistory ( saveData . scenarioManager . history || [ ] )
935- ; ( this . scenarioManager as any ) . progress = { ...( this . scenarioManager as any ) . progress , ...saveData . scenarioManager . progress }
921+ // 読んだところまで復元
922+ this . scenarioManager . setSceneName ( saveData . scenarioManager . sceneName )
923+ this . scenarioManager . setIndex ( saveData . scenarioManager . currentIndex )
924+ this . scenarioManager . setHistory ( saveData . scenarioManager . history || [ ] )
925+ ; ( this . scenarioManager as any ) . progress = { ...( this . scenarioManager as any ) . progress , ...saveData . scenarioManager . progress }
936926
937- // 画面の復元
938- this . displayedImages = { }
939- if ( saveData . backgroundImage ) {
940- const background = await new ImageObject ( ) . setImageAsync ( saveData . backgroundImage )
941- this . displayedImages [ 'background' ] = {
942- image : background ,
943- size : {
944- width : this . gameContainer . clientWidth ,
945- height : this . gameContainer . clientHeight ,
946- } ,
927+ // 画面の復元
928+ this . displayedImages = { }
929+ if ( saveData . backgroundImage ) {
930+ const background = await new ImageObject ( ) . setImageAsync ( saveData . backgroundImage )
931+ this . displayedImages [ 'background' ] = {
932+ image : background ,
933+ size : {
934+ width : this . gameContainer . clientWidth ,
935+ height : this . gameContainer . clientHeight ,
936+ } ,
937+ }
947938 }
948- }
949939
950- for ( const [ key , imageData ] of Object . entries ( saveData . displayedImages ) as [ string , any ] [ ] ) {
951- if ( imageData . src ) {
952- const image = await new ImageObject ( ) . setImageAsync ( imageData . src )
953- this . displayedImages [ key ] = {
954- image : image ,
955- pos : imageData . pos ,
956- size : imageData . size ,
957- look : imageData . look ,
958- entry : imageData . entry ,
940+ for ( const [ key , imageData ] of Object . entries ( saveData . displayedImages ) as [ string , any ] [ ] ) {
941+ if ( imageData . src ) {
942+ const image = await new ImageObject ( ) . setImageAsync ( imageData . src )
943+ this . displayedImages [ key ] = {
944+ image : image ,
945+ pos : imageData . pos ,
946+ size : imageData . size ,
947+ look : imageData . look ,
948+ entry : imageData . entry ,
949+ }
959950 }
960951 }
961- }
962952
963- // BGMの復元
964- if ( saveData . bgmSrc ) {
965- this . soundHandler ( { mode : 'bgm' , src : saveData . bgmSrc , loop : true , play : true } )
966- }
953+ // BGMの復元
954+ if ( saveData . bgmSrc ) {
955+ this . soundHandler ( { mode : 'bgm' , src : saveData . bgmSrc , loop : true , play : true } )
956+ }
967957
968- this . drawer . show ( this . displayedImages )
958+ this . drawer . show ( this . displayedImages )
969959
970- if ( line . message !== false ) {
971- await this . textHandler ( `ゲームをロードしました: ${ saveData . name } ` )
960+ if ( line . message !== false ) {
961+ await this . textHandler ( `ゲームをロードしました: ${ saveData . name } ` )
962+ }
963+ } catch ( error : any ) {
964+ const errorMsg = `ロードに失敗しました: ${ error . message } `
965+
966+ if ( line . message !== false ) {
967+ await this . textHandler ( errorMsg )
968+ }
972969 }
973970 }
974971
0 commit comments