Juaxix

Abudction code for Codea 4/4 - StreamingLines

Nov 3rd, 2011
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | None | 0 0
  1.  
  2. -- This class draws the lines streaming past in the background
  3. -- of the game. We spawn and delete them in the self.lines table
  4.  
  5. ----------------------------------------------
  6. -- Single line
  7. ----------------------------------------------
  8. StreamLine = class()
  9. function StreamLine:init(pos, vel)
  10.     self.position = pos
  11.     self.velocity = vel
  12. end
  13.  
  14. function StreamLine:update()
  15.     self.position.y = self.position.y - self.velocity
  16. end
  17.  
  18. function StreamLine:draw()
  19.     p = self.position
  20.     line(p.x,p.y,p.x,p.y + self.velocity)
  21. end
  22.  
  23. function StreamLine:shouldCull()
  24.     -- Check if off the bottom of the screen
  25.     if (self.position.y + self.velocity) < 0 then
  26.         return true
  27.     end
  28.  
  29.     return false
  30. end
  31.  
  32. ----------------------------------------------
  33. -- All lines
  34. ----------------------------------------------
  35. StreamingLines = class()
  36.  
  37. function StreamingLines:init()
  38.     self.minSpeed = 5
  39.     self.speed = 30
  40.     self.spawnRate = 2
  41.     self.lines = {}
  42. end
  43.  
  44. function StreamingLines:updateAndCull()
  45.     toCull = {}
  46.     for i,v in ipairs(self.lines) do
  47.         if v:shouldCull() then
  48.             -- table.insert( toCull, i )
  49.             table.remove( self.lines, i )
  50.         else
  51.             v:update()
  52.         end
  53.     end
  54.  
  55.     -- print("Removing ", #toCull)
  56.     --for i = #toCull,1,-1 do
  57.     --    table.remove( self.lines, i )
  58.     --end
  59. end
  60.  
  61. function StreamingLines:update()
  62.     -- Create spawnRate lines per update
  63.     for i = 1,self.spawnRate do
  64.         -- Generate random spawn location
  65.         vel = math.random(self.minSpeed, self.speed)
  66.         spawn = vec2( math.random(WIDTH), HEIGHT + vel )
  67.  
  68.         table.insert(self.lines, StreamLine(spawn, vel))
  69.     end
  70.  
  71.     -- Update and cull offscreen lines
  72.     self:updateAndCull()
  73. end
  74.  
  75. function StreamingLines:draw()
  76.     --print("Num lines = ", #self.lines)
  77.  
  78.     pushStyle()
  79.  
  80.     noSmooth()
  81.     stroke(179, 153, 180, 173)
  82.     strokeWidth(2)
  83.     lineCapMode(SQUARE)
  84.  
  85.     for i,v in ipairs(self.lines) do
  86.         v:draw()
  87.     end
  88.  
  89.     popStyle()
  90. end
  91.  
Add Comment
Please, Sign In to add comment