function OnSceneLoad(screen)
    LoadData(screen)

    local pf = play_field
    local level = pf:GetLevelScreen()
    custom_cursors = {}
    locked_objects = {}
    
    SetContinueMode(2)                                              -- миниигру нужно заканчивать вручную
        
    g_screen = screen
    g_timer = screen:GetTimer()
    
    left_scale = screen:GetObject('left')
    src = screen:GetObject('area_src')
    right_weight = 15.0
            
    screen:IgnoreOnTestPoint('area_dest')   -- to have an ability to take weights from scale
    
    screen:IgnoreOnTestPoint('left2')
    screen:IgnoreOnTestPoint('background')
    screen:IgnoreOnTestPoint('gear')
    screen:IgnoreOnTestPoint('right_scale')
    screen:IgnoreOnTestPoint('beam')
    screen:IgnoreOnTestPoint('left')
    screen:IgnoreOnTestPoint('coin_take')
    screen:IgnoreOnTestPoint('coin_blink')
    screen:IgnoreOnTestPoint('area_src')
    screen:IgnoreOnTestPoint('area_weight1')
    screen:IgnoreOnTestPoint('area_weight2')
    screen:IgnoreOnTestPoint('area_weight3')
    screen:IgnoreOnTestPoint('area_weight4')
    screen:IgnoreOnTestPoint('area_weight5')
    screen:IgnoreOnTestPoint('area_weight6')
    
    gear_anim = screen:GetObject('gear'):GetAnimation()
    gear_anim:Stop()
    
    CreateObjects()
    AttachItems()
    TurnBalance(-right_weight, 1)                       -- initial not balanced
end

function CreateObjects()
    OBJECTS_AMOUNT = 6
    Objects = {}
    for i = 1, OBJECTS_AMOUNT do
        Objects[i] = {}
        Objects[i].Index = i
        Objects[i].Entity = g_screen:GetObject('weight' .. i)
        Objects[i].SrcPos = Vector(Objects[i].Entity:GetScrX(), Objects[i].Entity:GetScrY())   -- Pos on floor
        Objects[i].DestArea = g_screen:GetObject('area_weight' .. i)   -- Area to link on left scale
        --Objects[i].Weight = i + 1
        Objects[i].OnScale = false
    end
    Objects[1].Weight = 6.0
    Objects[2].Weight = 5.0
    Objects[3].Weight = 4.0
    Objects[4].Weight = 3.0
    Objects[5].Weight = 2.0
    Objects[6].Weight = 1.0
end

function AttachItems()
    AttachInPlace(g_screen:GetObject('left2'), left_scale)
    AttachInPlace(g_screen:GetObject('area_dest'), left_scale)    
    AttachInPlace(left_scale, g_screen:GetObject('beam'))
    AttachInPlace(g_screen:GetObject('right_scale'), g_screen:GetObject('beam'))
    for i = 1, OBJECTS_AMOUNT do
        AttachInPlace(Objects[i].Entity, src)
        AttachInPlace(Objects[i].DestArea, left_scale)
    end
end

function GetObjectByName(obj_name)
    for i = 1, OBJECTS_AMOUNT do
        if obj_name == Objects[i].Entity:GetName() then
            return Objects[i]
        end
    end

    return nil
end

function AttachInPlace(object, parentObject)
    local scrX, scrY = object:GetScrX(), object:GetScrY()
	local z = object:GetScrPosZ() - parentObject:GetScrPosZ()
	object:SetZ(z)
	object:AttachTo(parentObject)
	object:SetScrXY(scrX, scrY)
end

function TurnBalance(weight, _time)
    _time = _time or 0.5
    g_screen:GetObject('beam'):Rotate(- math.rad(1.2 * weight), _time)
    g_screen:GetObject('right_scale'):Rotate(math.rad(1.2 * weight), _time)
    left_scale:Rotate(math.rad(1.2 * weight), _time)
    
    local rand = math.random(1, 3)
    PlaySound('06_mg_move_'..rand)
end

function GetLeftScaleWeight()
    local w = 0
    for i = 1, OBJECTS_AMOUNT do
        if Objects[i].OnScale then
            w = w + Objects[i].Weight
        end
    end
    
    return w
end

function IsCompleted()
    if GetLeftScaleWeight() ~= right_weight then
        return false
    end
    
    TimerDoString('FinishMiniGame()', 0.5, g_timer)
    return true
end

function FinishMiniGame()
    gear_anim:SetMode(0)                               -- due to HGE Animation needs to be initialized
    gear_anim:Play()
    PlaySound("06_mg_open")
    
    if DragObject then
        AttachInPlace(DragObject.Entity, src); 
        Objects[DragObject.Index].Entity:Move(DragObject.SrcPos.x - DragObject.Entity:GetScrX(), DragObject.SrcPos.y - DragObject.Entity:GetScrY(), 0.4);
        DragObject.OnScale = false;
        DragObject = nil
        DragDeltaX = nil
        DragDeltaY = nil
    end
    
    g_screen:GetObject("coin_blink"):SetOpacity(0.0)
    local fin_anim = CreateAnimationEffector()
    fin_anim:AttachTo(g_screen:GetObject("coin_blink"))
    fin_anim:SetMode(_ANIM_FWD)
    
    fin_anim:SetKey(_TRACK_O, 0, 0)
    fin_anim:SetKey(_TRACK_O, 0.4, 1)
    fin_anim:SetKey(_TRACK_O, 0.8, 0)
    fin_anim:SetKey(_TRACK_O, 1.2, 1)
    fin_anim:SetKey(_TRACK_O, 1.6, 0)
    fin_anim:Play()
    
    --[[TimerDo(g_timer, 1.6,   function()
                                if 
                            end)]]
    TimerDoString('g_screen:DoNotIgnoreOnTestPoint("coin_take")', 1.6, g_timer)
    MiniGameCompleted()
end

------------------------------------------------------------------------
-- Drag functions
function StartDrag(obj)
    DragObject = obj
    AttachInPlace(DragObject.Entity, src);  -- always link dragged object to its initial pos
    DragObject.Entity:SetZ(40 + DragObject.Index);
    DragDeltaX = GetMousePos().x - DragObject.Entity:GetX()
    DragDeltaY = GetMousePos().y - DragObject.Entity:GetY()
end

function UpdateDrag()
    if DragObject then
        local mouse_x = GetMousePos().x - DragDeltaX
        local mouse_y = GetMousePos().y - DragDeltaY
        DragObject.Entity:SetXY(mouse_x, mouse_y)
    end
end

function StopDrag()
    if DragObject then
        DragObject.Entity:SetZ(10 + DragObject.Index);
        g_screen:DoNotIgnoreOnTestPoint('area_dest')    -- temporary do this to pass hit test on scale
        local obj = g_screen:FindObjectByPos(Vector(DragObject.Entity:GetScrX(), DragObject.Entity:GetScrY()));
        g_screen:IgnoreOnTestPoint('area_dest')
        if obj and obj:GetName() == 'area_dest' then
            AttachInPlace(DragObject.Entity, left_scale);
            PlaySound("06_mg_weight_set")
            DragObject.Entity:Move(DragObject.DestArea:GetScrX() - DragObject.Entity:GetScrX(), DragObject.DestArea:GetScrY() - DragObject.Entity:GetScrY(), 0.1);
            if not DragObject.OnScale then
                TurnBalance(DragObject.Weight);
            end
            DragObject.OnScale = true;
        else
            AttachInPlace(DragObject.Entity, src); 
            Objects[DragObject.Index].Entity:Move(DragObject.SrcPos.x - DragObject.Entity:GetScrX(), DragObject.SrcPos.y - DragObject.Entity:GetScrY(), 0.4);
            if DragObject.OnScale then
                TurnBalance(-DragObject.Weight);
            end
            DragObject.OnScale = false;
        end
        DragObject = nil
        DragDeltaX = nil
        DragDeltaY = nil
        IsCompleted();
    end
end
------------------------------------------------------------------------

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_BALANCE')
end

function OnUpdate(dt)
    DefaultOnUpdate(dt);
    if DragObject then
        UpdateDrag();
    end
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local pf = play_field
    local is_wrong_click = true

--  is_wrong_click = DefaultOnObjectMouseDown(object);
    
    local obj = GetObjectByName(obj_name)
    if not game_state.minigame_complete then
        if obj then
            StartDrag(obj)
        end
    else
        if obj_name == 'coin_take' then
            g_screen:GetObject('coin_blink'):Hide()
            g_screen:IgnoreOnTestPoint('coin_take')
            local ef = CreateAnimationEffector();
            ef:AttachTo(object)
            ef:SetMode(_ANIM_FWD)
                
            ef:SetKey(_TRACK_O, 0, object:GetOpacity())
            ef:SetKey(_TRACK_O, 0.75, 0)
                                
            ef:Play()
            TimerDo(g_timer, 0.8, OnCompletedGame)
        end
    end
    
    return is_wrong_click;
end

function OnObjectMouseUp(object)
    if DragObject and not game_state.minigame_complete then
        StopDrag();
    end
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object);
end

function OnObjectReachPanel(object)
end

function OnSkipMiniGame()
    if game_state.minigame_complete then
        return
    end
    
    local weight_to_rot = 0;
    local time = 0.5;
        
    for i = 1, OBJECTS_AMOUNT do
        if not Objects[i].OnScale and (Objects[i].Weight == 4.0 or Objects[i].Weight == 5.0 or Objects[i].Weight == 6.0) then
            AttachInPlace(Objects[i].Entity, left_scale);
            
            local ef = CreateAnimationEffector();
            ef:AttachTo(Objects[i].Entity);
            ef:SetMode(_ANIM_FWD);
            
            ef:SetKey(_TRACK_O, 0, Objects[i].Entity:GetOpacity());
            ef:SetKey(_TRACK_X, 0, Objects[i].Entity:GetX());
            ef:SetKey(_TRACK_Y, 0, Objects[i].Entity:GetY());
            
            ef:SetKey(_TRACK_O, 0.2, 0);
            ef:SetKey(_TRACK_X, 0.2, Objects[i].Entity:GetX());
            ef:SetKey(_TRACK_Y, 0.2, Objects[i].Entity:GetY());
            
            ef:SetKey(_TRACK_O, 0.3, 0);
            ef:SetKey(_TRACK_X, 0.3, Objects[i].DestArea:GetX());
            ef:SetKey(_TRACK_Y, 0.3, Objects[i].DestArea:GetY());
            
            ef:SetKey(_TRACK_O, time, 1);
            ef:Play();

            Objects[i].OnScale = true;
            weight_to_rot = weight_to_rot + Objects[i].Weight;
        elseif Objects[i].OnScale and (Objects[i].Weight == 1.0 or Objects[i].Weight == 2.0 or Objects[i].Weight == 3.0) then
            AttachInPlace(Objects[i].Entity, src);
            
            local ef = CreateAnimationEffector();
            ef:AttachTo(Objects[i].Entity);
            ef:SetMode(_ANIM_FWD);
            
            ef:SetKey(_TRACK_O, 0, Objects[i].Entity:GetOpacity());
            ef:SetKey(_TRACK_X, 0, Objects[i].Entity:GetX());
            ef:SetKey(_TRACK_Y, 0, Objects[i].Entity:GetY());
            
            ef:SetKey(_TRACK_O, 0.2, 0);
            ef:SetKey(_TRACK_X, 0.2, Objects[i].Entity:GetX());
            ef:SetKey(_TRACK_Y, 0.2, Objects[i].Entity:GetY());
            
            local dx = Objects[i].SrcPos.x - Objects[i].Entity:GetScrX();
            local dy = Objects[i].SrcPos.y - Objects[i].Entity:GetScrY();
            
            ef:SetKey(_TRACK_O, 0.3, 0);
            ef:SetKey(_TRACK_X, 0.3, Objects[i].Entity:GetX() + dx);
            ef:SetKey(_TRACK_Y, 0.3, Objects[i].Entity:GetY() + dy);
            
            ef:SetKey(_TRACK_O, time, 1);
            ef:Play();
            
            Objects[i].OnScale = false;
            weight_to_rot = weight_to_rot - Objects[i].Weight;
        end
    end
    TimerDo(g_timer, time, TurnBalance, weight_to_rot)
    TimerDo(g_timer, time, IsCompleted)
    game_state.minigame_complete = true
end

function OnButtonPress(title)
    DefaultMinigameOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnMinigameCompleted()
end

function OnLostFocus()
    DefaultMinigameOnLostFocus()
end

function OnGainFocus()
    DefaultMinigameOnGainFocus()
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/dverca.png',0,0,216,78,8,8)
	anim:SetHotSpot(108,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('gear',anim,496,687,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/pravo.png',0,0,210,367,1,0)
	anim:SetHotSpot(101,4)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('right_scale',anim,706,196,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/koromislo.png',0,0,428,110,1,0)
	anim:SetHotSpot(216,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('beam',anim,508,126,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/levo1.png',0,0,211,369,1,0)
	anim:SetHotSpot(112,10)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('left',anim,310,202,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/levo2.png',0,0,207,167,1,0)
	anim:SetHotSpot(103,83)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('left2',anim,302,477,130,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia2.png',0,0,65,118,1,0)
	anim:SetHotSpot(32,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('weight1',anim,886,692,112,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia4.png',0,0,65,112,1,0)
	anim:SetHotSpot(32,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('weight2',anim,239,698,114,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia5.png',0,0,69,99,1,0)
	anim:SetHotSpot(34,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('weight3',anim,347,700,115,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia1.png',0,0,60,94,1,0)
	anim:SetHotSpot(30,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('weight4',anim,832,703,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia3.png',0,0,57,87,1,0)
	anim:SetHotSpot(28,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('weight5',anim,937,720,113,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia6.png',0,0,57,68,1,0)
	anim:SetHotSpot(28,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('weight6',anim,293,727,116,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia2.png',0,0,65,118,1,0)
	anim:SetHotSpot(32,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_weight1',anim,330,493,92,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia4.png',0,0,65,112,1,0)
	anim:SetHotSpot(32,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_weight2',anim,256,499,94,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia5.png',0,0,69,99,1,0)
	anim:SetHotSpot(34,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_weight3',anim,290,508,95,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia1.png',0,0,60,94,1,0)
	anim:SetHotSpot(30,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_weight4',anim,369,505,91,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia3.png',0,0,57,87,1,0)
	anim:SetHotSpot(28,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_weight5',anim,346,518,93,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/gantelia6.png',0,0,57,68,1,0)
	anim:SetHotSpot(28,34)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_weight6',anim,239,523,96,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_dest',anim,304,453,131,0,25,25)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_src',anim,520,747,99,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/coin.png',0,0,76,49,1,0)
	anim:SetHotSpot(38,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('coin_take',anim,509,671,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/coin.png',0,0,76,49,1,0)
	anim:SetHotSpot(38,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('coin_blink',anim,509,671,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/6_rock_skull_day_balance/L_koromislo.png',0,0,44,59,1,0)
	anim:SetHotSpot(22,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('L_koromislo',anim,509,110,105,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
