Guest User

Untitled

a guest
Jan 6th, 2013
651
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. require "player"
  2. require "SpriteAnimation"
  3. require "camera"
  4. require "menu"
  5. require "importantstuff"
  6.  
  7. -- MASSIVE THANKS TO Sean Laurvick FOR THE BASE, THE TUTORIAL AND SUCH, WE COULD NOT HAE DONE IT WITHOUT YOU. YOU SHAL COME THIRD IN THE CREDITS, AND WE WILL LINK YOUR FORUM TUTORIAL.
  8.  
  9.  
  10.  
  11. function love.load() --SET VARIABLES HERE PLZ THX BYE
  12.  
  13. medium = love.graphics.newFont(25)
  14.  
  15.  
  16. gamestate = "menu"
  17.  
  18. if gamestate == "playing" then
  19. loadgeneral()
  20. end
  21.  
  22.  
  23. love.graphics.setBackgroundColor(255,255,255)
  24.  
  25.  
  26. --buttons yay
  27. button_spawn(5,200,"Start","start")
  28. button_spawn(5,550,"Quit","quit")
  29. end
  30. function love.update(dt)
  31.  
  32.  
  33. -- The controls
  34.  
  35. -- D - Right
  36. if love.keyboard.isDown("d") then
  37. if grid[math.floor(p.x/blocksize) + 2][math.floor(p.y/blocksize) + 1] == 0 or grid[math.floor(p.x/blocksize) + 2][math.floor(p.y/blocksize) + 1] == 3 then
  38. p:moveRight()
  39. else
  40. p:stop()
  41. end
  42. animation:flip(false, false)
  43. end
  44. -- A - Left
  45. if love.keyboard.isDown("a") then
  46. if p.x == 0 then
  47. p:stop()
  48. else if grid[math.ceil(p.x/blocksize)][math.floor(p.y/blocksize) + 1] == 0 or grid[math.ceil(p.x/blocksize)][math.floor(p.y/blocksize) + 1] == 3 then
  49. p:moveLeft()
  50. else
  51. p:stop()
  52. end
  53. end
  54. animation:flip(true, false)
  55. end
  56. -- W - Jump
  57. if love.keyboard.isDown("w") and not(hasJumped) then
  58. if grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1] == 3 and grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize)] == 0 or grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize)] == 3 then
  59. p.y = p.y - 32
  60. else if grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize)] == 0 or grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize)] == 3 then
  61. p:jump()
  62. hasJumped = true
  63. else p:stop()
  64. end
  65. end
  66. end
  67. -- R - Reset to spawn
  68. if love.keyboard.isDown("r") then
  69. p.x = 0
  70. p.y = 0
  71. end
  72. --F - Place a ladder
  73. if love.keyboard.isDown("f") and grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1] == 0 then
  74. grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1] = 3
  75. end
  76. -- K - Place block on your right
  77. if love.keyboard.isDown("k") then
  78. if # inv > 0 then
  79. if grid[math.floor(p.x/blocksize) + 2][math.floor(p.y/blocksize) + 1] == 0 then
  80. grid[math.floor(p.x/blocksize) + 2][math.floor(p.y/blocksize) + 1] = inv[1]
  81. table.remove(inv, 1)
  82. end
  83. end
  84. end
  85. -- H - Place block on your left
  86. if love.keyboard.isDown("h") then
  87. if # inv > 0 then
  88. if math.floor(p.x/blocksize) > 0 then
  89. if grid[math.floor(p.x/blocksize)][math.floor(p.y/blocksize) + 1] == 0 then
  90. grid[math.floor(p.x/blocksize)][math.floor(p.y/blocksize) + 1] = inv[1]
  91. table.remove(inv, 1)
  92. end
  93. end
  94. end
  95. end
  96. -- J - Place blocks below you
  97. if love.keyboard.isDown("j") then
  98. if # inv > 0 then
  99. if grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 2] == 0 then
  100. grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 2] = inv[1]
  101. table.remove(inv, 1)
  102. end
  103. end
  104. end
  105. -- U - Place blocks above you
  106. if love.keyboard.isDown("u") then
  107. if # inv > 0 then
  108. if grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize)] == 0 then
  109. grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize)] = inv[1]
  110. table.remove(inv, 1)
  111. end
  112. end
  113. end
  114. -- E - Camera Rotate anti-clockwise
  115. if love.keyboard.isDown("e") then
  116. camera:rotate(0.1)
  117.  
  118.  
  119.  
  120. -- update the player's position
  121. p:update(dt, gravity)
  122.  
  123. -- stop the player when they hit the borders
  124. p.x = math.clamp(p.x, 0, width * 2 - p.width)
  125. if p.y < 0 then p.y = 0 end
  126. if grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 2] > 0 then
  127. p:hitFloor( (math.floor(p.y/blocksize) + 1 )* blocksize)
  128. end
  129.  
  130. end
  131.  
  132. -- update the sprite animation
  133. if (p.state == "stand") then
  134. animation:switch(1, 4, 200)
  135. end
  136. if (p.state == "moveRight") or (p.state == "moveLeft") then
  137. animation:switch(2, 4, 120)
  138. end
  139. if (p.state == "jump") or (p.state == "fall") then
  140. animation:reset()
  141. animation:switch(3, 1, 300)
  142. end
  143. animation:update(dt)
  144.  
  145. -- center the camera on the player
  146. camera:setPosition(math.floor(p.x - width / 2), math.floor(p.y - height / 2))
  147.  
  148. -- M - Dig right
  149. if love.keyboard.isDown("m") then
  150. if grid[math.floor(p.x/blocksize) + 2][math.floor(p.y/blocksize) + 1] > 0 then
  151. items[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1] = grid[math.floor(p.x/blocksize) + 2][math.floor(p.y/blocksize) + 1]
  152. grid[math.floor(p.x/blocksize) + 2][math.floor(p.y/blocksize) + 1] = 0
  153. end
  154. end
  155. -- N - Dig left
  156. if love.keyboard.isDown("n") then
  157. if math.floor(p.x/blocksize) > 0 then
  158. if grid[math.floor(p.x/blocksize)][math.floor(p.y/blocksize) + 1] > 0 then
  159. items[math.floor(p.x/blocksize)][math.floor(p.y/blocksize) + 1] = grid[math.floor(p.x/blocksize)][math.floor(p.y/blocksize) + 1]
  160. grid[math.floor(p.x/blocksize)][math.floor(p.y/blocksize) + 1] = 0
  161. end
  162. end
  163. end
  164. -- B - Dig down
  165. if love.keyboard.isDown("b") then
  166. if grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 2] > 0 then
  167. items[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1] = grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 2]
  168. grid[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 2] = 0
  169. end
  170. end
  171.  
  172. -- Pick up items in your block
  173. if items[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1] > 0 then
  174. table.insert(inv, items[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1])
  175.  
  176. items[math.floor(p.x/blocksize) + 1][math.floor(p.y/blocksize) + 1] = 0
  177.  
  178.  
  179.  
  180. end
  181.  
  182. function love.draw()
  183. camera:set()
  184.  
  185. if gamestate == "menu" then
  186. button_draw()
  187. end
  188.  
  189. -- round down our x, y values
  190. local x, y = math.floor(p.x), math.floor(p.y)
  191.  
  192. -- draw the ground
  193. for i = 1, blockcount do
  194. for j = 1, blockcount do
  195. if grid[i][j] == 1 then
  196. g.setColor(groundColor)
  197. g.rectangle("fill", blocksize * (i - 1), blocksize * (j - 1), blocksize, blocksize)
  198. else if grid[i][j] == 2 then
  199. g.setColor(100, 100, 100)
  200. g.rectangle("fill", blocksize * (i - 1), blocksize * (j - 1), blocksize, blocksize)
  201. else if grid[i][j] == 3 then
  202. love.graphics.draw(Ladders, blocksize * (i - 1), blocksize * (j - 1))
  203. end
  204. end
  205. end
  206. end
  207. end
  208.  
  209. -- draw the player
  210. g.setColor(255, 255, 255)
  211. animation:draw(x, y)
  212.  
  213. camera:unset()
  214.  
  215. -- debug information
  216. g.print("Player coordinates: ("..x..","..y..")", 5, 5)
  217. g.print("Current state: "..p.state, 5, 20)
  218. for i = 1, # inv do
  219. g.print(inv[i],5 + (i * 10),35)
  220. end
  221. g.print("Health = "..life, 5, 50)
  222. end
  223.  
  224. end
  225.  
  226. function love.keyreleased(key)
  227. if key == "escape" then
  228. love.event.push("quit") -- actually causes the app to quit
  229. end
  230. if (key == "d") or (key == "a") then
  231. p:stop()
  232. end
  233. if (key == "w") then
  234. hasJumped = false
  235. end
  236. if (key == "q") then
  237. invshuffle = inv[1]
  238. for i = 1, # inv - 1 do
  239. inv[i] = inv[i + 1]
  240. end
  241. inv[# inv] = invshuffle
  242. end
  243. if (key == "delete") then
  244. if # inv > 0 then
  245. table.remove(inv, 1)
  246. end
  247. end
  248.  
  249. end
  250. function love.mousepressed(x,y)
  251. if gamestate == "menu" then
  252. button_click(x,y)
  253. end
  254.  
  255. function math.clamp(x, min, max)
  256. return x < min and min or (x > max and max or x) --DAHECK?
  257. end
  258. end
Advertisement
Add Comment
Please, Sign In to add comment