#apply
#include "../_include.txt"
#include "../../map/_header.txt"
#include "../../define/_vector2.txt"
#include "../../define/_const_pinball.txt"
#include "../../define/_const_pinball2.txt"

cev[CEV_PB_ON_FRAME] .name(">最初に実行される") {
    defv {
        ipt = TV_PB_ON_FRAME
        dot
        state
        bit
        pid
        cell
        iA
        iB
        pA
        pB
        angA
        angB
        ax
        ay
        bx
        by
        cx
        cy
        dx
        dy
    }

    deft {
        timeStr = TS_PB_ON_ENTER_FRAME
    }

    def {
        COND_BALL_LOCK = (eventMode || hitStopTimer > 0)
    }

    //玉のピクチャを設置する
    //iA, ax, cell, pid
    __fn _showBallPicture $layer {
        //本体
        pid = PIC_BALL_BODY + iA
        @pic.show[pid] .pos(ax, ay) .center .cell(cell) .mapLayer($layer) .rgbs(angA, angA, angB, angB) ballBodyPicCmd
        //影
        pid = PIC_BALL_SHADOW + iA
        @pic.show[pid] .pos(ax, ay) .center .mapLayer($layer) ballShadowPicCmd
        //残像
        pid = iA * BA_MAX_AFTER + sceneTimer % BA_MAX_AFTER + PIC_BALL_AFTER
        @pic.show[pid] .pos(ax, ay) .center .mapLayer($layer) ballAfterPicCmd
        @pic.move[pid] bl {
            .relative
            .pos(0, 0)
            .scale(-80)
            .trans(100)
            .keepRgbs
            .keepBlend
            .time(2)
        }
    }

    //玉が当たる音を鳴らす
    __fn _playSoundOfBallHit $path $vol $tempo $div bl {
        localA = lerp(0, 100, $vol, $div)
        @if localA > 30 bl {
            @se.play $path .opt(localA, $tempo, 50)
        }
    }

    //----------------------------------------
    //タイマー
    //ゲーム中でイベント処理中でない
    @if `sceneState == SC_ST_PLAY && !eventMode bl {
        //プレイ時間
        //スコアがカンストすると止まる
        @if `gameTimer < MAX_TIME && score < MAX_SCORE bl {
            gameTimer += 1
            @if `!(gameTimer % UNIT_FPS) bl {
                bitOn(triggerFlg, SC_BIT_TIMER)
            }
        }
        //パワーアップタイマー
        @if powerupTimer > 0 bl {
            powerupTimer -= 1
            //時間切れ
            @if powerupTimer <= 0 bl {
                //BGMを即変える
                bgmTimer = 1
                @se.play "Debuff" .opt(70, 100, 50)
            }
            //1秒ごとに更新する
            @if `!(powerupTimer % UNIT_FPS) bl {
                bitOn(triggerFlg, SC_BIT_POWERUP)
                //カウントダウン
                @if `between(powerupTimer / UNIT_FPS, 1, 9) bl {
                    @se.play "Cursor2" .opt(70, 105, 50)
                }
            }
        }
        //タイムボーナス
        @if timeBonusTimer > 0 bl {
            timeBonusTimer -= 1
            //1秒ごとに更新する
            @if `!(timeBonusTimer % UNIT_FPS) bl {
                bitOn(triggerFlg, SC_BIT_TIMEBONUS)
            }
        }
    }
    //----------------------------------------
    //ヒットストップ
    @if hitStopTimer > 0 bl {
        hitStopTimer -= 1
    }

    //----------------------------------------
    //カメラ位置を更新する
    @sys.getInfo .camera .dst(cameraX)

    //----------------------------------------
    //キー入力を更新する

    //A:3D Pinballに寄せた
    //Z:左F X:左N 下:右N 右:右F 左:プランジャー
    //B:Xキー未使用
    //Shift:左F Z:左N 上:右N 右:右F 下:プランジャー
    //C:片手操作
    //左:左F 右:右F 上+左;左N 上+右:右N 下:プランジャー
    pseudoSwitchState(pbControllMode,
    [ CTRL_TYPE_A, bl {
        @key.input .dst(ax) .left
        .input .dst(bx) .decision
        .input .dst(by) .right
        .input .dst(cx) .cancel
        .input .dst(cy) .down
    },
    CTRL_TYPE_B, bl {
        @key.input .dst(ax) .down
        .input .dst(bx) .shift
        .input .dst(by) .right
        .input .dst(cx) .decision
        .input .dst(cy) .up
    },
    CTRL_TYPE_C, bl {
        @key.input .dst(ax) .down
        .input .dst(bx) .left
        .input .dst(by) .right
        .input .dst(ipt) .up

        //同時押し
        cx = 0
        cy = 0
        @if `ipt bl {
            @if bx != 0 bl {
                cx = 1
            }
            @if by != 0 bl {
                cy = 1
            }
        }
    } ] )

    //共通操作
    @key.input .dst(ipt) .number
    abortInput = (ipt == ABORT_KEY) ? (abortInput + 1) : (0)
    cheatInput = (ipt == CHEAT_KEY) ? (cheatInput + 1) : (0)
    //何かしらのキーを押した瞬間を判定したい
    getAnyKey(ipt, FALSE)
    anyInput = (ipt) ? (anyInput + 1) : (0)
    //プランジャー
    plungerInput = (ax) ? (plungerInput + 1) : (0)
    //左右入力
    leftFlipperInput  = (bx) ? (leftFlipperInput + 1) : (0)
    rightFlipperInput = (by) ? (rightFlipperInput + 1) : (0)
    leftNudgeInput    = (cx) ? (leftNudgeInput + 1) : (0)
    rightNudgeInput   = (cy) ? (rightNudgeInput + 1) : (0)

    //----------------------------------------
    //強制終了
    @if abortInput > ABORT_TIMER bl {
        goToRelayMap()
        @ev.abort
    }

    //----------------------------------------
    //チート
    @if cheatInput > 0 bl {
        V2_getInput(ax)
        V2_asMulScalar(ax, CHEAT_VEL)
        //全ての玉が影響を受ける
        @loop MAX_BALL .dst(iA) bl {
            getBallPtr(pA, iA)
            @if `v[pA + MB_STATE] != BA_ST_ENABLE bl {
                @continue
            }
            v[pA + MB_VX] = ax
            v[pA + MB_VY] = ay
        }
    }

    //----------------------------------------
    //ナッジング
    @if `!eventMode bl {
        @if nudgeTimer > 0 bl {
            nudgeTimer -= 1
        } .else bl {
            ipt = (leftNudgeInput > 0 ? 1 : 0) - (rightNudgeInput > 0 ? 1 : 0)
            @if ipt != 0 bl {
                nudgeTimer = NU_TIMER1
                nudgeDir = ipt
                //演出
                @se.play "Blow7" .opt(100, 100, 50)
                @scr.shake .once(1) .value(2, 7)
            }
        }
        @if `between(nudgeTimer, NU_TIMER1 - NU_TIMER2, NU_TIMER1) bl {
            //全ての玉が影響を受ける
            //イベント中でも
            @loop MAX_BALL .dst(iA) bl {
                getBallPtr(pA, iA)
                @if `v[pA + MB_STATE] != BA_ST_ENABLE bl {
                    @continue
                }
                //揺り戻しがある
                v[pA + MB_VX] += ((nudgeTimer == NU_TIMER1) ? (NU_VX1) : (NU_VX2)) * nudgeDir
                v[pA + MB_VY] += ((nudgeTimer == NU_TIMER1) ? (NU_VY1) : (NU_VY2))
            }
        }
    }

    //----------------------------------------
    //フリッパー
    @loop MAX_FLIPPER .dst(iA) bl {
        getFlipperPtr(pA, iA)
        state = v[pA + MF_STATE]
        @if state <= FL_ST_NONE bl {
            @continue
        }
        //削除
        @if state == FL_ST_DELETE bl {
            v[pA + MF_STATE] = FL_ST_NONE
            pid = PIC_FLIPPER + iA
            hidePicture(pid)
            @continue
        }

        //ビットフラグ
        bit = v[pA + MF_BIT]

        //イベント中は角度を更新しない
        @if `!eventMode bl {
            //角度を更新する
            ax = (bit & FL_BIT_HREV) ? (rightFlipperInput) : (leftFlipperInput)
            ax = (ax ? 1 : -1)
            ay = v[pA + MF_STEP]
            v[pA + MF_STEP] = clamp(ay + ax * FL_STEP, 0, FL_MAX_STEP)
            //値が変わった
            @if `v[pA + MF_STEP] != ay bl {
                //効果音
                //小さいフリッパーは鳴らない
                @if `!(bit & FL_BIT_SMALL) bl {
                    @if ay >= FL_MAX_STEP bl {
                        @se.play "flipper1" .opt(60, 80, 50)
                    } .elif ay <= 0 bl {
                        @se.play "flipper1" .opt(100, 100, 50)
                    }
                }
            }

            //更新前と更新後の角度
            angA = lerp(FL_START_ANGLE, FL_END_ANGLE, ay, FL_MAX_STEP)
            angB = lerp(FL_START_ANGLE, FL_END_ANGLE, v[pA + MF_STEP], FL_MAX_STEP)
            @if `bit & FL_BIT_HREV bl {
                angA = 180 * UNIT_ANGLE - angA
                angB = 180 * UNIT_ANGLE - angB
            }

            //玉との衝突判定
            @loop MAX_BALL .dst(iB) bl {
                getBallPtr(pB, iB)
                @if `v[pB + MB_STATE] != BA_ST_ENABLE bl {
                    @continue
                }
                //玉と回転する線分の衝突判定
                //ipt:衝突時間
                circleToRotatingSegment(ipt, ax, ay, bx, by, unusedV, unusedV, cx, cy,
                v[pB + MB_X], v[pB + MB_Y], v[pB + MB_NX], v[pB + MB_NY], BA_RADxUNIT,
                v[pA + MF_X], v[pA + MF_Y], 0,
                (bit & FL_BIT_SMALL) ? (FL_RAD_ROOT_SMALL) : (FL_RAD_ROOT_NORMAL),
                (bit & FL_BIT_SMALL) ? (FL_LEN_SMALL) : (FL_LEN_NORMAL),
                (bit & FL_BIT_SMALL) ? (FL_RAD_TIP_SMALL) : (FL_RAD_TIP_NORMAL),
                angA, angB)
                //反射
                @if ipt >= 0 bl {
                    pbBallReflection(unusedV,
                    iB, ipt, ax, ay, bx, by, cx, cy, FL_FRI, FL_REF)
                }
            }
        }

        //ピクチャを更新する
        //(16, 26)
        V2_set(ax,
            v[pA + MF_X] / UNIT_POS + FL_OFFSET_X * ((bit & FL_BIT_HREV) ? -1 : 1),
            v[pA + MF_Y] / UNIT_POS + FL_OFFSET_Y
        )
        V2_asSub(ax, cameraX)

        pid = PIC_FLIPPER + iA
        cell = lerp(0, 3, v[pA + MF_STEP], FL_MAX_STEP)
        cell += (bit & FL_BIT_SMALL) ? 5 : 1

        @if `bit & FL_BIT_HREV bl {
            @pic.show[pid] .pos(ax, ay) .topRight .cell(cell) .hrev flipperPicCmd
        } .else bl {
            @pic.show[pid] .pos(ax, ay) .topLeft .cell(cell) flipperPicCmd
        }
    }

    //----------------------------------------
    //玉
    ballCount = 0
    @loop MAX_BALL .dst(iA) bl {
        getBallPtr(pA, iA)
        state = v[pA + MB_STATE]
        @if state <= BA_ST_NONE bl {
            @continue
        }
        //削除
        @if state == BA_ST_DELETE bl {
            v[pA + MB_STATE] = BA_ST_NONE

            pid = PIC_BALL_BODY + iA
            hidePicture(pid)
            pid = PIC_BALL_SHADOW + iA
            hidePicture(pid)

            @continue
        }

        //有効な玉の数をカウントする
        ballCount += 1

        //更新する
        @if `state == BA_ST_ENABLE && !COND_BALL_LOCK bl {
            //マップとの衝突判定
            //ipt:衝突時間
            pid = PIC_COLLMAP + v[pA + MB_LAYER]
            pointToCollmap(ipt, ax, ay, bx, by,
            v[pA + MB_X], v[pA + MB_Y], v[pA + MB_NX], v[pA + MB_NY], UNIT_POS, BA_REACT_MAP, pid)

            @if ipt >= 0 bl {
                pbBallReflection(dot,
                iA, ipt, ax, ay, bx, by, 0, 0, MAP_FRI, MAP_REF)
    
                //衝突速度に応じて音が鳴る
                ipt = clamp(-dot, 0, 500)
                _playSoundOfBallHit("knock1", ipt, 120, 500)
            }

            //角度
            v[pA + MB_ANGLE] = (v[pA + MB_ANGLE] + v[pA + MB_SPIN] + UNIT_LOOP * 2) % UNIT_LOOP

            //重力
            ax = v[pA + MB_VX]
            ay = v[pA + MB_VY]
            V2_asAdd(ax, gravityX)
            //速度を制限する
            V2_clampMag(ax, maxVelocity)
            v[pA + MB_VX] = ax
            v[pA + MB_VY] = ay

            //座標更新
            v[pA + MB_X] = v[pA + MB_NX]
            v[pA + MB_Y] = v[pA + MB_NY]
            v[pA + MB_NX] += v[pA + MB_VX]
            v[pA + MB_NY] += v[pA + MB_VY]

            //画面端でループする
            @sys.getInfo .mapSize .dst(bx)
            V2_asMulScalar(bx, MAPTILE_SIZE * UNIT_POS)
            V2_set(ax, v[pA + MB_X], v[pA + MB_Y])

            @if `ax < -BA_RADxUNIT bl {
                v[pA + MB_X] += bx + BA_RADxUNIT * 2
                v[pA + MB_NX] = v[pA + MB_X]
            } .elif `ax > bx + BA_RADxUNIT bl {
                v[pA + MB_X] -= bx + BA_RADxUNIT * 2
                v[pA + MB_NX] = v[pA + MB_X]
            }

            @if `ay < -BA_RADxUNIT bl {
                v[pA + MB_Y] += by + BA_RADxUNIT * 2
                v[pA + MB_NY] = v[pA + MB_Y]
            } .elif `ay > by + BA_RADxUNIT bl {
                //画面下に入ると玉を消去する
                @se.play "Fall1" .opt(50, 100, 50)
                v[pA + MB_STATE] = BA_ST_DELETE
            }
        }

        //ピクチャを更新する
        @if state == BA_ST_HIDE bl {
            pid = PIC_BALL_BODY + iA
            hidePicture(pid)
            pid = PIC_BALL_SHADOW + iA
            hidePicture(pid)

        } .else bl {
            V2_set(ax,
                (v[pA + MB_X] + UNIT_POS / 4) / UNIT_POS,//振動を抑えたい
                (v[pA + MB_Y] + UNIT_POS / 4) / UNIT_POS
            )
            V2_asSub(ax, cameraX)
            cell = muldiv(v[pA + MB_ANGLE], BA_MAX_CELL, UNIT_LOOP) + pbBallPattern * BA_MAX_CELL + 1

            //色
            @if powerupTimer > 0 bl {
                //黄金色
                angA = 150
                angB = 100
            } .else bl {
                //通常
                angA = 100
                angB = 100
            }

            //レイヤー
            ipt = v[pA + MB_LAYER]

            //中層
            @if ipt == GIM_LAYER_LOW bl {
                _showBallPicture(LAY_BALL_LOW)
            } .elif ipt == GIM_LAYER_HI bl {
                _showBallPicture(LAY_BALL_HI)
            } .else bl {
                _showBallPicture(LAY_BALL_MID)
            }
        }
    }

    //玉同士の衝突
    @if `ballCount >= 2 && !COND_BALL_LOCK bl {
        @loop MAX_BALL .dst(iA) bl {
            getBallPtr(pA, iA)
            @if `v[pA + MB_STATE] != BA_ST_ENABLE bl {
                @continue
            }

            //ループ回数
            pid = MAX_BALL - (iA + 1)
            @loop pid .dst(iB) bl {
                iB += iA + 1

                getBallPtr(pB, iB)
                @if `v[pB + MB_STATE] != BA_ST_ENABLE bl {
                    @continue
                }

                //玉同士の衝突判定
                //ipt:衝突時間
                circleToCircle(ipt, ax, ay, bx, by, cx, cy, unusedV, unusedV,
                    v[pA + MB_X], v[pA + MB_Y], v[pA + MB_NX], v[pA + MB_NY], BA_RADxUNIT, BA_MASS,
                    v[pB + MB_X], v[pB + MB_Y], v[pB + MB_NX], v[pB + MB_NY], BA_RADxUNIT, BA_MASS
                )
                //衝突した
                @if ipt >= 0 bl {
                    //速度
                    V2_set(dx,
                        v[pA + MB_VX],
                        v[pA + MB_VY]
                    )
                    //反射
                    //ang:内積
                    pbBallReflection(angA,
                    iA, ipt, ax, ay,  cx,  cy, v[pB + MB_VX], v[pB + MB_VY], BA_FRI, BA_REF)
                    pbBallReflection(angB,
                    iB, ipt, bx, by, -cx, -cy, dx, dy, BA_FRI, BA_REF)
                    //衝突速度に応じて音が鳴る
                    ipt = max(clamp(-angA, 0, 1000), clamp(-angB, 0, 1000))
                    _playSoundOfBallHit("con2", ipt, 100, 300)
                }
            }
        }
    }

    //----------------------------
    //カメラの位置と、簡易判定用の範囲を更新する
    V2_set(ballAreaMinX, 9999, 9999)
    V2_set(ballAreaMaxX, -1, -1)
    V2_set(ax, -1, -1)
    
    @if `!COND_BALL_LOCK bl {
        @loop MAX_BALL .dst(iA) bl {
            getBallPtr(pA, iA)
            state = v[pA + MB_STATE]
            //ロックされた玉も対象に含む
            //でないとスクロールがガクつく
            //マルチボールを採用する場合はHIDEを使う
            @if `state != BA_ST_ENABLE && state != BA_ST_LOCK bl {
                @continue
            }
            //カメラの位置
            ax = 0
            ay = max(ay, (v[pA + MB_Y] + BA_CORRECT) / UNIT_POS)//振動を抑える為に補正している
            //簡易判定用の範囲を更新する
            $DIV = (UNIT_POS * MAPTILE_SIZE)
            //左上
            ballAreaMinX = min(ballAreaMinX, (v[pA + MB_X] - BA_RADxUNIT) / $DIV)
            ballAreaMinY = min(ballAreaMinY, (v[pA + MB_Y] - BA_RADxUNIT) / $DIV)
            //右下
            ballAreaMaxX = max(ballAreaMaxX, (v[pA + MB_X] + BA_RADxUNIT) / $DIV)
            ballAreaMaxY = max(ballAreaMaxY, (v[pA + MB_Y] + BA_RADxUNIT) / $DIV)
        }
        @if ax >= 0 bl {
            //画面の左上座標に変換する
            ax = 0
            ay -= WINDOW_HEIGHT / 2

            // スクロールをずらす
            ay -= pbScrollEx == TRUE ? SCROLL_SLIDE_EX : SCROLL_SLIDE

            //目標位置と現在位置の差
            V2_sub(bx, ax, cameraX)
            //差の絶対値が一定以上の場合、画面を寄せる
            @if `abs(by) > SCROLL_EXTRA bl {
                ay -= (by > 0 ? 1 : -1) * SCROLL_EXTRA
            //変化しない
            } .else bl {
                ay = cameraY
            }
            //スクロールをマップ内に収まるよう制限する
            @sys.getInfo .mapSize .dst(bx)
            ay = clamp(ay, 0, by * MAPTILE_SIZE - WINDOW_HEIGHT)

            //画面移動を滑らかにする
            ax = cameraX + (ax - cameraX) / SCROLL_EASE_DIV
            ay = cameraY + (ay - cameraY) / SCROLL_EASE_DIV
        
            //スクロールを更新する
            setScroll(ax, ay, 0)
        }
    }



    //----------------------------
    //ディスプレイ
    @if displayState > DI_ST_NONE bl {
        displayTimer += 1
        pseudoSwitchState(displayState,
            [DI_ST_NORMAL, bl {
                //ピクチャ生成
                @if displayTimer == 0 bl {
                    @pic.show [PIC_DISPLAY_BG] .pos(DISPLAY_X + 320, DISPLAY_Y) .topLeft dpHoriBgPicCmd
                    @pic.move [PIC_DISPLAY_BG] .relative .pos(-352, 0) .scale(0) .trans(0) .time(2) .keepBlend
                    @pic.strpic [PIC_DISPLAY] strDisplay .pos(DISPLAY_X + 320, DISPLAY_Y + 4) .topLeft .bold dpStrPicCmd
                }
                //移動
                @if `between(displayTimer, 12, 60) bl {
                    @pic.move [PIC_DISPLAY] .pos(DISPLAY_X + 160, DISPLAY_Y + 4) .top .time(1)
                }
                //消える
                @if displayTimer >= 150 bl {
                    @pic.move[PIC_DISPLAY_BG] .relative .pos(0, -20) .scale(0) .trans(100) .time(2) .keepBlend
                    @pic.move[PIC_DISPLAY] .relative .pos(0, -20) .scale(0) .trans(100) .time(2) .keepBlend
                    changeDisplayState(DI_ST_NONE)
                }
            },
            DI_ST_ZOOM, bl {
                //ピクチャ生成
                @if displayTimer == 0 bl {
                    @pic.show [PIC_DISPLAY_BG] .pos(DISPLAY_X + 320, DISPLAY_Y) .topLeft dpHoriBgPicCmd
                    @pic.move [PIC_DISPLAY_BG] .relative .pos(-352, 0) .scale(0) .trans(0) .time(2) .keepBlend
                    @pic.strpic [PIC_DISPLAY] strDisplay .pos(DISPLAY_X + 160, DISPLAY_Y + 4) .top .scale(0) .bold dpStrPicCmd
                }
                //移動
                @if displayTimer == 12 bl {
                    @pic.move [PIC_DISPLAY] .relative .pos(0, 0) .trans(0) .scale(100) .time(4)
                }
                //消える
                @if displayTimer >= 150 bl {
                    @pic.move[PIC_DISPLAY_BG] .relative .pos(0, -20) .scale(0) .trans(100) .time(2) .keepBlend
                    @pic.move[PIC_DISPLAY] .relative .pos(0, -20) .scale(0) .trans(100) .time(2) .keepBlend
                    changeDisplayState(DI_ST_NONE)
                }
            },
            DI_ST_LIQUID, bl {
                //ピクチャ生成
                @if displayTimer == 0 bl {
                    @pic.strpic [PIC_DISPLAY] strDisplay .pos(0 + 320, 240 / 2) .center .bold dpMiddleStrPicCmd
                    @pic.show [PIC_DISPLAY_BG] .pos(0 + 320, 240 / 2) .left .scale2(100, 200) dpHoriBgPicCmd
                    @pic.move [PIC_DISPLAY_BG] .relative .pos(-352, 0) .scale2(0, 0) .trans(0) .time(2) .keepBlend
                }
                //移動
                @if `between(displayTimer, 0, 60) bl {
                    @pic.move [PIC_DISPLAY] .pos(0 + 160, 240 / 2) .center .time(1)
                }

                //入力するまで消えない
                @if `displayTimer == 62 bl {
                    @if anyInput != 1 bl {
                        displayTimer = 62 - 1
                    }
                }
                //消える
                @if displayTimer > 62 bl {
                    @pic.move[PIC_DISPLAY_BG] .relative .pos(0, 4) .scale2(0, 0) .trans(100) .time(2) .keepBlend
                    @pic.move[PIC_DISPLAY] .relative .pos(0, 4) .scale(0) .trans(100) .time(2) .keepBlend

                    changeDisplayState(DI_ST_NONE)
                }
            },
            DI_ST_CENTER, bl {
                //ピクチャ生成
                @if displayTimer == 0 bl {
                    @pic.strpic [PIC_DISPLAY] strDisplay .pos(0 + 160, 240 / 2 + 8) .center .trans(100) .bold dpLargeStrPicCmd
                    @pic.move [PIC_DISPLAY] .relative .pos(0, -8) .center .scale(0) .trans(-100) .time(4) .keepBlend

                    //文章の行数によって可変する
                    //20px, padding 8px
                    @pic.getInfo [PIC_DISPLAY] .currentRect(0, 0, ax, ay) .cewh
                    V2_set(bx,
                        100,
                        muldiv(ay + 8, 100, 20)
                    )

                    @pic.show [PIC_DISPLAY_BG] .pos(-32, 240 / 2 + 8) .left .scale2(bx, by) .trans(100) dpHoriBgPicCmd
                    @pic.move [PIC_DISPLAY_BG] .relative .pos(0, -8) .scale2(0, 0) .trans(-100) .time(4) .keepBlend
                }
                //入力するまで消えない
                @if `displayTimer == 62 bl {
                    @if anyInput != 1 bl {
                        displayTimer = 62 - 1
                    }
                }
                //消える
                @if displayTimer > 62 bl {
                    @pic.move[PIC_DISPLAY_BG] .relative .pos(0, 4) .scale2(0, 0) .trans(100) .time(2) .keepBlend
                    @pic.move[PIC_DISPLAY] .relative .pos(0, 4) .scale(0) .trans(100) .time(2) .keepBlend

                    changeDisplayState(DI_ST_NONE)
                }
            },
            DI_ST_SELECT, bl {
                //ピクチャ生成
                @if displayTimer == 0 bl {
                    @pic.show [PIC_SELECT_BG] .pos(0 + 320, 240 / 2) .left .scale2(100, 300) dpHoriBgPicCmd
                    @pic.move [PIC_SELECT_BG] .relative .pos(-352, 0) .scale2(0, 0) .trans(0) .time(2) .keepBlend
                    @pic.strpic [PIC_SELECT] strDisplay .pos(0 + 320, 240 / 2) .center dpStrPicCmd

                    //変数初期化
                    displaySelect = 0
                }

                //移動
                @if `between(displayTimer, 0, 30) bl {
                    @pic.move [PIC_SELECT] .pos(0 + 160, 240 / 2) .center .time(1)
                }

                //入力するまで消えない
                @if `displayTimer == 32 bl {
                    //キャンセル
                    @if leftFlipperInput == 1 bl {
                        @se.play "Cancel1" .opt(100, 100, 50)
                        displaySelect = 0
                    //決定
                    } .elif rightFlipperInput == 1 bl {
                        @se.play "Decision2" .opt(80, 100, 50)
                        displaySelect = 1
                    } .else bl {
                        displayTimer = 32 - 1
                    }
                }

                //ピクチャを片付ける
                @if displayTimer == 42 bl {
                    @pic.move[PIC_SELECT_BG] .relative .pos(0, 4) .scale2(0) .trans(100) .time(2) .keepBlend
                    @pic.move[PIC_SELECT] .relative .pos(0, 4) .scale(0) .trans(100) .time(2) .keepBlend
                }

                //少し間をおいてから終わる
                @if displayTimer == 60 bl {
                    changeDisplayState(DI_ST_NONE)
                }
            } ]
        )
    }

    //----------------------------
    //スコアカウンター
    @if scoreDisplay < score bl {
        @if `!(sceneTimer % 2) && scoreStep > 0 bl { 
            scoreDisplay += min(score - scoreDisplay, scoreStep)

            @if scoreDisplay >= score bl {
                scoreStep = 0
                @se.play "money1" .opt(100, 100, 50)

            } .else bl {
                @se.play "money2" .opt(80, 80, 50)
            }

            bitOn(triggerFlg, SC_BIT_SCORE)
        }
    }

    //----------------------------
    //フラグ
    triggerSwitchState(triggerFlg,
    [SC_BIT_SCORE, bl {
        @pic.strpic[PIC_SCORE] __str("\c[2]\", scoreDisplay, " score") .pos(SCORE_X, SCORE_Y) .bottomRight .scale(120) .trans(0) .rgbs(200, 200, 200, 100) dpStrPicCmd
        @pic.move[PIC_SCORE] .relative .pos(0, 0) .scale(-20) .trans(0) .rgbs(100, 100, 100, 100) .time(1) .keepBlend
    },
    SC_BIT_BONUS, bl {
        @pic.strpic[PIC_BONUS] __str("\c[5]\", bonus, " bonus") .pos(BONUS_X, BONUS_Y) .bottomRight .scale(120) .trans(0) .rgbs(200, 200, 200, 100) dpStrPicCmd
        @pic.move[PIC_BONUS] .relative .pos(0, 0) .scale(-20) .trans(0) .rgbs(100, 100, 100, 100) .time(1) .keepBlend
    },
    SC_BIT_MULTI, bl {
        @pic.strpic[PIC_MULTIPLIER] __str("\c[7]x \", multiplier) .pos(MULTI_X, MULTI_Y) .bottomRight .scale(120) .trans(0) .rgbs(200, 200, 200, 100) dpStrPicCmd
        @pic.move[PIC_MULTIPLIER] .relative .pos(0, 0) .scale(-20) .trans(0) .rgbs(100, 100, 100, 100) .time(1) .keepBlend
    },
    SC_BIT_CHANCE, bl {
        @pic.strpic[PIC_CHANCE] __str("\c[9]\", chance, " c") .pos(CHANCE_X, CHANCE_Y) .bottomRight .scale(120) .trans(0) .rgbs(200, 200, 200, 100) dpStrPicCmd
        @pic.move[PIC_CHANCE] .relative .pos(0, 0) .scale(-20) .trans(0) .rgbs(100, 100, 100, 100) .time(1) .keepBlend
    },
    SC_BIT_TIMER, bl {
        //一時間経つと表示が変わる
        ipt = (gameTimer >= 60 * 60 * 60) ? 0 : 1
        getTimerString(ipt, __id(strLine), gameTimer)
        @pic.strpic[PIC_TIMER] __str("\c[0]\", strLine) .pos(TIMER_X, TIMER_Y) .topRight .scale(100) .trans(0) .bold dpStrPicCmd
    },
    SC_BIT_POWERUP, bl {
        ipt = powerupTimer / UNIT_FPS
        @if ipt <= 0 bl {
            hidePicture(PIC_POWERUP)
        } .else bl {
            @pic.strpic[PIC_POWERUP] __str("\c[2]\", ipt) .pos(POWERUP_X, POWERUP_Y) .topRight .scale(100) .trans(0) .bold dpStrPicCmd
        }
    },
    SC_BIT_TIMEBONUS, bl {
        ipt = timeBonusTimer / UNIT_FPS
        @if ipt <= 0 bl {
            hidePicture(PIC_TIMEBONUS)
        } .else bl {
            @pic.strpic[PIC_TIMEBONUS] __str("\", ipt) .pos(TIMEBONUS_X, TIMEBONUS_Y) .topRight .scale(100) .trans(0) .bold dpStrPicCmd
        }
    },
    SC_BIT_LOG, bl {
        //ログを消去する
        @if logCount > MAX_LOG bl {
            @while logCount > MAX_LOG bl {
                logCount -= 1
                strLog .popLine(strLine)
            }
            @pic.strpic [PIC_LOG] strLog .pos(LOG_X, LOG_Y + 12) .bottomRight dpStrPicCmd
            @pic.move [PIC_LOG] .relative .pos(0, -12) .scale(0) .trans(0) .time(1)
        } .else bl {
            @pic.strpic [PIC_LOG] strLog .pos(LOG_X, LOG_Y) .bottomRight dpStrPicCmd
        }
    },
    SC_BIT_ICON, bl {
        @if `enableIcon bl {
            @loop MAX_ICON .dst(iA) bl {
                pid = PIC_ICON + iA

                cell = 1 + iA
                V2_set(ax,
                    ICON_X,
                    ICON_Y + iA * ICON_HEIGHT
                )

                @if `(iconFlag & (1 << iA)) bl {
                    bx = 100
                } .else bl {
                    bx = 0
                }

                @pic.show[pid] .pos(ax, ay) .topLeft .trans(30) .cell(cell) .rgbs(bx, bx, bx, bx) dpIconPicCmd
            }
        }
    } ] )
}