ユーザ用ツール

サイト用ツール


vci:sample:game:no1

ちゃんばら(ビートセイバーモック)

サンプルデータ:https://seed.online/items/9ad5d8a11736511382b7206a8b4b652015ca971c0d8a0191be5ded63172ecea3

main.lua
local scene = 0     -- 0:ready 1:game 2:end
 
local HIDE_POSITION = Vector3.__new(-1000,-1000,-1000)
 
-- ready
local BtnStart = vci.assets.GetSubItem("BtnStart")
local Title = vci.assets.GetSubItem("Title")
 
local BtnStartPos = BtnStart.GetLocalPosition()
local TitlePos = Title.GetLocalPosition()
 
-- end
local BtnTitle = vci.assets.GetSubItem("BtnTitle")
local Result = vci.assets.GetSubItem("Result")
local saber_numbers_100 = vci.assets.GetSubItem("saber_numbers_100")
local saber_numbers_10 = vci.assets.GetSubItem("saber_numbers_10")
local saber_numbers_1 = vci.assets.GetSubItem("saber_numbers_1")
local saber_numbers_percent = vci.assets.GetSubItem("saber_numbers_percent")
 
local BtnTitlePos = BtnTitle.GetLocalPosition()
local ResultPos = Result.GetLocalPosition()
local saber_numbers_100Pos = saber_numbers_100.GetLocalPosition()
local saber_numbers_10Pos = saber_numbers_10.GetLocalPosition()
local saber_numbers_1Pos = saber_numbers_1.GetLocalPosition()
local saber_numbers_percentPos = saber_numbers_percent.GetLocalPosition()
 
-- game
local saber = vci.assets.GetSubItem("saber")
local notes = {}
local current_note = 0
local current_stage_note_index = 1 --stage_noteの現在表示しているインデックス
local show_notes = {}
local notesDieList = {}
local notesColorAlphaList = {}
local score = 0
local noteTimer = vci.me.Time.TotalSeconds
for i = 1,9 do
    notes[i] = vci.assets.GetSubItem("note"..tostring(i))
    notesDieList[i] = 0
    notesColorAlphaList[i] = 1
end
local green = Color.__new(0,1,0,0.5)
local red = Color.__new(1,0,0,0.5)
local invisible = Color.__new(0,0,0,0)
local noteAppearPos = Vector3.__new(0,2,30)
local noteAppearPosList = {}
noteAppearPosList[1] = Vector3.__new(-0.5,0,0)
noteAppearPosList[2] = Vector3.__new(0,0,0)
noteAppearPosList[3] = Vector3.__new(0.5,0,0)
local noteSpeed = Vector3.__new(0,0,-0.1)
 
-- 各ステージの次のノート出現時間リスト(秒)
local stage1_notes = {
    1, 1, 0.5, 1, 1,
    1.5, 1.5, 0.5, 0.5, 0.5,
    1.5, 1.5, 0.5, 0.5, 0.5,
    1.5, 1.5, 0.5, 0.5, 0.5,
    1.5, 1.5, 0.5, 0.5, 0.5
}
local current_notes = {}
current_notes = {unpack(stage1_notes)}
 
-- 指定したシーン番号のオブジェクトを隠す
function hideSceneObject(sceneNumber)
    if sceneNumber == 0 then
        -- 0:ready
        BtnStart.SetLocalPosition(HIDE_POSITION)
        Title.SetLocalPosition(HIDE_POSITION)
    elseif sceneNumber == 1 then
        -- 1:game
        for i = 1,9 do
            vci.assets.GetSubItem("note"..tostring(i)).SetLocalPosition(noteAppearPos + noteAppearPosList[math.random(1,3)])
            vci.assets._ALL_SetMaterialColorFromName("note"..tostring(i), invisible)
        end
    elseif sceneNumber == 2 then
        -- 2:end
        BtnTitle.SetLocalPosition(HIDE_POSITION)
        Result.SetLocalPosition(HIDE_POSITION)
        saber_numbers_100.SetLocalPosition(HIDE_POSITION)
        saber_numbers_10.SetLocalPosition(HIDE_POSITION)
        saber_numbers_1.SetLocalPosition(HIDE_POSITION)
        saber_numbers_percent.SetLocalPosition(HIDE_POSITION)
    end
end
 
-- テクスチャ変更
function SetTextureOffset( _count, _materialName )
    local offset = Vector2.zero
 
    -- y shift
    local Yshift = math.floor(_count / 4)
    offset.y = -(1/4) * Yshift
 
    -- x shift
    local Xshift = _count % 4
    offset.x = (1/4) * Xshift
 
    vci.assets._ALL_SetMaterialTextureOffsetFromName(_materialName, offset)
end
 
-- スコアセットアップ
function setupScore( _score )
    if _score >= 100 then
        SetTextureOffset(1, "saber_numbers_100")
        SetTextureOffset(0, "saber_numbers_10")
        SetTextureOffset(0, "saber_numbers_1")
    elseif _score >= 10 then
        SetTextureOffset(15, "saber_numbers_100")
        local str10 = string.sub(tostring(_score), 1,1)
        SetTextureOffset(tonumber(str10), "saber_numbers_10")
        local str1 = string.sub(tostring(_score), 2)
        SetTextureOffset(tonumber(str1), "saber_numbers_1")
    else
        local str1 = string.sub(tostring(_score), 1)
        SetTextureOffset(15, "saber_numbers_100")
        SetTextureOffset(15, "saber_numbers_10")
        SetTextureOffset(tonumber(str1), "saber_numbers_1")
    end
    SetTextureOffset(10, "saber_numbers_percent")
end
 
-- シーンセットアップ
function setupScene( sceneNumber )
    if sceneNumber == 0 then
        -- 0:ready
        hideSceneObject( 1 )
        hideSceneObject( 2 )
 
        BtnStart.SetLocalPosition(BtnStartPos)
        Title.SetLocalPosition(TitlePos)
    elseif sceneNumber == 1 then
        -- 1:game
        score = 0
        current_notes = {unpack(stage1_notes)}
        current_stage_note_index = 1
        hideSceneObject( 0 )
        hideSceneObject( 2 )
    elseif sceneNumber == 2 then
        -- 2:end
        hideSceneObject( 0 )
        hideSceneObject( 1 )
 
        local resultScore = math.floor(score / #current_notes * 100)
        print("resultScore is "..tostring(resultScore))
        setupScore(resultScore)
 
        BtnTitle.SetLocalPosition(BtnTitlePos)
        Result.SetLocalPosition(ResultPos)
        saber_numbers_100.SetLocalPosition(saber_numbers_100Pos)
        saber_numbers_10.SetLocalPosition(saber_numbers_10Pos)
        saber_numbers_1.SetLocalPosition(saber_numbers_1Pos)
        saber_numbers_percent.SetLocalPosition(saber_numbers_percentPos)
    end
    scene = sceneNumber
end
 
 
function updateAll()
    if scene == 1 then
        if current_stage_note_index <= #current_notes then
            if vci.me.Time.TotalSeconds - noteTimer > current_notes[current_stage_note_index] then
                --print(current_stage_note_index)
 
                noteTimer = vci.me.Time.TotalSeconds -- noteTimer更新
                --
                if current_note < 9 then
                    current_note = current_note + 1
                else
                    current_note = 1
                end
                table.insert(show_notes, current_note) -- show_notes末尾に追加
                --
                local noteLength = #show_notes
                notes[show_notes[noteLength]].SetLocalPosition(noteAppearPos + noteAppearPosList[math.random(1,3)])
                vci.assets._ALL_SetMaterialColorFromName("note"..tostring(show_notes[noteLength]), green)
                --
                current_stage_note_index = current_stage_note_index + 1
            end
        end
 
        if #show_notes > 0 then
            for key, value in pairs(show_notes) do
                if notesDieList[value] < 1 then
                    notes[value].SetLocalPosition(notes[value].GetLocalPosition() + noteSpeed)
                else
                    -- 切った
                    notesColorAlphaList[value] = notesColorAlphaList[value] - 0.05
                    if notesColorAlphaList[value] > 0 then
                        -- 切ったあとのフェードアウト処理
                        local color = red * Color.__new(1,1,1,notesColorAlphaList[value])
                        vci.assets._ALL_SetMaterialColorFromName("note"..tostring(value), color)
                    else
                        -- リセット
                        notes[value].SetLocalPosition(noteAppearPos + noteAppearPosList[math.random(1,3)])
                        vci.assets._ALL_SetMaterialColorFromName("note"..tostring(value), invisible)
                        notesColorAlphaList[value] = 1
                        notesDieList[value] = 0
 
                        -- show_notesから削除
                        table.remove(show_notes, key) -- show_notes末尾に追加
 
                        if #show_notes == 0 then
                            setupScene(2)
                        end
                    end
                end
 
                if notes[value].GetLocalPosition().z <= -3 then
                    -- 空振りリセット
                    notes[value].SetLocalPosition(noteAppearPos + noteAppearPosList[math.random(1,3)])
                    vci.assets._ALL_SetMaterialColorFromName("note"..tostring(value), invisible)
 
                    -- show_notesから削除
                    table.remove(show_notes, key) -- show_notes末尾に追加
 
                    if #show_notes == 0 then
                        setupScene(2)
                    end
                end
            end
        end
    end
end
 
function onTriggerEnter(item,hit)
    -- ready
    if item == "saber" and hit == "BtnStart" then
        setupScene(1)
    end
    -- game
    for i = 1,9 do
        local note_name = "note"..tostring(i)
        if item == "saber" and hit == note_name then
            -- 切った
            score = score + 1
            --print("score is "..tostring(score))
            notesDieList[i] = 1
            vci.assets._ALL_SetMaterialColorFromName(note_name, red)
 
            -- サウンド再生
            vci.assets._ALL_PlayAudioFromName("shot-struck1")
        end
    end
    -- end
    if item == "saber" and hit == "BtnTitle" then
        setupScene(0)
    end
end
 
setupScene(0)
vci/sample/game/no1.txt · 最終更新: 2019/05/27 20:54 by navana

ページ用ツール