initial stage...
[yu.git] / test / test2.lx
blob40b2d2a9ac81014827dc32336bbb996ef39b6cfe
1 import "lua/std.lx"
2 import "gfx.lx"
5 func randomBetween(minvalue,maxvalue#) => math.random()*(maxvalue-minvalue)+minvalue
7 global images:={
8         green=Image.load "image/green_ball.png",
9         love=Image.load "image/love_ball.png",
10         mars=Image.load "image/3.png"
13 class Blob      @{attribute, rename="good", virtual}
14         
15         const minsize, maxsize:=40,80
16         const minspeed,maxspeed:=6,40
17         
18         global maxcolor:=255
19         
20         func randomcolor()=>math.random()*maxcolor
21         
22         field x,y:= 0,0
23         field img:Image=images[
24                 math.random()>0.6 ? "love" 
25           | math.random()>0.5 ? "mars"
26           | "green" 
27         ]
28         
29         field r:=randomcolor()
30         field g:=randomcolor()
31         field b:=randomcolor()
32         
33         
34         field xs,ys:=0, 0.8
35         
36         field size:=randomBetween(minsize,maxsize)
37         
38         method update()
39                 
40                 x+=xs
41                 y+=ys
42                 ys+=0.1
43                 
44                 if x>800-size then
45                         xs=-xs*0.2
46                         x=800-size
47                 elseif x<0 then
48                         xs=-xs*0.2 
49                         x=0
50                 end
51                 
52                 if y>500-size then
53                         ys=-ys*0.2
54                         xs*=0.8
55                         y=500-size
56                 end
57         end
58         
59         method dosomething()
60                 local s:=size
61         end
62         
63         method draw()   @{limit=-1}
64                 -- setColor(r,g,b)
65                 -- setColor(180,2,20)
66                 local s:=size
67                 -- drawRect(x,y,s,s)
68                 setScale(s/img.getWidth(),s/img.getHeight())
69                 img.draw(x,y)
70                 --drawRect(x,y,5,5)
71         end
72         
73         
74 end
76 class OvalBlob extends Blob
77         const ax:=0.01
78         const ay:=0.2
79         
80         method update()
81                 xs+= ax
82                 ys+= ay
83                 super.update()
84         end
85         
86 end
89 class FPSCounter 
90         field frameCount#=0
91         field startTime, totalTime#=0,0
92         
93         method reset()
94                 startTime=MilliSecs()
95         end
96         
97         method update()
98                 frameCount+=1
99                 totalTime=MilliSecs()-startTime+1
100         end
101         
102         field fps#
103                 ::get => math.floor(frameCount/totalTime*1000)
104         
108 global fpsCounter:= new FPSCounter()
110 Graphics(800,600,0)
112 const maxcount:=200
114 local blobs:Blob[]={}
115 for i=1, maxcount do
116         local b:=new OvalBlob()
117         b.xs=math.cos(i/50*math.pi)*2
118         b.ys=math.sin(i/50*math.pi)*2
119         blobs[i]=b
122 local falling:=1
123 fpsCounter.reset()
124 local ox,oy#
125 while not KeyHit(27) do
126         setClsColor(80,50,180)
127         cls()
128         fpsCounter.update()
129         setBlend(Blend.alpha)
130         local mx:=MouseX()
131         local my:=MouseY()
132         
133         if MouseDown(1) then
134                 local a1:=math.atan2(my-oy,mx-ox)
135                 local d:=((my-oy)^2+(mx-ox)^2)/60000
136                 if d>1 then d=1 end
137                 
138                 for i= 1, math.random()*6 do
139                         falling=(falling ) % maxcount + 1
140                         local a:=(math.random()-0.5)*3.14/20+a1
141                         local s:=randomBetween(Blob.minspeed,Blob.maxspeed)*d
142                         local b1:=blobs[falling]
143                         b1.(x,y,xs,ys) =ox,oy, math.cos(a)* s ,math.sin(a)* s
144                         
145                 end
146                 ox=ox*0.9+mx*0.1
147                 oy=oy*0.9+my*0.1
148         else
149                 ox,oy = mx,my
150         end
151         setColor(255,255,255)
152         
153         for i, b in blobs do
154                 b.update()
155                 b.draw()
156         end
157         
158         setScale(1,1)
159         setColor(80,80,80)
160         drawRect(0,500,800,100)
161         drawText("fps:"..fpsCounter.fps,0,0)
162         drawText("mem:"..collectgarbage("count"),0,20)
163         flip(MouseDown(2)? 0 | -1)