-- Location - базовый класс локации (из пяти ХО сцен)

----------------------------------------------------------------------------------------------------
Location = {}

Location.list = {}

----------------------------------------------------------------------------------------------------
function Location:resetAll()
    table.foreach(Location.list,
        function(name)
            Location.list[name].needInit = true
        end)
end

----------------------------------------------------------------------------------------------------
function Location:get(aName)
    if Location.list[aName] == nil then
        Location.list[aName] = Location:new(aName)
    end

    return Location.list[aName]
end

----------------------------------------------------------------------------------------------------
function Location:new(aName)

    if Location.list[aName] ~= nil then
        return Location.list[aName]
    end

	local obj = {}
	setmetatable(obj, self)
	self.__index = self
	
    obj.mName = aName
    obj.mCurrentScene = 1
    obj.mSceneHandlers = {}
    obj.mSharedHids = {}

    Location.list[aName] = obj

    obj.needInit = true

	return obj
end

----------------------------------------------------------------------------------------------------
function Location:onShow()
    if not HOLevel:isExtraLevel() then
        if self.needInit then
            self:init()
        end
        self.mScenes[self.mCurrentScene]:enter()
    end
end

----------------------------------------------------------------------------------------------------
function Location:onHide()
    if not HOLevel:isExtraLevel() then
        self.mScenes[self.mCurrentScene]:leave()
    else
        HOLevelExtra.list[self.mName]:exit()
    end
end

----------------------------------------------------------------------------------------------------
function Location:update()
	return self.mScenes[self.mCurrentScene]:update()
end

----------------------------------------------------------------------------------------------------
function Location:onEvent(aScene, aEvent)
    tHandler = self.mSceneHandlers[aScene]
    if tHandler ~= nil then
        tHandler(self.mScenes[aScene], aEvent)
    end
end

----------------------------------------------------------------------------------------------------
function Location:setSceneHandler(aScene, aFunc)
    self.mSceneHandlers[aScene] = aFunc
end

----------------------------------------------------------------------------------------------------
function Location:init()

    self.mScenes = {}
    --self.mCurrentScene = 1    
    self.mExtras = {}
    self.mKeys = Profile:getKeys(self.mName)
    if self.mKeys == nil then
        self.mKeys = {false, false, false, false, false,
            false, false, false, false, false,
            false, false, false, false, false}
        Profile:setKeys(self.mName, self.mKeys)
    end
    self.mAmbients = {}

    for i = 1, 5 do
        self.mScenes[i] = {}
        self.mScenes[i] = HOLevel:new(self, i)
    	self.mScenes[i]:init()
    end
    self.needInit = false
end
