Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. --main.lua
  2.  
  3. StaticBody = require"StaticBody"
  4. collision = require"collision"
  5.  
  6. player = StaticBody.StaticBody:new(300, 200, 40, 30, "RECT", 2000, 5000, 8.9, 900, 900, 5)
  7.  
  8. function love.load()
  9. playerimg = love.graphics.newImage("player.png")
  10. player:setSprite(playerimg, 1, 1, 0, 0)
  11. end
  12.  
  13. function love.draw()
  14. player:draw()
  15. end
  16.  
  17. function love.update(d)
  18.  
  19. if love.keyboard.isDown("a") then
  20. input = -1
  21. end
  22. if love.keyboard.isDown("d") then
  23. input = 1
  24. end
  25. if not love.keyboard.isDown("a") and not love.keyboard.isDown("d") then
  26. input = 0
  27. end
  28.  
  29. player:update(d,input)
  30. end
  31.  
  32.  
  33. function love.keypressed( key, scancode, isrepeat)
  34. if key == "space" then
  35. player:jump(800)
  36. end
  37. end
  38.  
  39. -----------------------------------------------------------------------------------------------------------------
  40. --StaticBody.lua
  41. -----------------------------------------------------------------------------------------------------------------
  42.  
  43. StaticBody = {x = 0, y = 0, w = 1, h = 1, speedx = 0, speedy = 0, maxxspd = 1, maxyspd = 1, collosionmode = "RECT", sprite = nil, scalex = 1,
  44. scaley = 1, offsetx = 0, offsety = 0, acc = 1, decel = 1, grav = 1, velx = 0, colbox = nil, hitbox = nil, turningfriction = 2}
  45.  
  46. collision = require"collision"
  47.  
  48. function clamp(variable,vmin,vmax)
  49. if variable < vmin then
  50. variable = vmin
  51. end
  52. if variable > vmax then
  53. variable = vmax
  54. end
  55. end
  56.  
  57. function StaticBody:new(x, y, w, h, colmode, acc, decel, grav, maxxspd, maxyspd, turningfriction)
  58. setmetatable({},StaticBody)
  59.  
  60. self.x = x
  61. self.y = y
  62. self.w = w
  63. self.h = h
  64. self.hitbox = collision.hitbox:new(x,y,w,h,false)
  65. self.colbox = collision.ColObjekt:new(colmode,self.hitbox)
  66. self.acc = acc
  67. self.decel = decel
  68. self.grav = grav
  69. self.maxxspd = maxxspd
  70. self.maxyspd = maxyspd
  71. self.speedx = 0
  72. self.speedy = 0
  73. self.turningfriction = turningfriction
  74. self.velx = 0
  75.  
  76. return self
  77. end
  78.  
  79. function StaticBody:setSprite(sprite, scalex, scaley, offsetx, offsety)
  80. self.sprite = sprite
  81. self.scalex = scalex
  82. self.scaley = scaley
  83. self.offsetx = offsetx
  84. self.offsety = offsety
  85. end
  86.  
  87. function StaticBody:draw()
  88. love.graphics.draw(self.sprite,self.x,self,y,0,self.scalex,self.scaley,self.offsetx,self.offsety,0,0)
  89. end
  90.  
  91. function StaticBody:update(d,input)
  92. local dir = 0
  93. if input == -dir then
  94. self.speedx = self.speedx / self.turningfriction
  95. end
  96. if input then
  97. dir = input
  98. end
  99. if not self.colbox:getOnGround() then
  100. self.speedy = self.speedy + self.grav * d
  101. end
  102.  
  103. if input ~= 0 then
  104. self.speedx = self.speedx + self.acc * d
  105. else
  106. self.speedx = self.speedx - self.decel * d
  107. end
  108.  
  109. clamp(self.speedx,0,self.maxxspd)
  110. clamp(self.speedy,0,self.maxyspd)
  111.  
  112. self.velx = self.speedx * d * dir
  113. self.y = self.y + self.speedy * d
  114. self.x = self.x + self.speedx * d * dir
  115. end
  116.  
  117. function StaticBody:jump(jumpforce)
  118. self.speedy = -jumpforce
  119. end
  120.  
  121. ------------------------------------------------------------------------------------------------
  122. --collision.lua
  123. ------------------------------------------------------------------------------------------------
  124.  
  125. ColObjekts = {}
  126. ColObjekt = {mode, x, y, w, h, collisionObjekts = ColObjekts}
  127. --Constructer
  128. function ColObjekt:new(mode,x,y,w,h,collisionObjekts)
  129. setmetatable({},ColObjekt)
  130.  
  131. self.mode = mode
  132. self.collisionObjekts = collisionObjekts
  133. self.x = x
  134. self.y = y
  135. self.w = w
  136. self.h = h
  137. ColObjekts.push(self)
  138.  
  139. return self
  140. end
  141.  
  142. --Constructer with Hitbbox
  143. function ColObjekt:new(mode,Hb,collisionObjekts)
  144. setmetatable({},ColObjekt)
  145.  
  146. self.mode = mode
  147. self.collisionObjekts = collisionObjekts
  148. self.x = Hb.x
  149. self.y = Hb.y
  150. self.w = Hb.w
  151. self.h = Hb.h
  152. end
  153.  
  154. function ColObjekt:setcolObjekts(colobs)
  155. self.collisionObjekts = colobs
  156. end
  157.  
  158. function ColObjekt:getColliding()
  159. for key,i in pairs(self.collisionObjekts) do
  160. if getCollidingWith(self,i) then
  161. return true
  162. end
  163. return false
  164. end
  165. end
  166.  
  167. function ColObjekt:getCollidingWhich()
  168. for key,i in pairs(self.collisionObjekts) do
  169. if getCollidingWith(self,i) then
  170. return true, i
  171. end
  172. return false
  173. end
  174. end
  175.  
  176. function ColObjekt:getCollidingDir(Colob1,Colob2)
  177. if not getCollidingWith(Colob1,Colob2) then
  178. return false
  179. elseif Colob1.y > Colob2.y then return "ONTOP"
  180. elseif Colob1.x < Colob2.x and Colob1.y > Colob2.y+Colob2.h then return "LEFT"
  181. elseif Colob1.x > Colob2.x and Colob1.y > Colob2.y+Colob2.h then return "RIGHT"
  182. else return "UNDER"
  183. end
  184. end
  185.  
  186. function ColObjekt:getOnGround()
  187. if self:getCollidingDir(self,self:getCollidingWhich()) == "ONTOP" then
  188. return true
  189. else
  190. return false
  191. end
  192.  
  193. function getCollidingWith(Colob1,Colob2)
  194. --if Colob1 == Rectangle
  195. if Colob1.mode == "RECT" then
  196. if Colob2.mode == "RECT" then
  197. ColRect_Rect(Colob1,Colob2)
  198. end
  199. if Colob2.mode == "CIRCLE" then
  200. ColRect_Circle(Colob2,Colob1)
  201. end
  202. end
  203.  
  204. --if Colob1 == Circle
  205. if Colob1.mode == "CIRCLE" then
  206. if Colob2.mode == "CIRCLE" then
  207. ColCircle_Circle(Colob1,Colob2)
  208. end
  209. if Colob2.mode == "RECT" then
  210. ColRect_Circle(Colob1,Colob2)
  211. end
  212. end
  213. end
  214.  
  215. function ColRect_Edges(c,r)
  216. --OBEN
  217. if c.x > r.x and c.x < r.x + r.w
  218. and c.y > r.y - c.h and c.y < r.y then return true
  219. --UNTEN
  220. elseif c.x > r.x and c.x < r.x + r.w
  221. and c.y > r.y + r.h - c.h and c.y < r.y + r.h then return true
  222. --LINKS
  223. elseif c.y > r.y and c.y < r.y + r.h
  224. and c.x > r.x - c.w and c.x < r.x then return true
  225. --RECHTS
  226. elseif c.y > r.y and c.y < r.y + r.h
  227. and c.x > r.x + r.w - c.w and c.x < r.x + r.w then return true
  228. --WENN NICHTS ZUTRIFFT
  229. else return false end
  230. end
  231.  
  232. function ColRect_Corners(c,r)
  233. if math.sqrt((c.x - r.x * c.x - r.x) + (c.y - r.y * c.y - r.y)) < c.w/2 then
  234. return true
  235. elseif math.sqrt((c.x - (r.x + r.w) * c.x - (r.x + r.w)) + (c.y - r.y * c.y - r.y)) < c.w/2 then
  236. return true
  237. elseif math.sqrt((c.x - (r.x + r.w) * c.x - (r.x + r.w)) + (c.y - (r.y + r.h) * c.y - (r.y + r.h))) < c.w/2 then
  238. return true
  239. elseif math.sqrt((c.x - r.x * c.x - r.x) + (c.y - (r.y + r.h) * c.y - (r.y + r.h))) then
  240. return true
  241. else
  242. return false
  243. end
  244. end
  245.  
  246. function ColRect_Rect(r1,r2)
  247. if r1.x < r2.x + r2.w and r1.x > r2.x - r1.x and r1.y > r2.y - r1.h and r1.y < r2.y + r2.h then
  248. return true
  249. end
  250. return false
  251. end
  252.  
  253. function ColCircle_Circle(c1,c2)
  254. local scndx = c1.x - c2.x
  255. local scndy = c1.y - c2.y
  256. if math.sqrt((scndx * scndx) + (scndy * scndy)) < c1.w/2 + c2.w/2 then
  257. return true
  258. end
  259. return false
  260. end
  261.  
  262. function ColRect_Circle(c,r)
  263. if c.x > r.x and c.x < r.x + r.w
  264. and c.y > r.y and c.y < r.y + r.h then
  265. return true
  266. end
  267. if ColRect_Corners(c,r) then
  268. return true
  269. end
  270. if ColRect_Edges(c,r) then
  271. return true
  272. end
  273. return false
  274. end
  275.  
  276.  
  277.  
  278. Hitbox = {x,y,w,h,show = false}
  279. function Hitbox:new(x,y,w,h,show)
  280. setmetatable({},Hitbox)
  281.  
  282. self.x = x
  283. self.y = y
  284. self.w = w
  285. self.h = h
  286. self.show = show
  287.  
  288. return self
  289. end
  290.  
  291. function Hitbox:update()
  292. if self.show then
  293. love.graphics.Rectangle("fill",self.x,self.y,self.w,self.h)
  294. end
  295. end
  296.  
  297. function Hitbox:setVisible(b)
  298. self.show = b
  299. end
  300.  
  301. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement