Advertisement
Guest User

Untitled

a guest
Feb 14th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.76 KB | None | 0 0
  1.  
  2. --# Main
  3. --The name of the project must match your Codea project name if dependencies are used.
  4. --Project: Space Jam
  5. --Version: Alpha 1.0
  6. --Comments: shuttle fighter with enemies using visible boundaries for detection
  7. function setup()
  8. displayMode(FULLSCREEN)
  9. scr = vec2(WIDTH/2,HEIGHT/2)
  10. local verts = {vec2(-22.5,-15),vec2(-22.5,15),vec2(22.5,15),vec2(22.5,-15)}
  11. ply = physics.body(POLYGON,unpack(verts))
  12. ply.type = DYNAMIC
  13. ply.interpolate = true
  14. ply.sleepingAllowed = false
  15. ply.gravityScale = 0
  16. ply.position = vec2(WIDTH/2,HEIGHT/2)
  17. plymesh = mesh()
  18. plymesh.texture = readImage("Documents:ply")
  19. plymesh:setColors(255,255,255,255)
  20. plyrect = plymesh:addRect(ply.x,ply.y,40,60)
  21. plymesh:setRectTex(plyrect,0,0,1,1)
  22. plyhp = 100
  23. Score = 0
  24. shop = 1
  25. menu = 0
  26. scroll = 0
  27. buy = 0
  28. equip1 = 0
  29. equip2 = 0
  30. shopbg = mesh()
  31. shopimg = image(215,567)
  32. shopscrl = mesh()
  33. bg = mesh()
  34. Shop:setup()
  35. bgr = bg:addRect(0,0,4000,4000)
  36. bg.texture = readImage("Documents:boundaries")
  37. bg:setRectTex(bgr,0,0,1,1)
  38.  
  39. if readLocalData("score") then
  40. Score = readLocalData("score")
  41. else
  42. Score = 0
  43. end
  44. --[[saveLocalData("score",1000)
  45. saveLocalData("it2",nil)
  46. saveLocalData("it3",nil)
  47. saveLocalData("ow",nil)]]
  48. pos = vec2(WIDTH/2, HEIGHT/2)
  49. steer = vec2(0,0)
  50. speed = 400
  51.  
  52. controller = VirtualStick {
  53. moved = function(v) steer = v end,
  54. released = function(v) steer = vec2(0,0) end
  55. }
  56. controller:activate()
  57. Etbl = {}
  58. bangle = {}
  59. for i=1,40 do
  60. local pos = vec2(math.random(-2000,2000),math.random(-2000,2000))
  61. Enemies(0)
  62. Etbl[i] = Enemies:create(pos.x,pos.y,math.random(1,2),100)
  63. if pos:dist(ply.position) < 300 then
  64. Etbl[i][3].position = Etbl[i][3].position + (Etbl[i][3].position-ply.position):normalize()*(300-Etbl[i][3].position:dist(ply.position))
  65. end
  66. bangle[i] = math.random(0,360)
  67. end
  68. local verts = {vec2(-2000,-2000),vec2(-2000,2000),vec2(2000,2000),vec2(2000,-2000),vec2(-2000,-2000)}
  69. walls = physics.body(CHAIN,false,unpack(verts))
  70.  
  71. shooting = 0
  72. shootingdir = vec2(10,0)
  73. plyb = {}
  74. plyamnt = 0
  75. plybtime = 0
  76. bulletspeed = 5
  77. firerate = 5
  78. guntype = 1
  79. explosion = nil
  80. explosions = {}
  81.  
  82. Inventory = {}
  83. Inventory[1] = 1
  84. if readLocalData("it2") ~= nil then
  85. Inventory[2] = readLocalData("it2")
  86. else
  87. Inventory[2] = nil
  88. end
  89.  
  90. if readLocalData("it3") ~= nil then
  91. Inventory[3] = readLocalData("it3")
  92. else
  93. Inventory[3] = nil
  94. end
  95.  
  96. touches = {}
  97.  
  98. val = 50
  99. ast = {}
  100. for i = 1,val do
  101. local size = math.random(20,100)
  102. ast[i] = {}
  103. ast[i][1] = vec2(math.random(-2000,2000),math.random(-2000,2000))
  104. ast[i][2] = size
  105. ast[i][4] = mesh()
  106. local m = ast[i][4]
  107. local verts = {}
  108. local verts2 = {}
  109. local newverts = {}
  110. local height = math.random(-2000,2000)
  111. local width = math.random(-2000,2000)
  112. local p = {}
  113. local a = 9.5
  114. local size = math.random(5,15)/10
  115. for i = 1,60 do
  116. local rndm = math.random(65,100)
  117. local pos = vec2(math.sin(i/a-(90/57.3))*rndm*size+width,math.cos(i/a-(90/57.3))*rndm*size+height)
  118. if pos:dist(vec2(width,height)) > 90*size then
  119. table.insert(p,pos)
  120. end
  121. end
  122. table.insert(p,p[1])
  123. m:setColors(150,150,150,255)
  124. m.vertices = triangulate(p)
  125. m:setColors(150,150,150,255)
  126. end
  127.  
  128. sbutton = mesh()
  129. srect = sbutton:addRect(150,125,200,150)
  130. sbutton.texture = readImage("Documents:fireb")
  131. sbutton:setColors(255,255,255,150)
  132.  
  133. button = {}
  134. brect = {}
  135. button[1] = mesh()
  136. brect[1] = button[1]:addRect(125,300,150,100)
  137. button[1].texture = readImage("Documents:lasergun")
  138. button[1]:setColors(255,255,255,150)
  139.  
  140. button[2] = mesh()
  141. brect[2] = button[2]:addRect(125,450,150,100)
  142. if Inventory[2] == nil then
  143. button[2].texture = readImage("Documents:noweapon")
  144. else
  145. button[2].texture = Equipped(Inventory[2])[1]
  146. end
  147. button[2]:setColors(255,255,255,150)
  148.  
  149. button[3] = mesh()
  150. brect[3] = button[3]:addRect(125,600,150,100)
  151. if Inventory[3] == nil then
  152. button[3].texture = readImage("Documents:noweapon")
  153. else
  154. button[3].texture = Equipped(Inventory[3])[1]
  155. end
  156. button[3]:setColors(255,255,255,150)
  157.  
  158. shopbuttontex = {readImage("Documents:plasmag"),readImage("Documents:Protongun"),
  159. readImage("Documents:photong"),readImage("Documents:iongun"),readImage("Documents:blitzer")}
  160. shopbutton = {}
  161. shoprect = {}
  162. for n = 2,6 do
  163. shopbutton[n] = mesh()
  164. shoprect[n] = shopbutton[n]:addRect(107.5,640-n*160,150,150)
  165. shopbutton[n].texture = shopbuttontex[n-1]
  166. shopbutton[n]:setColors(150,150,150,255)
  167. end
  168.  
  169. m2 = mesh()
  170. mr2 = m2:addRect(1,1,1,1)
  171. m2:setColors(200,0,0,50)
  172.  
  173. shopb = mesh()
  174. shopr = shopb:addRect(1,1,100,75)
  175. shopb.texture = readImage("Documents:shopb")
  176. shopb:setRectTex(shopr,0,0,1,1)
  177. shopb:setColors(255,255,255,255)
  178. end
  179.  
  180. function angleOfPoint( pt )
  181. local x, y = pt.x, pt.y
  182. local radian = math.atan2(y,x)
  183. local angle = radian*180/math.pi
  184. if angle < 0 then angle = 360 + angle end
  185. return angle
  186. end
  187.  
  188. function Equipped(x)
  189. if x == 1 then
  190. return {readImage("Documents:lasergun"),1,5,7}
  191. elseif x == 2 then
  192. return {readImage("Documents:plasmag"),2,7,5}
  193. elseif x == 3 then
  194. return {readImage("Documents:Protongun"),3,2,12}
  195. elseif x == 4 then
  196. return {readImage("Documents:photong"),4,4,12}
  197. elseif x == 5 then
  198. return {readImage("Documents:iongun"),5,5,9}
  199. elseif x == 6 then
  200. return {readImage("Documents:blitzer"),6,3,10}
  201. end
  202. end
  203.  
  204. function changeGun(x)
  205. local gun = Equipped(x)
  206. if x == 1 then
  207. guntype = gun[2]
  208. firerate = gun[3]
  209. bulletspeed = gun[4]
  210. elseif gun then
  211. guntype = gun[2]
  212. firerate = gun[3]
  213. bulletspeed = gun[4]
  214. end
  215. end
  216.  
  217. function Size(ent)
  218. if Etbl[ent] ~= nil then
  219. local type = Etbl[ent][5]
  220. if type == 1 then return 1
  221. elseif type == 2 then return 2
  222. end
  223. else
  224. return 0
  225. end
  226. end
  227.  
  228. function moveScreen()
  229. scr.x = WIDTH/2 - ply.x
  230. scr.y = HEIGHT/2 - ply.y
  231. translate(scr.x,scr.y)
  232. end
  233.  
  234. function applyAngle(ent,ang)
  235. if ang == nil then return end
  236. ent.angularVelocity = ang
  237. end
  238. -- This function gets called once every frame
  239. function draw()
  240. if shop == 0 then
  241. if menu == 0 then
  242. -- This sets a dark background color
  243. background(40, 40, 50)
  244. -- This sets the line thickness
  245. strokeWidth(5)
  246. ----
  247.  
  248. ----
  249. pos = pos + steer*speed*DeltaTime
  250.  
  251.  
  252. ----
  253. moveScreen()
  254. for z = 1,val do
  255. ast[z][4]:draw()
  256. end
  257. bg:setRect(bgr,0,0,4000,4000)
  258. bg:draw()
  259.  
  260. if steer ~= vec2() then
  261. angle = angleOfPoint(steer)
  262. while angle - ply.angle > 180 do
  263. angle = angle - 360
  264. end
  265. while angle - ply.angle < -180 do
  266. angle = angle + 360
  267. end
  268. ang = (angle-ply.angle)/10
  269. else
  270. ang = 0
  271. end
  272.  
  273. --ply angle normalising
  274. while (ply.angle + ang) > 360 do
  275. ply.angle = ply.angle + ang - 360
  276. end
  277. while (ply.angle - ang) < 0 do
  278. ply.angle = ply.angle + ang + 360
  279. end
  280.  
  281. --car angle force
  282. applyAngle(ply,((ply.angle+ang)-ply.angle)*100 - ply.angularVelocity/2)
  283. if steer:len() > 0.1 then
  284. ply:applyForce(steer*17.5)
  285. end
  286. ply.linearVelocity = ply.linearVelocity*0.965
  287.  
  288. local angt = bangle[i]
  289.  
  290. if body ~= nil then
  291. if body.position:dist(ply.position) > 150 then
  292. bangle[i] = math.random(-50,50)*7.5
  293. body.angularVelocity = bangle[i]
  294. body:applyForce(vec2(0,1000):rotate((body.angle-90)/57.3))
  295. end
  296. body.linearVelocity = body.linearVelocity * 0.97
  297. body.angularVelocity = body.angularVelocity * 0.9
  298. mb:setRect(mbr,body.x,body.y,400,400)
  299. mb:draw()
  300. m:setRect(r,body.x,body.y,40*Size(i),30*Size(i),(body.angle)/57.3)
  301. m:draw()
  302. end
  303.  
  304. --end
  305. sbutton:setRect(srect,scr.x*-1+150,scr.y*-1+100,200,100)
  306. if shooting == 0 then
  307. sbutton:draw()
  308. end
  309. controller:draw()
  310. button[1]:setRect(brect[1],scr.x*-1+125,scr.y*-1+300,100,100)
  311. button[1]:draw()
  312. button[2]:setRect(brect[2],scr.x*-1+125,scr.y*-1+450,100,100)
  313. button[2]:draw()
  314. button[3]:setRect(brect[3],scr.x*-1+125,scr.y*-1+600,100,100)
  315. button[3]:draw()
  316. shopb:setRect(shopr,scr.x*-1+125,scr.y*-1+700,150,70)
  317. shopb:draw()
  318. plymesh:setRect(plyrect,ply.x,ply.y,50,35,ply.angle/57.3)
  319. plymesh:draw()
  320.  
  321. for k,v in pairs(explosions) do
  322. if v:isDone() then
  323. table.remove(explosions,k)
  324. else
  325. v:draw()
  326. end
  327. end
  328.  
  329. fontSize(15)
  330. stroke(150,150,150,200)
  331. fill(255,255)
  332. text("Health:",scr.x*-1+50,scr.y*-1+230)
  333. text("FPS: "..math.ceil(1/DeltaTime),scr.x*-1+54,scr.y*-1+200)
  334. text("Score: "..Score,scr.x*-1+55,scr.y*-1+170)
  335. fill(255-plyhp*2.55,plyhp*2.55,0,255)
  336. strokeWidth(4)
  337. rect(scr.x*-1+80,scr.y*-1+220,plyhp,20)
  338. elseif menu == 1 then
  339. background(40,255)
  340. strokeWidth(1)
  341. fill(75, 128, 86, 131)
  342. rect(0,0,WIDTH,HEIGHT)
  343. end
  344. elseif shop == 1 then
  345. background(0)
  346. Shop:draw()
  347. for i=1,#Etbl do
  348. if Etbl[i] ~= nil then
  349. local body = Etbl[i][3]
  350. if body.position:dist(ply.position) < 400 then
  351. body.linearVelocity = (body.position-ply.position):normalize()*100
  352. end
  353. end
  354. end
  355. end
  356. end
  357.  
  358.  
  359. --# Controller
  360.  
  361. Controller = class()
  362.  
  363. function Controller:activate()
  364. touched = function(t)
  365. self:touched(t)
  366. end
  367. end
  368.  
  369. function Controller:draw()
  370. -- nothing
  371. end
  372.  
  373. -- Utility functions
  374.  
  375. function touchPos(t)
  376. return vec2(t.x, t.y)
  377. end
  378.  
  379. function clamp(x, min, max)
  380. return math.max(min, math.min(max, x))
  381. end
  382.  
  383. function clampAbs(x, maxAbs)
  384. return clamp(x, -maxAbs, maxAbs)
  385. end
  386.  
  387. function clampLen(vec, maxLen)
  388. if vec == vec2(0,0) then
  389. return vec
  390. else
  391. return vec:normalize() * math.min(vec:len(), maxLen)
  392. end
  393. end
  394.  
  395. -- projects v onto the direction represented by the given unit vector
  396. function project(v, unit)
  397. return v:dot(unit)
  398. end
  399.  
  400. function sign(x)
  401. if x == 0 then
  402. return 0
  403. elseif x < 0 then
  404. return -1
  405. elseif x > 0 then
  406. return 1
  407. else
  408. return x -- x is NaN
  409. end
  410. end
  411.  
  412. function doNothing()
  413. end
  414.  
  415. --# VirtualStick
  416.  
  417. VirtualStick = class(Controller)
  418.  
  419. function VirtualStick:init(args)
  420. self.radius = args.radius or 100
  421. self.deadZoneRadius = args.deadZoneRadius or 25
  422. self.releasedCallback = args.released or doNothing
  423. self.steerCallback = args.moved or doNothing
  424. self.pressedCallback = args.pressed or doNothing
  425. touches = {}
  426. pos1 = vec2()
  427. pos2 = vec2()
  428. nt={}
  429. end
  430.  
  431. function abs(v2)
  432. return math.abs(v2)
  433. end
  434.  
  435. function VirtualStick:touched(t)
  436. if shop == 0 then
  437. if t.state == BEGAN and self.touchId == nil and touchPos(t).x > WIDTH/2 and touches[1] == nil then
  438. ended = 0
  439. touches[1] = t
  440. self.touchId = touches[1].id
  441. self.touchStart = vec2(WIDTH-150,150)
  442. self.stickOffset = vec2(0,0)
  443. self.pressedCallback()
  444. elseif touches[1] and t.id == touches[1].id then
  445. touches[1] = t
  446. local pos = touchPos(touches[1])
  447. if touches[1].state == MOVING then
  448. self.stickOffset = clampLen(pos - self.touchStart, self.radius)
  449. self.steerCallback(self:vector())
  450. elseif touches[1].state == ENDED or touches[1].state == CANCELLED then
  451. ended = 1
  452. self:reset()
  453. self.releasedCallback()
  454. touches[1] = nil
  455. end
  456. end
  457. if t.state == BEGAN and touchPos(t).x < WIDTH/2 and touches[2] == nil then
  458. if t.x > 50 and t.y > 50 and t.x < 250 and t.y < 150 then
  459. shooting = 1
  460. touches[2] = t
  461. sbutton:setColors(150,150,150,150)
  462. end
  463. elseif t.state == MOVING and touches[2] and t.id == touches[2].id then
  464. touches[2] = t
  465. if vec2(t.x,t.y):dist(vec2(150,100)) then
  466. shootingdir = (vec2(t.x,t.y)-vec2(150,100)):normalize()
  467. else
  468. shootingdir = vec2(0,10):rotate((ply.angle-90)/57.3):normalize()
  469. end
  470. elseif touches[2] and t.id == touches[2].id then
  471. if t.state == ENDED then
  472. shooting = 0
  473. sbutton:setColors(255,255,255,150)
  474. touches[2] = nil
  475. saveLocalData("score",Score)
  476. end
  477. end
  478. if t.state == BEGAN and touchPos(t).x < WIDTH/2 and touches[3] == nil then
  479. touches[3] = t
  480. if t.x > 50 and t.y > 250 and t.x < 200 and t.y < 350 then
  481. changeGun(Inventory[1])
  482. button[1]:setColors(150,150,154,150)
  483. end
  484. if t.x > 50 and t.y > 400 and t.x < 200 and t.y < 500 then
  485. changeGun(Inventory[2])
  486. button[2]:setColors(150,150,150,150)
  487. end
  488. if t.x > 50 and t.y > 550 and t.x < 200 and t.y < 650 then
  489. changeGun(Inventory[3])
  490. button[3]:setColors(150,150,150,150)
  491. end
  492. elseif touches[3] and t.id == touches[3].id then
  493. if t.state == ENDED then
  494. button[1]:setColors(255,255,255,150)
  495. button[2]:setColors(255,255,255,150)
  496. button[3]:setColors(255,255,255,150)
  497. touches[3] = nil
  498. if t.x > 50 and t.y > 665 and t.x < 200 and t.y < 735 then
  499. shop = 1
  500. end
  501. end
  502. end
  503. end
  504. if shop == 1 then
  505. Shop:touched(t)
  506. end
  507. end
  508.  
  509. function VirtualStick:vector()
  510. local stickRange = self.radius - self.deadZoneRadius
  511. local stickAmount = math.max(self.stickOffset:len(), 0)
  512. local stickDirection = self.stickOffset:normalize()
  513.  
  514. return stickDirection * (stickAmount/stickRange)
  515. end
  516.  
  517. function VirtualStick:reset()
  518. self.touchId = nil
  519. self.touchStart = nil
  520. self.stickOffset = nil
  521. end
  522.  
  523. function VirtualStick:draw()
  524. if self.touchId ~= nil then
  525. pushStyle()
  526. ellipseMode(RADIUS)
  527. strokeWidth(1)
  528. stroke(255, 255, 255, 255)
  529. noFill()
  530.  
  531. pushMatrix()
  532. translate(scr.x*-1+self.touchStart.x,scr.y*-1+self.touchStart.y)
  533. fill(150,150,150,100)
  534. ellipse(0, 0, self.radius, self.radius)
  535. fill(164, 164, 164, 150)
  536. ellipse(0, 0, self.deadZoneRadius, self.deadZoneRadius)
  537. fill(150,150,150,100)
  538. translate(self.stickOffset.x, self.stickOffset.y)
  539. ellipse(0, 0, 25, 25)
  540. popMatrix()
  541. popStyle()
  542. else
  543. pushStyle()
  544. ellipseMode(RADIUS)
  545. strokeWidth(3)
  546. pushMatrix()
  547. translate(scr.x*-1+WIDTH-150,scr.y*-1+150)
  548. fill(150,150,150,100)
  549. ellipse(0,0,40)
  550. popMatrix()
  551. popStyle()
  552. end
  553. if touches[2] ~= nil then
  554. pushStyle()
  555. ellipseMode(RADIUS)
  556. strokeWidth(3)
  557. pushMatrix()
  558. translate(-scr.x,-scr.y)
  559. fill(150,150,150,200)
  560. ellipse(150,100,70)
  561. stroke(100,100,100,255)
  562. strokeWidth(10)
  563. local pos = vec2(150,100)+(vec2(touches[2].x,touches[2].y)-vec2(150,100)):normalize()*clamp(vec2(touches[2].x,touches[2].y):dist(vec2(150,100)),5,70)
  564. line(pos.x,pos.y,150,100)
  565. ellipse(pos.x,pos.y,25)
  566. popMatrix()
  567. popStyle()
  568. end
  569. end
  570. --# Enemies
  571. Enemies = class()
  572.  
  573. function Enemies:init(deatht)
  574. self.dtime = deatht
  575. end
  576.  
  577. function Enemies:create(x,y,type,health)
  578. -- you can accept and set parameters here
  579. local verts = {vec2(-20,-15)*type,vec2(-20,15)*type,vec2(20,15)*type,vec2(20,-15)*type}
  580. self.body = physics.body(POLYGON,unpack(verts))
  581. --self.body.type = DYNAMIC
  582. self.body.interpolate = false
  583. self.body.sleepingAllowed = false
  584. self.body.gravityScale = 0
  585. self.body.mass = 2
  586. self.body.position = vec2(x,y)
  587. self.body.angle = math.random(0,360)/57.3
  588. self.health = health*type^1.5
  589. self.time = math.random(0,99)
  590. self.bullets = {}
  591. self.bullettime = 0
  592. self.bulletamnt = 0
  593. self.type = type
  594. self.mesh = mesh()
  595. if type == 1 then
  596. self.mesh.texture = readImage("Documents:spaceship")
  597. elseif type == 2 then
  598. self.mesh.texture = readImage("Documents:big spaceship")
  599. end
  600. self.mesh:setColors(255,255,255,255)
  601. self.rect = self.mesh:addRect(self.body.x,self.body.y,20,30,self.body.angle)
  602. self.mesh:setRectTex(self.rect,0,0,1,1)
  603. self.bmesh = mesh()
  604. self.bmesh.texture = readImage("Documents:circle")
  605. self.brect = self.bmesh:addRect(self.body.x,self.body.y,400,400)
  606. self.bmesh:setColors(255,255,255,100)
  607. self.bmesh:setRectTex(self.brect,0,0,1,1)
  608. return {self.mesh,self.rect,self.body,self.health,self.type,self.time,self.bullets,self.bullettime,self.bulletamnt,self.bmesh,self.brect}
  609. end
  610.  
  611. function Enemies:draw()
  612. -- Codea does not au
  613. end
  614.  
  615. function Enemies:touched(touch)
  616. -- Codea does not automatically call this method
  617. end
  618.  
  619. function Enemies:Die(x)
  620. local enemy = Etbl[x]
  621. local size = Size(x)
  622. Etbl[x][4] = 100*size^1.5
  623. Etbl[x][3].position = vec2(math.random(-1900,1900),math.random(-1900,1900))
  624. Score = Score + 25*size
  625. saveLocalData("score",Score)
  626. Etbl[x][1]:setColors(255,255,255,255)
  627. sound(SOUND_EXPLODE)
  628. end
  629.  
  630. --# Shop
  631. Shop = class()
  632.  
  633. function Shop:setup()
  634. -- you can accept and set parameters here
  635. self.m = mesh()
  636. self.r = self.m:addRect(WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
  637. self.m.texture = readImage("Documents:shop")
  638. self.m:setRectTex(self.r,0,0,1,1)
  639. shopbg = self.m
  640.  
  641. self.m = mesh()
  642. self.m.texture = shopimg
  643. self.r = self.m:addRect(207.5,383.5,215,567)
  644. self.m:setRectTex(self.r,0,0,1,1)
  645. shopscrl = self.m
  646.  
  647. dir = 0
  648. Selected = 0
  649. Cost = 0
  650. ydelta = 0
  651.  
  652. if readLocalData("ow") ~= nil then
  653. ownedwep = readLocalData("ow")
  654. else
  655. ownedwep = ""
  656. end
  657.  
  658. shopprices = {}
  659. shopprices[2] = 2000
  660. shopprices[3] = 3000
  661. shopprices[4] = 5000
  662. shopprices[5] = 12000
  663. shopprices[6] = 22000
  664. end
  665.  
  666. function Shop:draw()
  667. -- Codea does not automatically call this method
  668. background(40,40,40,255)
  669. shopbg:draw()
  670. if buy > 0.1 then
  671. fill(150,150,150,150)
  672. buy = buy - 0.1
  673. end
  674. if buy <= 0.1 then
  675. buy = 0
  676. end
  677. if buy == 0 then
  678. fill(255, 255, 255, 0)
  679. end
  680. rect(350,430,165,75)
  681.  
  682. if equip1 > 0.1 then
  683. fill(equip1*25.5,100)
  684. equip1 = equip1 - 0.1
  685. end
  686. if equip1 <= 0.1 then
  687. equip1 = 0
  688. end
  689. if equip1 == 0 then
  690. fill(255, 255, 255, 0)
  691. end
  692. rect(350,310,165,75)
  693.  
  694. if equip2 > 0.1 then
  695. fill(equip2*25.5+50,100)
  696. equip2 = equip2 - 0.1
  697. end
  698. if equip2 <= 0.1 then
  699. equip2 = 0
  700. end
  701. if equip2 == 0 then
  702. fill(255, 255, 255, 0)
  703. end
  704. rect(350,210,165,80)
  705. fill()
  706.  
  707.  
  708. fill(190, 190, 190, 150)
  709. strokeWidth(10)
  710. stroke(0,50,255,150)
  711. rect(WIDTH-250,45,200,70)
  712. pushMatrix()
  713. resetMatrix()
  714. setContext(shopimg)
  715. background(155, 155, 155, 255)
  716. for i = 2,#shopbutton do
  717. shopbutton[i]:setRect(shoprect[i],107.5,800-i*160+scroll,150,150)
  718. shopbutton[i]:draw()
  719. fontSize(20)
  720. if string.find(ownedwep,i) then
  721. text("Owned",107.5,742-i*160+scroll)
  722. else
  723. text("$"..shopprices[i],107.5,742-i*160+scroll)
  724. end
  725. end
  726. setContext()
  727. popMatrix()
  728. shopscrl:draw()
  729. fill(46, 46, 46, 255)
  730. fontSize(50)
  731. text("Exit",WIDTH-150,80)
  732. fontSize(30)
  733. fill(255,200)
  734. text("Score: "..Score,WIDTH-200,HEIGHT-70)
  735.  
  736. dir = dir + ydelta/20
  737. dir = dir *0.99
  738. scroll = scroll + dir
  739. if scroll < 0 then
  740. scroll = 0
  741. --dir = 0
  742. elseif scroll > 250 then
  743. scroll = 250
  744. --dir = 250
  745. end
  746. end
  747.  
  748. function Shop:touched(t)
  749. if t.state == BEGAN and t.x > WIDTH-250 and t.x < WIDTH-50 and t.y > 45 and t.y < 115 then
  750. shop = 0
  751. end
  752. for i = 2,#shopbutton do
  753. if Selected < 1 and t.state == BEGAN and vec2(t.x,t.y):dist(vec2(215,895-i*160+scroll)) < 75 then
  754.  
  755. if Score > shopprices[i] or string.find(ownedwep,i) ~= nil then
  756. shopbutton[i]:setColors(50,50,50,255)
  757. Selected = i
  758. Cost = shopprices[i]
  759. print(shopprices[i])
  760. else
  761. Selected = 0
  762. end
  763. elseif t.state == ENDED then
  764. if Selected == 0 then
  765. shopbutton[i]:setColors(150,150,150,255)
  766. end
  767. end
  768. if Selected > 0 and t.state == BEGAN and vec2(t.x,t.y):dist(vec2(215,895-i*160+scroll)) < 75 then
  769. if i ~= Selected and (Score > shopprices[i] or string.find(ownedwep,i) ~= nil) then
  770. shopbutton[Selected]:setColors(150,150,150,255)
  771. shopbutton[i]:setColors(50,50,50,255)
  772. Selected = i
  773. Cost = shopprices[i]
  774. print(shopprices[i])
  775. end
  776. end
  777. if t.state == MOVING and vec2(t.x,t.y):dist(vec2(215,895-i*160+scroll)) < 75 then
  778. if math.abs(t.deltaY) > 0.1 then
  779. ydelta = t.deltaY
  780. end
  781. end
  782. end
  783. if shopprices[Selected] and Score > shopprices[Selected] and Selected > 1 and t.state == BEGAN and t.x < 515 and t.x > 350 and t.y < 500 and t.y > 430 then
  784. if string.find(ownedwep,Selected) == nil then
  785. ownedwep = ownedwep..Selected..","
  786. saveLocalData("ow",ownedwep)
  787. Score = Score - shopprices[Selected]
  788. buy = 1
  789. saveLocalData("score",Score)
  790. end
  791. end
  792. if t.state == BEGAN and t.x < 510 and t.x > 350 and t.y < 385 and t.y > 310 and string.find(ownedwep,Selected) ~= nil and readLocalData("it3") ~= Selected then
  793. Inventory[2] = Selected
  794. button[2].texture = Equipped(Selected)[1]
  795. saveLocalData("it2",Selected)
  796. Selected = 0
  797. equip1 = 1
  798. end
  799. if t.state == BEGAN and t.x < 510 and t.x > 350 and t.y < 290 and t.y > 210 and string.find(ownedwep,Selected) ~= nil and readLocalData("it2") ~= Selected then
  800. Inventory[3] = Selected
  801. button[3].texture = Equipped(Selected)[1]
  802. saveLocalData("it3",Selected)
  803. Selected = 0
  804. equip2 = 1
  805. end
  806.  
  807. end
  808.  
  809. --# Explosion
  810. Explosion = class()
  811.  
  812. function Explosion:init(pos,size)
  813.  
  814. self.pos = pos+vec2(0,10):rotate(math.random(0,360)/57.3)
  815. self.time = 0
  816. self.size = size
  817. self.dir = {}
  818. self.rect = {}
  819. col = color(math.random(200,255),math.random(0,50),math.random(0,50))
  820. self.alpha = 255
  821.  
  822. self.mesh = mesh()
  823. self.s = {}
  824.  
  825. for i=1,10 do
  826. table.insert(self.dir,(vec2(0,1):rotate(math.rad(math.random(0,360))))*(math.random(2,5)/2)*self.size)
  827. self.mesh.texture = readImage("Tyrian Remastered:Explosion Huge")
  828. table.insert(self.rect,self.mesh:addRect(1,1,1,1))
  829. table.insert(self.s,math.random(10,40))
  830. end
  831.  
  832. end
  833.  
  834. function Explosion:draw()
  835. self.time = self.time + 0.5
  836. local p = self.pos
  837. self.mesh:setColors(255,255,255,self.alpha)
  838. for k,v in ipairs(self.dir) do
  839. kt = p + v*self.time
  840. --line(p.x,p.y,kt.x,kt.y)
  841. --ellipse(kt.x,kt.y,math.random(15,35))
  842. --local wh = self.s[k]
  843. local wh = self.s[k]+math.random(10,20)
  844. self.mesh:setRect(self.rect[k],kt.x,kt.y,wh,wh)
  845. self.mesh:draw()
  846. end
  847. end
  848.  
  849. function Explosion:touched(touch)
  850.  
  851. end
  852.  
  853. function Explosion:isDone()
  854. if self.time >= 25.5 then
  855. return true
  856. else
  857. self.alpha = 255-(10*self.time)
  858. return false
  859. end
  860. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement