#apply
#include "../_include.txt"


cev[CEV_SPIRIT_WATCH] .name("mg_スピリットウォッチ") {

    def {
        NUM_OF_VAR = 64
        NUM_OF_STR = 0
        NUM_OF_PIC = 36

        BASE_LAYER = 6

        PATH_CASE  = "sw\ca"
        PATH_LIGHT = "sw\li"
        PATH_COUNT = "sw\co"
        PATH_CHARA = "sw\ch"
        PATH_ICON  = "sw\ic"
        PATH_TEXT  = "sw\te"

        PIC_HEAD    = 1
        PIC_CASE    = 2
        PIC_LIGHT_A = 3
        PIC_LIGHT_B = 4
        PIC_LIGHT_C = 5
        PIC_CHARA   = 6
        PIC_LIFE    = 7
        PIC_TEXT    = 8
        PIC_TIMER   = 9//+0_+4
        PIC_HISCORE = 14//+0_+1
        PIC_LEVEL   = 16//+0_+1
        PIC_ICON    = 18//+0_+15
        PIC_SEEK    = 34

        //実績
        ACHIEVE_CLEAR = 40

        //フラグ
        TR_BIT_LIGHT_A    = 1 << 0
        TR_BIT_LIGHT_B    = 1 << 1
        TR_BIT_LIGHT_C    = 1 << 2
        TR_BIT_HIDE_PIC   = 1 << 3
        TR_BIT_HIDE_ICON  = 1 << 4
        TR_BIT_SHOW_LEVEL = 1 << 5
        TR_BIT_SHOW_HI    = 1 << 6
        TR_BIT_SHOW_LIFE  = 1 << 7
        TR_BIT_SHOW_WATCH = 1 << 8

        //シーン
        SCENE_WATCH = 0
        SCENE_EXIT
        SCENE_GAMEOVER
        SCENE_PLAY
        SCENE_EXAMPLE
        SCENE_SPEEDUP

        //キャラクターのアニメーション
        CHARA_SLEEP = 0
        CHARA_BYE
        CHARA_STAND

        ANIM_SUCCESS = 0
        ANIM_FAILED
        ANIM_STAND
        ANIM_GO
        ANIM_LEFT
        ANIM_CENTER
        ANIM_RIGHT

        //効果音
        SE_NONE = 0
        SE_GAMEOVER
        SE_BYE
        SE_SPEEDUP
        SE_SUCCESS
        SE_FAILED
        SE_EXAMPLE
        SE_BUTTON_A
        SE_BUTTON_B
        SE_BUTTON_C
        SE_DEC
        SE_BEAT

        //位置
        TIME_X = 128
        TIME_Y = 80
        CHARA_X = 144
        CHARA_Y = 120
        LEVEL_X = 178
        LEVEL_Y = 74
        HISCORE_X = 116
        HISCORE_Y = 74
        LIFE_X = 154
        LIFE_Y = 74
        TEXT_X = 144
        TEXT_Y = 88
        SEEK_X = 128
        SEEK_Y = 96
        SEEK_LEN = 32
        SEEK_BEAT= 16

        //定数
        SHEET_MAX = 16
        LIFE_MAX = 1
        INPUT_MARGIN = 8
        LEVEL_MAX = 50
        LEVEL_STEP = 8
        BEAT_FIRST = 14
        BEAT_MIN = 8
        BEAT_STEP = 2

    }

    //ピクチャの共通コマンド
    def {
        basePicCmd = bl {
            .chromakey(1)
            .mapLayer(BASE_LAYER)
            .eraseWhenTransfer
            .affectedByFlash
            .affectedByShake
        }

        countPicCmd = bl {
            PATH_COUNT
            .topLeft
            .scale(200)
            .grid(16, 1)
            basePicCmd
        }

        iconPicCmd = bl {
            PATH_ICON
            .topLeft
            .scale(200)
            .grid(4, 1)
            basePicCmd
        }

        textPicCmd = bl {
            PATH_TEXT
            .topLeft
            .scale(200)
            .grid(2, 1)
            basePicCmd
        }

        charaPicCmd = bl {
            PATH_CHARA
            .topLeft
            .scale(200)
            .grid(6, 6)
        }

    }

    defv {
        varHead = TV_MINIGAME
        
        ipt
        pid
        cell
        ax
        ay
        sceneState
        sceneTimer
        charaState
        charaTimer
        charAnim
        soundState
        soundTimer
        level
        life
        beat
        prevTime
        triggerFlg
        post
        pattern
        buttonA
        buttonB
        buttonC
        buttonE
        sheetA
        sheetB
        sheetC
        resA
        resB
        resC
        freqA
        freqB
        freqC

        varFoot

    }

    commentRangeOfVariables(varFoot, TV_MINIGAME, NUM_OF_VAR, NUM_OF_STR, NUM_OF_PIC)

    //譜面を入力する
    __fn _updateSheet $idx bl {
        //16
        //締め1, 連鎖2, 一つ, 裏打ち2, 嵐の歌, 裏打ち1, 子犬のマーチ, 破滅への使者, 連鎖1, C Jam Blues, この蝶々, 突撃ラッパ, ゴジラ, チョコボ, 玩具の, マリオ
        sheetA = [    0,  5396,     0,    0,   260,   256,   272,   512, 10280,  3276,  2080,     0, 16448,  4640,    84,   512][$idx]
        sheetB = [  212,     0, 16384, 8192,   520,  2080,  1092,  2228,  5204,  8192,     0,  9396,  4112, 17680,   256,  1200][$idx]
        sheetC = [  512, 16448,     0, 2856,  1040,  8196, 20480, 20544, 16384,     0,  1300,  2304,  1028,   132, 21504,  4096][$idx]
        freqA  = [ F_A5,  F_F5,  F_A5, F_A5,  F_D5,  F_F5,  F_C5,  F_D5,  F_G5,  F_G5,  F_E5,  F_F5,  F_F5,  F_G5,  F_C5,  F_C5][$idx]
        freqB  = [ F_B5,  F_A5,  F_B5, F_B5,  F_F5,  F_A5,  F_E5,  F_F5,  F_B5,  F_C6,  F_F5,  F_G5,  F_G5,  F_B5,  F_E5,  F_E5][$idx]
        freqC  = [ F_C6,  F_C6,  F_C6, F_C6,  F_D6,  F_B5,  F_G5,  F_A5,  F_D6,  F_D6,  F_G5,  F_B5,  F_A5,  F_D6,  F_A5,  F_G5][$idx]
    }

    //シーンの状態を変える
    __fn _changeSceneState $state bl {
        sceneState = $state
        sceneTimer = -1
    }

    //キャラの状態を変える
    __fn _changeCharaState $state $anim bl {
        charaState = $state
        charaTimer = -1
        __if $anim >= 0 bl {
            charAnim = $anim
        }
    }

    //効果音の状態を変える
    __fn _changeSoundState $state bl {
        soundState = $state
        soundTimer = -1
    }

    //ハイスコアを更新する
    __fn _updateHighScore() bl {
        spiritWatchScore = max(spiritWatchScore, level)
    }

    //sin波
    __fn _playSinSound $vol $freq bl {
        @se.play "sin1" .opt($vol, $freq, 50)
    }

    //------------------------------------------------------------------------------------
    //開始処理
    bl {
        initPicturesAndVariables(PIC_HEAD, NUM_OF_PIC, TV_MINIGAME, NUM_OF_VAR, 0, 0)
        storeAndStopBGM()
        msgBottom()
        
        //ピクチャ
        @pic.show [PIC_CASE] PATH_CASE .pos(160, 120) .center .scale(100) .trans(0) basePicCmd
        @pic.show [PIC_CHARA] .cell(0) charaPicCmd
        @pic.show [PIC_LIFE] .cell(0) iconPicCmd
        @pic.show [PIC_TEXT] .cell(0) textPicCmd
        @pic.show [PIC_SEEK] .trans(100) .cell(1) iconPicCmd

        @loop 3 .dst(pid) bl {
            pid += PIC_LIGHT_A
            @pic.show [pid] PATH_LIGHT .pos(0, 0) .center .scale(100) .trans(100) basePicCmd
        }
        @loop 5 .dst(pid) bl {
            pid += PIC_TIMER
            @pic.show [pid] .cell(0) countPicCmd
        }
        @loop 2 .dst(pid) bl {
            pid += PIC_LEVEL
            @pic.show [pid] .cell(0) countPicCmd
        }
        @loop 2 .dst(pid) bl {
            pid += PIC_HISCORE
            @pic.show [pid] .cell(0) countPicCmd
        }
        @loop SEEK_BEAT .dst(pid) bl {
            pid += PIC_ICON
            @pic.show [pid] .cell(0) iconPicCmd
        }

        @scr.tint .rgbs(50, 50, 50, 100) .time(1)

        @label.jump "gameLoop"
    }

    //------------------------------------------------------------------------------------
    //ゲーム処理
    @label.set "gameLoop"
    bl {
        _changeSceneState(SCENE_WATCH)
        triggerFlg = 0x00

        //入力を抑える
        v[__id(buttonA)..__id(buttonE)] = 2

        //----------------------------
        //ループ
        @loop bl {
            @wait(0)

            //----------------------------
            //入力状態を更新する
            __fn _updateInput $dvInput $block {
                @key.input .dst(ipt) $block
                v[__id($dvInput)] = ipt ? $dvInput + 1 : 0
            }

            _updateInput(buttonA, bl { .left .shift } )
            _updateInput(buttonB, bl { .down .decision } )
            _updateInput(buttonC, .right)
            _updateInput(buttonE, .cancel)

            //ボタンは押すと光る
            @if buttonA == 1 bl {
                bitOn(triggerFlg, TR_BIT_LIGHT_A)
            }

            @if buttonB == 1 bl {
                bitOn(triggerFlg, TR_BIT_LIGHT_B)
            }

            @if buttonC == 1 bl {
                bitOn(triggerFlg, TR_BIT_LIGHT_C)
            }

            //----------------------------
            //ゲームを終了させる
            @if `buttonE == 1 && sceneState != SCENE_EXIT bl {
                _changeSceneState(SCENE_EXIT)
            }

            //----------------------------
            //シーン
            sceneTimer += 1

            //----------------------------
            //ゲーム中
            @if `sceneState == SCENE_PLAY || sceneState == SCENE_EXAMPLE bl {
                //frame = fps^2 / bpm
                //8拍目
                @if `sceneTimer >= beat * SEEK_BEAT bl {
                    //手本終わり
                    @if sceneState == SCENE_EXAMPLE bl {
                        _changeSceneState(SCENE_PLAY)
                        _changeCharaState(CHARA_STAND, ANIM_GO)
                        _changeSoundState(SE_EXAMPLE)
                        sceneTimer = 0//微調整

                    //プレイヤーのターン終わり
                    } .elif sceneState == SCENE_PLAY bl {
                        //成功
                        @if `resA == sheetA && resB == sheetB && resC == sheetC bl {
                            _changeCharaState(CHARA_STAND, ANIM_SUCCESS)
                            bitOn(triggerFlg, TR_BIT_SHOW_LEVEL | TR_BIT_SHOW_LIFE)
                            //回復する
                            life = LIFE_MAX
                            //次のレベル
                            level += 1
                            //実績
                            checkSetAchievement(achievedSprit, level >= ACHIEVE_CLEAR)
                            //次の譜面を選ぶ
                            selectUsingBit(ipt, __id(pattern), SHEET_MAX)
                            //選べなかった場合、フラグを初期化してもう一度選ぶ
                            @if ipt < 0 bl {
                                pattern = 0x00
                                ipt = rnd(0, SHEET_MAX - 1)
                            }
                            //フラグを立てる
                            pattern |= 1 << ipt
                            //譜面をセットする
                            _updateSheet(ipt)
                            //最大レベルに達すると強制ゲームオーバーとなる
                            @if level >= LEVEL_MAX bl {
                                _changeSceneState(SCENE_GAMEOVER)
                            //一定レベルごとに速度が増す
                            } .elif `beat > BEAT_MIN && !(level % LEVEL_STEP) bl {
                                beat = max(beat - BEAT_STEP, BEAT_MIN)
                                _changeSceneState(SCENE_SPEEDUP)
                            } .else bl {
                                _changeSoundState(SE_SUCCESS)
                            }

                        //失敗
                        } .else bl {
                            _changeCharaState(CHARA_STAND, ANIM_FAILED)
                            bitOn(triggerFlg, TR_BIT_SHOW_LIFE)

                            //ゲームオーバー
                            @if life <= 0 bl {
                                _changeSceneState(SCENE_GAMEOVER)
                            //もう一度
                            } .else bl {
                                _changeSoundState(SE_FAILED)
                                //無敵モードか、レベルが最初の場合はライフが減らない
                                @if `level > 0 && !invincibleSwitch bl {
                                    life -= 1
                                }
                            }
                        }

                        //次のレベルへ
                        @if sceneState == SCENE_PLAY bl {
                            _changeSceneState(SCENE_EXAMPLE)
                            sceneTimer = 0//微調整
                        }
                        //アイコンを隠す
                        bitOn(triggerFlg, TR_BIT_HIDE_ICON)
                        //入力を初期化する
                        v[__id(resA)..__id(resC)] = 0x00
                    }

                } .else bl {
                    //シーク
                    post = sceneTimer / beat
                    //ax = sceneTimer * SEEK_LEN / (beat * 8)
                    ax = SEEK_X + (sceneTimer * SEEK_LEN / (SEEK_BEAT * beat)) * 2
                    ay = SEEK_Y
                    @pic.move [PIC_SEEK] .pos(ax, ay) .topLeft .scale(200) .trans(0)
                    //手本をなぞる
                    @if sceneState == SCENE_EXAMPLE bl {
                        @if `!(sceneTimer % beat) bl {
                            //対応したボタンを光らせ、アイコンを置く
                            __fn _lightUpButton_ex $dvLine $bit $se $cell bl {
                                @if `$dvLine & (1 << post) bl {
                                    bitOn(triggerFlg, $bit)
                                    _changeSoundState($se)
                                    cell = $cell
                                }
                            }

                            cell = -1
                            _lightUpButton_ex(sheetA, TR_BIT_LIGHT_A, SE_BUTTON_A, 2)
                            _lightUpButton_ex(sheetB, TR_BIT_LIGHT_B, SE_BUTTON_B, 3)
                            _lightUpButton_ex(sheetC, TR_BIT_LIGHT_C, SE_BUTTON_C, 4)

                            //アイコンを置く
                            @if cell >= 0 bl {
                                pid = post + PIC_ICON
                                ax = SEEK_X + (post * SEEK_LEN / SEEK_BEAT) * 2
                                ay = SEEK_Y
                                @pic.show [pid] .pos(ax, ay) .cell(cell) iconPicCmd
                            }
                        }

                    //ボタン入力
                    } .elif sceneState == SCENE_PLAY bl {
                        @if `between(post, 0, SEEK_BEAT - 1) bl {
                            //ポーズを変える
                            __fn _charaMove_pl $dvInput $dvLine $dvRes $anim $se {
                                @if $dvInput == 1 bl {
                                    _changeCharaState(CHARA_STAND, $anim)
                                    _changeSoundState($se)

                                    //ax:idx ay:bit
                                    @loop SEEK_BEAT .dst(ax) bl {
                                        ay = 1 << ax
                                        @if `($dvLine & ay) && !($dvRes & ay) && abs(sceneTimer - ax * beat) < INPUT_MARGIN bl {
                                            v[__id($dvRes)] |= ay

                                            pid = ax + PIC_ICON
                                            @pic.move[pid] .relative .pos(0, 0) .scale(0) .trans(100)
                                        }
                                    }
                                }
                            }
                            _charaMove_pl(buttonA, sheetA, resA, ANIM_LEFT,   SE_BUTTON_A)
                            _charaMove_pl(buttonB, sheetB, resB, ANIM_CENTER, SE_BUTTON_B)
                            _charaMove_pl(buttonC, sheetC, resC, ANIM_RIGHT,  SE_BUTTON_C)
                        }
                    }
                    
                    //2拍ごと
                    @if `(sceneTimer + beat * 2) % (beat * 4) == 0 bl {
                        //最初の1拍目
                        @if `sceneTimer == beat * 2 bl {
                            //既に構えていた場合はポーズを戻さない
                            @if `sceneTimer == beat * 2 && (charAnim == ANIM_LEFT || charAnim == ANIM_CENTER || charAnim == ANIM_RIGHT) bl {
                            } .else bl {
                                _changeCharaState(CHARA_STAND, ANIM_STAND)
                            }
                        } .else bl {
                            //屈伸
                            @if sceneState == SCENE_EXAMPLE bl {
                                _changeCharaState(CHARA_STAND, -1)
                            }
                        }
                        @if soundState == SE_NONE bl {
                            _changeSoundState(SE_BEAT)
                        }
                    }
                }

            //----------------------------
            //スピードアップ
            } .elif sceneState == SCENE_SPEEDUP bl {
                //SPEED UP
                @if sceneTimer == 0 bl {
                    _changeSoundState(SE_SPEEDUP)
                    @pic.show [PIC_TEXT] .pos(TEXT_X, TEXT_Y) .cell(2) textPicCmd
                }
                //屈伸
                @if `between(sceneTimer, 0 + 1, 24 * 3) && sceneTimer % 24 == 0 bl {
                    _changeCharaState(CHARA_STAND, ANIM_STAND)
                }
                //最初に戻る
                @if `sceneTimer >= 96 bl {
                    _changeSceneState(SCENE_EXAMPLE)
                    hidePicture(PIC_TEXT)
                }
            //----------------------------
            //ゲームオーバー
            } .elif sceneState == SCENE_GAMEOVER bl {
                //GAME OVER
                @if sceneTimer == 0 bl {
                    _changeSoundState(SE_GAMEOVER)
                    @pic.show [PIC_TEXT] .pos(TEXT_X, TEXT_Y) .cell(1) textPicCmd
                }
                //最初に戻る
                @if `sceneTimer >= 30 && (buttonA == 1 || buttonB == 1 || buttonC == 1) bl {
                    _changeSceneState(SCENE_WATCH)
                    _changeSoundState(SE_DEC)
                    bitOn(triggerFlg, TR_BIT_HIDE_PIC)
                    _updateHighScore()
                }
            //----------------------------
            //最初の画面
            } .elif sceneState == SCENE_WATCH bl {
                //寝る
                @if sceneTimer == 0 bl {
                    _changeCharaState(triggerFlg, CHARA_SLEEP)
                }

                //ゲーム開始
                @if `sceneTimer >= 1 && (buttonA == 1 || buttonB == 1 || buttonC == 1) bl {
                    _changeSceneState(SCENE_EXAMPLE)
                    _changeCharaState(CHARA_STAND, ANIM_SUCCESS)
                    _changeSoundState(SE_DEC)
                    bitOn(triggerFlg, TR_BIT_HIDE_PIC | TR_BIT_SHOW_LEVEL | TR_BIT_SHOW_LIFE | TR_BIT_SHOW_HI)

                    level = 0
                    life  = LIFE_MAX
                    beat  = BEAT_FIRST
                    pattern = 0x00

                    //最初は単純な形
                    //v[__id(sheetA)..__id(sheetC)] = [v[2], v[3], v[4]]
                    //v[__id(freqA)..__id(freqC)] = [v[5], v[6], v[7]]
                    v[__id(sheetA)..__id(sheetC)] = [0x4444, 0x00, 0x00]
                    v[__id(freqA)..__id(freqC)]   = [F_C5, F_D5, F_E5]
                    v[__id(resA)..__id(resC)] = 0

                //時計
                } .elif `prevTime != sys.time || sceneTimer == 0 bl {
                    prevTime = sys.time
                    bitOn(triggerFlg, TR_BIT_SHOW_WATCH)
                }

            //----------------------------
            //電源を切る
            } .elif sceneState == SCENE_EXIT bl {
                //ピクチャ
                @if sceneTimer == 0 bl {
                    _changeCharaState(CHARA_BYE, -1)
                    _changeSoundState(SE_BYE)
                    bitOn(triggerFlg, TR_BIT_HIDE_PIC | TR_BIT_HIDE_ICON)
                }

                //終了
                //もう一度ボタンを押すとすぐに終わる
                @if `sceneTimer >= 120 || (sceneTimer > 0 && buttonE == 1) bl {
                    @label.jump "gameEnd"
                }
            }

            //----------------------------
            //キャラクターアニメーション
            charaTimer += 1

            //この変数が0以上ならピクチャを置き直す
            cell = -1

            @if charaState == CHARA_SLEEP bl {
                @if `!(charaTimer % 60) bl {
                    cell = 1 + charaTimer % 120 / 60
                }

            } .elif charaState == CHARA_BYE bl {
                @if `!(charaTimer % 16) && charaTimer <= 16 * 4 bl {
                    cell = 3 + charaTimer % 32 / 16
                }

            } .elif charaState == CHARA_STAND bl {
                @if `!(charaTimer % 4) && charaTimer <= 4 * 2 bl {
                    cell = [7, 10, 13, 16, 19, 22, 25][charAnim] + charaTimer / 4
                }

            }

            @if cell >= 0 bl {
                @pic.show [PIC_CHARA] .pos(CHARA_X, CHARA_Y) .cell(cell) charaPicCmd
            }

            //----------------------------
            //効果音
            @if soundState > 0 bl {
                pid  = 100
                cell = 0

                //ゲームオーバー
                @if soundState == SE_GAMEOVER bl {
                    soundTimer += 1

                    $frame = 8
                    $num = 5
                    @if `!(soundTimer % $frame) bl {
                        cell = [190, 170, 150, 133, 120][soundTimer / $frame]
                    }
                    @if `soundTimer >= $frame * ($num - 1) bl {
                        soundState = 0
                    }

                //電源を切る
                } .elif soundState == SE_BYE bl {
                    soundTimer += 1

                    $frame = 12
                    $num = 2
                    @if `!(soundTimer % $frame) bl {
                        cell = [112, 94][soundTimer / $frame]
                    }

                    @if `soundTimer >= $frame * ($num - 1) bl {
                        soundState = 0
                    }

                //スピードアップ
                } .elif soundState == SE_SPEEDUP bl {
                    soundTimer += 1

                    $frame = 8
                    $num = 2
                    @if `!(soundTimer % $frame) bl {
                        cell = [377, 504][soundTimer / $frame]
                    }

                    @if `soundTimer >= $frame * ($num - 1) bl {
                        soundState = 0
                    }

                //成功
                } .elif soundState == SE_SUCCESS bl {
                    cell = 300
                    soundState = SE_NONE

                //失敗
                } .elif soundState == SE_FAILED bl {
                    cell = 126
                    soundState = SE_NONE

                //手本終わり
                } .elif soundState == SE_EXAMPLE bl {
                    cell = 189
                    soundState = SE_NONE

                //A
                } .elif soundState == SE_BUTTON_A bl {
                    cell = freqA
                    soundState = SE_NONE

                //B
                } .elif soundState == SE_BUTTON_B bl {
                    cell = freqB
                    soundState = SE_NONE

                //C
                } .elif soundState == SE_BUTTON_C bl {
                    cell = freqC
                    soundState = SE_NONE

                //決定音
                } .elif soundState == SE_DEC bl {
                    cell = 200
                    soundState = SE_NONE

                //拍
                } .elif soundState == SE_BEAT bl {
                    pid = 20
                    cell = 336
                    soundState = SE_NONE
                }

                @if cell > 0 bl {
                    @se.play "sin1" .opt(pid, cell, 50)
                }
            }

            //----------------------------
            //フラグ
            @if `triggerFlg bl {

                //ボタンを光らせる
                __fn _buttonLight $flg $pid $x $y {
                    @if `triggerFlg & ($flg) bl {
                        bitOff(triggerFlg, $flg)
                        @pic.show [$pid] PATH_LIGHT .pos($x, $y) .center .scale(100) .trans(-100) .rgbs(100, 100, 100, 100) .add basePicCmd
                        @pic.move [$pid] .relative .pos(0, 0) .scale(0) .trans(200) .rgbs(100, 100, 100, 100) .keepBlend .time(2)
                    }
                }

                _buttonLight(TR_BIT_LIGHT_A, PIC_LIGHT_A, 128, 188)
                _buttonLight(TR_BIT_LIGHT_B, PIC_LIGHT_B, 160, 190)
                _buttonLight(TR_BIT_LIGHT_C, PIC_LIGHT_C, 192, 188)

                //ピクチャを隠す
                @if `triggerFlg & TR_BIT_HIDE_PIC bl {
                    bitOff(triggerFlg, TR_BIT_HIDE_PIC)
                    hidePicturesForEach( [PIC_TIMER + 0, PIC_TIMER + 1, PIC_TIMER + 2, PIC_TIMER + 3, PIC_TIMER + 4, PIC_LEVEL + 0, PIC_LEVEL + 1, PIC_HISCORE + 0, PIC_HISCORE + 1, PIC_LIFE, PIC_HISCORE, PIC_TEXT] )
                }

                //アイコンピクチャを隠す
                @if `triggerFlg & TR_BIT_HIDE_ICON bl {
                    bitOff(triggerFlg, TR_BIT_HIDE_ICON)

                    hidePicture(PIC_SEEK)

                    @loop SEEK_BEAT .dst(pid) bl {
                        pid += PIC_ICON
                        @pic.move [pid] .pos(0, 0) .center .scale(100) .trans(100)
                    }
                }

                //時計を表示する
                @if `triggerFlg & TR_BIT_SHOW_WATCH bl {
                    bitOff(triggerFlg, TR_BIT_SHOW_WATCH)
                    //sys.time = h * 10000 + m * 100 + s
                    //h
                    cell = 1 + prevTime % 1000000 / 100000
                    @pic.show [PIC_TIMER + 0] .pos(TIME_X +  0, TIME_Y) .trans(0) .cell(cell) countPicCmd
                    cell = 1 + prevTime % 100000  / 10000
                    @pic.show [PIC_TIMER + 1] .pos(TIME_X + 16, TIME_Y) .trans(0) .cell(cell) countPicCmd
                    //m
                    cell = 1 + prevTime % 10000 / 1000
                    @pic.show [PIC_TIMER + 2] .pos(TIME_X + 40, TIME_Y) .trans(0) .cell(cell) countPicCmd
                    cell = 1 + prevTime % 1000  / 100
                    @pic.show [PIC_TIMER + 3] .pos(TIME_X + 56, TIME_Y) .trans(0) .cell(cell) countPicCmd
                    //s
                    cell = (prevTime % 100 % 2) ? 11 : 0
                    @pic.show [PIC_TIMER + 4] .pos(TIME_X + 28, TIME_Y) .trans(0) .cell(cell) countPicCmd
                }

                //レベルを表示する
                @if `triggerFlg & TR_BIT_SHOW_LEVEL bl {
                    bitOff(triggerFlg, TR_BIT_SHOW_LEVEL)

                    cell = 1 + level / 10
                    @pic.show [PIC_LEVEL + 0] .pos(LEVEL_X +  0, LEVEL_Y) .trans(0) .cell(cell) countPicCmd
                    cell = 1 + level % 10
                    @pic.show [PIC_LEVEL + 1] .pos(LEVEL_X + 16, LEVEL_Y) .trans(0) .cell(cell) countPicCmd
                }

                //ハイスコアを表示する
                @if `triggerFlg & TR_BIT_SHOW_HI bl {
                    bitOff(triggerFlg, TR_BIT_SHOW_HI)

                    ax = spiritWatchScore

                    cell = 1 + ax / 10
                    @pic.show [PIC_HISCORE + 0] .pos(HISCORE_X +  0, HISCORE_Y) .trans(0) .cell(cell) countPicCmd
                    cell = 1 + ax % 10
                    @pic.show [PIC_HISCORE + 1] .pos(HISCORE_X + 16, HISCORE_Y) .trans(0) .cell(cell) countPicCmd
                }

                //ライフを表示する
                @if `triggerFlg & TR_BIT_SHOW_LIFE bl {
                    bitOff(triggerFlg, TR_BIT_SHOW_LIFE)

                    cell = (life > 0) ? 14 : 0
                    @pic.show [PIC_LIFE] .pos(LIFE_X, LIFE_Y) .trans(0) .cell(cell) countPicCmd
                }
            }
        }
    }

    //----------------------------
    //終了する
    @label.set "gameEnd"
    bl {
        //ハイスコアを更新する
        _updateHighScore()

        initPicturesAndVariables(PIC_HEAD, NUM_OF_PIC, TV_MINIGAME, NUM_OF_VAR, 0, 0)
        restoreBGM()
        @scr.tint .rgbs(100, 100, 100, 100) .time(1) .wait
    }
}