Skin:
[NORMAL]
[BLUE] [DOS] [LIGHT]  / コピーするための表示 / 実行
このファイル: /home/web6047/www/cgi-bin/prj/20200515-RPG/基本的/20200620-ウィンドウ入力/20200814-パレット2/simple6 - snapshot 20200923.html
1 <html><!--ESCAPEPROCESS-->
2 <head>
3 <meta content="text/html; charset=UTF-8" http-equiv="content-type">
4 <script>
5 console.clear();
6 onclick = location.reload.bind( location );
7 </script>
8 <script>
9 /*
10 なぜこのバージョンを作り始めたんですか?
11
12 図を基にして作成することにした。
13
14 */
15
16 function onloadx() {
17 app = new App();
18 app.start();
19 }
20
21 //---App
22
23 class App {
24
25 constructor() {
26
27 this.chars = new Array();
28 this.monsters = new Array();
29
30 this.scripts = {
31 kaihuku : function( args ) {
32 // this.debugPrint( args );
33 let charA = args.charA.selectedItem;
34 let charB = args.charB.selectedItem;
35 let kind = args.command.selectedItem.title;
36 let dougu = args.dougu.selectedItem;
37 let str;
38
39 str = "";
40 str += charA.title + " は ";
41 if( charA != charB )
42 str += charB.title + " に ";
43 str += dougu.title + " を つかった!";
44 console.log( str );
45
46 str = "";
47 str += charB.title + " の体力が ";
48 str += dougu.kouka + " ポイント かいふくした!";
49 console.log( str );
50
51 charA[ kind ].splice( args.dougu.selectedNum, 1 );
52 this.debugPrint( charA[ kind ] );
53
54 },
55 };
56 //script中のthisはそのままだとwindowなので、appにする。
57 for( let name in this.scripts ) {
58 this.scripts[ name ] = this.scripts[ name ].bind( this );
59 }
60
61 this.dougusA = [
62 {
63 type : "dougu",
64 title : "なんこう",
65 kouka : 20,
66 contextual : {
67 camp : {
68 type : "menu",
69 argName : "suru",
70 title : "どうする?",
71 items : [
72 {
73 type : "item",
74 title : "つかう",
75 subMenus : [
76 {
77 type : "menu",
78 argName : "charB",
79 title : "だれに?",
80 items : this.chars,
81 }
82 ],
83 script : this.scripts.kaihuku,
84 },
85 {
86 type : "item",
87 title : "わたす",
88 },
89 {
90 type : "item",
91 title : "すてる",
92 },
93 ],
94 },
95 },
96 },
97 {
98 type : "dougu",
99 title : "とくべつなひやく",
100 },
101 ]
102 //配列→オブジェクト
103 this.dougus = Object.fromEntries( this.dougusA.map( dougu => [ dougu.title, dougu ] ) );
104
105 this.jumonsA = [
106 {
107 type : "jumon",
108 title : "テストまほう1",
109 },
110 {
111 type : "jumon",
112 title : "テストまほう2",
113 },
114 {
115 type : "jumon",
116 title : "テストまほう3",
117 },
118 {
119 type : "jumon",
120 title : "テストまほう4",
121 },
122 ];
123 //配列→オブジェクト
124 this.jumons = Object.fromEntries( this.jumonsA.map( jumon => [ jumon.title, jumon ] ) );
125
126 //---this.batcoms
127
128 this.batcomsA = [
129 {
130 type : "batcom",
131 title : "たたかう",
132 subMenus : [
133 {
134 type : "menu",
135 argName : "monster",
136 title : "てき",
137 items : this.monsters,
138 },
139 ],
140 script : function( args ) {
141 // this.debugPrint( args );
142 let monster = args.monster.selectedItem;
143 let charA = args.charA.selectedItem;
144 let str;
145 str = "";
146 str += charA.title + " の こうげき!";
147 console.log( str );
148 str = "";
149 str += monster.title + " に 7 ポイント の ダメージ を あたえた!";
150 console.log( str );
151 }.bind( this ),
152 },
153 {
154 type : "batcom",
155 title : "にげる",
156 script : function( args ) {
157 // this.debugPrint( args );
158 let charA = args.charA.selectedItem;
159 let str;
160 str = "";
161 str += charA.title + " たちは にげだした!";
162 console.log( str );
163 str = "";
164 str += "しかし まわりこまれてしまった!";
165 console.log( str );
166 }
167 },
168 ];
169 //script中のthisはそのままだとwindowなので、appにする。
170 this.batcomsA.forEach( batcom => batcom.script = batcom.script.bind( this ) );
171 //配列→オブジェクト
172 this.batcoms = Object.fromEntries( this.batcomsA.map( batcom => [ batcom.title, batcom ] ) );
173
174 let char1, char2, char3;
175
176 char1 = {
177 type : "char",
178 title : "char1",
179 "どうぐ" : [
180 this.dougus[ "なんこう" ],
181 this.dougus[ "なんこう" ],
182 ],
183 }
184 char1.batcom = {
185 type : "menu",
186 context : "battle",
187 argName : "command",
188 title : char1.title,
189 items : [
190 this.batcoms[ "たたかう" ],
191 this.batcoms[ "にげる" ],
192 {
193 type : "item",
194 title : "どうぐ",
195 },
196 {
197 type : "item",
198 title : "ぼうぎょ",
199 },
200 ],
201 }//char1.batcom
202
203 char2 = {
204 type : "char",
205 title : "char2",
206 "どうぐ" : [
207 this.dougus[ "なんこう" ],
208 this.dougus[ "なんこう" ],
209 ],
210 "まほう" : [
211 this.jumons[ "テストまほう1" ],
212 this.jumons[ "テストまほう2" ],
213 ],
214 }
215 char2.batcom = {
216 type : "menu",
217 context : "battle",
218 argName : "command",
219 title : char2.title,
220 items : [
221 this.batcoms[ "たたかう" ],
222 {
223 type : "item",
224 title : "まほう",
225 },
226 {
227 type : "item",
228 title : "どうぐ",
229 },
230 {
231 type : "item",
232 title : "ぼうぎょ",
233 },
234 ],
235 }//char2.batcom
236
237 char3 = {
238 type : "char",
239 title : "char3",
240 "どうぐ" : [
241 this.dougus[ "なんこう" ],
242 this.dougus[ "なんこう" ],
243 ],
244 "まほう" : [
245 this.jumons[ "テストまほう3" ],
246 this.jumons[ "テストまほう4" ],
247 ],
248 }
249 char3.batcom = {
250 type : "menu",
251 context : "battle",
252 argName : "command",
253 title : char3.title,
254 items : [
255 this.batcoms[ "たたかう" ],
256 {
257 type : "item",
258 title : "まほう",
259 },
260 {
261 type : "item",
262 title : "どうぐ",
263 },
264 {
265 type : "item",
266 title : "ぼうぎょ",
267 },
268 ],
269 }//char3.batcom
270
271 this.chars.push( char1 );
272 this.chars.push( char2 );
273 this.chars.push( char3 );
274
275
276 this.camp = {
277 type : "menu",
278 context : "camp",
279 argName : "command",
280 title : "コマンド?",
281 items : [
282 {
283 type : "item",
284 title : "どうぐ",
285 subMenus : [
286 {
287 type : "menu",
288 argName : "charA",
289 title : "だれの?",
290 items : this.chars,
291 },
292 {
293 type : "menu",
294 argName : "dougu",
295 title : "",
296 itemsBy : true,
297 },
298 ],
299 },
300 {
301 type : "item",
302 title : "しらべる",
303 },
304 ],
305 }//this.camp
306
307 }//App.constructor
308
309 debugPrint() {
310 for( let i = 0; i < arguments.length; i++ ) {
311 let arg = arguments[ i ];
312 if( arg instanceof Array ) {
313 console.log( "debugPrint Array" );
314 for( let j = 0; j < arg.length; j++ ) {
315 console.log( "\t[ ", j, " ]", "=", arg[ j ] );
316 }
317 } else if( arg instanceof Object ) {
318 console.log( "debugPrint Object" );
319 for( let name in arg ) {
320 console.log( "\t", name, "\t:", arg[ name ] );
321 }
322 }
323 }
324 console.log( "ok." );
325 }
326
327 //---start()
328
329 async start() {
330 await this.testCamp();
331 // await this.testBattle();
332 console.log( "ok." );
333 }
334
335 //---testBattle()
336
337 async testBattle() {
338
339 //テストモンスター
340 this.monsters.length = 0;
341 this.monsters.push( {
342 type : "monster",
343 title : "モンスター1",
344 } );
345 this.monsters.push( {
346 type : "monster",
347 title : "モンスター2",
348 } );
349 this.monsters.push( {
350 type : "monster",
351 title : "モンスター3",
352 } );
353
354 let commands = new Array();
355
356 for( let i = 0; i < this.chars.length; i++ ) {
357 let char = this.chars[ i ];
358 let menus = new Menus( char.batcom );
359 let res = await menus.doit();
360 //check.
361 if( ! res ) {
362 i -= 2;
363 continue;
364 }
365 //check. 主語がないので追加しておく。
366 menus.scriptArgs.charA = {
367 selectedNum : i,
368 selectedItem : char,
369 }
370
371 commands.push( {
372 script : menus.script,
373 args : menus.scriptArgs,
374 } );
375 }
376
377 //debug.
378 for( let i = 0; i < commands.length; i++ ) {
379 let command = commands[ i ];
380 command.script( command.args );
381 }
382
383 }//testBattle()
384
385 //---testCamp
386
387 async testCamp() {
388 this.menus = new Menus( this.camp );
389
390 let res = await this.menus.doit();
391
392 //check. menusはキャンセルされた?
393 if( ! res ) return;
394
395 //debug.
396 console.log( "" );
397 console.log( "結果:" );
398 for( let name in this.menus.scriptArgs ) {
399 let arg = this.menus.scriptArgs[ name ];
400 console.log( name, ":", arg.selectedItem.title );
401 }
402 //check.
403 if( ! this.menus.script ) {
404 console.log( "script was not defined." );
405 }
406
407 this.menus.script( this.menus.scriptArgs );
408
409 }//testCamp
410
411 }//App
412
413 //---Menus
414
415 class Menus {
416
417 constructor( menusrc ) {
418 this.menusrc = menusrc;
419 this.array = new Array();
420 this.script = null;
421 this.scriptArgs = new Object();
422 this.context = "";
423 }//menus.constructor()
424
425 async doit() {
426 this.array.length = 0;
427 this.array.push( new Menu( this.menusrc ) );
428
429 //各メニューの入力を進める
430 for( let i = 0; i < this.array.length; i++ ) {
431 let menu = this.array[ i ];
432 //check. menuに設定されたコンテキストがあるならコピー
433 if( menu.context ) {
434 this.context = menu.context;
435 }
436 //check. itemsの動的な設定(暫定)
437 if( menu.itemsBy ) {
438 let dareno = this.scriptArgs.charA.selectedItem;
439 let naniwo = this.scriptArgs.command.selectedItem.title;
440 menu.items = dareno[ naniwo ];
441 }
442
443 //メニューの入力実行
444
445 console.log( "\n" );
446
447 let res = await menu.select();
448
449 //check. menuはキャンセルされた?
450 if( ! res ) {
451 //check. トップメニューがキャンセルされた?
452 if( i == 0 ) return false;
453
454 i -= 2;
455 continue;
456 }
457
458 //入力結果の取得
459
460 //入力結果をスクリプト引数として取得
461 this.scriptArgs[ menu.argName ] = menu.result;
462
463 let selectedItem = menu.result.selectedItem;
464
465 //スクリプト定義の取得
466 if( selectedItem.script ) {
467 this.script = selectedItem.script;
468 }
469 //サブメニュー定義の取得
470 if( selectedItem.subMenus ) {
471 for( let j = 0; j < selectedItem.subMenus.length; j++ ) {
472 this.array[ i + j + 1 ] = new Menu( selectedItem.subMenus[ j ] );
473 }
474 }
475 //サブメニュー定義(コンテクスチュアルメニュー)の取得
476 if( selectedItem.contextual && selectedItem.contextual[ this.context ] ) {
477 this.array[ i + 1 ] = new Menu( selectedItem.contextual[ this.context ] );
478 }
479
480 }//for this.array
481
482 return true;
483
484 }//menus.doit()
485 }//Menus
486
487 //---Menu
488
489 class Menu {
490
491 constructor( menusrc ) {
492
493 //menusrcの内容をmenuへコピー
494 for( let name in menusrc ) {
495 this[ name ] = menusrc[ name ];
496 }
497
498 this.result = null;
499 }
500
501 async select() {
502
503 //タイトル表示
504 console.log( "[", this.title, "]" );
505 //メニュー項目表示
506 for( let i = 0; i < this.items.length; i++ ) {
507 let item = this.items[ i ];
508 console.log( i, item.title );
509 }
510
511 //メニュー入力
512 let num;
513 let key;
514 do {
515 key = await this.inputKey();
516 //check. キャンセルキーが入力された?'x'
517 if( key == 88 ) return false;
518 num = key - 48;
519 } while( num < 0 || num >= this.items.length );
520 console.log( ">", num );
521
522 //結果をセット
523 this.result = {
524 selectedNum : num,
525 selectedItem : this.items[ num ],
526 }
527
528 return true;
529
530 }//menu.select()
531
532 inputKey() {
533 return new Promise(
534 function( tellOk ) {
535 window.onkeydown = function( e ) {
536 tellOk( e.which );
537 }
538 }
539 );
540 }
541 }//Menu
542
543
544 </script>
545 <style>
546 </style>
547 </head>
548 <body onload="onloadx()">
549 </body>
550 </html>