Created
October 3, 2012 22:13
-
-
Save Synvox/3830225 to your computer and use it in GitHub Desktop.
Spawn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --# Main | |
| function setup() | |
| displayMode(FULLSCREEN) | |
| backingMode(RETAINED) | |
| spawns={} | |
| for i=0, 200 do | |
| table.insert(spawns,Spawn(math.random(WIDTH),math.random(HEIGHT))) | |
| end | |
| lineCapMode(PROJECT) | |
| strokeWidth(6) | |
| touches={} | |
| touchLength=0 | |
| fakeTouch={} | |
| spawnable=0 | |
| end | |
| function touched(touch) | |
| if touch.tapCount==2 and touch.state==MOVING then | |
| return | |
| end | |
| if touch.state == ENDED then | |
| touches[touch.id] = nil | |
| touchLength = touchLength - 1 | |
| else | |
| if touch.tapCount==2 then | |
| for i,obj in ipairs(spawns) do | |
| obj.x=touch.x | |
| obj.y=touch.y | |
| obj.direction=math.random(math.pi*2) | |
| end | |
| touches[touch.id] = nil | |
| return | |
| end | |
| touches[touch.id] = touch | |
| if touch.state==BEGAN then | |
| touchLength = touchLength +1 | |
| math.randomseed(touch.id) | |
| local red=math.random(255) | |
| local green=math.random(255) | |
| local blue=math.random(255) | |
| while red+green+blue<255 do | |
| red=math.random(255) | |
| green=math.random(255) | |
| blue=math.random(255) | |
| end | |
| local mode=math.ceil(math.random(4)) | |
| if mode==4 then | |
| mode=1 | |
| end | |
| --touches[touch.id].mode=mode | |
| for i,obj in ipairs(spawns) do | |
| if math.ceil(math.random(4))>1 then | |
| obj.id=0 | |
| end | |
| if obj.id==0 then | |
| obj.id=touch.id | |
| obj.tored=red+math.random(-40,40) | |
| obj.togreen=green+math.random(-40,40) | |
| obj.toblue=blue+math.random(-40,40) | |
| obj.mode=mode | |
| end | |
| end | |
| end | |
| end | |
| end | |
| function fakeTouchDown() | |
| local touch={} | |
| touch.x=math.random(WIDTH) | |
| touch.y=math.random(HEIGHT) | |
| touch.state=BEGAN | |
| touch.tapCount=0 | |
| touch.id=math.random(1000,2000) | |
| touch.vx=math.random(-8,8)*10 | |
| touch.vy=math.random(-8,8)*10 | |
| touched(touch) | |
| table.insert(fakeTouch,touch.id) | |
| end | |
| function fakeTouchUp() | |
| local index=table.maxn(fakeTouch) | |
| index=math.ceil(math.random(index)) | |
| local id=fakeTouch[index] | |
| local touch={} | |
| touch.id=id | |
| touch.state=ENDED | |
| touch.tapCount=0 | |
| touched(touch) | |
| table.remove(fakeTouch,index) | |
| end | |
| function count(tab) | |
| local returned=0 | |
| for obj,i in ipairs(tab) do | |
| returned = returned + 1 | |
| end | |
| return returned | |
| end | |
| function shouldFakeTouch() | |
| if touchLength==0 then | |
| fakeTouchDown() | |
| end | |
| if math.ceil(math.random(45))>1 then | |
| return | |
| end | |
| local fakeLength=count(fakeTouch) | |
| local allLength=touchLength | |
| local realLength=allLength-fakeLength | |
| if realLength>0 then | |
| if fakeLength>0 then | |
| fakeTouchUp() | |
| end | |
| return | |
| end | |
| --print(allLength,fakeLength,realLength) | |
| if fakeLength==realLength and fakeLength>0 then | |
| fakeTouchUp() | |
| fakeTouchDown() | |
| end | |
| if math.ceil(math.random(2))==1 and fakeLength>0 then | |
| fakeTouchUp() | |
| else | |
| if math.ceil(math.random(2))==1 and fakeLength>0 and fakeLength<2 then | |
| fakeTouchDown() | |
| end | |
| end | |
| end | |
| function moveFakeTouch() | |
| for obj,i in ipairs(touches) do | |
| obj.state=MOVING | |
| obj.x = obj.x + obj.vx | |
| obj.y = obj.y + obj.vy | |
| touch(obj) | |
| end | |
| end | |
| function draw() | |
| shouldFakeTouch() | |
| --moveFakeTouch() | |
| fill(0, 0, 0, 28) | |
| noStroke() | |
| rect(0,0,WIDTH,HEIGHT) | |
| strokeWidth(6) | |
| stroke(255, 255, 255, 255) | |
| for i,obj in ipairs(spawns) do | |
| obj:draw(i) | |
| end | |
| end | |
| --# Spawn | |
| Spawn = class() | |
| function Spawn:init(x,y) | |
| self.x = x | |
| self.y = y | |
| self.direction=math.random(math.pi*2) | |
| self.speed=18+math.random(8) | |
| self.id=0 | |
| self.ax=0 | |
| self.ay=0 | |
| self.red=0 | |
| self.green=0 | |
| self.blue=0 | |
| self.tored=255 | |
| self.togreen=255 | |
| self.toblue=255 | |
| self.turning=math.random(3,20) | |
| self.mode=1 | |
| end | |
| function Spawn:attract(x,y) | |
| if self.mode==1 then | |
| local dir1=math.atan2(y-self.y,x-self.x) | |
| local dir2=2*math.pi+dir1 | |
| local dir | |
| if math.abs(dir1-self.direction)<math.abs(dir2-self.direction) then | |
| dir=dir1 | |
| else | |
| dir=dir2 | |
| end | |
| dir = dir + (math.random(2)-1)*.1 | |
| self.direction = self.direction + (dir-self.direction)/self.turning | |
| elseif self.mode==2 or self.mode==3 then | |
| local dir1=math.atan2(y-self.y,x-self.x) | |
| local dir2=2*math.pi+dir1 | |
| local dir | |
| if math.abs(dir1-self.direction)<math.abs(dir2-self.direction) then | |
| dir=dir1 | |
| else | |
| dir=dir2 | |
| end | |
| dir = dir + 1 + math.random(4)*.5/self.turning | |
| self.direction = (self.direction*4 + dir)/5 | |
| elseif self.mode==4 then | |
| local distance=math.sqrt( | |
| (x-self.x)*(x-self.x)+ | |
| (y-self.y)*(y-self.y) | |
| ) | |
| if distance < 200 then | |
| return | |
| end | |
| self.direction = self.direction + .1 | |
| --local dir1=math.atan2(y-self.y,x-self.x) | |
| --local dir2=2*math.pi+dir1 | |
| --local dir | |
| --if math.abs(dir1-self.direction)<math.abs(dir2-self.direction) then | |
| -- dir=dir1 | |
| --else | |
| -- dir=dir2 | |
| --end | |
| --dir = dir + (math.random(2)-1)*.1 | |
| --self.direction = self.direction + (dir-self.direction)/(1-(distance/200)+1) | |
| end | |
| end | |
| function Spawn:respond() | |
| if self.x<0 or self.x>WIDTH then | |
| self.direction=math.atan2(math.sin(self.direction),-math.cos(self.direction)) | |
| if self.x<0 then | |
| self.x=0 | |
| else | |
| self.x=WIDTH | |
| end | |
| else | |
| self.direction=math.atan2(-math.sin(self.direction),math.cos(self.direction)) | |
| if self.y<0 then | |
| self.y=0 | |
| else | |
| self.y=HEIGHT | |
| end | |
| end | |
| end | |
| function Spawn:draw() | |
| if self.id>0 then | |
| if touches[self.id]==nil then | |
| self.id=0 | |
| self.tored=0 | |
| self.togreen=128 | |
| self.toblue=255 | |
| self.mode=1 | |
| else | |
| local touch=touches[self.id] | |
| self.ax=touch.x | |
| self.ay=touch.y | |
| self:attract(touch.x,touch.y) | |
| self.red = self.red + (self.tored-self.red)/100 | |
| self.green = self.green + (self.togreen-self.green)/100 | |
| self.blue = self.blue + (self.toblue-self.blue)/100 | |
| end | |
| else | |
| end | |
| if self.x<0 or self.y<0 or self.x>WIDTH or self.y>HEIGHT then | |
| if self.mode==1 or self.mode==3 or self.mode==4 then | |
| self:respond(index) | |
| end | |
| end | |
| --local length=math.sqrt(math.exp(Gravity.x,2)+math.exp(Gravity.y,2))/1.41 | |
| --local direction=math.atan2(Gravity.x,Gravity.y) | |
| --self.direction = (self.direction*(1-length) + direction*length)/2 | |
| --self.x = self.x + Gravity.x*2 | |
| --self.y = self.y + Gravity.y*2 | |
| self.red = self.red + (self.tored-self.red)/30 | |
| self.green = self.green + (self.togreen-self.green)/30 | |
| self.blue = self.blue + (self.toblue-self.blue)/30 | |
| local x=self.x | |
| local y=self.y | |
| if self.direction>2*math.pi then | |
| self.direction = self.direction - 2*math.pi | |
| elseif self.direction<0 then | |
| self.direction = self.direction + 2*math.pi | |
| end | |
| self.x = self.x + self.speed *math.cos(self.direction) | |
| self.y = self.y + self.speed *math.sin(self.direction) | |
| stroke(255, 255, 255, 255) | |
| --tint(255, 255, 255, 255) | |
| --line(x,y+1,self.x,self.y+1) | |
| stroke(self.red,self.green,self.blue) | |
| line(x,y,self.x,self.y) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment