Advertisement
Guest User

Untitled

a guest
Aug 6th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. gamescene = gideros.class(Sprite)
  2.  
  3.  
  4.  
  5.  
  6. function gamescene:init()
  7.  
  8. self:addEventListener("exitBegin", self.onExitBegin, self)
  9.  
  10. print("gamescene")
  11.  
  12. b2.setScale(7)
  13.  
  14. Level = TiledAsWorldEditor.new("level.lua")
  15.  
  16.  
  17. gamesceneSelf = self
  18.  
  19.  
  20. self:addChild(Level)
  21.  
  22. self.lander = self:lander(600, 50)
  23.  
  24.  
  25.  
  26.  
  27. currentscene = gamescene
  28.  
  29.  
  30. paused = false
  31. contact = false
  32.  
  33. destroy = false
  34.  
  35.  
  36. destroyed = false
  37.  
  38.  
  39.  
  40. gamesceneSelf:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
  41.  
  42.  
  43.  
  44. menubg = Bitmap.new(Texture.new("images/menubg.png"))
  45.  
  46. menubg:setPosition(200, 120)
  47.  
  48.  
  49.  
  50.  
  51.  
  52. mousedown = false
  53. --Level:EnableDebugDrawing(1)
  54.  
  55. fuelbar = DT_HealthBar.new({
  56. width = 200,
  57. height = 20,
  58. front_color = 0x48b6db,
  59. back_color = 0x5b5b5b,
  60. max_value = 200
  61. })
  62.  
  63. self:addChild(fuelbar)
  64.  
  65. fuelbar:setPosition(10, 210)
  66. fuelbar:setRotation(-90)
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. self:addEventListener("openMenu", self.openMenu, self)
  74.  
  75. Level.B2_Worlds[1]:addEventListener(Event.POST_SOLVE, test)
  76.  
  77.  
  78. Level.B2_Worlds[1]:addEventListener(Event.BEGIN_CONTACT, self.onBeginContact, self)
  79. Level.B2_Worlds[1]:addEventListener(Event.END_CONTACT, self.onEndContact, self)
  80.  
  81. self:addEventListener(Event.KEY_UP, onKeyUp)
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. fuel = 600
  89.  
  90. accelerometer:start()
  91. self.filter = 0.2
  92. self.fx = 0
  93. self.fy = 0
  94.  
  95.  
  96. local menuButton = Button.new(Bitmap.new(Texture.new("images/menu_up.png")), Bitmap.new(Texture.new("images/menu_down.png")))
  97. menuButton:setPosition(740, 35)
  98. self:addChild(menuButton)
  99.  
  100. menuButton:addEventListener("click",
  101. function()
  102. gamescene:openMenu()
  103.  
  104. end
  105. )
  106.  
  107.  
  108. restartButton = Button.new(Bitmap.new(Texture.new("images/restart_up.png")), Bitmap.new(Texture.new("images/restart_down.png")))
  109. restartButton:setPosition(300, 200)
  110.  
  111.  
  112. restartButton:addEventListener("click",
  113. function()
  114.  
  115. sceneManager:changeScene("gamescene", 1, SceneManager.crossfade, easing.outBack)
  116. end
  117. )
  118.  
  119.  
  120.  
  121. self.worldW = 1800
  122. self.worldH = 480
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. function self:onMouseDown(event)
  132.  
  133.  
  134. mousedown = true
  135. print("mousedown")
  136.  
  137.  
  138. end
  139.  
  140.  
  141. function self:onMouseMove(event)
  142.  
  143.  
  144.  
  145. end
  146.  
  147.  
  148. function self:onMouseUp(event)
  149.  
  150. mousedown = false
  151. print("mouseup")
  152.  
  153. end
  154.  
  155. self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
  156. self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
  157. self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
  158.  
  159.  
  160.  
  161.  
  162. end
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. function gamescene:lander(x, y)
  171.  
  172.  
  173.  
  174. local lander1 = Bitmap.new(Texture.new("images/lander.png"))
  175. local lander2 = Bitmap.new(Texture.new("images/lander2.png"))
  176. local lander3 = Bitmap.new(Texture.new("images/lander3.png"))
  177.  
  178. lander1:setAnchorPoint(0.5, 0.5)
  179. lander2:setAnchorPoint(0.5, 0.5)
  180. lander3:setAnchorPoint(0.5, 0.5)
  181.  
  182. local lander = MovieClip.new{
  183. {1, 1, lander1},
  184. {2, 4, lander2},
  185. {5, 7, lander3},
  186. }
  187.  
  188. lander:setGotoAction(7, 2)
  189.  
  190. function lander:switchAnimation()
  191.  
  192. if stopped == true then
  193. self:gotoAndPlay(2)
  194.  
  195. stopped = false
  196.  
  197. end
  198.  
  199. end
  200.  
  201. function lander:switchAnimation2()
  202. self:gotoAndStop(1)
  203.  
  204. stopped = true
  205. end
  206.  
  207. lander:setPosition(x,y)
  208.  
  209.  
  210.  
  211.  
  212.  
  213. local body = Level.B2_Worlds[1]:createBody{type = b2.DYNAMIC_BODY}
  214. body:setPosition(lander:getX(), lander:getY())
  215. body:setAngle(lander:getRotation() * math.pi/180)
  216. body:setLinearVelocity(5, 0)
  217.  
  218.  
  219.  
  220. local polygon = b2.PolygonShape.new()
  221. polygon:set(0, -22, 16, 10, -16, 10)
  222. local fixture = body:createFixture{shape = polygon, density = 1.0,
  223. friction = 2, restitution = 0.01}
  224. lander.body = body
  225. lander.body.type = "lander"
  226. lander.body.Name = "lander"
  227.  
  228. Level:addChild(lander)
  229.  
  230. print("body created")
  231.  
  232.  
  233.  
  234. return lander
  235. end
  236.  
  237.  
  238. function gamescene:onEnterFrame()
  239.  
  240.  
  241.  
  242. if not paused then
  243. Level.B2_Worlds[1]:step(1/60, 8, 3)
  244.  
  245.  
  246. end
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254. fuelbar:set(fuel / 3)
  255.  
  256.  
  257.  
  258. if destroyed == false and paused == false then
  259.  
  260.  
  261. local screenW = application:getContentWidth()
  262. local screenH = application:getContentHeight()
  263. local offsetX = 0;
  264. local offsetY = 0;
  265.  
  266.  
  267. if((self.worldW - self.lander:getX()) < screenW/2) then
  268. offsetX = -self.worldW + screenW
  269. elseif(self.lander:getX() >= screenW/2) then
  270. offsetX = -(self.lander:getX() - screenW/2)
  271. end
  272.  
  273. Level:setX(offsetX)
  274.  
  275. if((self.worldH - self.lander:getY()) < screenH/2) then
  276. offsetY = -self.worldH + screenH
  277. elseif(self.lander:getY()>= screenH/2) then
  278. offsetY = -(self.lander:getY() - screenH/2)
  279. end
  280.  
  281. Level:setY(offsetY)
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291. if fuel >= 0 and mousedown == true then
  292.  
  293. local forcex = math.cos(self.lander.body:getAngle() - math.pi / 2)
  294. local forcey = math.sin(self.lander.body:getAngle() - math.pi / 2)
  295.  
  296. self.lander.body:applyLinearImpulse(forcex * 0.8, forcey * 0.8 , self.lander:getX(), self.lander:getY())
  297. print("angle:", self.lander.body:getAngle(), "x:", forcex, "y:", forcey, "fuel:", fuel)
  298.  
  299. self.lander:switchAnimation()
  300.  
  301. fuel = (fuel - 2)
  302.  
  303.  
  304.  
  305. elseif fuel < 0 or mousedown == false then
  306.  
  307. self.lander:switchAnimation2()
  308.  
  309.  
  310.  
  311.  
  312. end
  313.  
  314.  
  315. if contact == false then
  316.  
  317. local x, y = accelerometer:getAcceleration()
  318.  
  319. self.fx = x * self.filter + self.fx * (1 - self.filter)
  320. self.fy = y * self.filter + self.fy * (1 - self.filter)
  321.  
  322. self.lander.body:setAngularVelocity(-self.fy * 8)
  323.  
  324. end
  325.  
  326.  
  327. if destroy == true then
  328.  
  329. destroyLander()
  330.  
  331. end
  332.  
  333. end
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340. end
  341.  
  342. function destroyLander()
  343.  
  344. destroyed = true
  345.  
  346. Level:removeChild(gamesceneSelf.lander)
  347. Level.B2_Worlds[1]:destroyBody(gamesceneSelf.lander.body)
  348.  
  349. print("game over!")
  350.  
  351. end
  352.  
  353.  
  354. function gamescene:onBeginContact(e)
  355.  
  356. local fixtureA = e.fixtureA
  357. local fixtureB = e.fixtureB
  358. local bodyA = fixtureA:getBody()
  359. local bodyB = fixtureB:getBody()
  360.  
  361. if bodyA.Name == "Terrain" and bodyB.type == "lander" then
  362.  
  363. print("contact")
  364.  
  365.  
  366.  
  367. contact = true
  368.  
  369. elseif bodyB.Name == "Terrain" and bodyA.type == "lander" then
  370.  
  371. print("contact")
  372.  
  373.  
  374.  
  375. contact = true
  376.  
  377. end
  378.  
  379.  
  380. end
  381.  
  382.  
  383.  
  384. function gamescene:onEndContact(e)
  385.  
  386. local fixtureA = e.fixtureA
  387. local fixtureB = e.fixtureB
  388. local bodyA = fixtureA:getBody()
  389. local bodyB = fixtureB:getBody()
  390.  
  391. if bodyA.Name == "Terrain" and bodyB.type == "lander" then
  392.  
  393.  
  394.  
  395. contact = false
  396.  
  397. print("no contact")
  398.  
  399. elseif bodyB.Name == "Terrain" and bodyA.type == "lander" then
  400.  
  401.  
  402.  
  403. contact = false
  404.  
  405. print("no contact")
  406. end
  407.  
  408.  
  409. end
  410.  
  411. function test(event)
  412. if event.maxImpulse > 30 then
  413. print("Impulse:", event.maxImpulse)
  414.  
  415. if contact == true and destroyed == false then
  416. destroy = true
  417. end
  418.  
  419. end
  420. end
  421.  
  422.  
  423. function gamescene:openMenu()
  424.  
  425.  
  426.  
  427.  
  428. if paused == false then
  429. TiledAsWorldEditor:Pause()
  430. paused = true
  431. print("game paused")
  432. gamesceneSelf:addChild(menubg)
  433. gamesceneSelf:addChild(restartButton)
  434.  
  435. elseif paused == true then
  436. TiledAsWorldEditor:UnPause()
  437. paused = false
  438. gamesceneSelf:removeChild(menubg)
  439. gamesceneSelf:removeChild(restartButton)
  440. print("game resumed")
  441.  
  442.  
  443. end
  444.  
  445. end
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452. function onKeyUp(event)
  453. if event.keyCode == KeyCode.MENU then
  454.  
  455. gamescene:openMenu()
  456.  
  457. end
  458. end
  459.  
  460. function gamescene:onExitBegin()
  461. gamesceneSelf:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
  462.  
  463. collectgarbage()
  464.  
  465. print("exiting")
  466.  
  467.  
  468. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement