Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gamescene = gideros.class(Sprite)
- function gamescene:init()
- self:addEventListener("exitBegin", self.onExitBegin, self)
- print("gamescene")
- b2.setScale(7)
- Level = TiledAsWorldEditor.new("level.lua")
- gamesceneSelf = self
- self:addChild(Level)
- self.lander = self:lander(600, 50)
- currentscene = gamescene
- paused = false
- contact = false
- destroy = false
- destroyed = false
- gamesceneSelf:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
- menubg = Bitmap.new(Texture.new("images/menubg.png"))
- menubg:setPosition(200, 120)
- mousedown = false
- --Level:EnableDebugDrawing(1)
- fuelbar = DT_HealthBar.new({
- width = 200,
- height = 20,
- front_color = 0x48b6db,
- back_color = 0x5b5b5b,
- max_value = 200
- })
- self:addChild(fuelbar)
- fuelbar:setPosition(10, 210)
- fuelbar:setRotation(-90)
- self:addEventListener("openMenu", self.openMenu, self)
- Level.B2_Worlds[1]:addEventListener(Event.POST_SOLVE, test)
- Level.B2_Worlds[1]:addEventListener(Event.BEGIN_CONTACT, self.onBeginContact, self)
- Level.B2_Worlds[1]:addEventListener(Event.END_CONTACT, self.onEndContact, self)
- self:addEventListener(Event.KEY_UP, onKeyUp)
- fuel = 600
- accelerometer:start()
- self.filter = 0.2
- self.fx = 0
- self.fy = 0
- local menuButton = Button.new(Bitmap.new(Texture.new("images/menu_up.png")), Bitmap.new(Texture.new("images/menu_down.png")))
- menuButton:setPosition(740, 35)
- self:addChild(menuButton)
- menuButton:addEventListener("click",
- function()
- gamescene:openMenu()
- end
- )
- restartButton = Button.new(Bitmap.new(Texture.new("images/restart_up.png")), Bitmap.new(Texture.new("images/restart_down.png")))
- restartButton:setPosition(300, 200)
- restartButton:addEventListener("click",
- function()
- sceneManager:changeScene("gamescene", 1, SceneManager.crossfade, easing.outBack)
- end
- )
- self.worldW = 1800
- self.worldH = 480
- function self:onMouseDown(event)
- mousedown = true
- print("mousedown")
- end
- function self:onMouseMove(event)
- end
- function self:onMouseUp(event)
- mousedown = false
- print("mouseup")
- end
- self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
- self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
- self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
- end
- function gamescene:lander(x, y)
- local lander1 = Bitmap.new(Texture.new("images/lander.png"))
- local lander2 = Bitmap.new(Texture.new("images/lander2.png"))
- local lander3 = Bitmap.new(Texture.new("images/lander3.png"))
- lander1:setAnchorPoint(0.5, 0.5)
- lander2:setAnchorPoint(0.5, 0.5)
- lander3:setAnchorPoint(0.5, 0.5)
- local lander = MovieClip.new{
- {1, 1, lander1},
- {2, 4, lander2},
- {5, 7, lander3},
- }
- lander:setGotoAction(7, 2)
- function lander:switchAnimation()
- if stopped == true then
- self:gotoAndPlay(2)
- stopped = false
- end
- end
- function lander:switchAnimation2()
- self:gotoAndStop(1)
- stopped = true
- end
- lander:setPosition(x,y)
- local body = Level.B2_Worlds[1]:createBody{type = b2.DYNAMIC_BODY}
- body:setPosition(lander:getX(), lander:getY())
- body:setAngle(lander:getRotation() * math.pi/180)
- body:setLinearVelocity(5, 0)
- local polygon = b2.PolygonShape.new()
- polygon:set(0, -22, 16, 10, -16, 10)
- local fixture = body:createFixture{shape = polygon, density = 1.0,
- friction = 2, restitution = 0.01}
- lander.body = body
- lander.body.type = "lander"
- lander.body.Name = "lander"
- Level:addChild(lander)
- print("body created")
- return lander
- end
- function gamescene:onEnterFrame()
- if not paused then
- Level.B2_Worlds[1]:step(1/60, 8, 3)
- end
- fuelbar:set(fuel / 3)
- if destroyed == false and paused == false then
- local screenW = application:getContentWidth()
- local screenH = application:getContentHeight()
- local offsetX = 0;
- local offsetY = 0;
- if((self.worldW - self.lander:getX()) < screenW/2) then
- offsetX = -self.worldW + screenW
- elseif(self.lander:getX() >= screenW/2) then
- offsetX = -(self.lander:getX() - screenW/2)
- end
- Level:setX(offsetX)
- if((self.worldH - self.lander:getY()) < screenH/2) then
- offsetY = -self.worldH + screenH
- elseif(self.lander:getY()>= screenH/2) then
- offsetY = -(self.lander:getY() - screenH/2)
- end
- Level:setY(offsetY)
- if fuel >= 0 and mousedown == true then
- local forcex = math.cos(self.lander.body:getAngle() - math.pi / 2)
- local forcey = math.sin(self.lander.body:getAngle() - math.pi / 2)
- self.lander.body:applyLinearImpulse(forcex * 0.8, forcey * 0.8 , self.lander:getX(), self.lander:getY())
- print("angle:", self.lander.body:getAngle(), "x:", forcex, "y:", forcey, "fuel:", fuel)
- self.lander:switchAnimation()
- fuel = (fuel - 2)
- elseif fuel < 0 or mousedown == false then
- self.lander:switchAnimation2()
- end
- if contact == false then
- local x, y = accelerometer:getAcceleration()
- self.fx = x * self.filter + self.fx * (1 - self.filter)
- self.fy = y * self.filter + self.fy * (1 - self.filter)
- self.lander.body:setAngularVelocity(-self.fy * 8)
- end
- if destroy == true then
- destroyLander()
- end
- end
- end
- function destroyLander()
- destroyed = true
- Level:removeChild(gamesceneSelf.lander)
- Level.B2_Worlds[1]:destroyBody(gamesceneSelf.lander.body)
- print("game over!")
- end
- function gamescene:onBeginContact(e)
- local fixtureA = e.fixtureA
- local fixtureB = e.fixtureB
- local bodyA = fixtureA:getBody()
- local bodyB = fixtureB:getBody()
- if bodyA.Name == "Terrain" and bodyB.type == "lander" then
- print("contact")
- contact = true
- elseif bodyB.Name == "Terrain" and bodyA.type == "lander" then
- print("contact")
- contact = true
- end
- end
- function gamescene:onEndContact(e)
- local fixtureA = e.fixtureA
- local fixtureB = e.fixtureB
- local bodyA = fixtureA:getBody()
- local bodyB = fixtureB:getBody()
- if bodyA.Name == "Terrain" and bodyB.type == "lander" then
- contact = false
- print("no contact")
- elseif bodyB.Name == "Terrain" and bodyA.type == "lander" then
- contact = false
- print("no contact")
- end
- end
- function test(event)
- if event.maxImpulse > 30 then
- print("Impulse:", event.maxImpulse)
- if contact == true and destroyed == false then
- destroy = true
- end
- end
- end
- function gamescene:openMenu()
- if paused == false then
- TiledAsWorldEditor:Pause()
- paused = true
- print("game paused")
- gamesceneSelf:addChild(menubg)
- gamesceneSelf:addChild(restartButton)
- elseif paused == true then
- TiledAsWorldEditor:UnPause()
- paused = false
- gamesceneSelf:removeChild(menubg)
- gamesceneSelf:removeChild(restartButton)
- print("game resumed")
- end
- end
- function onKeyUp(event)
- if event.keyCode == KeyCode.MENU then
- gamescene:openMenu()
- end
- end
- function gamescene:onExitBegin()
- gamesceneSelf:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
- collectgarbage()
- print("exiting")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement