InksaverTutor

lib.SceneMgr.lua 20250914.1600

Sep 14th, 2025 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | Source Code | 0 0
  1. local version = 20250914.1600
  2. -- pastebin(1): 9u6ex4YP lib.SceneMgr.lua
  3. -- pastebin(2): YT0xET0n
  4. local Class = require("lib.Class")
  5. local Scene = require("lib.Scene")
  6. local SM = Class:derive("SceneMgr")
  7. --local U = require("lib.TurtleUtils") -- using _G.U loaded in tk3
  8. -- Only 1 SceneMgr is created in a project
  9. -- It conains all the scenes likely to be used
  10. function SM:new(sceneDir, scenes)
  11.     self.scenes = {}
  12.     if not sceneDir then sceneDir = "" end
  13.     self.sceneDir = sceneDir
  14.     if scenes ~= nil then
  15.         assert(type(scenes) == "table","parameter scenes must be a table")
  16.         for i = 1, #scenes do
  17.             local name = scenes[i]
  18.             local M = require(sceneDir.."."..scenes[i]) -- eg local M = require("scenes.Quit")
  19.             assert(M:is(Scene), "File: "..sceneDir.."."..scenes[i]..".lua is not of type Scene")
  20.             self.scenes[scenes[i]] = M(self)
  21.         end
  22.     end
  23.     -- string keys to scenes table
  24.     self.prevSceneName = nil
  25.     self.currentSceneName = nil
  26.     -- scene object
  27.     self.current = nil
  28. end
  29. -- adds a scene if an instance of a sub-class of Scene
  30. function SM:add(scene, sceneName)
  31.     if scene then
  32.         assert(sceneName ~= nil, "parameter sceneName must be specified")
  33.         assert(type(sceneName) == "string", "parameter sceneName must be a string")
  34.         assert(type(scene) == "table","parameter scene must be a table")
  35.         assert(scene:is(Scene), "Cannot add non-scene object to the scene manager")
  36.         assert(self.scenes[sceneName] == nil, "Scene named "..sceneName.." already exists")
  37.         self.scenes[sceneName] = scene
  38.     end
  39. end
  40.  
  41. function SM:control(sceneName)
  42.     return self.scenes[sceneName]
  43. end
  44.  
  45. function SM:getCurrentScene()
  46.     return self.current
  47. end
  48.  
  49. function SM:getSceneByName(name)
  50.     for k,v in pairs(self.scenes) do
  51.         if k == name then
  52.             return v
  53.         end
  54.     end
  55.    
  56.     return nil
  57. end
  58.  
  59. function SM:getCurrentSceneName()
  60.     return self.currentSceneName
  61. end
  62.  
  63. function SM:remove(sceneName)
  64.     if sceneName then
  65.         for k,v in pairs(self.scenes) do
  66.             if k == sceneName then
  67.                 self.scenes[k]:destroy()
  68.                 self.scenes[k] = nil
  69.                 if sceneName == self.currentSceneName then
  70.                     self.current = nil
  71.                 end
  72.                 break
  73.             end
  74.         end
  75.     end
  76. end
  77.  
  78. function SM:switch(nextSceneName, cmd)
  79.     U.clear(true)
  80.     --_G.Log:saveToLog("SM:switch("..nextSceneName..")")
  81.     if self.current then
  82.         self.current:exit()
  83.     end
  84.     if nextSceneName then
  85.         assert(self.scenes[nextSceneName] ~= nil, "Unable to find scene: "..nextSceneName)
  86.         self.prevSceneName = self.currentSceneName
  87.         self.currentSceneName = nextSceneName
  88.         self.current = self.scenes[nextSceneName]
  89.         --_G.Log:saveToLog("SM:self.current:enter()")
  90.         self.current:enter(cmd)
  91.         self.current:update()
  92.         self.current:draw()
  93.     end
  94. end
  95.  
  96. -- return to previous scene
  97. function SM:pop()
  98.     if self.prevSceneName then
  99.         self:switch(self.prevSceneName)
  100.         prevSceneName = nil
  101.     end
  102. end
  103.  
  104. function SM:getAvailableScenes()
  105.     local sceneNames = {}
  106.     for k,v in pairs(self.scenes) do
  107.         sceneNames[#sceneNames + 1] = k
  108.     end
  109.    
  110.     return sceneNames
  111. end
  112.  
  113. function SM:enter()
  114.    
  115. end
  116.  
  117. function SM:update(data)
  118.     local continue = false
  119.     if self.scenes["Dialog"] ~= nil then
  120.         if self.scenes["Dialog"]:isVisible() then
  121.             self.scenes["Dialog"]:update(data)
  122.         else
  123.             continue = true
  124.         end
  125.     else
  126.         continue = true
  127.     end
  128.  
  129.     if continue and self.current then
  130.         --_G.Log:saveToLog("SM:update(data): Scene = "..SM.getCurrentSceneName(self))   -- output = SM:update(data): table: 613bbe5c
  131.         self.current:update(data)   -- updates current Scene via Scene:update(data)
  132.     end
  133. end
  134.  
  135. function SM:draw()
  136.     term.setBackgroundColor(colors.black)
  137.     term.clear()
  138.     if self.current then
  139.         --_G.Log:saveToLog("SM:draw(): Scene = "..SM.getCurrentSceneName(self))
  140.         self.current:draw()
  141.     end
  142. end
  143.  
  144. return SM
  145.  
Advertisement
Add Comment
Please, Sign In to add comment