SHARE
TWEET

Codea codify menu system

a guest Nov 26th, 2011 172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --the main functions stay quite small
  2. --your code needs to go into an object
  3. --see MiGame as an example
  4.  
  5. function setup()
  6.     --the only thing you need is
  7.     --a variable (like pss)
  8.     --to store a pointer to the Partition class (Parti)
  9.     --pass it your object (MiGame)
  10.     --and the number of rows and columns for the menu (4)
  11.     --(4 is a 4 by 4 grid or 16 partitions)
  12.     pss = Parti(MiGame,4)
  13. end
  14.  
  15. function draw()  
  16.     --required (the menu draws even if you don't
  17.     pss:draw()
  18. end
  19.  
  20. function touched(touch)
  21.     --required (the menu uses tocuhes even if you don't)
  22.     pss:touched()
  23. end
  24.  
  25.  
  26. MiGame = class()
  27.  
  28. function MiGame:init()
  29.     self.x = math.random(WIDTH)
  30.     self.y = math.random(HEIGHT)
  31.     self.w = math.random(WIDTH)
  32.     self.h = math.random(HEIGHT)
  33. end
  34.  
  35. function MiGame:draw()
  36.     --background(0,0,0,255)
  37.     strokeWidth(20)
  38.     stroke(255, 0, 0, 255)
  39.     fill(0, 0, 255, 255)
  40.     ellipse(self.x,self.y,self.w,self.h)
  41. end
  42.  
  43. function MiGame:Heartbeat()
  44.     background(0,0,0,255)
  45.     self.x = self.x + math.random(2) - 1.5
  46.     self.y = self.y + math.random(2) - 1.5
  47.     self.w = self.w + math.random(2) - 1.5
  48.     self.h = self.h + math.random(2) - 1.5
  49. end
  50.  
  51. function MiGame:touched(touch)
  52.     self:init()
  53. end
  54.  
  55.  
  56. Parti = class()
  57.  
  58. function Parti:init(clss,np)
  59.     self.clss = clss --the class to execute
  60.     self.aclss = nil --active class
  61.     self.menu = true --is the menu being displayed
  62.     self.np = np -- number of partitions across 
  63.     self.gb = math.sqrt(5)/4 --golden border
  64.     self.gr = (1+math.sqrt(5))/2 --golden ratio
  65.     self.gp = WIDTH / ((self.gb * (np + 1)) + (np)) --golden partion
  66.     self.ps = {} --table of partitions (of menu and classes)
  67.     local i
  68.     local j
  69.     for i = 1, np do
  70.         local ix = ((i)*self.gb*self.gp) + ((i - 1)*self.gp)
  71.         self.ps[i] = {}
  72.         for j = 1, np do
  73.             local iy = ((j)*self.gb*self.gp) + ((j - 1)*self.gp)
  74.             self.ps[i][j] = {x=ix,y=iy,clss=nil}            
  75.         end  
  76.     end 
  77. end
  78.  
  79. function Parti:draw()
  80.     if self.menu == true then
  81.         self:drawmenu()  
  82.     else
  83.         self.aclss:Heartbeat()
  84.         self.aclss:draw()
  85.         self:drawreturn()
  86.     end
  87. end
  88.  
  89. function Parti:drawmenu()
  90.     rect(0,0,10,10)
  91.     background(0, 0, 0, 255)
  92.     
  93.     for i = 1, self.np do
  94.         for j = 1, self.np do
  95.             if self.ps[i][j].clss ~= nil then
  96.                 --print(i,j)
  97.                 pushMatrix()
  98.                 translate(self.ps[i][j].x,self.ps[i][j].y)
  99.                 scale(self.gp/WIDTH)    
  100.                 self.ps[i][j].clss:draw()
  101.                 popMatrix()
  102.             end
  103.         end
  104.     end
  105.     
  106.     stroke(0, 255, 0, 255)
  107.     local gbgp = (self.gb*self.gp)
  108.     for i = 1, self.np do
  109.         for j = 1, self.np do
  110.             noFill()
  111.             strokeWidth(3)
  112.             rect(self.ps[i][j].x,self.ps[i][j].y,self.gp,self.gp)
  113.             noStroke()
  114.             fill(0,0,0,255)   
  115.             rect(self.ps[i][j].x-gbgp,0,gbgp,HEIGHT)  
  116.             rect(0,self.ps[i][j].y-gbgp,WIDTH,gbgp)     
  117.         end  
  118.     end  
  119.     rect(self.ps[self.np][self.np].x+self.gp,0,gbgp,HEIGHT)
  120.     rect(0,self.ps[self.np][self.np].x+self.gp,WIDTH,gbgp)
  121.     
  122. end
  123.  
  124. function Parti:drawreturn()
  125.     --background(0,0,0,255)
  126.     stroke(0, 255, 0, 255)
  127.     fill(0,0,0,255)
  128.     strokeWidth(7)
  129.     rect(0,0,50*((1+math.sqrt(5))/2),50)
  130. --ipad41001 arrows
  131.     line(20,25,40,40)
  132.     line(20,25,40,10)
  133.     line(30,25,50,40)
  134.     line(30,25,50,10)
  135. end
  136.  
  137. function Parti:touched(touch)
  138.     if CurrentTouch.state == BEGAN then 
  139.         if self.menu then
  140.             self.menu = false
  141.             for i = 1, self.np do
  142.                 for j = 1, self.np do
  143.                     if CurrentTouch.x > self.ps[i][j].x and
  144.                        CurrentTouch.x < self.ps[i][j].x + self.gp and
  145.                        CurrentTouch.y > self.ps[i][j].y and
  146.                        CurrentTouch.y < self.ps[i][j].y + self.gp then                        
  147.                         if self.ps[i][j].clss == nil then 
  148.                             self.ps[i][j].clss = self.clss()
  149.                         end --init class
  150.                         self.aclss = self.ps[i][j].clss
  151.                     end --touch in box
  152.                 end --j
  153.             end --i            
  154.         else --not on menu
  155.             if CurrentTouch.x < (50*self.gr) and CurrentTouch.y < 50 then
  156.                 self.menu = true
  157.             else
  158.                 self.aclss:touched()   
  159.             end 
  160.         end    
  161.     end
  162. end
  163.  
  164.  
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top