Advertisement
RyanDolan123

Main.lua

Mar 26th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.72 KB | None | 0 0
  1. function setup()
  2.     print("CubeGame Loaded")
  3.     devmode = true
  4.     recordonstart = false
  5.     onmenu = true
  6.     enableRecording = false
  7.     errorrendered = false
  8.     enableInventory = false
  9.     SpritelyLoader:init()
  10.     ImageService:init()
  11.     isphone = HEIGHT < 641
  12.     buttonsize = isphone and 70 or 90
  13.     characterImage = ImageService:GetImage("Character")
  14.     enemyImage = ImageService:GetImage("Enemy")
  15.     flagImage = ImageService:GetImage("Flag")
  16.     attackImage = ImageService:GetImage("attack")
  17.     frame = 0
  18.     font("Arial-BoldMT")
  19.     --displayMode(FULLSCREEN_NO_BUTTONS)
  20.     displayMode(FULLSCREEN)
  21.     supportedOrientations(LANDSCAPE_ANY)
  22.     Menu:init()
  23. end
  24.  
  25. function findFirstUntakenId()
  26.     for a,i in pairs(enemies) do
  27.         if i == nil then
  28.             return a
  29.         end
  30.     end
  31.     return #enemies + 1
  32. end
  33.  
  34.  
  35. function GetFPS()
  36.     lasttick = tick
  37.     tick = os.clock()
  38.     return math.floor(1/(tick-lasttick)*6.6/(isRecording() and 1.3 or not isRecording() and 1))
  39. end
  40.  
  41. function save()
  42.     saveProjectData("dead",character.dead)
  43.     saveProjectData("health",character.health)
  44.     saveProjectData("maxhealth",character.maxhealth)
  45.     saveProjectData("speedboost",character.speedboost)
  46.     saveProjectData("jumpboost",character.jumpboost)
  47.     saveProjectData("frame",frame)
  48.     saveProjectData("jump",character.jump)
  49.     saveProjectData("x",character.x)
  50.     saveProjectData("y",character.y)
  51. end
  52.  
  53. function Delay(num,callback)
  54.     tween(num,{0},{1},nil,callback)
  55. end
  56.  
  57. -- This function gets called once every frame
  58. function draw()
  59.    
  60.     if errored and not errorrendered then
  61.         errorrendered = true
  62.         ErrorHandler:draw()
  63.         return
  64.     end
  65.    
  66.     local ok, err = pcall(function()
  67.    
  68.     noSmooth()
  69.    
  70.     local a = os.clock()
  71.    
  72.     if onmenu then
  73.         Menu:draw()
  74.         return
  75.     end
  76.    
  77.     if frame == 0 then
  78.         cammove = 0
  79.         cammovey = 0-(HEIGHT < 641 and 200 or 0)
  80.         time = 0
  81.         touchcammove = {0}
  82.         stars = {}
  83.         daylight = 0
  84.         touchcammovey = {0}
  85.         enemiesspawninstantly = true
  86.         enemynum = 0
  87.         numberbeforerejection = 150
  88.         fadingin = true
  89.         fade = {255}
  90.         tween(0.5,fade,{0},nil,function()
  91.             fadingin = false
  92.         end)
  93.         starnum = 250
  94.         lasttouch = {0,0}
  95.         paused = false
  96.         enemies = {}
  97.         enemyrange = 50000
  98.         characterImage = nil
  99.         paused = false
  100.         fps = 1337
  101.         tick = os.clock()
  102.         lasttick = os.clock()
  103.         touchmovelast = 0
  104.         touchmovetapped = false
  105.         touchmovesingletap = false
  106.         touchmovetween = {0,0}
  107.         touchmovetweening = false
  108.         inventorytween = {0}
  109.         --parameter.watch("touchmovesingletap")
  110.         textSize(30)
  111.         background(0,0,0,255)
  112.         characterImage = ImageService:GetImage("Character")
  113.         character = Character:init(math.random(-2000,2000),135,false)
  114.         flag = Flag:init(character.spawnx,character.spawny)
  115.         leftpress = false
  116.         rightpress = false
  117.         closepress = false
  118.         jumppress = false
  119.         recordpress = false
  120.         respawnpress = true
  121.         attackpress = false
  122.         flagpress = false
  123.         inventoryOpen = false
  124.         frame = frame + 1
  125.         for i = 1,starnum do
  126.             table.insert(stars,{math.random(1,WIDTH),math.random(1,WIDTH)})
  127.         end
  128.         return
  129.     end
  130.    
  131.     if not paused then
  132.         frame = frame + 1
  133.         time = time + 1
  134.         if time >= 10000 then
  135.             time = 0
  136.         end
  137.     end
  138.     if time > 5000 and time < 6000 then
  139.         daylight = (time-5000)/1000*255
  140.     end
  141.    
  142.     if time < 1000 then
  143.         daylight = 260-(time/4)
  144.     end
  145.    
  146.     rise = math.sin(daylight/25)
  147.    
  148.     background(rise, 114-daylight, 255-daylight, 255) --Renders sky
  149.    
  150.     fill(255,255,255,daylight)
  151.     if daylight > 0 then
  152.         for _, i in pairs(stars) do
  153.             rect(i[1],i[2],2,2)
  154.         end
  155.     end
  156.    
  157.     fill(47-(daylight/10), 89-(daylight/10), 44-(daylight/10), 255) --renders grass
  158.     rect(-1,-100-cammovey,WIDTH+2,235)
  159.    
  160.     enemynum = 0
  161.    
  162.     for i,_ in pairs(enemies)do --Renders all enemies
  163.         Enemy:draw(i)
  164.     end
  165.    
  166.     if enemynum < 30 then
  167.         local pos = math.random(0,1) == 0 and math.random(character.x-5000,character.x-WIDTH-HEIGHT) or math.random(character.x+WIDTH+HEIGHT,character.x+5000)
  168.         Enemy:init(pos,135,findFirstUntakenId())
  169.         enemynum = enemynum + 1
  170.         --table.insert(enemies,#enemies+1)
  171.     end
  172.    
  173.     if enemynum > 30 then
  174.         for i = 1, 100 do
  175.             enemies[#enemies] = nil
  176.             if enemynum <= 30 then
  177.                 break
  178.             end
  179.         end
  180.         if enemynum > 30 then
  181.             print("Enemy cap despawning is taking too long!!")
  182.         end
  183.     end
  184.    
  185.     if enemynum > 70 then
  186.         print("Enemy spawning out of control!!")
  187.         enemies = {} --If it gets out of control, lets just flush the enemy data
  188.         enemynum = 0
  189.     end
  190.    
  191.     character:draw() --Renders character
  192.    
  193.     if frame % 500 == 0 then
  194.         save()
  195.     end
  196.    
  197.     flag:draw()
  198.    
  199.     fontSize(18)
  200.    
  201.     buttonsizesm = buttonsize-10
  202.  
  203.     --sprite(grassImage,-1,-1,WIDTH+2,151)
  204.     fill(25,25,25,jumppress and 150 or not jumppress and 100)
  205.     rect(WIDTH-buttonsize+5,5,buttonsizesm,buttonsizesm) --Renders Jump button
  206.    
  207.      fill(25,25,25,flagpress and 150 or not flagpress and 100)
  208.     rect(WIDTH-(buttonsize*2)+10,5,buttonsizesm,buttonsizesm) --Renders flag button
  209.    
  210.     sprite(flagImage,WIDTH-(buttonsizesm*1.625),buttonsizesm/2,buttonsizesm/2,buttonsizesm/2)
  211.    
  212.     fill(25,25,25,attackpress and 150 or not attackpress and 100)
  213.     rect(WIDTH-buttonsize+5,buttonsize,buttonsizesm,buttonsizesm) --Renders attack button
  214.    
  215.     fill(25,25,25,inventorypress and 150 or not inventorypress and 100)
  216.     rect(5,buttonsize,buttonsizesm,buttonsizesm) --Renders inventory button
  217.    
  218.     sprite(attackImage,WIDTH-(buttonsize/2),buttonsizesm*1.625,buttonsizesm-20,buttonsizesm-20)
  219.    
  220.     if enableRecording then
  221.         fill(25,25,25,recordpress and 150 or not recordpress and 100) --Renders record button
  222.         rect(WIDTH-55,HEIGHT-55,50,50)
  223.         if isRecording() and (math.floor(os.clock()*4)/4) % 0.5 == 0 then
  224.             fill(60, 29, 29, 200)
  225.         else --Creates flashing record button effect
  226.             fill(36, 36, 36, 200)
  227.         end
  228.         ellipse(WIDTH-29,HEIGHT-31,25) --Renders record icon
  229.         if isRecording() and (math.floor(os.clock()*4)/4) % 0.5 == 0 then
  230.             fill(170, 36, 36, 200)
  231.         else --Creates flashing record button effect
  232.             fill(71, 71, 71, 200)
  233.         end
  234.         ellipse(WIDTH-30,HEIGHT-30,25) --Renders record icon
  235.     end
  236.     fill(25,25,25,closepress and 150 or not closepress and 100)
  237.     rect(WIDTH-110+(enableRecording and 0 or 50),HEIGHT-55,50,50) --Renders close button
  238.     textSize(20)
  239.     textMode(CENTER)
  240.     fill(0,0,0)
  241.     text("X",WIDTH-84+(enableRecording and 0 or 50),HEIGHT-33) --Renders close button 'X'
  242.     fill(215, 199, 199)
  243.     text("X",WIDTH-85+(enableRecording and 0 or 49),HEIGHT-32)
  244.     fill(25,25,25,leftpress and 150 or not leftpress and 100)
  245.     rect(5,5,buttonsizesm,buttonsizesm) --Renders left move button
  246.     fill(25,25,25,rightpress and 150 or not rightpress and 100)
  247.     rect(buttonsize,5,buttonsizesm,buttonsizesm) --Renders right move button
  248.  
  249.     textSize(25) --prepared text size
  250.    
  251.     if character.dead then
  252.         fill(255,0,0,125)
  253.         rect(-1,-1,WIDTH+2,HEIGHT+2)
  254.         fill(25,25,25,respawnpress and 150 or not respawnpress and 100)
  255.         rect(WIDTH/2-100,HEIGHT/2-30,200,60) --respawn button
  256.         fill(0,0,0,230)
  257.         textMode(CENTER)
  258.         text("Respawn",WIDTH/2+1,HEIGHT/2-1)
  259.         fill(255,255,255,230)
  260.         text("Respawn",WIDTH/2,HEIGHT/2)
  261.     end
  262.    
  263.     local per = character.health/character.maxhealth
  264.     if character.health == math.huge then
  265.         per = 1
  266.     end
  267.    
  268.     fill(25,25,25, 100)--health bar
  269.     rect(WIDTH/2-105,1,210,39)
  270.     if per > .2 then
  271.         fill(19, 185, 54, 150)
  272.         rect(WIDTH/2-100,20,200*per,15)
  273.         fill(26, 152, 55, 150)
  274.         rect(WIDTH/2-100,6,200*per,15)
  275.     else
  276.         fill(185, 30, 18, 150)
  277.         rect(WIDTH/2-100,20,200*per,15)
  278.         fill(152, 26, 29, 150)
  279.         rect(WIDTH/2-100,6,200*per,15)
  280.         fill(255, 0, 0, 150)
  281.         if not character.dead then
  282.             fill(255,0,0,-(per-.2)*700)
  283.             rect(0,0,WIDTH,HEIGHT)
  284.         end
  285.     end
  286.     textMode(CORNER)
  287.     fill(0,0,0,170)
  288.     text("Health "..math.floor(per*100).."%",WIDTH/2-90,8)
  289.     fill(255,255,255,220)
  290.     text("Health "..math.floor(per*100).."%",WIDTH/2-91,9)
  291.     --renders health
  292.     if frame == 1 or frame % 10 == 0 then
  293.         fps = GetFPS()
  294.     end
  295.     fill(0, 0, 0)
  296.     if fps == nil then
  297.         fps = "Loading..."
  298.     end
  299.     text("FPS: "..fps.."   Enemies: "..enemynum.."  X: "..character.x.."  Y: "..character.y,6,HEIGHT-31)
  300.     fill(255,255,255)
  301.     text("FPS: "..fps.."   Enemies: "..enemynum.."  X: "..character.x.."  Y: "..character.y,5,HEIGHT-30)
  302.    
  303.     if inventoryOpen then
  304.         Inventory:draw()
  305.     end
  306.     if fadingin then
  307.         fill(0,0,0,fade[1])
  308.         rect(-1,-1,WIDTH+2,HEIGHT+2)
  309.     end
  310.    
  311.     end)
  312.    
  313.     if not ok then
  314.         ErrorHandler:init("com.ryandolan123.main.draw",err)
  315.     end
  316. end
  317.  
  318. function touched(touch)
  319.     if errored then
  320.         return
  321.     end
  322.     if onmenu then
  323.         Menu:touched(touch)
  324.     end
  325.     if touch.y < buttonsize and touch.x < buttonsize and not fadingin and not paused then
  326.         if touch.state == ENDED then
  327.             leftpress = false
  328.         else
  329.             leftpress = true
  330.             rightpress = false
  331.         end
  332.     elseif touch.y < buttonsize and touch.x < buttonsize*2 and not fadingin and not paused then
  333.         if touch.state == ENDED then
  334.             rightpress = false
  335.         else
  336.             rightpress = true
  337.             leftpress = false
  338.         end
  339.     elseif touch.y < buttonsize and touch.x > WIDTH-(buttonsize*2) and touch.x < WIDTH-buttonsize and not fadingin and not paused then
  340.         if touch.state == ENDED then
  341.             flagpress = false
  342.         else
  343.             flagpress = true
  344.             jumppress = false
  345.             attackpress = false
  346.         end
  347.     elseif touch.y < buttonsize*2 and touch.y > buttonsize and touch.x > WIDTH-buttonsize and not fadingin and not paused then
  348.         if touch.x > WIDTH-buttonsize then
  349.             if touch.state == ENDED then
  350.                 attackpress = false
  351.             else
  352.                 attackpress = true
  353.                 jumppress = false
  354.                 flagpress = false
  355.             end
  356.         end
  357.     elseif touch.y < buttonsize and touch.x > WIDTH-buttonsize and not fadingin and not paused then
  358.         if touch.state == ENDED then
  359.             jumppress = false
  360.         elseif character ~= nil and character.onground then
  361.             jumppress = true
  362.             flagpress = false
  363.             attackpress = false
  364.         end
  365.     elseif touch.y > buttonsize and touch.y < buttonsize*2 and touch.x < buttonsize and not fadingin and enableInventory then
  366.         if touch.state == ENDED then
  367.             inventorypress = false
  368.             --inventoryOpen = not inventoryOpen
  369.             if inventoryOpen then
  370.                 tween(0.4,inventorytween,{invslotsx*(70)},"backIn",function()
  371.                     inventoryOpen = false
  372.                     paused = false
  373.                 end)
  374.             else
  375.                 inventoryOpen = true
  376.                 inventorytween = {invslotsx*(70)}
  377.                 tween(0.4,inventorytween,{0},"backOut")
  378.                 paused = true
  379.             end
  380.         else
  381.             inventorypress = true
  382.         end
  383.     elseif touch.x < WIDTH-buttonsize and touch.x > WIDTH-(buttonsize*2) and not fadingin and enableRecording then
  384.         if touch.state == ENDED and fade ~= nil and fade[1] == 0 then
  385.             save()
  386.             fadingin = true
  387.             paused = true
  388.             tween(0.5,fade,{255},nil,restart)
  389.         else
  390.             closepress = true
  391.         end
  392.     elseif touch.y > HEIGHT-buttonsize and touch.x < WIDTH and touch.x > WIDTH-buttonsize and not fadingin then
  393.         if enableRecording then
  394.             if touch.state == ENDED then
  395.                 recordpress = false
  396.                 if isRecording() then
  397.                     stopRecording()
  398.                 else
  399.                     startRecording()
  400.                 end
  401.             else
  402.                 recordpress = true
  403.             end
  404.         else
  405.             if touch.state == ENDED and fade ~= nil and fade[1] == 0 then
  406.                 save()
  407.                 fadingin = true
  408.                 paused = true
  409.                 tween(0.5,fade,{255},nil,restart)
  410.             else
  411.                 closepress = true
  412.             end
  413.         end
  414.     elseif character ~= nil and character.dead and not fadingin then
  415.         if touch.state == ENDED then
  416.             fadingin = true
  417.             tween(0.35,fade,{255},nil,function()
  418.                 character = Character:init(character.spawnx,character.spawny,true)
  419.                 respawnpress = false
  420.                 save()
  421.                 tween(0.35,fade,{0},nil,function()fadingin = false end)
  422.             end)
  423.         else
  424.             respawnpress = true
  425.         end
  426.     elseif character ~= nil and not character.dead and not fadingin and touch then
  427.         if touch.state ~= ENDED then --as a programmer I hate multitouch's complexity
  428.             if touch.state == BEGAN then
  429.                 if os.clock() - touchmovelast < 0.4 and not touchmovetweening then
  430.                     --print("hi")
  431.                     touchmovesingletap = false
  432.                     touchmovetweening = true
  433.                     pcall(function()--Tweening function woes
  434.                         tween(0.4,touchcammove,{0})
  435.                         tween(0.4,touchcammovey,{0},function()touchmovetweening = false end)
  436.                     end)
  437.                     touchmovelast = os.clock()
  438.                     touchmovesingletap = true
  439.                     Delay(0.4,function()touchmovesingletap = false end)
  440.                     return
  441.                 end
  442.                 lasttouch = {touch.x,touch.y}
  443.                 touchmovelast = os.clock()
  444.             end
  445.             touchcammove = {(lasttouch[1] or 0)-touch.x+(touchcammove[1] or 0)}
  446.             touchcammovey = {(lasttouch[2] or 0)-touch.y+(touchcammovey[1] or 0)}
  447.             if touchcammovey[1] < 0 then
  448.                 touchcammovey[1] = 0
  449.             end
  450.             lasttouch = {touch.x,touch.y}
  451.         end
  452.     end
  453. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement