Advertisement
Hendrix000007

GameCenter config AdagioMusicGame

Apr 11th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.62 KB | None | 0 0
  1. -- PLEASE TAKE A LOOK AT THIS IMAGE CLOSELY FIRST: HTTP://WWW.HIDEAWAYSPA.NO/HENDRIX/gcExpl01.jpg
  2. -- This is some of the code (_G.scoreUser = 580 under is just to check gc and will be commented out of course)
  3. -- gameCenter.lua-----------
  4. ----------------------------
  5. local widget = require( "widget" )
  6. local composer = require "composer"
  7. --local scene = composer.newScene()
  8. local ui = require "userinterface"  -- handles various user-interface related tasks
  9. local gameNetwork = require "gameNetwork"
  10.  
  11. -- Define reference points locations anchor ponts
  12. local TOP_REF = 0
  13. local BOTTOM_REF = 1
  14. local LEFT_REF = 0
  15. local RIGHT_REF = 1
  16. local CENTER_REF = 0.5
  17. _G.scoreUser = 580 --comment out when ready to build
  18. local centerX = display.contentCenterX
  19. local centerY = display.contentCenterY
  20. local _W = display.contentWidth
  21. local _H = display.contentHeight
  22.  
  23. -- setup composer scenes (non-external module scenes)
  24. local boardScene = composer.newScene( "boardScene" )
  25.  
  26. -- variables (and forward declarations)
  27.  
  28.  
  29. local logo, bg, currentScoreLabel, titleBar, titleText, shadow
  30. local requestCallback, userScoreText, currentBoardText, userBestText, bestLabel, bestText, butLeaderboard, butGoBack, butSubmitScore, butShowLeaderboard
  31.  
  32.  
  33. local userScore, userBest, bestTextValue, topScorer = 0, "points", "points", "???"
  34. local setUserScore = {}
  35. local offlineAlert = {}
  36. local onIncrementScore = {}
  37. local onSubmitScore = {}
  38. local onChangeBoard = {}
  39. local onShowBoards = {}
  40. local onShowAchievements = {}
  41. local onGoBack = {}
  42. userScore = _G.scoreUser
  43.  
  44.  
  45. --FUNCTIONS--
  46. function requestCallback( event )
  47.     if event.type == "setHighScore" then
  48. ----UNLOCK ACHEIVMENTS-----------------------------
  49.         if _G.scoreAlbumCounter == 1 then
  50.             gameNetwork.request( "unlockAchievement", {
  51.                 achievement = {
  52.                     identifier=achievements["1_record"],
  53.                     percentComplete=25,
  54.                     showsCompletionBanner=true,
  55.                 }          
  56.             })
  57.        
  58.         elseif _G.scoreAlbumCounter == 2 then
  59.             gameNetwork.request( "unlockAchievement", {
  60.                 achievement = {
  61.                     identifier=achievements["2_record"],
  62.                     percentComplete=50,
  63.                     showsCompletionBanner=true,
  64.                 }              
  65.             })
  66.  
  67.         elseif _G.scoreAlbumCounter == 3 then
  68.             gameNetwork.request( "unlockAchievement", {
  69.                 achievement = {
  70.                     identifier=achievements["3_record"],
  71.                     percentComplete=75,
  72.                     showsCompletionBanner=true,
  73.                 }
  74.             })
  75.             )
  76.         elseif _G.scoreAlbumCounter == 4 then
  77.             gameNetwork.request( "unlockAchievement", {
  78.                 achievement = {
  79.                     identifier=achievements["4_record"],
  80.                     percentComplete=100,
  81.                     showsCompletionBanner=true,
  82.                 }
  83.             })
  84.         end
  85. -------------------------------------
  86.         local function alertCompletion() gameNetwork.request( "loadScores", { leaderboard={ category="overall_allTime", playerScope="Global", timeScope="AllTime", range={1,3} }, listener=requestCallback } ); end
  87.         native.showAlert( "High Score Reported!", "", { "OK" }, alertCompletion )
  88.  
  89.    
  90.     elseif event.type == "loadScores" then
  91.         if event.data then
  92.             local topRankID = event.data[1].playerID
  93.             local topRankScore = event.data[1].formattedValue
  94.             bestTextValue = string.sub( topRankScore, 1, 12 )
  95.            
  96.             if topRankID then gameNetwork.request( "loadPlayers", { playerIDs={ topRankID }, listener=requestCallback} ); end
  97.         end
  98.        
  99.        
  100.         if event.localPlayerScore then
  101.             userBest = event.localPlayerScore.formattedValue
  102.         else
  103.             userBest = "Not ranked"
  104.         end
  105.        
  106.         if userBestText then ui.updateLabel( userBestText, userBest, display.contentWidth-25, 177, TOP_REF, RIGHT_REF ); end
  107.    
  108.     elseif event.type == "loadPlayers" then
  109.         if event.data then
  110.             local topRankAlias = event.data[1].alias
  111.            
  112.             if topRankAlias then
  113.                 topScorer = topRankAlias
  114.                 if bestLabel and bestText then
  115.                     ui.updateLabel( bestLabel, topScorer .. " got:", 25, 212, TOP_REF, LEFT_REF )
  116.                     ui.updateLabel( bestText, bestTextValue, display.contentWidth-25, 212, TOP_REF, RIGHT_REF )
  117.                 end
  118.             end
  119.         end
  120.     end
  121. end
  122.  
  123. function offlineAlert()
  124.     native.showAlert( "GameCenter Offline", "Please check your internet connection.", { "OK" } )
  125. end
  126.  
  127.  
  128. function onSubmitScore( )
  129.     if loggedIntoGC then gameNetwork.request( "setHighScore", { localPlayerScore={ category="overall_allTime", value=userScore }, listener=requestCallback } ); else offlineAlert(); end
  130. end
  131.  
  132.  
  133. function onShowBoards(  )
  134.     if loggedIntoGC then gameNetwork.show( "leaderboards", { leaderboard={ category="overall_allTime", timeScope="Week" } } ); else offlineAlert(); end
  135. end
  136.  
  137. function onShowAchievements(  )
  138.     if loggedIntoGC then gameNetwork.show( "achievements" ); else offlineAlert(); end
  139. end
  140.  
  141. function onGoBack(  )
  142.     timer.performWithDelay(600, function() composer.gotoScene("cred", "fade", 400) end )
  143. end
  144.  
  145.  
  146.  
  147. -- boardScene (second tab) --------------------------------------------------------------
  148.  
  149. function boardScene:create( event )
  150.     local sceneGroup = self.view
  151.     bg = display.newImageRect( "bgImage.jpg", 480, 320 )
  152.     bg.x, bg.y = display.contentWidth*0.5, display.contentHeight*0.5
  153.     bg.alpha = 1
  154.         sceneGroup:insert( bg )
  155.     --Leaderboard headding
  156.     butLeaderboard = display.newImageRect( "images/butLeaderBoard.png", 325, 80 )
  157.     butLeaderboard.x, butLeaderboard.y = display.contentWidth*0.5, 40
  158.     butLeaderboard.alpha = 1
  159.         sceneGroup:insert( butLeaderboard )
  160.     -------------------
  161.     butShowLeaderboard = display.newImageRect( "images/butShowLeaderboard.png", 260*0.7, 44*0.7 )
  162.     butShowLeaderboard.x, butShowLeaderboard.y = display.contentWidth /2+20 , butLeaderboard.height + 20
  163.     butShowLeaderboard:addEventListener("tap", onShowBoards)
  164.     butShowLeaderboard.alpha = 1
  165.         sceneGroup:insert( butShowLeaderboard )
  166.     -------------------
  167.     butGoBack = display.newImageRect( "images/butGoBack.png", 145*0.7, 44*0.7 )
  168.     butGoBack.x, butGoBack.y = display.contentWidth-10 , butLeaderboard.height + 20
  169.     butGoBack.anchorX = 1
  170.     butGoBack:addEventListener("tap", onGoBack)
  171.     butGoBack.alpha = 1
  172.         sceneGroup:insert( butGoBack )
  173.     -------------------
  174.     butSubmitScore = display.newImageRect( "images/butSubmitScore.png", 212*0.7, 44*0.7 )
  175.     butSubmitScore.x, butSubmitScore.y = 10, butLeaderboard.height + 20
  176.     butSubmitScore.anchorX = 0
  177.     butSubmitScore:addEventListener("tap", onSubmitScore)
  178.     butSubmitScore.alpha = 1
  179.         sceneGroup:insert( butSubmitScore )
  180.  
  181.     if loggedIntoGC then gameNetwork.request( "setHighScore", { localPlayerScore={ category="overall_allTime", value=scoreUser }, listener=requestCallback } ); end
  182.     print("userScore = " .. userScore)
  183.        
  184.     local yourLabel = ui.createLabel( sceneGroup, "Your Best score", 25, 177, TOP_REF, LEFT_REF, true )
  185.    
  186.     display.remove( userBestText )
  187.     userBestText = ui.createLabel( sceneGroup, userBest, display.contentWidth-25, 177, TOP_REF, RIGHT_REF  )
  188.    
  189.     display.remove( bestLabel )
  190.     bestLabel = ui.createLabel( sceneGroup, topScorer .. " got:", 25, 212, TOP_REF, LEFT_REF, true )
  191.    
  192.     display.remove( bestText )
  193.     bestText = ui.createLabel( sceneGroup, bestTextValue, display.contentWidth-25, 212, TOP_REF, RIGHT_REF  )
  194.     native.showAlert( "unlockAchievement 1", { "OK" } )
  195.  
  196. end
  197.  
  198.  
  199. boardScene:addEventListener( "create", boardScene )
  200.  
  201. ---------------------------------------------------------------------------------
  202. --Runtime:addEventListener( "system", onSystemEvent )
  203. --ui.createTabs( widget )
  204. return boardScene
  205.  
  206.  
  207. --main.lua--------------------
  208. ------------------------------
  209. display.setStatusBar(display.HiddenStatusBar)
  210. local gameNetwork = require "gameNetwork"
  211. local composer = require "composer"
  212. initCallback = {}
  213. onSystemEvent = {}
  214.  
  215. _G.messageFont = "Gartentika" -- Custom font used in the game
  216. _G.scoreFont = "Cheboygan"
  217. _G.debugMode = 0            -- Switch to 0 when done debugging
  218. _G.help = 0                 -- Check if the user use helpfunction or not
  219. _G.scorePath = "images/"
  220.  
  221. _G.livesLeft = 3
  222. _G.scoreUser = 600          -- [ Default values = 0 ] The Total user score
  223. _G.scoreTrackCounter = 1    -- [ Default values = 1 ] Amount of tracks you need to manage before ure done w the Album, this is set in main and increased in playGame on sucess
  224. _G.scoreTrackCounter = 1    -- [ Default values = 1 ] Amount of tracks you need to manage before ure done w the Album, this is set in main and increased in playGame on sucess
  225. _G.scoreAlbumCounter = 1    -- [ Default values = 0 ] This is increased by 1 for every 5 tracks u manage
  226. _G.doneSplash = false   -- Starts the initial mode with the intro, this changes to true when entering playGame
  227.                         -- so we dont have to see the intro every time we decide to hit replay.
  228.                         -- thats why we set this boolean to true when entering playGame
  229. _G.trackCounter = 1
  230.  
  231. --audio.setVolume(0)        --when ure sick of music and want to hear other music when coding !
  232.  print("\n\n¡!¡!¡!¡!¡ REMEMBER TO COMMENT audio.setVolume IN MAIN.LUA ¡!¡!¡!¡!¡\n\n")
  233. print("\n\n¡!¡!¡!¡!¡ REMEMBER DEBUGMODE = 0 BEFORE BUILD FOR STORE¡!¡!¡!¡!¡\n\n")
  234. print(_G.scoreUser , "ScoreUser")
  235. print(_G.scoreAlbumCounter , "scoreAlbumCounter")
  236. print(_G.scoreAlbumCounter , "scoreAlbumCounter")
  237.  
  238. --Log in to GC ---------------------------------------------------------------------------------
  239. function initCallback( event )
  240.     -- "showSignIn" is only available on iOS 6+
  241.     if event.type == "showSignIn" then
  242.     elseif event.data then
  243.         loggedIntoGC = true
  244.     end
  245. end
  246.  
  247. -- system event handler -----------------------------------------------------------------
  248. function onSystemEvent( event )
  249.     if "applicationStart" == event.type then
  250.         loggedIntoGC = false
  251.         gameNetwork.init( "gamecenter", { listener=initCallback } )
  252.         return true
  253.     end
  254. end
  255. -- END OF GAME CENTER CODE -----------------------------------------------------------------
  256.  
  257. Runtime:addEventListener( "system", onSystemEvent )
  258. composer.gotoScene( "intro", "fade", 400 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement