Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --***********************************************************************************************************************
- -- *
- -- THIS SECTION OF CODE BLATANTLY COPIED FROM http://www.mohiji.org/2012/12/14/lua-coroutines/ *
- -- *
- --***********************************************************************************************************************
- -- This table is indexed by coroutine and simply contains the time at which the coroutine
- -- should be woken up.
- WAITING_ON_TIME = {}
- -- Keep track of how long the game has been running.
- local CURRENT_TIME = 0
- function waitSeconds(seconds)
- -- Grab a reference to the current running coroutine.
- local co = coroutine.running()
- -- If co is nil, that means we're on the main process, which isn't a coroutine and can't yield
- assert (co ~= nil, "The main thread cannot wait!")
- -- Store the coroutine and its wakeup time in the WAITING_ON_TIME table
- local wakeupTime = CURRENT_TIME + seconds
- WAITING_ON_TIME[co] = wakeupTime
- -- And suspend the process
- return coroutine.yield (co)
- end
- function wakeUpWaitingThreads (deltaTime)
- -- This function should be called once per game logic update with the amount of time
- -- that has passed since it was last called
- CURRENT_TIME = CURRENT_TIME + deltaTime
- -- First, grab a list of the threads that need to be woken up. They'll need to be removed
- -- from the WAITING_ON_TIME table which we don't want to try and do while we're iterating
- -- through that table, hence the list.
- local threadsToWake = {}
- for co, wakeupTime in pairs (WAITING_ON_TIME) do
- if wakeupTime < CURRENT_TIME then
- table.insert (threadsToWake, co)
- end
- end
- -- Now wake them all up.
- for _, co in ipairs (threadsToWake) do
- WAITING_ON_TIME[co] = nil -- Setting a field to nil removes it from the table
- coroutine.resume (co)
- end
- end
- function runCoroutine (func)
- -- This function is just a quick wrapper to start a coroutine.
- local co = coroutine.create (func)
- return coroutine.resume (co)
- end
- --[[ DEMONSTRATION
- runProcess (function ()
- print ("Hello world. I will now astound you by waiting for 2 seconds.")
- waitSeconds(2)
- print ("Haha! I did it!")
- end)
- From the original author of this script:
- "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.
- Note: this might not scale to thousands of coroutines. You might need to store them in a priority queue or something at that point."
- --]]
- --*******************************************************************************************************
- -- *
- -- END OF COPYPASTA'D CODE *
- -- *
- --*******************************************************************************************************
- --*******************************************************************************************************
- -- *
- -- ACTOR FUNCTIONS *
- -- I have no idea if I'm doing this right but let's hope y'all can make use of this stuff anyway *
- --*******************************************************************************************************
- function Actor:relativeX (posX)
- return (posX - self.x)
- end
- function Actor:relativeY (posY)
- return (posY - self.y)
- end
- function Actor:distanceX (posX)
- return math.abs(self.relativeX (posX))
- end
- function Actor:distanceY (posY)
- return math.abs(self.relativeY (posY))
- end
- function Actor:relativeActorX (actorObj)
- return self.relativeX (actorObj.x)
- end
- function Actor:distanceActorY (actorObj)
- return self.relativeY (actorObj.y)
- end
- function Actor:distanceActorX (actorObj)
- return math.abs(self.relativeActorX (actorObj))
- end
- function Actor:distanceActorY (actorObj)
- return math.abs(self.relativeActorY (actorObj))
- end
- function Actor:distanceActor (actorObj)
- local xDist = distanceActorX (actorObj)
- local yDist = distanceActorY (actorObj)
- local diagDist = math.sqrt ((xDist^2) + (yDist^2))
- return diagDist
- end
- function Actor:dirToX (posX)
- if (self.relativeX (posX) > 0) then
- return DIR_RIGHT
- else
- return DIR_LEFT
- end
- function Actor:dirToActorX (actorObj)
- return dirToX (actorObj.x)
- end
- -- Movement stuff --
- function Actor:breakMoveCoroutines ()
- -- 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,
- -- we can just go ahead and not bother with all that stuff.
- end
- function Actor:walkDir (speed, dir)
- self.speedX = speed * dir
- end
- function Actor:walkDirWithBreak (speed, dir)
- self.breakMoveCoroutines ()
- self.walkDir (speed, dir)
- end
- function Actor:walkLeft (speed)
- self.walkDirWithBreak (speed, DIR_LEFT)
- end
- function Actor:walkRight (speed)
- self.walkDirWithBreak (speed, DIR_RIGHT)
- end
- function Actor:walkForward (speed)
- self.walkDirWithBreak (speed, self.direction)
- end
- function Actor:walkDirX (speed, posX)
- self.walkDirWithBreak (speed, self.dirToX(posX))
- end
- function Actor:walkDirActorX (speed, actorObj)
- self.walkDirWithBreak (speed, self.dirToActorX(actorObj))
- end
- function Actor:walkToX (speed, posX)
- self.breakMoveCoroutines ()
- runProcess (function ()
- while (self.distanceX (posX) > 16)
- self.walkDirX (posX, speed)
- waitSeconds(0)
- end
- self.walkForward (0)
- end)
- end
- function Actor:walkToActorX (speed, actorObj)
- self.walkToX (speed, actorObj.x)
- end
- function Actor:basicFollowActor (speed, actorObj, minGap)
- -- This just covers walking to the target continuously, but assuming we can make coroutines work I might be
- -- tempted to try some more sophisticated AI later because I can be kinda stupid like that
- self.breakMoveCoroutines ()
- runProcess (function ()
- while (true)
- if (self.distanceActor (actorObj) > minGap) then
- self.walkDirActorX (speed, actorObj)
- else
- self.walkDirActorX (0, actorObj)
- end
- waitSeconds (0)
- end
- self.walkForward (0)
- end)
- end
- function Actor:performJump (strength)
- self.speedY = strength * -1
- end
- -- Player member variables
- Player:charindex = function ()
- return self:mem (0xF0, FIELD_WORD)
- end
- Player:charNameSet = function (marioName, luigiName, peachName, toadName, linkName)
- local indexVal = self.charindex
- if indexVal == 0 then
- return "NONE"
- elseif indexVal == 1 then
- return marioName
- elseif indexVal == 2 then
- return luigiName
- elseif indexVal == 3 then
- return peachName
- elseif indexVal == 5 then
- return linkName
- else
- return toadName
- end
- end
- Player:charname = function ()
- return self.charNameSet ("Mario", "Luigi", "Peach", "Toad", "Link")
- end
- Player:charnameASXT = function ()
- return self.charNameSet ("Demo", "Iris", "Kood", "raocow", "Sheath")
- end
- Player:charnameASXT2 = function ()
- return self.charNameSet ("Nevada", "Pily", "Alt P3", "Alt P4", "Broadsword")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement