Advertisement
Guest User

Untitled

a guest
May 29th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.70 KB | None | 0 0
  1.  
  2. --# Main
  3. -- BlockTown
  4.  
  5. displayMode(FULLSCREEN)
  6.  
  7. function setup()
  8. Settings()
  9. Move=false
  10. CreateScene(T2)
  11. end
  12.  
  13. function Settings()
  14. --lighting
  15. ambient=0.3
  16. diffuse=0.6
  17. lightDirection=vec3(1,1,1):normalize()
  18. lightIntensity=1
  19. mist=100
  20. mistColor=color(230)
  21. camDist=50 --??
  22. camY=4
  23. FPS=60
  24. --controls
  25. joy1=JoyStick({radius=150})
  26. turnSensitivity=0.25
  27. --joy2=JoyStick({centre=vec2(WIDTH-joy1.centre.x,joy1.centre.y)})
  28. speed=0
  29. speedFactor=0.5
  30. speedSensitivity=0.25
  31. lookFactor=0.03
  32. velocity=vec3(0,0,0)
  33. currTileCode=""
  34. end
  35.  
  36.  
  37. function draw()
  38. background(mistColor) --(150*lightIntensity)
  39. FPS=FPS*0.95+0.05/DeltaTime
  40. AdjustPosition()
  41. perspective()
  42. cam=playerPos+vec3(0,camY,0)
  43. camLook=cam+look
  44. camera(cam.x,cam.y,cam.z,camLook.x,camLook.y,camLook.z)
  45. DrawScene(cam,camLook,mist+math.sin(ElapsedTime*.5)*10)
  46. DrawHUD()
  47. end
  48.  
  49. function touched(t)
  50. joy1:touched(t)
  51. --joy2:touched(t)
  52. end
  53.  
  54. function AdjustPosition()
  55. --update velocity
  56. local v1=joy1:update()
  57. if joy1.touch then
  58. local t=turnSensitivity
  59. if v1.y<0 then v1.y=math.min(0,v1.y+t)/(1-t) else v1.y=math.max(0,v1.y-t)/(1-t) end
  60. speed=v1.y*speedFactor
  61. --update player position
  62. local p=playerPos+look*speed --get new position, test if valid
  63. playerPos=PreventCollision(playerPos,p)
  64. --update look direction
  65. local t=speedSensitivity
  66. if v1.x<0 then v1.x=math.min(0,v1.x+t)/(1-t) else v1.x=math.max(0,v1.x-t)/(1-t) end
  67. lookAngle=lookAngle-v1.x*lookFactor
  68. look=vec3(math.cos(lookAngle),0,-math.sin(lookAngle))
  69. UpdateSettings(playerPos)
  70. end
  71. --local v2=joy2:update()
  72. --if joy2.touch then look=(look+vec3(v2.x,0,-v2.y):normalize()*lookFactor):normalize() end
  73. end
  74.  
  75. function DrawHUD()
  76. ortho()
  77. viewMatrix(matrix())
  78. fill(0,0,0)
  79. fontSize(16)
  80. text("FPS="..math.floor(FPS),WIDTH-50,HEIGHT-25)
  81. joy1:draw()
  82. --joy2:draw()
  83. end
  84.  
  85.  
  86. --# Maps
  87. --Maps
  88. local M,sceneWidth,sceneDepth,block,prevTileCode
  89.  
  90. function CreateScene(map)
  91. M=map
  92. local s=M.spawn[math.random(1,#M.spawn)]
  93. playerPos=PosToPixel(s[1])
  94. lookAngle=math.deg(s[2])
  95. look=vec3(math.cos(lookAngle),0,-math.sin(lookAngle))
  96. mapSize=map.mapSize*map.tileSize
  97. margin=map.tileSize/2
  98. spriteSheet=readImage(map.spriteSheet)
  99. spriteWidth,spriteHeight=spriteSheet.width,spriteSheet.height
  100. --surface mesh
  101. surface=CreateSurface(M.images.surface)
  102. --build fence
  103. fence=BuildFence(M.images.fence)
  104. --build basic objects
  105. objects=BuildObjects()
  106. --read map and create mesh containing objects
  107. objMesh=ReadMap()
  108. print(objMesh.size)
  109. local x,y,z=M.tileSize*M.mapSize.x/2,M.tileSize*8,M.tileSize*M.mapSize.y
  110. CullDupFaces(objMesh)
  111. print(objMesh.size)
  112. end
  113.  
  114. function UpdateSettings(p)
  115. local t=PixelToPos(p)
  116. local m=memMap[t.x*1000000+t.y*1000+t.z] or "default"
  117. if m~=prevTileCode then
  118. prevTileCode=m
  119. if M.tileSettings[m] then
  120. ambient=M.tileSettings[m].ambient
  121. diffuse=M.tileSettings[m].diffuse
  122. mist=M.tileSettings[m].mist
  123. end
  124. end
  125. end
  126.  
  127. function DrawScene(cam,look,mist)
  128. local cam4=vec4(cam.x,cam.y,cam.z,1)
  129. SetLighting(surface,cam4,mist) surface:draw()
  130. SetLighting(fence,cam4,mist) fence:draw()
  131. SetLighting(objMesh,cam4,mist)
  132. objMesh.shader.ambient=ambient
  133. objMesh.shader.diffuse=diffuse
  134. objMesh:draw()
  135. end
  136.  
  137. function SetLighting(m,cam4,mist)
  138. m.shader.intensity=lightIntensity
  139. m.shader.mModel=modelMatrix()
  140. m.shader.cam=cam4
  141. m.shader.mist=mist
  142. end
  143.  
  144. function CullDupFaces(m)
  145. local bv=m:buffer("position")
  146. local bt=m:buffer("texCoord")
  147. local bn=m:buffer("normal")
  148. local bc=m:buffer("color")
  149. local A,B={},{}
  150. local r=100000
  151. for i=1,m.size,6 do
  152. local c=bv[i]+bv[i+1]+bv[i+2]+bv[i+4]
  153. local cc=math.floor(r+c.x*100+0.5)..","..math.floor(r+c.y*100+0.5)..","..math.floor(r+c.z*100+0.5)
  154. A[cc]=(A[cc] or 0)+1
  155. for j=0,5 do B[i+j]=cc end
  156. end
  157.  
  158. local v,t,n,c={},{},{},{}
  159. local u=0
  160. for i=1,m.size do
  161. if A[B[i]]==1 then
  162. u=u+1
  163. v[u]=bv[i]
  164. t[u]=bt[i]
  165. n[u]=bn[i]
  166. if bc then c[u]=bc[i] end
  167. end
  168. end
  169. m.vertices=v
  170. m.texCoords=t
  171. m.normals=n
  172. if bc then m.colors=c end
  173. --print(m.size)
  174. end
  175.  
  176. function ReadMap()
  177. memMap={}
  178. local A={}
  179. A.v,A.t,A.n,A.c={},{},{},{}
  180. for _,a in pairs(M.mapData) do
  181. local p0=PosToPixel(a.start) --bottom left tile location
  182. local n=string.len(a.tiles[1])
  183. for i=#a.tiles,1,-1 do
  184. for j=1,n do
  185. e=string.sub(a.tiles[i],j,j)
  186. memMap[(a.start.x+j-1)*1000000+a.start.y*1000+(a.start.z+i-1)]=e
  187. local f=objects[e]
  188. if f then
  189. for k=1,#f.v do
  190. A.v[#A.v+1]=f.v[k]+p0+vec3(j-1,0,-i+1)*M.tileSize
  191. A.t[#A.t+1]=f.t[k]
  192. A.n[#A.n+1]=f.n[k]
  193. A.c[#A.c+1]=f.c[k]
  194. end
  195. end
  196. end
  197. end
  198. end
  199. --fill in fence around edge in memMap
  200. for i=0,M.mapSize.x+1 do
  201. memMap[i*1000000]="_"
  202. memMap[i*1000000+M.mapSize.y+1]="_"
  203. end
  204. for i=0,M.mapSize.y+1 do
  205. memMap[i]="_"
  206. memMap[(M.mapSize.x+1)*1000000+i]="_"
  207. end
  208. --create mesh
  209. local m=mesh()
  210. m.vertices=A.v
  211. m.texCoords=A.t
  212. m.texture=mapImage
  213. m.normals=A.n
  214. m.colors=A.c
  215. m.shader=shader(Diffuse.v,Diffuse.f)
  216. m.shader.lightDirection=lightDirection
  217. m.shader.intensity=lightIntensity
  218. m.shader.ambient=ambient
  219. m.shader.diffuse=diffuse
  220. m.shader.mistColor=mistColor
  221. return m
  222. end
  223.  
  224. function CreateSurface(img)
  225. local w,d=mapSize.x,mapSize.y
  226. local x1,x2=-w/2,w/2
  227. local y=-0.1
  228. local z1,z2=0,-d
  229. local v={vec3(x1,y,z1),vec3(x2,y,z1),vec3(x2,y,z2),vec3(x1,y,z2)}
  230. local tw,td=w/img[3]/img[5],d/img[4]/img[5]
  231. local t={vec2(0,0),vec2(tw,0),vec2(tw,td),vec2(0,td)}
  232. local m=mesh()
  233. m.vertices={v[1],v[2],v[3],v[3],v[4],v[1]}
  234. m.texCoords={t[1],t[2],t[3],t[3],t[4],t[1]}
  235. local c=vec4(img[1]/spriteWidth,img[2]/spriteHeight,img[3]/spriteWidth,img[4]/spriteHeight)
  236. m.colors={c,c,c,c,c,c}
  237. m.texture=M.spriteSheet
  238. m.shader=shader(Surface.v,Surface.f)
  239. m.shader.light=ambient+diffuse
  240. m.shader.mistColor=mistColor
  241. return m
  242. end
  243.  
  244. function BuildFence(img)
  245. local w,d,h0=mapSize.x,mapSize.y,-0.1
  246. local v11,v21,v22,v12=vec3(-w/2,h0,0),vec3(w/2,h0,0),vec3(w/2,h0,-d),vec3(-w/2,h0,-d)
  247. local h=vec3(0,M.tileSize,0)
  248. local m=mesh()
  249. m.vertices={v11,v21,v21+h,v21+h,v11+h,v11, v11,v12,v12+h,v12+h,v11+h,v11,
  250. v12,v22,v22+h,v22+h,v12+h,v12, v21,v22,v22+h,v22+h,v21+h,v21}
  251. local tw,th,td=w/img[3]/img[5],M.tileSize/img[4]/img[5],d/img[4]/img[5]
  252. local t11, t21, t22, t12 =vec2(0,0),vec2(tw,0),vec2(tw,th),vec2(0,th)
  253. local t11d,t21d,t22d,t12d=vec2(0,0),vec2(td,0),vec2(td,th),vec2(0,th)
  254. m.texCoords={t11 ,t21 ,t22 ,t22 ,t12 ,t11 , t11 ,t21 ,t22 ,t22 ,t12 ,t11,
  255. t11d,t21d,t22d,t22d,t12d,t11d, t11d,t21d,t22d,t22d,t12d,t11d}
  256. local c=vec4(img[1]/spriteWidth,img[2]/spriteHeight,img[3]/spriteWidth,img[4]/spriteHeight)
  257. local col={} for i=1,24 do col[i]=c end
  258. m.colors=col
  259. m.texture=M.spriteSheet
  260. m.shader=shader(Surface.v,Surface.f)
  261. m.shader.light=ambient+diffuse
  262. m.shader.mistColor=mistColor
  263. return m
  264. end
  265.  
  266. function BuildObjects()
  267. B={} --object list
  268. for a,b in pairs(M.objects) do
  269. if b.type=="block" then B[a]=CreateBlock(M.images[b.image]) end
  270. end
  271. return B
  272. end
  273.  
  274.  
  275. function CreateBlock(img,bottom)
  276. local s=M.tileSize
  277. local s2=s/2
  278. local v={vec3(-s2,0, s2),vec3(s2,0, s2),vec3(s2,s, s2),vec3(-s2,s, s2),
  279. vec3(-s2,0,-s2),vec3(s2,0,-s2),vec3(s2,s,-s2),vec3(-s2,s,-s2)}
  280. local vert={v[1],v[2],v[3],v[3],v[4],v[1],v[6],v[5],v[8],v[8],v[7],v[6],
  281. v[2],v[6],v[7],v[7],v[3],v[2],v[5],v[1],v[4],v[4],v[8],v[5],
  282. v[8],v[4],v[3],v[3],v[7],v[8],v[5],v[6],v[2],v[2],v[1],v[5]}
  283. local tw,th=s/img[3]/img[5],s/img[4]/img[5]
  284. local t={vec2(0,0),vec2(tw,0),vec2(tw,th),vec2(0,th)}
  285. local tex={t[1],t[2],t[3],t[3],t[4],t[1],t[1],t[2],t[3],t[3],t[4],t[1],
  286. t[1],t[2],t[3],t[3],t[4],t[1],t[1],t[2],t[3],t[3],t[4],t[1],
  287. t[1],t[2],t[3],t[3],t[4],t[1],t[1],t[2],t[3],t[3],t[4],t[1]}
  288. local n={vec3(0,0,1),vec3(0,0,-1),vec3(1,0,0),vec3(-1,0,0),vec3(0,1,0),vec3(0,-1,0)}
  289. local norm,col={},{}
  290. local c=vec4(img[1]/spriteWidth,img[2]/spriteHeight,img[3]/spriteWidth,img[4]/spriteHeight)
  291. for i=1,#n do
  292. for j=1,6 do
  293. norm[#norm+1]=n[i]
  294. col[#col+1]=c
  295. end
  296. end
  297. return {v=vert,t=tex,n=norm,c=col}
  298. end
  299.  
  300. function AddBlock(p,tbl,obj)
  301. pos=PosToPixel(p)
  302. for i=1,#obj.v do
  303. tbl.v[#bb.v+1]=obj.v[i]+pos
  304. tbl.t[#bb.t+1]=obj.t[i]
  305. tbl.n[#bb.n+1]=obj.n[i]
  306. tbl.c[#bb.c+1]=obj.c[i]
  307. end
  308. return tbl
  309. end
  310.  
  311. function PosToPixel(p)
  312. return vec3(p.x-0.5-M.mapSize.x/2,p.y,-p.z+0.5)*M.tileSize
  313. end
  314.  
  315. function PixelToPos(p)
  316. return vec3(math.floor((p.x+mapSize.x/2)/M.tileSize)+1,math.floor(p.y/M.tileSize),
  317. math.floor(-p.z/M.tileSize)+1)
  318. end
  319.  
  320. function PreventCollision(p0,p1)
  321. local t=PixelToPos(p1)
  322. local m=memMap[t.x*1000000+t.y*1000+t.z]
  323. if m=="b" then
  324. --calculate direction of movement by using tile positions
  325. local v=PixelToPos(p0)-PixelToPos(p1)
  326. --move back towards previous tile
  327. p1=p0+vec3(v.x,0,-v.z)*M.tileSize/3
  328. end
  329. return p1
  330. end
  331.  
  332. --# MapData
  333. --Map Data
  334.  
  335. --T2
  336. T2={}
  337. T2.tileSize=8
  338. T2.mapSize=vec2(30,30) --even numbers for x, please
  339. T2.spawn={{vec3(2,0,2),45},{vec3(2,0,29),-45},{vec3(29,0,2),135},{vec3(29,0,29),-135}}
  340.  
  341. T2.spriteSheet="Dropbox:T1"
  342. T2.images={
  343. block={611,1701,294,300,0.0166},
  344. surface={1,1401,599,600,0.02},
  345. fence={1,880,511,511,0.01},
  346. jump={610,1390,300,300,0.05}
  347. }
  348.  
  349. T2.objects={
  350. b={type="block5",image="block"},
  351. B={type="block6",image="block"},
  352. j={type="jump",image="jump"}
  353. }
  354.  
  355. T2.tileSettings={
  356. default={ambient=0.4,diffuse=0.6,mist=100},
  357. [" "]={ambient=0.7,diffuse=0,mist=99999999}
  358. }
  359.  
  360. T2.mapData={
  361. {start=vec3(8,0,8),tiles=
  362. {"bbbbbbbbbbb bbbb",
  363. "b b b b",
  364. "b bbbb j b",
  365. "b b b bbb b",
  366. "b b b bbb b",
  367. "b bbb bbbbbbb b",
  368. "b bb b b",
  369. "bbbbb b",
  370. "b bbbbbbbbbbbb",
  371. "b b b bbb",
  372. "bbbb b b b",
  373. " jb bbbbb b",
  374. "bb b b b b",
  375. "bb bb bb b b",
  376. "bb bb b",
  377. "bbbbbbbbbbbbbbbb"},
  378. },
  379. -- {start=vec3(8,1,8),tiles=
  380. -- {"bbbbbbbbbbbbbbbb",
  381. -- "bbbbbbbbbbbbbbbb",
  382. -- "bbbbbbbbbbbbbbbb",
  383. -- "bbbbbbbbbbbbbbbb",
  384. -- "bbbbbbbbbbbbbbbb",
  385. -- "bbbbbbbbbbbbbbbb",
  386. -- "bbbbbbbbbbbbbbbb",
  387. -- "bbbbbbbbbbbbbbbb",
  388. -- "bbbbbbbbbbbbbbbb",
  389. -- "bbbbbbbbbbbbbbbb",
  390. -- "bbbbbbbbbbbbbbbb",
  391. -- "bbbbbbbbbbbbbbbb",
  392. -- "bbbbbbbbbbbbbbbb",
  393. -- "bbbbbbbbbbbbbbbb",
  394. -- "bbbbbbbbbbbbbbbb",
  395. -- "bbbbbbbbbbbbbbbb"},
  396. {start=vec3(8,1,8),tiles=
  397. {"BBBBBBBBBBBBBBBB",
  398. "BBBBBBBBBBBBBBBB",
  399. "BBBBBBBBBBBBBBBB",
  400. "BBBBBBBBBBBBBBBB",
  401. "BBBBBBBBBBBBBBBB",
  402. "BBBBBBBBBBBBBBBB",
  403. "BBBBBBBBBBBBBBBB",
  404. "BBBBBBBBBBBBBBBB",
  405. "BBBBBBBBBBBBBBBB",
  406. "BBBBBBBBBBBBBBBB",
  407. "BBBBBBBBBBBBBBBB",
  408. "BBBBBBBBBBBBBBBB",
  409. "BBBBBBBBBBBBBBBB",
  410. "BBBBBBBBBBBBBBBB",
  411. "BBBBBBBBBBBBBBBB",
  412. "BBBBBBBBBBBBBBBB"}
  413. }
  414. }
  415.  
  416.  
  417. --# Maps2
  418. --Maps
  419. local sceneWidth,sceneDepth,block,prevTileCode
  420.  
  421. function CreateScene(map)
  422. M=map
  423. local s=M.spawn[math.random(1,#M.spawn)]
  424. playerPos=PosToPixel(s[1])
  425. lookAngle=math.rad(s[2])
  426. look=vec3(math.cos(lookAngle),0,-math.sin(lookAngle))
  427. mapSize=map.mapSize*map.tileSize
  428. margin=map.tileSize/2
  429. spriteSheet=readImage(map.spriteSheet)
  430. spriteWidth,spriteHeight=spriteSheet.width,spriteSheet.height
  431. --surface mesh
  432. surface=CreateSurface(M.images.surface)
  433. --build fence
  434. fence=BuildFence(M.images.fence)
  435. --build basic objects
  436. objects=BuildObjects()
  437. --read map and create mesh containing objects
  438. objMesh=ReadMap()
  439. print(objMesh.size)
  440. local x,y,z=M.tileSize*M.mapSize.x/2,M.tileSize*8,M.tileSize*M.mapSize.y
  441. CullDupFaces(objMesh)
  442. print(objMesh.size)
  443. end
  444.  
  445. function UpdateSettings(p)
  446. local t=PixelToPos(p)
  447. local m=memMap[t.x*1000000+t.y*1000+t.z] or "default"
  448. if m~=prevTileCode then
  449. prevTileCode=m
  450. if M.tileSettings[m] then
  451. ambient=M.tileSettings[m].ambient
  452. diffuse=M.tileSettings[m].diffuse
  453. mist=M.tileSettings[m].mist
  454. end
  455. end
  456. end
  457.  
  458. function DrawScene(cam,look,mist)
  459. --local cam4=vec4(cam.x,cam.y,cam.z,1)
  460. --SetLighting(surface,cam4,mist)
  461. surface.shader.intensity=lightIntensity
  462. surface:draw()
  463. --SetLighting(fence,cam4,mist)
  464. fence.shader.intensity=lightIntensity
  465. fence:draw()
  466. --SetLighting(objMesh,cam4,mist)
  467. objMesh.shader.ambient=ambient
  468. objMesh.shader.diffuse=diffuse
  469. objMesh.shader.intensity=lightIntensity
  470. objMesh.shader.mModel=modelMatrix()
  471. objMesh:draw()
  472. end
  473.  
  474. function SetLighting(m,cam4,mist)
  475. m.shader.intensity=lightIntensity
  476. m.shader.mModel=modelMatrix()
  477. --m.shader.cam=cam4
  478. --m.shader.mist=mist
  479. end
  480.  
  481. function CullDupFaces(m,seq)
  482. local bv=m:buffer("position")
  483. local bt=m:buffer("texCoord")
  484. local bn=m:buffer("normal")
  485. local bc=m:buffer("color")
  486. local A,B={},{}
  487. local r=100000
  488. for i=1,m.size,6 do
  489. local c=bv[i]+bv[i+1]+bv[i+2]+bv[i+4]
  490. local cc=math.floor(r+c.x*100+0.5)..","..math.floor(r+c.y*100+0.5)..","..math.floor(r+c.z*100+0.5)
  491. A[cc]=(A[cc] or 0)+1
  492. for j=0,5 do B[i+j]=cc end
  493. end
  494.  
  495. local v,t,n,c={},{},{},{}
  496. local u=0
  497. for i=1,m.size do
  498. if A[B[i]]==1 then
  499. u=u+1
  500. v[u]=bv[i]
  501. t[u]=bt[i]
  502. n[u]=bn[i]
  503. if bc then c[u]=bc[i] end
  504. end
  505. end
  506. m.vertices=v
  507. m.texCoords=t
  508. m.normals=n
  509. if bc then m.colors=c end
  510. end
  511.  
  512. function ReadMap()
  513. memMap={}
  514. local A={}
  515. A.v,A.t,A.n,A.c={},{},{},{}
  516. for _,a in pairs(M.mapData) do
  517. local p0=PosToPixel(a.start) --bottom left tile location
  518. local n=string.len(a.tiles[1])
  519. for i=#a.tiles,1,-1 do
  520. for j=1,n do
  521. e=string.sub(a.tiles[i],j,j)
  522. memMap[(a.start.x+j-1)*1000000+a.start.y*1000+(a.start.z+i-1)]=e
  523. local f=objects[e]
  524. if f then
  525. --memMap[(a.start.x+j-1)*1000000+a.start.y*1000+(a.start.z+i-1)]=e
  526. for k=1,#f.v do
  527. A.v[#A.v+1]=f.v[k]+p0+vec3(j-1,0,-i+1)*M.tileSize
  528. A.t[#A.t+1]=f.t[k]
  529. A.n[#A.n+1]=f.n[k]
  530. A.c[#A.c+1]=f.c[k]
  531. end
  532. end
  533. end
  534. end
  535. end
  536. --fill in fence around edge in memMap
  537. for i=0,M.mapSize.x+1 do
  538. memMap[i*1000000]="_"
  539. memMap[i*1000000+M.mapSize.y+1]="_"
  540. end
  541. for i=0,M.mapSize.y+1 do
  542. memMap[i]="_"
  543. memMap[(M.mapSize.x+1)*1000000+i]="_"
  544. end
  545. --create mesh
  546. local m=mesh()
  547. m.vertices=A.v
  548. m.texCoords=A.t
  549. m.texture=mapImage
  550. m.normals=A.n
  551. m.colors=A.c
  552. m.shader=shader(Diffuse.v,Diffuse.f)
  553. m.shader.lightDirection=lightDirection
  554. m.shader.intensity=lightIntensity
  555. m.shader.ambient=ambient
  556. m.shader.diffuse=diffuse
  557. m.shader.mistColor=mistColor
  558. return m
  559. end
  560.  
  561. function CreateSurface(img)
  562. local w,d=mapSize.x,mapSize.y
  563. local x1,x2=-w/2,w/2
  564. local y=-0.1
  565. local z1,z2=0,-d
  566. local v={vec3(x1,y,z1),vec3(x2,y,z1),vec3(x2,y,z2),vec3(x1,y,z2)}
  567. local tw,td=w/img[3]/img[5],d/img[4]/img[5]
  568. local t={vec2(0,0),vec2(tw,0),vec2(tw,td),vec2(0,td)}
  569. local m=mesh()
  570. m.vertices={v[1],v[2],v[3],v[3],v[4],v[1]}
  571. m.texCoords={t[1],t[2],t[3],t[3],t[4],t[1]}
  572. local c=vec4(img[1]/spriteWidth,img[2]/spriteHeight,img[3]/spriteWidth,img[4]/spriteHeight)
  573. m.colors={c,c,c,c,c,c}
  574. m.texture=M.spriteSheet
  575. m.shader=shader(Surface.v,Surface.f)
  576. m.shader.light=ambient+diffuse
  577. m.shader.mistColor=mistColor
  578. return m
  579. end
  580.  
  581. function BuildFence(img)
  582. local w,d,h0=mapSize.x,mapSize.y,-0.1
  583. local v11,v21,v22,v12=vec3(-w/2,h0,0),vec3(w/2,h0,0),vec3(w/2,h0,-d),vec3(-w/2,h0,-d)
  584. local h=vec3(0,M.tileSize,0)
  585. local m=mesh()
  586. m.vertices={v11,v21,v21+h,v21+h,v11+h,v11, v11,v12,v12+h,v12+h,v11+h,v11,
  587. v12,v22,v22+h,v22+h,v12+h,v12, v21,v22,v22+h,v22+h,v21+h,v21}
  588. local tw,th,td=w/img[3]/img[5],M.tileSize/img[4]/img[5],d/img[4]/img[5]
  589. local t11, t21, t22, t12 =vec2(0,0),vec2(tw,0),vec2(tw,th),vec2(0,th)
  590. local t11d,t21d,t22d,t12d=vec2(0,0),vec2(td,0),vec2(td,th),vec2(0,th)
  591. m.texCoords={t11 ,t21 ,t22 ,t22 ,t12 ,t11 , t11 ,t21 ,t22 ,t22 ,t12 ,t11,
  592. t11d,t21d,t22d,t22d,t12d,t11d, t11d,t21d,t22d,t22d,t12d,t11d}
  593. local c=vec4(img[1]/spriteWidth,img[2]/spriteHeight,img[3]/spriteWidth,img[4]/spriteHeight)
  594. local col={} for i=1,24 do col[i]=c end
  595. m.colors=col
  596. m.texture=M.spriteSheet
  597. m.shader=shader(Surface.v,Surface.f)
  598. m.shader.light=ambient+diffuse
  599. m.shader.mistColor=mistColor
  600. return m
  601. end
  602.  
  603. function BuildObjects()
  604. B={} --object list
  605. for a,b in pairs(M.objects) do
  606. if b.type=="block5" then B[a]=CreateBlock(M.images[b.image],false)
  607. elseif b.type=="block6" then B[a]=CreateBlock(M.images[b.image],true)
  608. elseif b.type=="jump" then B[a]=CreateBillboard(M.images[b.image],"bottom")
  609. end
  610. end
  611. return B
  612. end
  613.  
  614.  
  615. function CreateBlock(img,bottom)
  616. local s=M.tileSize
  617. local s2=s/2
  618. local v={vec3(-s2,0, s2),vec3(s2,0, s2),vec3(s2,s, s2),vec3(-s2,s, s2),
  619. vec3(-s2,0,-s2),vec3(s2,0,-s2),vec3(s2,s,-s2),vec3(-s2,s,-s2)}
  620. local vert={v[1],v[2],v[3],v[3],v[4],v[1],v[6],v[5],v[8],v[8],v[7],v[6],
  621. v[2],v[6],v[7],v[7],v[3],v[2],v[5],v[1],v[4],v[4],v[8],v[5],
  622. v[8],v[4],v[3],v[3],v[7],v[8],v[5],v[6],v[2],v[2],v[1],v[5]}
  623. local tw,th=s/img[3]/img[5],s/img[4]/img[5]
  624. local t={vec2(0,0),vec2(tw,0),vec2(tw,th),vec2(0,th)}
  625. local tex={t[1],t[2],t[3],t[3],t[4],t[1],t[1],t[2],t[3],t[3],t[4],t[1],
  626. t[1],t[2],t[3],t[3],t[4],t[1],t[1],t[2],t[3],t[3],t[4],t[1],
  627. t[1],t[2],t[3],t[3],t[4],t[1],t[1],t[2],t[3],t[3],t[4],t[1]}
  628. local n={vec3(0,0,1),vec3(0,0,-1),vec3(1,0,0),vec3(-1,0,0),vec3(0,1,0),vec3(0,-1,0)}
  629. local norm,col={},{}
  630. local c=vec4(img[1]/spriteWidth,img[2]/spriteHeight,img[3]/spriteWidth,img[4]/spriteHeight)
  631. for i=1,#n do
  632. for j=1,6 do
  633. norm[#norm+1]=n[i]
  634. col[#col+1]=c
  635. end
  636. end
  637. if bottom~=true then
  638. for i=1,6 do
  639. table.remove(vert,#vert)
  640. table.remove(tex,#tex)
  641. table.remove(norm,#norm)
  642. table.remove(col,#col)
  643. end
  644. end
  645. return {v=vert,t=tex,n=norm,c=col}
  646. end
  647.  
  648. function AddBlock(p,tbl,obj)
  649. pos=PosToPixel(p)
  650. for i=1,#obj.v do
  651. tbl.v[#bb.v+1]=obj.v[i]+pos
  652. tbl.t[#bb.t+1]=obj.t[i]
  653. tbl.n[#bb.n+1]=obj.n[i]
  654. tbl.c[#bb.c+1]=obj.c[i]
  655. end
  656. return tbl
  657. end
  658.  
  659. function CreateBillboard(img,pos,s)
  660. if s==nil then s=M.tileSize/math.max(img[3],img[4]) end
  661. local w,h=img[3]*s,img[4]*s print(w,h)
  662. local v11,v21,v22,v12,n
  663. if pos=="bottom" then
  664. local m=0.1
  665. v11,v21,v22,v12=vec3(-w/2,m,-h/2),vec3(w/2,m,-h/2),vec3(w/2,m,h/2),vec3(-w/2,m,h/2)
  666. n=vec3(0,1,0)
  667. end
  668. local t11,t21,t22,t12=vec2(0,0),vec2(1,0),vec2(1,1),vec2(0,1)
  669. local vert={v11,v21,v22,v22,v12,v11}
  670. local tex ={t11,t21,t22,t22,t12,t11}
  671. local norm={} for i=1,6 do norm[i]=n end
  672. local c=GetTextureVector(img)
  673. local col={} for i=1,6 do col[i]=c end
  674. return {v=vert,t=tex,n=norm,c=col}
  675. end
  676.  
  677.  
  678. function AddBillboard(p,tbl,obj)
  679. pos=PosToPixel(p)
  680. for i=1,#obj.v do
  681. tbl.v[#bb.v+1]=obj.v[i]+pos
  682. tbl.t[#bb.t+1]=obj.t[i]
  683. tbl.n[#bb.n+1]=obj.n[i]
  684. tbl.c[#bb.c+1]=obj.c[i]
  685. end
  686. return tbl
  687. end
  688.  
  689. function GetTextureVector(img)
  690. return color(img[1]/spriteSheet.width,img[2]/spriteSheet.height,
  691. img[3]/spriteSheet.width,img[4]/spriteSheet.width)*255
  692. end
  693.  
  694. function PosToPixel(p)
  695. return vec3(p.x-0.5-M.mapSize.x/2,p.y,-p.z+0.5)*M.tileSize
  696. end
  697.  
  698. function PixelToPos(p)
  699. return vec3(math.floor((p.x+mapSize.x/2)/M.tileSize)+1,math.floor(p.y/M.tileSize),
  700. math.floor(-p.z/M.tileSize)+1)
  701. end
  702.  
  703. function PreventCollision(p0,p1)
  704. local t=PixelToPos(p1)
  705. local m=memMap[t.x*1000000+t.y*1000+t.z]
  706. if m=="b" then
  707. --calculate direction of movement by using tile positions
  708. local v=PixelToPos(p0)-PixelToPos(p1)
  709. --move back towards previous tile
  710. p1=p0+vec3(v.x,0,-v.z)*M.tileSize/3
  711. end
  712. return p1
  713. end
  714.  
  715. --# Objects
  716. --Objects
  717.  
  718. Objects={}
  719.  
  720. JumpPad=class()
  721.  
  722. function JumpPad:init(p,h)
  723. self.pos=p
  724. self.height=h
  725. table.insert(Objects,self)
  726. end
  727.  
  728. function JumpPad:draw()
  729.  
  730. end
  731.  
  732.  
  733. --# ShaderMist
  734. --Shaders
  735.  
  736. Diffuse={
  737. v=[[
  738. uniform mat4 modelViewProjection;
  739. uniform mat4 mModel;
  740. uniform float ambient;
  741. uniform float diffuse;
  742. uniform vec3 lightDirection;
  743. attribute vec4 position;
  744. attribute vec4 color;
  745. attribute vec2 texCoord;
  746. attribute vec3 normal;
  747. varying highp vec2 vTexCoord;
  748. varying lowp vec4 vColor;
  749. varying lowp float vLight;
  750. varying lowp vec4 vPosition;
  751.  
  752. void main()
  753. {
  754. vTexCoord=texCoord;
  755. vColor=color;
  756. vPosition = mModel * position;
  757. vec4 n = normalize(mModel * vec4(normal,0.0));
  758. float d = clamp(dot(n.xyz, lightDirection ),0.0,1.0);
  759. vLight = (ambient + diffuse * d) ;
  760. gl_Position = modelViewProjection * position;
  761. }
  762.  
  763. ]],
  764. f=[[
  765. precision highp float;
  766. uniform lowp sampler2D texture;
  767. uniform float mist;
  768. uniform vec4 mistColor;
  769. uniform vec4 cam;
  770. uniform float intensity;
  771. varying highp vec2 vTexCoord;
  772. varying lowp vec4 vColor;
  773. varying lowp float vLight;
  774. varying lowp vec4 vPosition;
  775.  
  776. void main()
  777. {
  778. float x=vColor.r+vColor.b*mod(vTexCoord.x,1.0);
  779. float y=vColor.g+vColor.a*mod(vTexCoord.y,1.0);
  780. lowp vec4 col = texture2D(texture, vec2(x,y));
  781. float f = min(1.0, distance(vPosition,cam)/mist);
  782. col = col * (1.0-f) * vLight + mistColor * f;
  783. col.a=1.0;
  784. gl_FragColor=col;
  785. }
  786. ]]
  787. }
  788.  
  789. Surface={
  790. v=[[
  791. uniform mat4 modelViewProjection;
  792. uniform mat4 mModel;
  793. attribute vec4 position;
  794. attribute vec2 texCoord;
  795. attribute vec4 color;
  796. varying highp vec2 vTexCoord;
  797. varying lowp vec4 vColor;
  798. varying lowp vec4 vPosition;
  799.  
  800. void main()
  801. {
  802. vTexCoord=texCoord;
  803. vPosition = mModel * position;
  804. vColor=color;
  805. gl_Position = modelViewProjection * position;
  806. }
  807.  
  808. ]],
  809. f=[[
  810. precision highp float;
  811. uniform lowp sampler2D texture;
  812. uniform float light;
  813. uniform float intensity;
  814. uniform float mist;
  815. uniform vec4 mistColor;
  816. uniform vec4 cam;
  817. varying highp vec2 vTexCoord;
  818. varying lowp vec4 vColor;
  819. varying lowp vec4 vPosition;
  820.  
  821. void main()
  822. {
  823. float x=vColor.r+vColor.b*mod(vTexCoord.x,1.0);
  824. float y=vColor.g+vColor.a*mod(vTexCoord.y,1.0);
  825. lowp vec4 col = texture2D(texture, vec2(x,y));
  826. float f = min(1.0, distance(vPosition,cam)/mist);
  827. col = col * (1.0-f) *light * intensity + mistColor * f;
  828. col.a=1.0;
  829. col.a=1.0;
  830. gl_FragColor=col;
  831. }
  832. ]]
  833. }
  834.  
  835. --# Shaders
  836. --Shaders
  837.  
  838. Diffuse={
  839. v=[[
  840. uniform mat4 modelViewProjection;
  841. uniform mat4 mModel;
  842. uniform float ambient;
  843. uniform float diffuse;
  844. uniform vec3 lightDirection;
  845. uniform float intensity;
  846. attribute vec4 position;
  847. attribute vec4 color;
  848. attribute vec2 texCoord;
  849. attribute vec3 normal;
  850. varying highp vec2 vTexCoord;
  851. varying lowp vec4 vColor;
  852. varying lowp float vLight;
  853.  
  854. void main()
  855. {
  856. vTexCoord=texCoord;
  857. vColor=color;
  858. vec4 n = normalize(mModel * vec4(normal,0.0));
  859. vLight = (ambient + diffuse * clamp(dot(n.xyz, lightDirection ),0.0,1.0)) * intensity ;
  860. gl_Position = modelViewProjection * position;
  861. }
  862.  
  863. ]],
  864. f=[[
  865. precision highp float;
  866. uniform lowp sampler2D texture;
  867. varying highp vec2 vTexCoord;
  868. varying lowp vec4 vColor;
  869. varying lowp float vLight;
  870.  
  871. void main()
  872. {
  873. lowp vec4 col = texture2D(texture, vec2(vColor.r+vColor.b*mod(vTexCoord.x,1.0),vColor.g+vColor.a*mod(vTexCoord.y,1.0))) * vLight;
  874. col.a=1.0;
  875. gl_FragColor=col;
  876. }
  877. ]]
  878. }
  879.  
  880. Surface={
  881. v=[[
  882. uniform mat4 modelViewProjection;
  883. attribute vec4 position;
  884. attribute vec2 texCoord;
  885. attribute vec4 color;
  886. varying highp vec2 vTexCoord;
  887. varying lowp vec4 vColor;
  888.  
  889. void main()
  890. {
  891. vTexCoord=texCoord;
  892. vColor=color;
  893. gl_Position = modelViewProjection * position;
  894. }
  895.  
  896. ]],
  897. f=[[
  898. precision highp float;
  899. uniform lowp sampler2D texture;
  900. uniform float intensity;
  901. varying highp vec2 vTexCoord;
  902. varying lowp vec4 vColor;
  903.  
  904. void main()
  905. {
  906. lowp vec4 col = texture2D(texture, vec2(vColor.r+vColor.b*mod(vTexCoord.x,1.0),vColor.g+vColor.a*mod(vTexCoord.y,1.0)))*intensity;
  907. col.a=1.0;
  908. gl_FragColor=col;
  909. }
  910. ]]
  911. }
  912.  
  913. --# Utility
  914. --Utility
  915.  
  916.  
  917.  
  918. function CullFaces(meshes,views)
  919. --loop through meshes provided
  920. local m={}
  921. for u=1,#meshes do
  922. m[u],c=mesh(),{}
  923. --copy vertices to new mesh
  924. local vv=meshes[u]:buffer("position")
  925. local v={}
  926. for i=1,meshes[u].size do v[i]=vv[i] end
  927. m[u].vertices=v
  928. --set colours with code (r=mesh id, g*255+b = face id)
  929. local n=0
  930. for i=1,meshes[u].size,3 do
  931. n=n+1 --face count
  932. local b=math.fmod(n,255)
  933. local g=(n-b)/255
  934. c[i]=color(u,g,b)
  935. c[i+1],c[i+2]=c[i],c[i]
  936. --print(u,n,g,b)
  937. end
  938. m[u].colors=c
  939. end
  940. --draw image to memory
  941. local img=image(WIDTH,HEIGHT)
  942. f={}
  943. for _,v in pairs(views) do
  944. setContext(img,true)
  945. perspective()
  946. camera(v[1],v[2],v[3],v[4],v[5],v[6])
  947. for i=1,#m do m[i]:draw() end
  948. setContext()
  949. --read visible faces out of image
  950. for i=1,WIDTH do
  951. for j=1,HEIGHT do
  952. local u,g,b,a=img:get(i,j)
  953. if b>0 and a==255 then
  954. local ff=g*255+b
  955. f[u]=f[u] or {}
  956. f[u][ff]=(f[u][ff] or 0) + 1
  957. end
  958. end
  959. end
  960. end
  961. for u=1,#meshes do
  962. local mv,mt=meshes[u]:buffer("position"),meshes[u]:buffer("texCoord")
  963. local mc,mn=meshes[u]:buffer("color"), meshes[u]:buffer("normal")
  964. local v,t,c,n={},{},{},{}
  965. local j=0
  966. for i=1,meshes[u].size,3 do
  967. j=j+1
  968. if f[u] and f[u][j] and f[u][j]>20 then
  969. --print(u,j,f[u][j])
  970. --print(mv[i]) print(mv[i+1]) print(mv[i+2])
  971. v[#v+1]=mv[i] v[#v+1]=mv[i+1] v[#v+1]=mv[i+2]
  972. if mt then t[#t+1]=mt[i] t[#t+1]=mt[i+1] t[#t+1]=mt[i+2] end
  973. if mc then c[#c+1]=mc[i] c[#c+1]=mc[i+1] c[#c+1]=mc[i+2] end
  974. if mn then n[#n+1]=mn[i] n[#n+1]=mn[i+1] n[#n+1]=mn[i+2] end
  975. end
  976. end
  977. meshes[u].vertices=v
  978. if mt then meshes[u].texCoords=t end
  979. if mc then meshes[u].colors=c end
  980. if mn then meshes[u].normals=n end
  981. end
  982. end
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990. --# Controls
  991. --Controls
  992.  
  993. --contains Joystick controls and Tilting controls
  994.  
  995. --1. Tilting the iPad
  996.  
  997. --This is as easy as just returning the Gravity values, which range from -1 to +1 for both x and y
  998. --You may want to make all three directions incremental [self.mode in Flight tab = (1,1,1)] so you
  999. --can control how much the plane reacts to tilting, and so you can do 360 degree rolls without
  1000. --dislocating your shoulders
  1001. function Tilt()
  1002. return vec2(Gravity.x,Gravity.y)
  1003. end
  1004.  
  1005. --2. Joystick
  1006.  
  1007. JoyStick = class()
  1008.  
  1009. --Note all the options you can set below. Pass them through in a named table
  1010. function JoyStick:init(t)
  1011. t = t or {}
  1012. self.radius = t.radius or 100 --size of joystick on screen
  1013. self.stick = t.stick or 30 --size of inner circle
  1014. self.centre = t.centre or self.radius * vec2(1,1) + vec2(5,5)
  1015. self.position = vec2(0,0) --initial position of inner circle
  1016. self.target = vec2(0,0) --current position of inner circle (used when we interpolate movement)
  1017. self.value = vec2(0,0)
  1018. self.delta = vec2(0,0)
  1019. self.mspeed = 120
  1020. self.moving = 0
  1021. end
  1022.  
  1023. function JoyStick:draw()
  1024. ortho()
  1025. viewMatrix(matrix())
  1026. pushStyle()
  1027. fill(160, 182, 191, 50)
  1028. stroke(118, 154, 195, 100)
  1029. strokeWidth(1)
  1030. ellipse(self.centre.x,self.centre.y,2*self.radius)
  1031. fill(78, 131, 153, 50)
  1032. ellipse(self.centre.x+self.position.x, self.centre.y+self.position.y, self.stick*2)
  1033. popStyle()
  1034. end
  1035.  
  1036. function JoyStick:touched(t)
  1037. if t.state == BEGAN then
  1038. local v = vec2(t.x,t.y)
  1039. if v:dist(self.centre)<self.radius-self.stick then
  1040. self.touch = t.id
  1041. end
  1042. end
  1043. if t.id == self.touch then
  1044. if t.state~=ENDED then
  1045. local v = vec2(t.x,t.y)
  1046. if v:dist(self.centre)>self.radius-self.stick then
  1047. v = (v - self.centre):normalize()*(self.radius - self.stick) + self.centre
  1048. end --set x,y values for joy based on touch
  1049. self.target=v - self.centre
  1050. else --reset joystick to centre when touch ends
  1051. self.target=vec2(0,0)
  1052. self.touch = false
  1053. end
  1054. end
  1055. end
  1056.  
  1057. function JoyStick:update()
  1058. local p = self.target - self.position
  1059. if p:len() < DeltaTime * self.mspeed then
  1060. self.position = self.target
  1061. if not self.touch then
  1062. if self.moving ~= 0 then
  1063. self.moving = self.moving - 1
  1064. end
  1065. else
  1066. self.moving = 2
  1067. end
  1068. else
  1069. self.position = self.position + p:normalize() * DeltaTime * self.mspeed
  1070. self.moving = 2
  1071. end
  1072. return self.position/(self.radius - self.stick)
  1073. end
  1074.  
  1075. function JoyStick:isMoving()
  1076. return self.moving
  1077. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement