-- ------------------------------------------------------------------------------------------------------------- -- -- TObject = {} function TObject:new(_param) _param =_param or {} setmetatable(_param, self) self.__index = self return _param end -- ------------------------------------------------------------------------------------------------------------- -- -- TState = TObject:new{ m_name = "unnamed", m_actors = {}, m_anims = {}, m_nextState = nil, -- Cuando se hace finish se salta a este subestado, si fuera nil el padre toma el control events = { onBegin = nil, onClick = nil, onChildFinished = nil, onAnimEnded = nil, onFinish = nil, }, superState = nil, } function TState:begin() log("begin on " .. self.m_name) self.m_nextState = nil if self.events.onBegin ~= nil then self.events.onBegin(self) end if not(#self.m_actors == 0 and #self.m_anims == 0) then -- Creamos los actores contenidos en m_actors for i,actor in pairs(self.m_actors) do log ("creo el actor: ".. actor.properties.name) if ExistsActor(actor.properties.name) then log("ya existe un actor con nombre " .. actor.properties.name) log(debug.traceback()) assert(false) else CreateActor(actor.actorType, actor.properties ) end end -- Asignamos las animaciones a los actores for i,animation in pairs(self.m_anims) do SetActorAnim(animation.actorName, animation.properties) --Enqueues para el onLoop log("setAnim en " .. animation.actorName .. " anim = " .. animation.properties.anim) if( not animation.properties.loop and g_currentState ~= nil and self.events.onAnimEnded ~= nil) then log("Enqueue en " .. animation.actorName) EnqueueExecuteScript(animation.actorName, [[ if ExistsActor("]] .. animation.actorName .. [[") then if g_currentState.events.onAnimEnded ~= nil then if g_currentState.events.onAnimEnded.]] .. animation.actorName .. [[ ~= nil then log("Existe ]] .. animation.actorName .. [[, y voy a su onAnimEnded propio") g_currentState.events.onAnimEnded.]] .. animation.actorName .. [[(g_currentState) g_currentState:changeCurrentState() elseif g_currentState.events.onAnimEnded.default ~= nil then log("Existe ]] .. animation.actorName .. [[, y voy a su onAnimEnded default") g_currentState.events.onAnimEnded.default(g_currentState,"]] .. animation.actorName .. [[") g_currentState:changeCurrentState() end end end ]] ) end end else self:changeCurrentState() end end function TState:pause() log("<<<<<<<<> " .. _nextState.m_name) self.m_nextState = _nextState if self.m_nextState ~= nil then self.m_nextState.superState = self.superState end end --devuelve el primer estado en la sucesion de padres del actual con el nombre que se pasa como parametro function TState:findParent(_parentName) local auxState = self.superState while auxState ~= nil do log("padre " .. auxState.m_name) if auxState.m_name == _parentName then return auxState else auxState = auxState.superState end end return auxState end function TState:changeCurrentState() if self.m_nextState ~= nil then -- Si el estado siguiente es el mismo estado en el que estoy, -- no finalizo ni hago transición a un estado hijo if self.m_name ~= self.m_nextState.m_name then -- Ejecuta estado hijo g_currentState = self.m_nextState self.m_nextState = nil g_currentState.superState = self g_currentState:begin() end else -- Finaliza el estado self:finish() end end --m_nextState es lo que controla que se cree un estado hijo o que se finalice el estado function TState:click(_nodeName) log("click node: " .. _nodeName .. " on " .. self.m_name) if (self.events.onClick ~= nil) then if(self.events.onClick[_nodeName]~=nil) then log("?????????????? hay onclick para " .. _nodeName) self.events.onClick[_nodeName](self) end --de momento lo dejo asi, se llama default si existe, luego de llamar al onClick del nodo. if(self.events.onClick.default~=nil) then log("?????????????? hay onclick para default") self.events.onClick.default(self) end --si no hay accion para el nodo ni default, no hago nada. if(self.events.onClick[_nodeName]==nil and self.events.onClick.default==nil) then log("??????????????no hay onclick para " .. _nodeName .. "ni default") return end self:changeCurrentState() end end function TState:finish() log("finish on " .. self.m_name) -- Llamada a onFinish en caso de existir if self.events.onFinish ~= nil then log("onFinish de ".. self.m_name) self.events.onFinish(self) end -- Desctruimos los actores contenidos en la propiedad 'm_toDestroyActors' for i,element in pairs(self.m_actors) do log ("destruyo el actor:".. element.properties.name) DestroyActor(element.properties.name) end if (self.superState ~= nil) then log("!!!salto al padre: self.superState " .. self.superState.m_name) g_currentState = self.superState g_currentState:changeCurrentState() else log(">>>>!!!FinishMenuState()") end end