Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This class draws the lines streaming past in the background
- -- of the game. We spawn and delete them in the self.lines table
- ----------------------------------------------
- -- Star
- ----------------------------------------------
- Star = class()
- function Star:init(pos, vel)
- self.position = pos
- self.velocity = vel
- end
- function Star:update()
- self.position.x = self.position.x - self.velocity
- end
- function Star:draw()
- p = self.position
- line(p.x-self.velocity,p.y,p.x,p.y)
- end
- function Star:shouldCull()
- -- Check if off the left of the screen
- if (self.position.x - self.velocity) < 0 then
- return true
- end
- return false
- end
- ----------------------------------------------
- -- All stars
- ----------------------------------------------
- Stars = class()
- function Stars:init()
- self.minSpeed = 6
- self.speed = 23
- self.spawnRate = 1
- self.stars = {}
- end
- function Stars:updateAndCull()
- toCull = {}
- for i,star in ipairs(self.stars) do
- if star:shouldCull() then
- table.remove( self.stars, i )
- else
- star:update()
- end
- end
- end
- function Stars:update()
- -- Create spawnRate lines per update
- for i = 1,self.spawnRate do
- -- Generate random spawn location
- vel = math.random(self.minSpeed, self.speed)
- spawn = vec2( WIDTH - vel, math.random(HEIGHT) )
- table.insert(self.stars, Star(spawn, vel))
- end
- -- Update and cull offscreen lines
- self:updateAndCull()
- end
- function Stars:draw()
- self:update()
- pushStyle()
- noSmooth()
- stroke(179, 153, 180, 173)
- strokeWidth(2)
- lineCapMode(SQUARE)
- for i,star in ipairs(self.stars) do
- star:draw()
- end
- popStyle()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement