gamestate.lua 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. --[[
  2. Copyright (c) 2010-2013 Matthias Richter
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. Except as contained in this notice, the name(s) of the above copyright holders
  12. shall not be used in advertising or otherwise to promote the sale, use or
  13. other dealings in this Software without prior written authorization.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ]]--
  22. local function __NULL__() end
  23. -- default gamestate produces error on every callback
  24. local state_init = setmetatable({leave = __NULL__},
  25. {__index = function() error("Gamestate not initialized. Use Gamestate.switch()") end})
  26. local stack = {state_init}
  27. local GS = {}
  28. function GS.new(t) return t or {} end -- constructor - deprecated!
  29. function GS.switch(to, ...)
  30. assert(to, "Missing argument: Gamestate to switch to")
  31. assert(to ~= GS, "Can't call switch with colon operator")
  32. local pre = stack[#stack]
  33. ;(pre.leave or __NULL__)(pre)
  34. ;(to.init or __NULL__)(to)
  35. to.init = nil
  36. stack[#stack] = to
  37. return (to.enter or __NULL__)(to, pre, ...)
  38. end
  39. function GS.push(to, ...)
  40. assert(to, "Missing argument: Gamestate to switch to")
  41. assert(to ~= GS, "Can't call push with colon operator")
  42. local pre = stack[#stack]
  43. ;(to.init or __NULL__)(to)
  44. to.init = nil
  45. stack[#stack+1] = to
  46. return (to.enter or __NULL__)(to, pre, ...)
  47. end
  48. function GS.pop(...)
  49. assert(#stack > 1, "No more states to pop!")
  50. local pre = stack[#stack]
  51. stack[#stack] = nil
  52. ;(pre.leave or __NULL__)(pre)
  53. return (stack[#stack].resume or __NULL__)(pre, ...)
  54. end
  55. function GS.current()
  56. return stack[#stack]
  57. end
  58. local all_callbacks = {
  59. 'draw', 'errhand', 'focus', 'keypressed', 'keyreleased', 'mousefocus',
  60. 'mousepressed', 'mousereleased', 'quit', 'resize', 'textinput',
  61. 'threaderror', 'update', 'visible', 'gamepadaxis', 'gamepadpressed',
  62. 'gamepadreleased', 'joystickadded', 'joystickaxis', 'joystickhat',
  63. 'joystickpressed', 'joystickreleased', 'joystickremoved'
  64. }
  65. function GS.registerEvents(callbacks)
  66. local registry = {}
  67. callbacks = callbacks or all_callbacks
  68. for _, f in ipairs(callbacks) do
  69. registry[f] = love[f] or __NULL__
  70. love[f] = function(...)
  71. registry[f](...)
  72. return GS[f](...)
  73. end
  74. end
  75. end
  76. -- forward any undefined functions
  77. setmetatable(GS, {__index = function(_, func)
  78. return function(...)
  79. return (stack[#stack][func] or __NULL__)(stack[#stack], ...)
  80. end
  81. end})
  82. return GS