@@ -15,11 +15,23 @@ export default () => {
1515 //SECTOR_2.5:準const変数群
1616 let PUZ_BOARD_BONE = new Array ( ) ;
1717 let DATA = { } ;
18- // SECTOR_3:関数マニュアル
19- // onmouce_cell(cell) : チェイン中の処理とかやってます
20- const endscene = ( ) => {
18+ // SECTOR 3 : 関数群
19+ const object_copy = x => JSON . parse ( JSON . stringify ( x ) ) ;
20+ const objectevery = ( o , f ) => Object . keys ( o ) . every ( x => f ( o [ x ] ) ) ;
21+ const endscene = ( x = null ) => {
2122 DIV_PUZ_DISPLAY . style . display = "none" ;
2223 document . getElementById ( 'move_START' ) . onclick ( ) ;
24+ if ( x ) throw x ;
25+ }
26+ const gameClear = ( ) => {
27+ if ( ! ( DATA . target . score >= 0 && objectevery ( DATA . target ?. obj , x => x <= 0 ) && objectevery ( DATA . target ?. field , x => x <= 0 ) ) ) return ;
28+ alert ( "GAME CLEAR" ) ;
29+ endscene ( ) ;
30+ }
31+ const addScore = dscore => {
32+ const wasNegativeScore = DATA . target . score < 0 ;
33+ DATA . target . score += dscore ;
34+ wasNegativeScore && DATA . target . score >= 0 && gameClear ( ) ;
2335 }
2436 const getType = obj => obj [ 0 ] ;
2537 const isnullobj = obj => getType ( obj ) === 0 ;
@@ -34,28 +46,35 @@ export default () => {
3446 [ `Pictures/Orbs/${ getType ( DATA . board . obj [ y ] [ x ] ) } .svg` , `Pictures/Fields/${ getType ( DATA . board . field [ y ] [ x ] ) } .svg` ,
3547 alt_text ( getType ( DATA . board . obj [ y ] [ x ] ) , true ) , alt_text ( getType ( DATA . board . field [ y ] [ x ] ) , false ) ] ;
3648 }
37- const object_copy = x => JSON . parse ( JSON . stringify ( x ) ) ;
3849 const update_display = ( ) => {
3950 for ( let i = 0 ; i < DATA . size . Height ; i ++ ) for ( let j = 0 ; j < DATA . size . Width ; j ++ ) update_cell ( i , j ) ;
4051 document . getElementById ( 'puz_info' ) . innerText = `Score : ${ DATA . target . score } Hand : ${ DATA . target . hand } ` ;
4152 }
4253 const obj_erase = obj => [ obj [ 0 ] , obj [ 1 ] ] = [ 0 , 0 ] ;
54+ /**
55+ Todo : ゲームクリア時の処理を適切なものに置き換え
56+ */
4357 const break_obj = ( y , x , ischain , isobj = true ) => {
4458 const TARGET = isobj ?DATA . board . obj [ y ] [ x ] :DATA . board . field [ y ] [ x ] ;
45- if ( -- TARGET [ 1 ] <= 0 || ischain ) {
46- if ( ! isobj && getType ( TARGET ) === 1 ) DATA . target . score += BASE_SCORE ;
59+ const OBJTYPE = getType ( TARGET ) ;
60+ if ( -- TARGET [ 1 ] <= 0 || ischain ) {
61+ if ( ! isobj && OBJTYPE === 1 ) addScore ( BASE_SCORE ) ;
62+ const TARGETrEMAIN = DATA . target ?. [ isobj ?"obj" :"field" ] ;
63+ if ( TARGETrEMAIN ?. [ OBJTYPE ] && TARGETrEMAIN [ OBJTYPE ] > 0 ) {
64+ TARGETrEMAIN [ OBJTYPE ] -- ;
65+ console . info ( `${ isobj ?"obj" :"field" } ${ OBJTYPE } : Remaing ${ TARGETrEMAIN [ OBJTYPE ] } ` ) ;
66+ ( TARGETrEMAIN [ OBJTYPE ] <= 0 ) && gameClear ( ) ;
67+ }
4768 obj_erase ( TARGET ) ;
4869 update_cell ( y , x ) ;
4970 isobj && dest_sync ( DATA . board . field [ y ] [ x ] ) && break_obj ( y , x , false , false ) ;
5071 }
5172 }
5273 const fall_obj = ( obj_from , obj_to ) => {
53- if ( isnullobj ( obj_to ) && fallable ( obj_from ) ) {
54- [ obj_to [ 0 ] , obj_to [ 1 ] ] = obj_from ;
55- obj_erase ( obj_from ) ;
56- return true ;
57- }
58- else return false ;
74+ if ( ! ( isnullobj ( obj_to ) && fallable ( obj_from ) ) ) return false ;
75+ [ obj_to [ 0 ] , obj_to [ 1 ] ] = obj_from ;
76+ obj_erase ( obj_from ) ;
77+ return true ;
5978 }
6079 const falling_orb = ( ) => {
6180 chainable = false ;
@@ -90,7 +109,7 @@ export default () => {
90109 if ( chain_now ) { //チェイン終了時の処理
91110 chain_now = false ;
92111 if ( ! ( chain_yx . length < SHORTEST_CHAIN ) ) {
93- DATA . target . score += ~ ~ ( chain_yx . length ** SCORE_EXPONENT * BASE_SCORE ) ;
112+ addScore ( ~ ~ ( chain_yx . length ** SCORE_EXPONENT * BASE_SCORE ) ) ;
94113 DATA . target . hand -- ;
95114 chain_yx . forEach ( pos => {
96115 break_obj ( pos . y , pos . x , true ) ;
@@ -124,8 +143,8 @@ export default () => {
124143 const [ HEIGHT , WIDTH ] = [ DATA . size . Height , DATA . size . Width ] ;
125144 PUZ_BOARD_BONE = new Array ( HEIGHT ) . fill ( ) . map ( _ => Array ( WIDTH ) ) ;
126145 MAIN_BOARD . innerHTML = null ;
127- if ( HEIGHT <= 0 ) { endscene ( ) ; throw RangeError ( `GUARD! Height : ${ HEIGHT } isn't positive` ) ; }
128- if ( WIDTH <= 0 ) { endscene ( ) ; throw RangeError ( `GUARD! Height : ${ WIDTH } isn't positive` ) ; }
146+ if ( HEIGHT <= 0 ) endscene ( RangeError ( `GUARD! Height : ${ HEIGHT } isn't positive` ) ) ;
147+ if ( WIDTH <= 0 ) endscene ( RangeError ( `GUARD! Height : ${ WIDTH } isn't positive` ) ) ;
129148 for ( let i = 0 ; i < HEIGHT ; i ++ ) {
130149 const TR = document . createElement ( "tr" ) ;
131150 TR . classList . add ( "puz_board_tr" ) ;
@@ -150,14 +169,11 @@ export default () => {
150169 }
151170 const startgame = ( ) => {
152171 const StageID = document . getElementById ( 'StageLink' ) . value ;
153- if ( isNaN ( StageID ) ) {
154- endscene ( ) ;
155- throw TypeError ( `GUARD! StageID ${ StageID } is NaN` ) ;
156- }
172+ if ( isNaN ( StageID ) ) endscene ( TypeError ( `GUARD! StageID ${ StageID } is NaN` ) ) ;
157173 const DATALINK = "../Data/Stage/" + StageID + ".js" ;
158174 DIV_PUZ_DISPLAY . style . display = "block" ;
159175 import ( DATALINK )
160- . then ( x => { DATA = object_copy ( x . default ) ; board_init ( ) } ) . catch ( x => { endscene ( ) ; throw x } ) ;
161- } ;
176+ . then ( x => { DATA = object_copy ( x . default ) ; board_init ( ) } ) . catch ( x => endscene ( x ) ) ;
177+ }
162178 document . getElementById ( 'move_GAME' ) . onclick = startgame ;
163179}
0 commit comments