local drone_body = vci.assets.GetSubItem("drone_body")
local handle_R = vci.assets.GetSubItem("handle_R")
local drone_home = vci.assets.GetSubItem("drone_home")

local dummy_R = vci.assets.GetTransform("dummy_R")
local handle_target_R = vci.assets.GetTransform("handle_target_R")
local respawn_dummy = vci.assets.GetTransform("respawn_dummy")

local state_grab_R = false

local move_power = 0
local pre_move_power = 0
local pre_pre_move_power = 0

local offset_vec = Vector3.__new(0,0,0)
local pre_offset_vec = Vector3.__new(0,0,0)
local pre_pre_offset_vec = Vector3.__new(0,0,0)

local body_pos= drone_body.GetPosition()
local pre_body_pos= drone_body.GetPosition()
local pre_pre_body_pos= drone_body.GetPosition()
local pre_pre_pre_body_pos= drone_body.GetPosition()
local pre_pre_pre_pre_body_pos= drone_body.GetPosition()

local face_index = 0


function SetAtoAway(A)
    local A = vci.assets.GetSubItem(A)
    local pos = Vector3.__new(0,-100,0)
    if A ~= nil then
        -- 位置と回転の合わせ
        A.SetPosition(pos)
        A.SetVelocity(Vector3.zero)--rigitbodyにかかる速度をゼロに
        A.SetAngularVelocity(Vector3.zero)--rigitbodyにかかる回転をゼロに
    end
end
function SetAtoBPos(A,B)
    local A = vci.assets.GetSubItem(A)
    local B = vci.assets.GetTransform(B)

    if A ~= nil then
        if B~= nil then
        --追従
        A.SetLocalRotation(B.GetLocalRotation())
        A.SetPosition(B.GetPosition())

        A.SetVelocity(Vector3.zero)--rigitbodyにかかる速度をゼロに
        A.SetAngularVelocity(Vector3.zero)--rigitbodyにかかる回転をゼロに
        end
    end
end


function onGrab(target)
    if target == "handle_R" then
        state_grab_R = true
        
        -- 位置のリセット
        body_pos= drone_body.GetPosition()
        pre_body_pos= drone_body.GetPosition()
        pre_pre_body_pos= drone_body.GetPosition()
        pre_pre_pre_body_pos= drone_body.GetPosition()
        pre_pre_pre_pre_body_pos= drone_body.GetPosition()
    end
end

function onUse(use)
    if use =="drone_home" then
        body_pos= drone_body.GetPosition()
        pre_body_pos= drone_body.GetPosition()
        pre_pre_body_pos= drone_body.GetPosition()
        pre_pre_pre_body_pos= drone_body.GetPosition()
        pre_pre_pre_pre_body_pos= drone_body.GetPosition()

        handle_R.SetRotation(respawn_dummy.GetRotation())
        SetAtoBPos("drone_body","respawn_dummy")
        SetAtoBPos("handle_R","dummy_R")

        --顔変更
        face_index = face_index + 1
        if face_index >7 then
            face_index = 0
        end
        local x_index = face_index % 4
        local y_index = math.floor(face_index/4)

        local offset = Vector2.zero
        offset.y = -(1/4) * y_index
        offset.x = (1/4) * x_index
     
        vci.assets._ALL_SetMaterialTextureOffsetFromName("robo_face", offset)

    end
end


function onUngrab(target)
    if target == "handle_R" then
        state_grab_R = false

    end
end

function updateAll()
    --lookat
    drone_body.SetLocalRotation(handle_R.GetLocalRotation())

    if state_grab_R == true then
        --vci.assets.HapticPulseForceToRightController (10, 0.01)
        --vci.assets.HapticPulseForceToLeftController (10, 0.01)
        --オフセットで移動
        --オフセットベクトル取得
        pre_pre_offset_vec = pre_offset_vec
        pre_offset_vec = offset_vec
        offset_vec = handle_target_R.GetPosition() - dummy_R.GetPosition()

        local offset_distance = Vector3.Magnitude(offset_vec)
        local heikin_vec = Vector3.Normalize(pre_pre_offset_vec + pre_offset_vec + offset_vec)
        --local move_power =offset_distance


        pre_pre_move_power = pre_move_power
        pre_move_power = move_power
        move_power = offset_distance*0.25

        local heikin_move_power = (pre_pre_move_power + pre_move_power + move_power)/3
        --本体を移動
        pre_pre_pre_pre_body_pos = pre_pre_pre_body_pos
        pre_pre_pre_body_pos = pre_pre_body_pos
        pre_pre_body_pos = pre_body_pos
        pre_body_pos = body_pos
        body_pos = drone_body.GetPosition() + heikin_move_power * heikin_vec
        --print(heikin_move_power)
        drone_body.SetPosition((pre_pre_pre_pre_body_pos+pre_pre_pre_body_pos+pre_pre_body_pos+pre_body_pos+body_pos)/5)
    end
    if state_grab_R == false then
        --SetAtoBPos("handle_R","dummy_R")
        handle_R.SetPosition(dummy_R.GetPosition())

        handle_R.SetVelocity(Vector3.zero)--rigitbodyにかかる速度をゼロに
        handle_R.SetAngularVelocity(Vector3.zero)--rigitbodyにかかる回転をゼロに
    end
    
end
