Advertisement
Guest User

WIP LunaLua cutscene stuff

a guest
Oct 4th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.14 KB | None | 0 0
  1. --***********************************************************************************************************************
  2. --                                                          *
  3. --      THIS SECTION OF CODE BLATANTLY COPIED FROM http://www.mohiji.org/2012/12/14/lua-coroutines/     *
  4. --                                                          *
  5. --***********************************************************************************************************************
  6.  
  7. -- This table is indexed by coroutine and simply contains the time at which the coroutine
  8. -- should be woken up.
  9. WAITING_ON_TIME = {}
  10.  
  11. -- Keep track of how long the game has been running.
  12. local CURRENT_TIME = 0
  13.  
  14. function waitSeconds(seconds)
  15.     -- Grab a reference to the current running coroutine.
  16.     local co = coroutine.running()
  17.  
  18.     -- If co is nil, that means we're on the main process, which isn't a coroutine and can't yield
  19.     assert (co ~= nil, "The main thread cannot wait!")
  20.  
  21.     -- Store the coroutine and its wakeup time in the WAITING_ON_TIME table
  22.     local wakeupTime = CURRENT_TIME + seconds
  23.     WAITING_ON_TIME[co] = wakeupTime
  24.  
  25.     -- And suspend the process
  26.     return coroutine.yield (co)
  27. end
  28.  
  29. function wakeUpWaitingThreads (deltaTime)
  30.     -- This function should be called once per game logic update with the amount of time
  31.     -- that has passed since it was last called
  32.     CURRENT_TIME = CURRENT_TIME + deltaTime
  33.  
  34.     -- First, grab a list of the threads that need to be woken up. They'll need to be removed
  35.     -- from the WAITING_ON_TIME table which we don't want to try and do while we're iterating
  36.     -- through that table, hence the list.
  37.     local threadsToWake = {}
  38.     for co, wakeupTime in pairs (WAITING_ON_TIME) do
  39.         if wakeupTime < CURRENT_TIME then
  40.             table.insert (threadsToWake, co)
  41.         end
  42.     end
  43.  
  44.     -- Now wake them all up.
  45.     for _, co in ipairs (threadsToWake) do
  46.         WAITING_ON_TIME[co] = nil -- Setting a field to nil removes it from the table
  47.         coroutine.resume (co)
  48.     end
  49. end
  50.  
  51. function runCoroutine (func)
  52.     -- This function is just a quick wrapper to start a coroutine.
  53.     local co = coroutine.create (func)
  54.     return coroutine.resume (co)
  55. end
  56.  
  57.  
  58. --[[    DEMONSTRATION
  59.  
  60. runProcess (function ()
  61.     print ("Hello world. I will now astound you by waiting for 2 seconds.")
  62.     waitSeconds(2)
  63.     print ("Haha! I did it!")
  64. end)
  65.  
  66.  
  67. From the original author of this script:
  68. "And that’s it. Call wakeUpWaitingThreads from your game logic loop and you’ll be able to have a bunch of functions waking up after sleeping for some period of time.
  69.  
  70. Note: this might not scale to thousands of coroutines. You might need to store them in a priority queue or something at that point."
  71. --]]
  72.  
  73. --*******************************************************************************************************
  74. --                                                  *
  75. --      END OF COPYPASTA'D CODE                                 *
  76. --                                                  *
  77. --*******************************************************************************************************
  78.  
  79.  
  80.  
  81. --*******************************************************************************************************
  82. --                                                  *
  83. --      ACTOR FUNCTIONS                                     *
  84. --  I have no idea if I'm doing this right but let's hope y'all can make use of this stuff anyway   *
  85. --*******************************************************************************************************
  86.  
  87. function Actor:relativeX (posX)
  88.   return (posX - self.x)
  89. end
  90.  
  91. function Actor:relativeY (posY)
  92.   return (posY - self.y)
  93. end
  94.  
  95. function Actor:distanceX (posX)
  96.   return math.abs(self.relativeX (posX))
  97. end
  98.  
  99. function Actor:distanceY (posY)
  100.   return math.abs(self.relativeY (posY))
  101. end
  102.  
  103.  
  104.  
  105. function Actor:relativeActorX (actorObj)
  106.   return self.relativeX (actorObj.x)
  107. end
  108.  
  109. function Actor:distanceActorY (actorObj)
  110.   return self.relativeY (actorObj.y)
  111. end
  112.  
  113.  
  114.  
  115. function Actor:distanceActorX (actorObj)
  116.   return math.abs(self.relativeActorX (actorObj))
  117. end
  118.  
  119. function Actor:distanceActorY (actorObj)
  120.   return math.abs(self.relativeActorY (actorObj))
  121. end
  122.  
  123. function Actor:distanceActor (actorObj)
  124.   local xDist = distanceActorX (actorObj)
  125.   local yDist = distanceActorY (actorObj)
  126.  
  127.   local diagDist = math.sqrt ((xDist^2) + (yDist^2))
  128.   return diagDist
  129. end
  130.  
  131.  
  132.  
  133. function Actor:dirToX (posX)
  134.   if (self.relativeX (posX) > 0) then
  135.     return DIR_RIGHT
  136.   else
  137.     return DIR_LEFT
  138. end
  139.  
  140. function Actor:dirToActorX (actorObj)
  141.   return dirToX (actorObj.x)
  142. end
  143.  
  144.  
  145.  
  146. -- Movement stuff --
  147.  
  148. function Actor:breakMoveCoroutines ()
  149.   -- So I don't know just yet how I'd go about cleanly aborting coroutines; if it's not possible or not worth the trouble,
  150.   --   we can just go ahead and not bother with all that stuff.
  151. end
  152.  
  153. function Actor:walkDir (speed, dir)
  154.   self.speedX = speed * dir
  155. end
  156.  
  157. function Actor:walkDirWithBreak (speed, dir)
  158.   self.breakMoveCoroutines ()
  159.   self.walkDir (speed, dir)
  160. end
  161.  
  162. function Actor:walkLeft (speed)
  163.   self.walkDirWithBreak (speed, DIR_LEFT)
  164. end
  165.  
  166. function Actor:walkRight (speed)
  167.   self.walkDirWithBreak (speed, DIR_RIGHT)
  168. end
  169.  
  170. function Actor:walkForward (speed)
  171.   self.walkDirWithBreak (speed, self.direction)
  172. end
  173.  
  174. function Actor:walkDirX (speed, posX)
  175.   self.walkDirWithBreak (speed, self.dirToX(posX))
  176. end
  177.  
  178. function Actor:walkDirActorX (speed, actorObj)
  179.   self.walkDirWithBreak (speed, self.dirToActorX(actorObj))
  180. end
  181.  
  182.  
  183.  
  184. function Actor:walkToX (speed, posX)
  185.   self.breakMoveCoroutines ()
  186.  
  187.   runProcess (function ()
  188.     while (self.distanceX (posX) > 16)
  189.       self.walkDirX (posX, speed)
  190.       waitSeconds(0)
  191.     end
  192.    
  193.     self.walkForward (0)
  194.   end)
  195. end
  196.  
  197.  
  198. function Actor:walkToActorX (speed, actorObj)
  199.   self.walkToX (speed, actorObj.x)
  200. end
  201.  
  202.  
  203. function Actor:basicFollowActor (speed, actorObj, minGap)
  204.   -- This just covers walking to the target continuously, but assuming we can make coroutines work I might be
  205.   --   tempted to try some more sophisticated AI later because I can be kinda stupid like that
  206.  
  207.   self.breakMoveCoroutines ()
  208.  
  209.   runProcess (function ()
  210.     while (true)
  211.       if  (self.distanceActor (actorObj) > minGap)  then
  212.         self.walkDirActorX (speed, actorObj)
  213.       else
  214.         self.walkDirActorX (0, actorObj)
  215.       end
  216.       waitSeconds (0)
  217.     end
  218.    
  219.     self.walkForward (0)
  220.   end)
  221. end
  222.  
  223.  
  224.  
  225. function Actor:performJump (strength)
  226.   self.speedY = strength * -1
  227. end
  228.  
  229.  
  230.  
  231. -- Player member variables
  232.  
  233. Player:charindex = function ()
  234.   return self:mem (0xF0, FIELD_WORD)
  235. end
  236.  
  237.  
  238. Player:charNameSet = function (marioName, luigiName, peachName, toadName, linkName)
  239.   local indexVal = self.charindex
  240.  
  241.   if      indexVal == 0  then
  242.     return "NONE"
  243.   elseif  indexVal == 1  then
  244.     return marioName
  245.   elseif  indexVal == 2  then
  246.     return luigiName
  247.   elseif  indexVal == 3  then
  248.     return peachName
  249.   elseif  indexVal == 5  then
  250.     return linkName
  251.   else
  252.     return toadName
  253.   end
  254. end
  255.  
  256.  
  257. Player:charname = function ()
  258.   return self.charNameSet ("Mario", "Luigi", "Peach", "Toad", "Link")
  259. end
  260.  
  261. Player:charnameASXT = function ()
  262.   return self.charNameSet ("Demo", "Iris", "Kood", "raocow", "Sheath")
  263. end
  264.  
  265. Player:charnameASXT2 = function ()
  266.   return self.charNameSet ("Nevada", "Pily", "Alt P3", "Alt P4", "Broadsword")
  267. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement