Advertisement
Guest User

Fish eye forest from above - codea lua

a guest
Mar 10th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1.  
  2. --# Main
  3. displayMode(FULLSCREEN)
  4. supportedOrientations(PORTRAIT)
  5.  
  6. function setup()
  7.     print("Hello World!")
  8.     t=Trees()
  9.     mx = 0
  10.     my = 0
  11.     lt = ElapsedTime
  12. end
  13.  
  14. function draw()
  15.     background(25, 32, 26, 255)
  16.     t:draw()
  17.     t:move(mx,my)
  18.     fill(195, 39, 39, 255)
  19.     --text(1/(ElapsedTime - lt),100,100)
  20.     lt = ElapsedTime
  21. end
  22.  
  23. function touched(touch)
  24.     if touch.state == BEGAN then
  25.         if touch.x > WIDTH/2 + WIDTH/4 then
  26.             mx = mx - 1
  27.         elseif touch.x < WIDTH/2 - WIDTH/4 then
  28.             mx = mx + 1
  29.         end
  30.         if touch.y > HEIGHT/2 + HEIGHT/4 then
  31.             my = my - 1
  32.         elseif touch.y < HEIGHT/2 - HEIGHT/4 then
  33.             my = my + 1
  34.         end
  35.     end
  36. end
  37. --# Tree
  38. Tree = class()
  39.  
  40. function Tree:init()
  41.     self.x = math.random(WIDTH+200)-100
  42.     self.y = math.random(HEIGHT+200)
  43.     self.tl = 16
  44.     self.tw = 8
  45.     self.tb = self.tw * 1.2
  46.     self.lw = 32
  47. end
  48.  
  49. function Tree:drawTrunk()
  50.     stroke(125, 81, 37, 255)
  51.     fill(125, 81, 37, 255)
  52.     strokeWidth(self.tw)
  53.     local v1 = vec2(self.x,self.y)
  54.     local v2 = vec2(WIDTH/2,HEIGHT/2)
  55.     local v3 = v2 - v1
  56.     local v4 = v3:normalize()
  57.     local d = v1:dist(v2)
  58.     local ttx = self.x-(v4.x*(d/self.tl))
  59.     local tty = self.y-(v4.y*(d/self.tl))
  60.     line(self.x,self.y,ttx,tty)
  61.     ellipse(self.x,self.y,self.tb,self.tb)
  62. end
  63.  
  64. function Tree:drawLeaves()
  65.     noStroke()
  66.     fill(39, 133, 29, 127 + 64)
  67.     local v1 = vec2(self.x,self.y)
  68.     local v2 = vec2(WIDTH/2,HEIGHT/2)
  69.     local v3 = v2 - v1
  70.     local v4 = v3:normalize()
  71.     local d = v1:dist(v2)
  72.     local ttx = self.x-(v4.x*(d/self.tl))
  73.     local tty = self.y-(v4.y*(d/self.tl))
  74.    
  75.     ellipse(ttx,tty,self.lw,self.lw)
  76.    
  77. end
  78.  
  79. --# Trees
  80. Trees = class()
  81.  
  82. function Trees:init(n)
  83.     if n == nil then self.n = 128 else self.n = n end
  84.     self.t = {}
  85.     for i = 1, self.n do
  86.         self.t[i] = Tree()
  87.     end
  88. end
  89.  
  90. function Trees:draw()
  91.     for i = 1, self.n do
  92.         self.t[i]:drawTrunk()
  93.     end
  94.     for i = 1, self.n do
  95.         self.t[i]:drawLeaves()
  96.     end
  97.    
  98. end
  99.  
  100. function Trees:move(x,y)
  101.     for i = 1, self.n do
  102.         self.t[i].x = self.t[i].x + x
  103.         self.t[i].y = self.t[i].y + y
  104.         if self.t[i].y < -100 then
  105.             self.t[i].x = math.random(WIDTH+200)-100
  106.             self.t[i].y = HEIGHT + 100
  107.         elseif self.t[i].y > HEIGHT + 100 then
  108.             self.t[i].x = math.random(WIDTH+200)-100
  109.             self.t[i].y = -100
  110.         elseif self.t[i].x < -100 then
  111.             self.t[i].y = math.random(HEIGHT+200)-100
  112.             self.t[i].x = WIDTH + 100
  113.         elseif self.t[i].x > WIDTH + 100 then
  114.             self.t[i].y = math.random(HEIGHT+200)-100
  115.             self.t[i].x = -100
  116.         end        
  117.     end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement