x020195_g_scriptId = 020196

-- 玩家坐标
x020195_g_HostPos = {12.44,34.85, -0.03}
x020195_g_GuestPos = {58.44,35.02, 3.07}


x020195_g_StunBuff = 20254  -- 定身Buff 用于控制玩家开场时间内无法移动 30秒定身效果
x020195_g_BotId = 31001002  -- 测试怪物 策划配置该行
x020195_g_TotalTime = 90  -- 副本总时长

x020195_g_Host = 1 -- 主玩家ObjId
x020195_g_Guest = 2 -- 客玩家ObjId
x020195_g_State = 3
x020195_g_RoundTime = 4 -- 副本倒计时


function x020195_OnCopySceneOpen(scene)
    SetCopySceneParam(scene,x020195_g_Host,0)
    SetCopySceneParam(scene,x020195_g_State,0)
    SetCopySceneParam(scene,x020195_g_RoundTime,0)
end

function x020195_OnObjEnter(scene,objId)
    if (GetCopySceneParam(scene,x020195_g_Host) > 0) then
        SyncArenaMonsterHp(scene, GetCopySceneParam(scene,x020195_g_Host), GetCopySceneParam(scene, x020195_g_Guest))
        return
    end

    SetCopySceneParam(scene, x020195_g_Host, objId)
    --玩家初始化
    x020195_ResetPlayerState(scene, objId)
    RemoveUserAllCdTime(scene,objId)

    x020195_ResetRobot(scene)

    SetCopySceneParam(scene, x020195_g_State, 1)

    ArenaCountTime(scene, objId, 5)
    SyncArenaMonsterHp(scene, objId, GetCopySceneParam(scene, x020195_g_Guest))
end

function x020195_OnObjDie(scene,objId,rolebaseId,killerId)
    if GetCopySceneParam(scene, x020195_g_Host) == objId then
        x020195_ArenaEnd(scene, false)
    else
        x020195_ArenaEnd(scene, true)
    end
end

function x020195_OnCopySceneTick(scene, tickCount)
    local count = GetCopySceneParam(scene, x020195_g_RoundTime)
    count = count + 1
    SetCopySceneParam(scene, x020195_g_RoundTime, count)

    local state = GetCopySceneParam(scene, x020195_g_State)
    if state == 1 then
        if count > 5 then
            RemoveImpact(scene, GetCopySceneParam(scene, x020195_g_Host), x020195_g_StunBuff)
            RemoveImpact(scene, GetCopySceneParam(scene, x020195_g_Guest), x020195_g_StunBuff)
            LadderSelectTarget(scene, GetCopySceneParam(scene, x020195_g_Host), GetCopySceneParam(scene, x020195_g_Guest))
            SetCopySceneParam(scene, x020195_g_State, 2)
        end
    elseif state == 2 then
        if count > x020195_g_TotalTime then
            x020195_ArenaEnd(scene, false)
        end
    end

end

function x020195_ResetPlayerState(scene, objId)
    SendImpactToUnit(scene,objId,x020195_g_StunBuff)
    TestFullHp(scene,objId)
    RemoveCDTime(scene,objId)
    AutoCombat(scene,objId,true)
    SetScenePos(scene, objId, x020195_g_HostPos[1], x020195_g_HostPos[2], x020195_g_HostPos[3])
end

function x020195_ResetRobot(scene)
    local guest = GetCopySceneParam(scene, x020195_g_Guest)
    if guest > 0 then
        DeleMonsterByObjId(scene,guest)
    end

    MonId = CreateArenaRobot(scene, x020195_g_GuestPos[3],  x020195_g_GuestPos[1],  x020195_g_GuestPos[2])
    SetCopySceneParam(scene,x020195_g_Guest,MonId)
    SendImpactToUnit(scene,MonId,x020195_g_StunBuff)

    SetScenePos(scene, MonId, x020195_g_GuestPos[1], x020195_g_GuestPos[2], x020195_g_GuestPos[3])
end

function x020195_ArenaEnd(scene, win)
    if win then
        SetCopySceneSuccess(scene)
    else
        SetCopySceneFail(scene)
    end

    EndArenaBattle(scene, GetCopySceneParam(scene, x020195_g_Host), win)

    SetCopySceneParam(scene, x020195_g_State, 3)
end