michael@0: -- Initiate scene management michael@0: local sceneloc = director:createScene() michael@0: sceneloc.name = 'Scene: Begin' michael@0: michael@0: -- Calculate constants michael@0: local dsw = director.displayWidth michael@0: local dsh = director.displayHeight michael@0: local dmw = director.displayCenterX michael@0: local dmh = director.displayCenterY michael@0: local nFoodpoints = 2 michael@0: local nAsterpoints = -2 michael@0: local nCometpoints = -1 michael@0: michael@0: -- Initialize common variables michael@0: local spriteNull = director:createSprite(0, 0) michael@0: local timerHero = director:createNode() michael@0: local labReady = director:createNode() michael@0: local nHealth = 0 michael@0: local tweenOut = 0 michael@0: michael@0: -- Instantiate common fonts michael@0: local fontSegoe = director:createFont('fonts/SegoeUI-64pt_bold.fnt') michael@0: local fontArial = director:createFont('fonts/Arial-96pt_bold.fnt') michael@0: --local fontFxsys = director:createFont('fonts/Fixedsys-96pt_norm.fnt') michael@0: michael@0: -- Prepare math runtime michael@0: math.randomseed(os.time()) michael@0: math.random(); math.random(); math.random(); math.random() michael@0: michael@0: function sceneloc:setUp(event) michael@0: -- Create sprite, bottom left of image is at bottom left of display michael@0: --local spriteGalaxy = director:createSprite(0, 0, 'textures/galaxyback_720.png') michael@0: local spriteGalaxy = director:createSprite(0, 0, 'textures/galaxyback_360.png') michael@0: spriteGalaxy.xScale = 2 -- Needed due to small maximum sprite size michael@0: spriteGalaxy.yScale = 2 -- Needed due to small maximum sprite size michael@0: michael@0: -- Animate the image to scroll along the X axis over 2 minutes michael@0: funcyScroll = function(spriteIn) michael@0: if spriteIn.xFlip == false then michael@0: spriteIn.xFlip = true michael@0: else michael@0: spriteIn.xFlip = false michael@0: end michael@0: michael@0: spriteIn.x = spriteIn.x + dsh michael@0: end michael@0: michael@0: funcyGalstart = function(spriteIn) michael@0: --tween:cancel(tweenReady) michael@0: tweenReadweg = tween:dissolve(labReady, spriteNull, .5, 0) michael@0: labReady = '' michael@0: end michael@0: --tween:to(spriteGalaxy, {delay=5, time=120, x=dsw-spriteGalaxy.w, mode='repeat', onComplete=funcyScroll}) michael@0: local tweenBack = tween:to(spriteGalaxy, {delay=5, time=100, mode='repeat', x=dsw-spriteGalaxy.w*2, onStart=funcyGalstart}) -- *2 for xyScale (above) michael@0: michael@0: -- Instantiate star field particles behind hero michael@0: local partiStarfar = director:createParticles('particles/stars.plist') michael@0: partiStarfar.sourcePos = {dsw, dmh} michael@0: michael@0: -- Create hero, middle of left edge of screen michael@0: local atlasHero = director:createAtlas({ michael@0: width = 300, michael@0: height = 200, michael@0: numFrames = 8, michael@0: textureName = 'textures/heroatlas.png', michael@0: }) michael@0: michael@0: local animHero = director:createAnimation({ michael@0: start = 1, michael@0: count = 8, michael@0: atlas = atlasHero, michael@0: delay = 1/8, michael@0: }) michael@0: michael@0: local spriteHero = director:createSprite({ michael@0: name='hero', x=dsw/8, y=dmh, michael@0: xAnchor=0.5, yAnchor=0.5, michael@0: source=animHero, michael@0: }) michael@0: michael@0: -- Instantiate star field particles in front of hero michael@0: local partiStarnear = director:createParticles('particles/stars.plist') michael@0: partiStarnear.sourcePos = {dsw, dmh} michael@0: michael@0: -- Instantiate good things, like cheese michael@0: funcyFood = function(spriteIn) michael@0: --tween:cancel(tweenOut) michael@0: --spriteIn.tweens[1].delay = math.random(0, 5) michael@0: spriteIn.y = math.random(dsh/16, dsh-dsh/16) michael@0: spriteIn.isVisible = true michael@0: end michael@0: local spriteCattin = director:createSprite(dsw, dmh, 'textures/cattin.png') michael@0: local tweenCattin = tween:to(spriteCattin, {delay=math.random(1, 10), time=12, mode='repeat', x=-dsw, onComplete=funcyFood}) michael@0: spriteCattin.name = 'food' michael@0: spriteCattin.xAnchor = 0 michael@0: spriteCattin.yAnchor = 0 michael@0: spriteCattin.xScale = .5 michael@0: spriteCattin.yScale = .5 michael@0: local spriteCheese = director:createSprite(dsw, dmh, 'textures/cheesepiece.png') michael@0: local tweenCheese = tween:to(spriteCheese, {delay=math.random(1, 10), time=16, mode='repeat', x=-dsw, onComplete=funcyFood, xAnchor=0, yAnchor=0}) michael@0: spriteCheese.name = 'food' michael@0: spriteCheese.xAnchor = 0 michael@0: spriteCheese.yAnchor = 0 michael@0: spriteCheese.xScale = .8 michael@0: spriteCheese.yScale = .8 michael@0: local spriteFischkopf = director:createSprite(dsw, dmh, 'textures/fischkopfe.png') michael@0: local tweenFischkopf = tween:to(spriteFischkopf, {delay=math.random(1, 10), time=8, mode='repeat', x=-dsw, onComplete=funcyFood}) michael@0: spriteFischkopf.name = 'food' michael@0: spriteFischkopf.xAnchor = 0 michael@0: spriteFischkopf.yAnchor = 0 michael@0: spriteFischkopf.xScale = .325 michael@0: spriteFischkopf.yScale = .325 michael@0: michael@0: -- Instantiate comets and other dangerous particles michael@0: funcyStartdang = function(spriteIn) michael@0: -- Adjust delay to spread out danger objects michael@0: --spriteIn. michael@0: end michael@0: funcyComets = function(spriteIn) michael@0: local locVert = math.random(dsh/16, dsh-dsh/16) michael@0: spriteIn.tweens.delay = math.random(1, 10) michael@0: spriteIn.y = locVert michael@0: end michael@0: funcyGalies = function(spriteIn) michael@0: local locVert = math.random(dsh/16, dsh-dsh/16) michael@0: spriteIn.tweens.delay = math.random(1, 10) michael@0: spriteIn.y = locVert michael@0: end michael@0: michael@0: local posComety = math.random(dsh/16, dsh-dsh/16) michael@0: local timComety = math.random(5, 12) michael@0: local posGalaxy = math.random(dsh/16, dsh-dsh/16) michael@0: local timGalaxy = math.random(5, 12) michael@0: michael@0: -- Overlays needed for collision detection (particles arent detected) michael@0: local spriteCom = director:createSprite({name='comet', x=dsw+dsw/4, y=posComety}) michael@0: spriteCom.name = 'comet' michael@0: local tweenCom = tween:to(spriteCom, {delay=timComety, time=13, x=-dsw-dsw/4, mode='repeat', onStart=funcyStartdang, onComplete=funcyComets}) michael@0: local spriteGal = director:createSprite(dsw+dsw/4, posGalaxy) michael@0: spriteGal.name = 'comet' michael@0: local tweenGal = tween:to(spriteGal, {delay=timGalaxy, time=11, x=-dsw-dsw/4, mode='repeat', onStart=funcyStartdang, onComplete=funcyGalies}) michael@0: michael@0: local partiComet = director:createParticles('particles/comets.plist') michael@0: --partiComet.name = 'comet' michael@0: --partiComet.sourcePos = {dsw+dsw/4, posComety} michael@0: --partiComet.sourcePos = {0, 60} michael@0: --tween:to(partiComet, {delay=timComety, time=11, x=-dsw-dsw/4, mode='repeat', onStart=funcyStartdang, onComplete=funcyComets}) michael@0: local partiGalax = director:createParticles('particles/galaxies.plist') michael@0: --partiGalax.name = 'comet' michael@0: --partiGalax.sourcePos = {dsw+dsw/4, posGalaxy} michael@0: --partiGalax.sourcePos = {0, 40} michael@0: --tween:to(partiGalax, {delay=timGalaxy, time=9, x=-dsw-dsw/4, mode='repeat', onStart=funcyStartdang, onComplete=funcyGalies}) michael@0: spriteCom:addChild(partiComet) michael@0: spriteGal:addChild(partiGalax) michael@0: michael@0: -- Start audio streams michael@0: audio:loadStream('sounds/dream.mp3') michael@0: audio:playStream('sounds/dream.mp3', true) michael@0: michael@0: -- Instantiate motor smoke particles michael@0: -- Create from an initialiser list michael@0: local partiSmoke = director:createParticles({ michael@0: totalParticles=1500, michael@0: source='particles/fire.png', michael@0: emitterMode=particles.modeGravity, emitterRate=1500/3.5, michael@0: --sourcePos={dsw/8, dmh}, michael@0: sourcePos={spriteHero.w/6, spriteHero.h/2}, michael@0: sourcePosVar={0, 16}, michael@0: duration=particles.durationInfinity, michael@0: michael@0: modeGravity={ michael@0: --gravity={-90, 0}, michael@0: speed=180, speedVar=50, michael@0: radialAccel=0, radialAccelVar=0, michael@0: --tangentialAccel=0, tangentialAccelVar=40, michael@0: }, michael@0: michael@0: --angle=90, angleVar=20, michael@0: angle=180, angleVar=10, michael@0: life=4, lifeVar=2, michael@0: michael@0: --startColor={0x80, 0x80, 0x80, 0xff}, startColorVar={0x80, 0x80, 0x80, 25}, michael@0: --endColor={25, 25, 25, 50}, endColorVar={25, 25, 25, 50}, michael@0: startColor=color.white, endColor=color.black, michael@0: michael@0: startSize=24.0, startSizeVar=2.0, michael@0: endSize=particles.startSizeEqualToEndSize michael@0: }) michael@0: partiSmoke:stop() michael@0: spriteHero:addChild(partiSmoke) michael@0: michael@0: ---- Instantiate asteroids michael@0: funcyAster = function(spriteIn) michael@0: spriteIn:rotate(30) michael@0: spriteIn.y = math.random(dsh/16, dsh-dsh/16) michael@0: end michael@0: local spriteAster1 = director:createSprite(dsw, dmh, 'textures/asteroid1.png') michael@0: local tweenAster1 = tween:to(spriteAster1, {delay=math.random(5, 12), time=8, mode='repeat', x=-dsw, onComplete=funcyAster}) michael@0: spriteAster1.name = 'aster' michael@0: spriteAster1.xAnchor = 0 michael@0: spriteAster1.yAnchor = 0 michael@0: spriteAster1.xScale = .25 michael@0: spriteAster1.yScale = .25 michael@0: local spriteAster2 = director:createSprite(dsw, dmh, 'textures/asteroid2.png') michael@0: local tweenAster2 = tween:to(spriteAster2, {delay=math.random(5, 12), time=10, mode='repeat', x=-dsw, onComplete=funcyAster}) michael@0: spriteAster2.name = 'aster' michael@0: spriteAster2.xAnchor = 0 michael@0: spriteAster2.yAnchor = 0 michael@0: spriteAster2.xScale = .25 michael@0: spriteAster2.yScale = .25 michael@0: michael@0: -- Instantiate explanation text michael@0: labReady = director:createLabel({ michael@0: hAlignment='centre', michael@0: vAlignment='middle', michael@0: --font=fontFxsys, michael@0: font=fontArial, michael@0: text='Ready!', michael@0: color=color.white, michael@0: alpha=0}) michael@0: local tweenReady = tween:to(labReady, {time=.5, easing=ease.powIn, textXScale=2, textYScale=2, alpha=1}) michael@0: michael@0: -- Initialize physics simulation michael@0: physics:setGravity(0, 0) michael@0: local ptRndbox = {100,50, 260,50, 290,80, 290,120, 260,150, 80,150} michael@0: physics:addNode(spriteHero, {isSensor = true, shape=ptRndbox}) michael@0: physics:addNode(spriteCattin, {isSensor = true}) michael@0: physics:addNode(spriteCheese, {isSensor = true}) michael@0: physics:addNode(spriteFischkopf, {isSensor = true}) michael@0: --physics:addNode(partiComet, {isSensor = true}) michael@0: --physics:addNode(partiGalax, {isSensor = true}) michael@0: local ptTri = {-45,0, 75,-90, 75,90} michael@0: local ptSmal = {-30,0, 100,-20, 100,40} michael@0: physics:addNode(spriteCom, {isSensor = true, shape=ptTri}) michael@0: physics:addNode(spriteGal, {isSensor = true, shape=ptSmal}) michael@0: physics:addNode(spriteAster1, {isSensor = true}) -- Set in tween instead michael@0: physics:addNode(spriteAster2, {isSensor = true}) -- Set in tween instead michael@0: --spriteCom.physics.radius = 50 michael@0: --spriteGal.physics.radius = 50 michael@0: -- Marmalade bug if radius is different than actual perfectly round object image michael@0: spriteAster1.physics.radius = spriteAster1.w/2 michael@0: spriteAster2.physics.radius = spriteAster2.w/2 michael@0: --physics.debugDraw = true michael@0: michael@0: -- Implement collision listener michael@0: function hit(event) michael@0: if event.phase == 'began' then michael@0: --local labHit = director:createLabel({x=event.nodeA.x, y=event.nodeA.y, text=event.nodeB.name, font=fontSegoe, color=color.white}) michael@0: if event.nodeA.name == 'food' then michael@0: --tween:cancel(event.nodeA.tweens[1]) michael@0: --tweenOut = tween:dissolve(event.nodeA, spriteNull, 1, 0) michael@0: --table.insert(event.nodeA.tweens, tween:dissolve(event.nodeA, spriteNull, 1, 0)) michael@0: --event.nodeA.x = math.random(dsw/16, dsw-dsw/16) michael@0: --event.nodeA.y = math.random(dsh/16, dsh-dsh/16) michael@0: event.nodeA.isVisible = false michael@0: audio:playSound('sounds/belch.pcm') michael@0: nHealth = nHealth + nFoodPoints michael@0: local labPoints = director:createLabel({ michael@0: x=0, y=0, michael@0: hAlignment='centre', michael@0: vAlignment='middle', michael@0: textXScale=.5, michael@0: textYScale=.5, michael@0: --font=fontFxsys, michael@0: font=fontArial, michael@0: text='+' .. nFoodpoints, michael@0: color=color.white, michael@0: alpha=1}) michael@0: local tweenPoints = tween:to(labPoints, {time=1, easing=ease.powIn, textXScale=15, textYScale=15, alpha=0}) michael@0: elseif event.nodeB.name == 'food' then michael@0: event.nodeB.isVisible = false michael@0: audio:playSound('sounds/belch.pcm') michael@0: nHealth = nHealth + nFoodpoints michael@0: local labPoints = director:createLabel({ michael@0: x=0, y=0, michael@0: hAlignment='centre', michael@0: vAlignment='middle', michael@0: textXScale=.5, michael@0: textYScale=.5, michael@0: --font=fontFxsys, michael@0: font=fontArial, michael@0: text='+' .. nFoodpoints, michael@0: color=color.white, michael@0: alpha=1}) michael@0: michael@0: local tweenPoints = tween:to(labPoints, {time=1, easing=ease.powIn, textXScale=15, textYScale=15, alpha=0}) michael@0: elseif event.nodeA.name == 'aster' then michael@0: local diffiX = event.nodeB.x - event.nodeA.x michael@0: local diffiY = event.nodeB.y - event.nodeA.y michael@0: tween:cancel(event.nodeB.tweens[1]) michael@0: --tween:to(spriteHero, {time=0.5, easing=ease.powOut, easingValue=5, x=event.nodeB.x-diffiX*2, y=event.nodeB.y-diffiY*2}) michael@0: tween:to(spriteHero, {time=1, easing=ease.powOut, easingValue=5, x=spriteHero.x-dsw/16, y=spriteHero.y}) michael@0: --if device:isVibrationAvailable() then michael@0: -- device:vibrate(100) michael@0: --end michael@0: audio:playSound('sounds/ouch.pcm') michael@0: nHealth = nHealth + nAsterpoints michael@0: elseif event.nodeB.name == 'aster' then michael@0: tween:cancel(event.nodeA.tweens[1]) michael@0: tween:to(spriteHero, {time=1, easing=ease.powOut, easingValue=5, x=spriteHero.x-dsw/16, y=spriteHero.y}) michael@0: --if device:isVibrationAvailable() then michael@0: -- device:vibrate(100) michael@0: --end michael@0: audio:playSound('sounds/ouch.pcm') michael@0: nHealth = nHealth + nAsterpoints michael@0: elseif event.nodeA.name == 'comet' then michael@0: --if device:isVibrationAvailable() then michael@0: -- device:vibrate(800) michael@0: --end michael@0: audio:playSound('sounds/torche.pcm') michael@0: nHealth = nHealth + nCometpoints michael@0: elseif event.nodeB.name == 'comet' then michael@0: --if device:isVibrationAvailable() then michael@0: -- device:vibrate(800) michael@0: --end michael@0: audio:playSound('sounds/torche.pcm') michael@0: nHealth = nHealth + nCometpoints michael@0: end michael@0: end michael@0: return true michael@0: end michael@0: spriteHero:addEventListener('collision', hit) michael@0: michael@0: -- Implement event handlers michael@0: -- General screen touch move events michael@0: --Marmalade bugs cause audio to fail! michael@0: function moveStart(event) michael@0: --if not partiSmoke:isActive() then michael@0: partiSmoke:reset() michael@0: --end michael@0: --local audiMotor = audio:playSound('sounds/motor.pcm') -- distracting michael@0: end michael@0: function moveStop(event) michael@0: partiSmoke:stop() michael@0: audio:stopSound(audiMotor) michael@0: end michael@0: function touch(event) michael@0: if event.phase=='began' then michael@0: --also: powIn, expIn, sineIn, elasticIn, bounceIn, backIn michael@0: tween:to(spriteHero, {time=1, easing=ease.powOut, easingValue=2, x=event.x, y=event.y, onStart=moveStart, onComplete=moveStop}) michael@0: elseif event.phase=='ended' then michael@0: -- Noop michael@0: --dbg.print('Noop') michael@0: --system:sendEvent('stopMotor', {timeIn=2, sizeFart=5}) michael@0: elseif event.phase=='moved' then michael@0: tween:to(spriteHero, {time=1, easing=ease.powOut, easingValue=2, x=event.x, y=event.y, onStart=moveStart, onComplete=moveStop}) michael@0: end michael@0: end michael@0: system:addEventListener('touch', touch) michael@0: michael@0: -- Start health timers and label appropriately michael@0: nHealth = 90 -- A minute and a half of life michael@0: local labHealth = director:createLabel({ michael@0: x=-fontArial.height*1.75, y=fontArial.height/4, michael@0: hAlignment='right', michael@0: vAlignment='bottom', michael@0: font=fontArial, michael@0: text='Health', michael@0: color=color.white, michael@0: alpha=.375 michael@0: }) michael@0: local labDeath = director:createLabel({ michael@0: x=-fontArial.height/2, y=fontArial.height/4, michael@0: hAlignment='right', michael@0: vAlignment='bottom', michael@0: font=fontArial, michael@0: text=nHealth, michael@0: color=color.white, michael@0: alpha=.375 michael@0: }) michael@0: michael@0: funcyHealth = function(event) michael@0: thisNode = event.target michael@0: --timerObject = event.timer michael@0: --iterationsLeft = event.doneIterations michael@0: --thisNode.health = thisNode.health - 1 michael@0: nHealth = nHealth - 1 michael@0: labDeath.text = nHealth michael@0: michael@0: -- Is the game over? michael@0: if nHealth <= 0 then michael@0: timerHero:cancel() michael@0: --audio:stopStream() michael@0: --tween:cancel(tweenBack) michael@0: tween:cancel(tweenCattin) michael@0: tween:cancel(tweenCheese) michael@0: tween:cancel(tweenFischkopf) michael@0: tween:cancel(tweenCom) michael@0: tween:cancel(tweenGal) michael@0: tween:cancel(tweenAster1) michael@0: tween:cancel(tweenAster2) michael@0: system:removeEventListener('touch', touch) michael@0: michael@0: local rectGover = director:createRectangle({ michael@0: x=0, y=0, michael@0: w=dsw, h=dsh, michael@0: strokeWidth=0, michael@0: color=color.black, michael@0: alpha=0}) michael@0: local labGover = director:createLabel({ michael@0: --x=0, y=fontArial.height, michael@0: hAlignment='centre', michael@0: vAlignment='middle', michael@0: --font=fontFxsys, michael@0: font=fontArial, michael@0: text='GAME OVER', michael@0: color=color.white, michael@0: alpha=0}) michael@0: michael@0: --powIn, expIn, sineIn, elasticIn, bounceIn, backIn, easingValue michael@0: local tweenGvback = tween:to(rectGover, {time=2, alpha=.6}) michael@0: local tweenGvtext = tween:to(labGover, {time=3, easing=ease.expIn, easingValue=3, textXScale=2, textYScale=2, alpha=1}) michael@0: local tweenGvlast = tween:to(labGover, {delay=3, time=1.5, mode='mirror', easing=ease.backOut, textXScale=2.125, textYScale=2.125}) michael@0: michael@0: -- Not ready for prime time (must implement back stack noch) michael@0: ---- Instantiate replay and back buttons michael@0: --local butPlay = director:createSprite({xAnchor=1, source='images/butplay.png', alpha=0}) michael@0: --butPlay.x = dmw - butPlay.w/12 michael@0: --butPlay.y = dmh - butPlay.h michael@0: --local butBack = director:createSprite({source='images/butback2.png', alpha=0}) michael@0: --butBack.x = dmw + butBack.w/12 michael@0: --butBack.y = dmh - butBack.h michael@0: --tweenButplayin = tween:to(butPlay, {delay=4.0, time=1, alpha=1}) michael@0: --tweenButbackin = tween:to(butBack, {delay=4.5, time=1, alpha=1}) michael@0: --function butPlay:touch(event) michael@0: -- if event.phase == 'began' then michael@0: -- director:moveToScene(sceneBegin, transoptr) michael@0: -- end michael@0: -- return true michael@0: --end michael@0: --butPlay:addEventListener('touch', butPlay) michael@0: --function butBack:touch(event) michael@0: -- if event.phase == 'began' then michael@0: -- director:moveToScene(sceneIntro, transoptr) michael@0: -- end michael@0: -- return true michael@0: --end michael@0: --butBack:addEventListener('touch', butBack) michael@0: end michael@0: end michael@0: --timerHealth = system:addTimer(funcyHealth, 1, 0, 4) michael@0: timerHero = spriteHero:addTimer(funcyHealth, 1, 0, 4) michael@0: end michael@0: michael@0: function sceneloc:tearDown(event) michael@0: dbg.print('sceneloc:tearDown') michael@0: audio:stopStream() michael@0: --self.releaseResources() michael@0: --self. = self.:removeFromParent() michael@0: --self.obj0:removeFromParent() michael@0: --self.obj1:removeFromParent() michael@0: --self.obj0 = nil michael@0: --self.obj1 = nil michael@0: end michael@0: function sceneloc:enterPreTransition(event) michael@0: dbg.print('sceneloc:enterPreTransition') michael@0: end michael@0: function sceneloc:enterPostTransition(event) michael@0: dbg.print('sceneloc:enterPostTransition') michael@0: end michael@0: function sceneloc:exitPreTransition(event) michael@0: dbg.print('sceneloc:exitPreTransition') michael@0: end michael@0: function sceneloc:exitPostTransition(event) michael@0: dbg.print('sceneloc:exitPostTransition') michael@0: end michael@0: michael@0: sceneloc:addEventListener({'setUp', 'tearDown', 'enterPreTransition', michael@0: 'enterPostTransition', 'exitPreTransition', 'exitPostTransition'}, sceneloc) michael@0: michael@0: return sceneloc michael@0: