xXm0dzXx

portal

Nov 3rd, 2012
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. local charX = 0
  2. local charY = 0
  3. local currentMap = ""; -- DO NOT CHANGE
  4. local maps = {
  5. ["map_0"] = {
  6. " XXX",
  7. " XBX",
  8. "XXXXXXXBX",
  9. "XZBBBBBBX",
  10. "XXXXXXXBX",
  11. " XBX",
  12. " XXX",
  13. }
  14. }
  15.  
  16. local colorSet = {
  17. ["X"] = colors.lightGray,
  18. ["B"] = colors.black,
  19. ["Z"] = colors.cyan,
  20. }
  21.  
  22. local spawnX = 0
  23. local spawnY = 0
  24. local justTeleported = false
  25.  
  26. local blockInfo = {
  27. }
  28.  
  29. function isPortalSurface( posX, posY )
  30. if isPortal( posX, posY ) == 1 or isPortal == 2 then
  31. return false
  32. elseif blockInfo[ posX.. "/" ..posY ] then
  33. return true
  34. end
  35. return false
  36. end
  37.  
  38. function isPortal( posX, posY )
  39. if portalBlueX == posX and portalBlueY == posY then
  40. return 0
  41. elseif portalRedX == posX and portalRedY == posY then
  42. return 1
  43. end
  44. return false
  45. end
  46.  
  47. function loadmap( map )
  48. term.setBackgroundColour( colors.gray )
  49. term.clear()
  50. local theMap = maps[map]
  51. for i=1,#theMap do
  52. for iv=1,#(theMap[i]) do
  53. local pixel = string.sub( theMap[i], iv, iv )
  54. if colorSet[pixel] then
  55. paintutils.drawPixel( 3+iv, 3+i, colorSet[pixel] )
  56. if map ~= currentMap then --If first time loading then...
  57. if pixel == "Z" then
  58. charX = 3+iv
  59. charY = 3+i
  60. elseif pixel == "X" then
  61. blockInfo[3+iv.. "/" ..3+i] = true
  62. end
  63. end
  64. end
  65. end
  66. end
  67.  
  68. if map ~= currentMap then
  69. currentMap = map
  70. end
  71. end
  72.  
  73. local portalBlueX = 0
  74. local portalBlueY = 0
  75.  
  76. local portalRedX = 0
  77. local portalRedY = 0
  78.  
  79. local portalTeleport = nil
  80. local gravity = nil
  81.  
  82. function redraw()
  83. loadmap( "map_0" )
  84. paintutils.drawPixel( portalBlueX, portalBlueY, colors.blue )
  85. paintutils.drawPixel( portalRedX, portalRedY, colors.red )
  86. term.setBackgroundColour( colors.black )
  87. term.setTextColour( colors.lime )
  88. term.setCursorPos( charX, charY )
  89. write("A")
  90. end
  91.  
  92. while true do
  93. redraw()
  94. if isPortal( charX, charY ) then
  95. if portalTeleport == nil then
  96. portalTeleport = os.startTimer( 0.5 )
  97. end
  98. elseif not isPortalSurface( charX, charY-1 ) then
  99. if portalTeleport == nil and gravity == nil then
  100. gravity = os.startTimer( 0.5 )
  101. justTeleported = false
  102. end
  103. end
  104.  
  105. local event, key, keyX, keyY = os.pullEvent()
  106. if event == "key" then
  107. oldCharX = charX
  108. oldCharY = charY
  109. if key == keys.left then
  110. charX = charX -1
  111. justTeleported = false
  112. elseif key == keys.right then
  113. charX = charX +1
  114. justTeleported = false
  115. end
  116.  
  117. if isPortalSurface( charX, charY ) then
  118. charX = oldCharX
  119. charY = oldCharY
  120. justTeleported = false
  121. end
  122. elseif event == "mouse_click" then
  123. if isPortalSurface( keyX, keyY ) then
  124. if key == 1 then
  125. portalBlueX = keyX
  126. portalBlueY = keyY
  127. elseif key == 2 then
  128. portalRedX = keyX
  129. portalRedY = keyY
  130. end
  131. end
  132. elseif event == "timer" and key == gravity then
  133. gravity = nil
  134. if isPortal( charX, charY ) ~= true then
  135. charY = charY +1
  136. end
  137. elseif event == "timer" and key == portalTeleport then
  138. if not justTeleported then
  139. local portalType = isPortal( charX, charY )
  140.  
  141. portalTeleport = nil
  142.  
  143. if portalType == 0 then
  144. charX = portalRedX
  145. charY = portalRedY
  146. elseif portalType == 1 then
  147. charX = portalBlueX
  148. charY = portalBlueY
  149. end
  150.  
  151. justTeleported = true
  152. end
  153.  
  154. portalTeleport = nil
  155. elseif event == "char" then
  156. if key == "r" or key == "R" then
  157. portalBlueX = 0
  158. portalBlueY = 0
  159.  
  160. portalRedX = 0
  161. portalRedY = 0
  162. end
  163. end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment