Random Little Houses — Codea

a guest
May 16th, 2012
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
3.
4. function blendColor( c1, c2, a )
5.     return color( c1.r * a + c2.r * (1 - a),
6.                   c1.g * a + c2.g * (1 - a),
7.                   c1.b * a + c2.b * (1 - a),
8.                   255 )
9. end
10.
12.     -- you can accept and set parameters here
13.     local img = image(64,64)
14.
15.     local a = 1
16.     local c = nil
17.
18.     for y = 1,img.rawHeight do
19.         a = y / img.rawHeight
20.         for x = 1,img.rawWidth do
21.             c = blendColor( endCol, startCol, a )
22.             img:rawSet( x, y, c )
23.         end
24.     end
25.
26.     return img
27. end
28.
29.
30. --# Hill
31. Hill = class()
32.
34.     -- you can accept and set parameters here
35.     self.houses = {}
36.     self.color = col or color(0,0,0,255)
37.     self.position = pos or vec2(0,0)
39.
40.     -- add houses from -30 to 30 degrees
41.     local angle = -45
42.     local h = nil
43.     local v = vec2( 0, rad*0.99 )
44.     while angle < 45 do
45.         h = House(self.color)
46.         h.rotation = angle
47.         h.position = v:rotate( math.rad(angle) )
48.
49.         table.insert( self.houses, h )
50.
51.         angle = angle + math.random(7, 15)
52.     end
53. end
54.
55. function Hill:draw()
56.     pushMatrix()
57.     pushStyle()
58.
59.     fill(self.color)
60.
61.     translate( self.position.x, self.position.y )
62.
64.     ellipse( 0, 0, self.radius )
65.
66.     for _,v in pairs(self.houses) do
67.         v:draw()
68.     end
69.
70.     popStyle()
71.     popMatrix()
72. end
73.
74. function Hill:touched(touch)
75.     -- Codea does not automatically call this method
76. end
77.
78. --# House
79. House = class()
80.
81. function House:init(col)
82.     -- you can accept and set parameters here
83.     self.color = col or color(0,0,0,255)
84.     self.position = vec2(0,0)
85.     self.rotation = 0
86.     self.base = mesh()
87.     self.windows = mesh()
88.
89.     local houseWidth = math.random( 30, 60 )
90.     local roofWidth = houseWidth * 1.3
91.     local houseHeight = houseWidth * 0.67
92.     local roofHeight = houseHeight * (math.random() * 0.2 + 0.8)
93.
95.
96.     if math.random() < 0.5 then
97.         self:addChimney( houseWidth, houseHeight, roofHeight )
98.     end
99.
100.     self:addRoof( roofWidth, roofHeight, houseHeight )
101.
102.     -- Make the house black
103.     self.base:setColors( self.color )
104.
105.     if math.random() < 0.7 then
107.     end
108. end
109.
111.     self.base:addRect( 0, h/2, w, h )
112. end
113.
115.     local verts = self.base.vertices
116.
117.     table.insert( verts, vec3( -w/2, bh ) )
118.     table.insert( verts, vec3( 0, bh + h ) )
119.     table.insert( verts, vec3( w/2, bh ) )
120.
121.     self.base.vertices = verts
122. end
123.
125.     local chimWidth = w * (math.random() * 0.1 + 0.2)
126.     local pos = -w/2 + chimWidth
127.
128.     if math.random() < 0.5 then pos = w/2 - chimWidth end
129.
130.     self.base:addRect( pos, bh + rh/2, chimWidth, rh )
131. end
132.
134.     -- Add a random window
135.     local winSize = w * (math.random() * 0.2 + 0.2)
136.     local randomX = math.random( -w/2 + winSize, w/2 - winSize )
137.
138.     self.windows:addRect( randomX, h - winSize, winSize, winSize )
139.
140.     self.windows:setColors( 255, 200, 0, 255 )
141. end
142.
143. function House:draw()
144.     pushMatrix()
145.
146.     translate( self.position.x, self.position.y )
147.     rotate( self.rotation )
148.
149.     self.base:draw()
150.     self.windows:draw()
151.
152.     popMatrix()
153. end
154.
155.
156. --# Main
157.
158.
159. -- Use this function to perform your initial setup
160. function setup()
161.     displayMode(FULLSCREEN)
162.
163.     -- Lock renderer to starting orientation
164.     supportedOrientations(CurrentOrientation)
165.
166.     hills = {}
167.
168.     backgroundColor = color(22, 20, 27, 255)
169.     foregroundColor = color(54, 49, 69, 255)
170.
171.     -- Create some hills
172.     local h = Hill(400, vec2(WIDTH/2, -30), backgroundColor)
173.     table.insert(hills,h)
174.
175.     h = Hill(400, vec2(WIDTH/2 - 300, -100), backgroundColor)
176.     table.insert(hills,h)
177.
178.     h = Hill(400, vec2(WIDTH/2 + 300, -100), backgroundColor)
179.     table.insert(hills,h)
180.
181.     h = Hill(300, vec2(WIDTH/2 - 250, -150), foregroundColor)
182.     table.insert(hills,h)
183.
184.     h = Hill(300, vec2(WIDTH/2 + 250, -150), foregroundColor)
185.     table.insert(hills,h)
186.
187.     -- Create the sky
188.     local skyCol1 = color(168, 159, 197, 255)
189.     local skyCol2 = color(64, 48, 107, 255)
190.
192. end
193.
194. -- This function gets called once every frame
195. function draw()
196.     -- This sets a dark background color
197.     background(140, 94, 48, 255)
198.
199.     sprite( sky, WIDTH/2, HEIGHT/2, WIDTH + 10, HEIGHT + 10 )
200.
201.     -- Do your drawing here
202.     for _,v in pairs(hills) do
203.         v:draw()
204.     end
205. end