Advertisement
SpaceRanger4321

Game1 OS

Jan 9th, 2020
1,453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. -- SpaceOS Game = Platformer --
  2.  
  3. -- *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** --
  4. -- FUNCTIONS --
  5. local w,h = term.getSize()
  6. function valid(value,expected) if type(value) == expected then return true else return false end end
  7. function clr() term.clear() end
  8. function cp(x,y) if not valid(x,"number") then return error("Expected x to be a number, got "..type(x)) end if not valid(y,"number") then return error("Expected y to be a number, got "..type(y)) end term.setCursorPos(x,y) end
  9. function setText(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setTextColor(colors[col]) end end
  10. function setBack(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setBackgroundColor(colors[col]) end end
  11. function notnil(vari) return vari ~= nil end
  12. function lt(tab) if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end local lt = 0 for i=1, #tab do if #tab[i] > lt then lt = #tab end end return lt end
  13. function pullEvents(...) -- for pulling multiple events at a time, first event get priority
  14. local args = {...}
  15. --if #args == 0 then return os.pullEvent() end
  16. while true do
  17. event, arg1, arg2, arg3 = os.pullEvent()
  18. for i=1, #args do
  19. if args[i] == event then
  20. return event, arg1, arg2, arg3
  21. end
  22. end
  23. end
  24. end
  25. -- FUNCTIONS --
  26. -- COLLISION --
  27. col = {}
  28. function createCol(name,x,y,x2,y2,status)
  29. name = name or "name_"..#col table.insert(col,{}) status = status or true
  30. col[#col].x, col[#col].y, col[#col].x2, col[#col].y2, col[#col].name, col[#col].status = x, y, x2, y2, name, status
  31. end
  32. function colExists(name)
  33. for i=1, #col do
  34. if col[i].name == name then return i end
  35. end
  36. return false
  37. end
  38. function removeCol(name)
  39. if colExists(name) ~= false then table.remove(col, colExists(name)) end
  40. end
  41. function moveCol(name,newx,newy)
  42. col[name].x2,col[name].y2 = col[name].x2+newx-col[name].x, col[name].y2+newy-col[name].y
  43. col[name].x,col[name].y = newx,newy
  44. end
  45. function drawCol(name, color)
  46. if not notnil(color) then if col[name].status then color = colors.green else color = colors.red end end
  47. for i=1, #col do
  48. if name == col[i].name then
  49. return paintutils.drawFilledBox(col[i].x,col[i].y,col[i].x2,col[i].y2,color)
  50. end
  51. end
  52. return false
  53. end
  54. function checkCol(x,y)
  55. for i=1, #col do
  56. if x >= col[i].x and x <= col[i].x2 and y >= col[i].y and y <= col[i].y2 then
  57. return col[i].name
  58. end
  59. end
  60. return false
  61. end
  62. -- COLLISION --
  63. -- *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** --
  64. local player = {}
  65. local run = true
  66. player.x = 1
  67. player.y = 1
  68. player.isJumping = false
  69. player.isFalling = false
  70. player.jumpDisabled = false
  71. local settings = {}
  72. settings.jumpHeight = 4 -- 3 being 3 px
  73. settings.jumpGravity = .9 -- effect gravity for player
  74. settings.fallSpeed = 2
  75. settings.bounceHeight = 4
  76.  
  77. local function moveCharacter(x,y)
  78. for i=1, #col do
  79. drawCol(col[i].name,colors.white)
  80. end
  81. local valid = true
  82. if x < player.x then
  83. if checkCol(player.x-1,player.y) ~= false then
  84. valid = false
  85. end
  86. elseif x > player.x then
  87. if checkCol(player.x+1,player.y) ~= false then
  88. valid = false
  89. end
  90. end
  91. if y < player.y then
  92. if checkCol(player.x,player.y-1) ~= false then
  93. valid = false
  94. end
  95. elseif y > player.y then
  96. if checkCol(player.x,player.y+1) ~= false then
  97. valid = false
  98. end
  99. end
  100. if valid then
  101. cp(player.x,player.y) setBack("black") write(" ")
  102. cp(x,y) setBack("yellow") write(" ") setBack("black")
  103. player.x, player.y = x, y
  104. end
  105. end
  106. clr()
  107. moveCharacter(math.floor(w/2),1)
  108.  
  109. local function gravity()
  110. while run do
  111. if player.isJumping and not player.isFalling and not player.jumpDisabled then
  112. local cnt = 0
  113. while cnt <= settings.jumpHeight do
  114. if checkCol(player.x,player.y-1) ~= false then break end moveCharacter(player.x,player.y-1) sleep((cnt/settings.jumpGravity)*.06) cnt = cnt + 1
  115. end
  116. player.jumpDisabled = true
  117. else
  118. if checkCol(player.x,player.y+1) == false then
  119. player.isFalling = true
  120. local loop = settings.fallSpeed
  121. local force_quit = false
  122. repeat
  123. moveCharacter(player.x,player.y+1) sleep(loop*.09)
  124. if loop ~= 0 then loop = loop - 0.1 end
  125. if player.y == h then force_quit = true end
  126. until checkCol(player.x,player.y+1) ~= false or force_quit == true
  127. if force_quit == true then
  128. local cnt = 0
  129. while cnt <= settings.bounceHeight do
  130. if checkCol(player.x,player.y-1) ~= false then break end moveCharacter(player.x,player.y-1) sleep((cnt/settings.jumpGravity)*.06) cnt = cnt + 1
  131. end
  132. end
  133. player.isFalling = false
  134. player.isJumping = false
  135. player.jumpDisabled = false
  136. end
  137. end
  138. sleep(.0001)
  139. end
  140. end
  141.  
  142. local function playerController()
  143. while run do
  144. a,i,held = pullEvents("key")
  145. if i == keys.d or i == keys.right then
  146. moveCharacter(player.x+1,player.y)
  147. elseif i == keys.a or i == keys.left then
  148. moveCharacter(player.x-1,player.y)
  149. elseif i == keys.s or i == keys.down then
  150. moveCharacter(player.x,player.y+1)
  151. elseif i == keys.q then
  152. run = false
  153. elseif i == keys.space then
  154. if not player.jumpDisabled then player.isJumping = true else if checkCol(player.x,player.y+1) ~= false then player.jumpDisabled = false player.isJumping = true end end
  155. end
  156. end
  157. end
  158.  
  159. local function createFloor(x,y)
  160. if checkCol(x,y) == false then
  161. if x == player.x and y == player.y then
  162. -- for later things maby
  163. else
  164. createCol("floor_"..x.."_"..y,x,y,x,y,true)
  165. drawCol("floor_"..x.."_"..y,colors.white)
  166. end
  167. end
  168. end
  169. local function removeFloor(x,y)
  170. removeCol("floor_"..x.."_"..y)
  171. cp(x,y) setBack("black") write(" ")
  172. end
  173.  
  174. local function editController()
  175. while run do
  176. a,i,x,y = pullEvents("mouse_click","mouse_drag")
  177. if i == 1 then
  178. createFloor(x,y)
  179. elseif i == 2 then
  180. removeFloor(x,y)
  181. end
  182. end
  183. end
  184.  
  185. local function runtime()
  186. parallel.waitForAny(playerController,editController,gravity)
  187. end
  188.  
  189. runtime()
  190. cp(1,1) setBack("black") setText("white") clr() print("Thnks for playing...") sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement