Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --the main functions stay quite small
- --your code needs to go into an object
- --see MiGame as an example
- function setup()
- --the only thing you need is
- --a variable (like pss)
- --to store a pointer to the Partition class (Parti)
- --pass it your object (MiGame)
- --and the number of rows and columns for the menu (4)
- --(4 is a 4 by 4 grid or 16 partitions)
- pss = Parti(MiGame,4)
- end
- function draw()
- --required (the menu draws even if you don't
- pss:draw()
- end
- function touched(touch)
- --required (the menu uses tocuhes even if you don't)
- pss:touched()
- end
- MiGame = class()
- function MiGame:init()
- self.x = math.random(WIDTH)
- self.y = math.random(HEIGHT)
- self.w = math.random(WIDTH)
- self.h = math.random(HEIGHT)
- end
- function MiGame:draw()
- --background(0,0,0,255)
- strokeWidth(20)
- stroke(255, 0, 0, 255)
- fill(0, 0, 255, 255)
- ellipse(self.x,self.y,self.w,self.h)
- end
- function MiGame:Heartbeat()
- background(0,0,0,255)
- self.x = self.x + math.random(2) - 1.5
- self.y = self.y + math.random(2) - 1.5
- self.w = self.w + math.random(2) - 1.5
- self.h = self.h + math.random(2) - 1.5
- end
- function MiGame:touched(touch)
- self:init()
- end
- Parti = class()
- function Parti:init(clss,np)
- self.clss = clss --the class to execute
- self.aclss = nil --active class
- self.menu = true --is the menu being displayed
- self.np = np -- number of partitions across
- self.gb = math.sqrt(5)/4 --golden border
- self.gr = (1+math.sqrt(5))/2 --golden ratio
- self.gp = WIDTH / ((self.gb * (np + 1)) + (np)) --golden partion
- self.ps = {} --table of partitions (of menu and classes)
- local i
- local j
- for i = 1, np do
- local ix = ((i)*self.gb*self.gp) + ((i - 1)*self.gp)
- self.ps[i] = {}
- for j = 1, np do
- local iy = ((j)*self.gb*self.gp) + ((j - 1)*self.gp)
- self.ps[i][j] = {x=ix,y=iy,clss=nil}
- end
- end
- end
- function Parti:draw()
- if self.menu == true then
- self:drawmenu()
- else
- self.aclss:Heartbeat()
- self.aclss:draw()
- self:drawreturn()
- end
- end
- function Parti:drawmenu()
- rect(0,0,10,10)
- background(0, 0, 0, 255)
- for i = 1, self.np do
- for j = 1, self.np do
- if self.ps[i][j].clss ~= nil then
- --print(i,j)
- pushMatrix()
- translate(self.ps[i][j].x,self.ps[i][j].y)
- scale(self.gp/WIDTH)
- self.ps[i][j].clss:draw()
- popMatrix()
- end
- end
- end
- stroke(0, 255, 0, 255)
- local gbgp = (self.gb*self.gp)
- for i = 1, self.np do
- for j = 1, self.np do
- noFill()
- strokeWidth(3)
- rect(self.ps[i][j].x,self.ps[i][j].y,self.gp,self.gp)
- noStroke()
- fill(0,0,0,255)
- rect(self.ps[i][j].x-gbgp,0,gbgp,HEIGHT)
- rect(0,self.ps[i][j].y-gbgp,WIDTH,gbgp)
- end
- end
- rect(self.ps[self.np][self.np].x+self.gp,0,gbgp,HEIGHT)
- rect(0,self.ps[self.np][self.np].x+self.gp,WIDTH,gbgp)
- end
- function Parti:drawreturn()
- --background(0,0,0,255)
- stroke(0, 255, 0, 255)
- fill(0,0,0,255)
- strokeWidth(7)
- rect(0,0,50*((1+math.sqrt(5))/2),50)
- --ipad41001 arrows
- line(20,25,40,40)
- line(20,25,40,10)
- line(30,25,50,40)
- line(30,25,50,10)
- end
- function Parti:touched(touch)
- if CurrentTouch.state == BEGAN then
- if self.menu then
- self.menu = false
- for i = 1, self.np do
- for j = 1, self.np do
- if CurrentTouch.x > self.ps[i][j].x and
- CurrentTouch.x < self.ps[i][j].x + self.gp and
- CurrentTouch.y > self.ps[i][j].y and
- CurrentTouch.y < self.ps[i][j].y + self.gp then
- if self.ps[i][j].clss == nil then
- self.ps[i][j].clss = self.clss()
- end --init class
- self.aclss = self.ps[i][j].clss
- end --touch in box
- end --j
- end --i
- else --not on menu
- if CurrentTouch.x < (50*self.gr) and CurrentTouch.y < 50 then
- self.menu = true
- else
- self.aclss:touched()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement