Advertisement
Dermotb

Post-Classes(2)

Mar 23rd, 2013
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1.  
  2. --# Main
  3. -- class
  4.  
  5. -- Use this function to perform your initial setup
  6. function setup()
  7. b={}
  8. for i=1,5 do
  9. b[i]=Ball()
  10. end
  11. r={}
  12. for i=1,5 do
  13. r[i]=Rect()
  14. end
  15. CreateWalls()
  16. end
  17.  
  18. -- This function gets called once every frame
  19. function draw()
  20. background(195, 195, 201, 255)
  21. for i=1,#b do
  22. b[i]:draw()
  23. end
  24. for i=1,#r do
  25. r[i]:draw()
  26. end
  27. end
  28.  
  29. function CreateWalls()
  30. leftWall = CreateWall(1,1,1,HEIGHT-1,1.0)
  31. rightWall = CreateWall(WIDTH-1,0,WIDTH-1,HEIGHT-1,0.9)
  32. bottomWall = CreateWall(1,1,WIDTH-1,1,0.2)
  33. topWall = CreateWall(1,HEIGHT-1,WIDTH-1,HEIGHT-1,0.7)
  34. end
  35.  
  36. --this function creates one wall (actually just a line)
  37. function CreateWall(x,y,x1,y1,r)
  38. local w = physics.body(EDGE,vec2(x,y),vec2(x1,y1)) -- vec2
  39. w.restitution=r --see comment above
  40. return w
  41. end
  42.  
  43. --# Ball
  44. Ball = class()
  45.  
  46. function Ball:init(x,y,d,c)
  47. self.x=x or math.random(0,WIDTH)
  48. self.y=y or math.random(0,HEIGHT)
  49. self.diameter=d or math.random(50,200)
  50. self.colr=c or color(math.random(0,255),math.random(0,255),math.random(0,255))
  51. self.p=Physics(CIRCLE,{self.diameter/2},self.x,self.y) --physics uses radius
  52. end
  53.  
  54. function Ball:draw()
  55. pushStyle() -- store style settings
  56. fill(self.colr) --set color
  57. pushMatrix()
  58. local x,y,a=self.p:currentPosition()
  59. translate(x,y)
  60. rotate(a)
  61. ellipse(0,0,self.diameter)
  62. popMatrix()
  63. popStyle() -- put back style settings
  64. end
  65.  
  66.  
  67.  
  68. --# Rect
  69. Rect = class()
  70.  
  71. function Rect:init(x,y,w,h,c)
  72. self.x=x or math.random(0,WIDTH)
  73. self.y=y or math.random(0,HEIGHT)
  74. self.width=w or math.random(60,150)
  75. self.height=h or math.random(30,100)
  76. self.colr=c or color(math.random(0,255),math.random(0,255),math.random(0,255))
  77. local data={}
  78. table.insert(data,vec2(-self.width/2,-self.height/2))
  79. table.insert(data,vec2(-self.width/2,self.height/2))
  80. table.insert(data,vec2(self.width/2,self.height/2))
  81. table.insert(data,vec2(self.width/2,-self.height/2))
  82. self.p=Physics(POLYGON,data,self.x,self.y)
  83. end
  84.  
  85. function Rect:draw()
  86. pushStyle() -- store style settings
  87. fill(self.colr) --set color
  88. pushMatrix()
  89. local x,y,a=self.p:currentPosition()
  90. translate(x,y)
  91. rotate(a)
  92. rect(-self.width/2,-self.height/2,self.width,self.height)
  93. popMatrix()
  94. popStyle() -- put back style settings
  95. end
  96.  
  97.  
  98.  
  99. --# Physics
  100. Physics = class()
  101.  
  102. --type=CIRCLE or POLYGON
  103. --data is a table. For circles it is one value, the radius, while for a polygon it is a set of vec2
  104. --x,y is the initial position of the centre
  105. --a is the initial angle
  106. --g is the gravity value, 0=tabletop, 1=normal (things fall down)
  107. --r is the restitution, ie springiness
  108. --f is friction
  109. --lv is linear velocity
  110. --i is any info you want to store to identify this object, eg a name
  111. function Physics:init(type,data,x,y,a,g,r,f,lv,i)
  112. if type==CIRCLE then
  113. self.body=physics.body(CIRCLE,data[1])
  114. elseif type==POLYGON then
  115. self.body=physics.body(POLYGON,unpack(data))
  116. end
  117. self.body.x=x
  118. self.body.y=y
  119. self.body.angle=a or 0
  120. if g then self.body.gravityScale=1 else self.body.gravityScale=0 end
  121. self.body.restitution=r or 1
  122. self.body.friction=f or 0.1
  123. if lv==nil then lv=vec2(100+math.random(400),100+math.random(400)) end
  124. self.body.linearVelocity=lv
  125. self.body.info=i
  126. end
  127.  
  128. function Physics:currentPosition()
  129. return self.body.x, self.body.y,self.body.angle
  130. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement