Advertisement
Guest User

Codea Codify object sort and alpha transparency demo

a guest
Nov 15th, 2011
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. --alphas and sort comp
  2. --seeing though things and sorting a table of objects
  3.  
  4. iparameter("leafThickness",0,255,128)
  5.  
  6. function setup()
  7.     forest = {} --create a table called forest
  8.     for i = 1,10 do
  9.         --create 10 plants in the forest
  10.         --this calls the function Plant:init(x,y) 
  11.         --the x is adjusted to 80% of screen width with 10% added
  12.         --the trees show up in the middle 20% to 80% 
  13.         --this prevents the green part of the plant from going off the screen
  14.         --the y is adjusted so the the base of the tree will start in the lower 80% 
  15.         --this prevents a plant being drawn off the top of the screen
  16.         forest[i] = Plant((math.random() * WIDTH * .8)+(WIDTH*.1),math.random() * HEIGHT * .8)
  17.     end   
  18.     --print before sort
  19.     for i,v in ipairs(forest) do
  20.         print(forest[i].rooty,i,v)
  21.     end    
  22.     --sort the trees in reverse y (height) order
  23.     --this prevents the trunk of one plant being drawn over the green part of another
  24.     --reverse the "a.rooty > b.rooty" and respawn to see overlapping trunks
  25.     --rooty is the y value (height) of a plant
  26.     table.sort(forest, function(a,b) return a.rooty > b.rooty end)
  27.     --print after sort
  28.     for i,v in ipairs(forest) do
  29.         print(forest[i].rooty,i,v)
  30.     end    
  31. end
  32.  
  33. function draw()
  34.     background(0, 0, 0, 255)
  35.     --simple loop to draw all plants
  36.     --this calls plant:draw for every plant in the forest table
  37.     for i,v in ipairs(forest) do
  38.         forest[i]:draw()
  39.     end    
  40. end
  41.  
  42. Plant = class()
  43. --usally a seperate tab called plant
  44.  
  45. function Plant:init(x,y)
  46.     --store the given x and y as rootx and rooty
  47.     self.rootx = x
  48.     self.rooty = y
  49.     --create a table of berry within this plant object
  50.     self.berry = {}
  51.     for i = 1, 100 do
  52.         --create 100 berries
  53.         --the numbers are just to keep the berries within the green part of the plan
  54.         --the numbers are swags, using real math would have given a non-rectanle placement
  55.         x = (math.random() * 74) + self.rootx - 41
  56.         y = (math.random() * 30) + self.rooty + 39
  57.         --record the position of each berry x and y as x and y
  58.         self.berry[i]={x=x,y=y}
  59.     end    
  60. end
  61.  
  62. function Plant:draw()
  63.     --start with the bottom of the plant
  64.     x = self.rootx
  65.     y = self.rooty
  66.     --draw trunk -- basically a brown Y
  67.     stroke(156, 109, 25, 255)
  68.     lineCapMode(PROJECT)
  69.     strokeWidth(20)
  70.     line(x,y,x,y+32)
  71.     strokeWidth(10)
  72.     line(x,y+32,x+16,y+48)
  73.     line(x,y+32,x-16,y+48)
  74.     --draw leaves, a green oval
  75.     strokeWidth(5)
  76.     --the 128 is what makes the leaves semi-see though
  77.     fill(57, 255, 0, leafThickness)
  78.     noStroke()
  79.     ellipseMode(CORNERS)
  80.     ellipse(x-50,y+30,x+50,y+80)
  81.     fill(255, 0, 221, 255)
  82.     --draw all the berries
  83.     for i,v in ipairs(self.berry) do
  84.         ellipse(self.berry[i].x,self.berry[i].y,self.berry[i].x+5,self.berry[i].y+5)
  85.     end    
  86. end
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement