Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setup()
- print("CubeGame Loaded")
- devmode = true
- recordonstart = false
- onmenu = true
- enableRecording = false
- errorrendered = false
- enableInventory = false
- SpritelyLoader:init()
- ImageService:init()
- isphone = HEIGHT < 641
- buttonsize = isphone and 70 or 90
- characterImage = ImageService:GetImage("Character")
- enemyImage = ImageService:GetImage("Enemy")
- flagImage = ImageService:GetImage("Flag")
- attackImage = ImageService:GetImage("attack")
- frame = 0
- font("Arial-BoldMT")
- --displayMode(FULLSCREEN_NO_BUTTONS)
- displayMode(FULLSCREEN)
- supportedOrientations(LANDSCAPE_ANY)
- Menu:init()
- end
- function findFirstUntakenId()
- for a,i in pairs(enemies) do
- if i == nil then
- return a
- end
- end
- return #enemies + 1
- end
- function GetFPS()
- lasttick = tick
- tick = os.clock()
- return math.floor(1/(tick-lasttick)*6.6/(isRecording() and 1.3 or not isRecording() and 1))
- end
- function save()
- saveProjectData("dead",character.dead)
- saveProjectData("health",character.health)
- saveProjectData("maxhealth",character.maxhealth)
- saveProjectData("speedboost",character.speedboost)
- saveProjectData("jumpboost",character.jumpboost)
- saveProjectData("frame",frame)
- saveProjectData("jump",character.jump)
- saveProjectData("x",character.x)
- saveProjectData("y",character.y)
- end
- function Delay(num,callback)
- tween(num,{0},{1},nil,callback)
- end
- -- This function gets called once every frame
- function draw()
- if errored and not errorrendered then
- errorrendered = true
- ErrorHandler:draw()
- return
- end
- local ok, err = pcall(function()
- noSmooth()
- local a = os.clock()
- if onmenu then
- Menu:draw()
- return
- end
- if frame == 0 then
- cammove = 0
- cammovey = 0-(HEIGHT < 641 and 200 or 0)
- time = 0
- touchcammove = {0}
- stars = {}
- daylight = 0
- touchcammovey = {0}
- enemiesspawninstantly = true
- enemynum = 0
- numberbeforerejection = 150
- fadingin = true
- fade = {255}
- tween(0.5,fade,{0},nil,function()
- fadingin = false
- end)
- starnum = 250
- lasttouch = {0,0}
- paused = false
- enemies = {}
- enemyrange = 50000
- characterImage = nil
- paused = false
- fps = 1337
- tick = os.clock()
- lasttick = os.clock()
- touchmovelast = 0
- touchmovetapped = false
- touchmovesingletap = false
- touchmovetween = {0,0}
- touchmovetweening = false
- inventorytween = {0}
- --parameter.watch("touchmovesingletap")
- textSize(30)
- background(0,0,0,255)
- characterImage = ImageService:GetImage("Character")
- character = Character:init(math.random(-2000,2000),135,false)
- flag = Flag:init(character.spawnx,character.spawny)
- leftpress = false
- rightpress = false
- closepress = false
- jumppress = false
- recordpress = false
- respawnpress = true
- attackpress = false
- flagpress = false
- inventoryOpen = false
- frame = frame + 1
- for i = 1,starnum do
- table.insert(stars,{math.random(1,WIDTH),math.random(1,WIDTH)})
- end
- return
- end
- if not paused then
- frame = frame + 1
- time = time + 1
- if time >= 10000 then
- time = 0
- end
- end
- if time > 5000 and time < 6000 then
- daylight = (time-5000)/1000*255
- end
- if time < 1000 then
- daylight = 260-(time/4)
- end
- rise = math.sin(daylight/25)
- background(rise, 114-daylight, 255-daylight, 255) --Renders sky
- fill(255,255,255,daylight)
- if daylight > 0 then
- for _, i in pairs(stars) do
- rect(i[1],i[2],2,2)
- end
- end
- fill(47-(daylight/10), 89-(daylight/10), 44-(daylight/10), 255) --renders grass
- rect(-1,-100-cammovey,WIDTH+2,235)
- enemynum = 0
- for i,_ in pairs(enemies)do --Renders all enemies
- Enemy:draw(i)
- end
- if enemynum < 30 then
- 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)
- Enemy:init(pos,135,findFirstUntakenId())
- enemynum = enemynum + 1
- --table.insert(enemies,#enemies+1)
- end
- if enemynum > 30 then
- for i = 1, 100 do
- enemies[#enemies] = nil
- if enemynum <= 30 then
- break
- end
- end
- if enemynum > 30 then
- print("Enemy cap despawning is taking too long!!")
- end
- end
- if enemynum > 70 then
- print("Enemy spawning out of control!!")
- enemies = {} --If it gets out of control, lets just flush the enemy data
- enemynum = 0
- end
- character:draw() --Renders character
- if frame % 500 == 0 then
- save()
- end
- flag:draw()
- fontSize(18)
- buttonsizesm = buttonsize-10
- --sprite(grassImage,-1,-1,WIDTH+2,151)
- fill(25,25,25,jumppress and 150 or not jumppress and 100)
- rect(WIDTH-buttonsize+5,5,buttonsizesm,buttonsizesm) --Renders Jump button
- fill(25,25,25,flagpress and 150 or not flagpress and 100)
- rect(WIDTH-(buttonsize*2)+10,5,buttonsizesm,buttonsizesm) --Renders flag button
- sprite(flagImage,WIDTH-(buttonsizesm*1.625),buttonsizesm/2,buttonsizesm/2,buttonsizesm/2)
- fill(25,25,25,attackpress and 150 or not attackpress and 100)
- rect(WIDTH-buttonsize+5,buttonsize,buttonsizesm,buttonsizesm) --Renders attack button
- fill(25,25,25,inventorypress and 150 or not inventorypress and 100)
- rect(5,buttonsize,buttonsizesm,buttonsizesm) --Renders inventory button
- sprite(attackImage,WIDTH-(buttonsize/2),buttonsizesm*1.625,buttonsizesm-20,buttonsizesm-20)
- if enableRecording then
- fill(25,25,25,recordpress and 150 or not recordpress and 100) --Renders record button
- rect(WIDTH-55,HEIGHT-55,50,50)
- if isRecording() and (math.floor(os.clock()*4)/4) % 0.5 == 0 then
- fill(60, 29, 29, 200)
- else --Creates flashing record button effect
- fill(36, 36, 36, 200)
- end
- ellipse(WIDTH-29,HEIGHT-31,25) --Renders record icon
- if isRecording() and (math.floor(os.clock()*4)/4) % 0.5 == 0 then
- fill(170, 36, 36, 200)
- else --Creates flashing record button effect
- fill(71, 71, 71, 200)
- end
- ellipse(WIDTH-30,HEIGHT-30,25) --Renders record icon
- end
- fill(25,25,25,closepress and 150 or not closepress and 100)
- rect(WIDTH-110+(enableRecording and 0 or 50),HEIGHT-55,50,50) --Renders close button
- textSize(20)
- textMode(CENTER)
- fill(0,0,0)
- text("X",WIDTH-84+(enableRecording and 0 or 50),HEIGHT-33) --Renders close button 'X'
- fill(215, 199, 199)
- text("X",WIDTH-85+(enableRecording and 0 or 49),HEIGHT-32)
- fill(25,25,25,leftpress and 150 or not leftpress and 100)
- rect(5,5,buttonsizesm,buttonsizesm) --Renders left move button
- fill(25,25,25,rightpress and 150 or not rightpress and 100)
- rect(buttonsize,5,buttonsizesm,buttonsizesm) --Renders right move button
- textSize(25) --prepared text size
- if character.dead then
- fill(255,0,0,125)
- rect(-1,-1,WIDTH+2,HEIGHT+2)
- fill(25,25,25,respawnpress and 150 or not respawnpress and 100)
- rect(WIDTH/2-100,HEIGHT/2-30,200,60) --respawn button
- fill(0,0,0,230)
- textMode(CENTER)
- text("Respawn",WIDTH/2+1,HEIGHT/2-1)
- fill(255,255,255,230)
- text("Respawn",WIDTH/2,HEIGHT/2)
- end
- local per = character.health/character.maxhealth
- if character.health == math.huge then
- per = 1
- end
- fill(25,25,25, 100)--health bar
- rect(WIDTH/2-105,1,210,39)
- if per > .2 then
- fill(19, 185, 54, 150)
- rect(WIDTH/2-100,20,200*per,15)
- fill(26, 152, 55, 150)
- rect(WIDTH/2-100,6,200*per,15)
- else
- fill(185, 30, 18, 150)
- rect(WIDTH/2-100,20,200*per,15)
- fill(152, 26, 29, 150)
- rect(WIDTH/2-100,6,200*per,15)
- fill(255, 0, 0, 150)
- if not character.dead then
- fill(255,0,0,-(per-.2)*700)
- rect(0,0,WIDTH,HEIGHT)
- end
- end
- textMode(CORNER)
- fill(0,0,0,170)
- text("Health "..math.floor(per*100).."%",WIDTH/2-90,8)
- fill(255,255,255,220)
- text("Health "..math.floor(per*100).."%",WIDTH/2-91,9)
- --renders health
- if frame == 1 or frame % 10 == 0 then
- fps = GetFPS()
- end
- fill(0, 0, 0)
- if fps == nil then
- fps = "Loading..."
- end
- text("FPS: "..fps.." Enemies: "..enemynum.." X: "..character.x.." Y: "..character.y,6,HEIGHT-31)
- fill(255,255,255)
- text("FPS: "..fps.." Enemies: "..enemynum.." X: "..character.x.." Y: "..character.y,5,HEIGHT-30)
- if inventoryOpen then
- Inventory:draw()
- end
- if fadingin then
- fill(0,0,0,fade[1])
- rect(-1,-1,WIDTH+2,HEIGHT+2)
- end
- end)
- if not ok then
- ErrorHandler:init("com.ryandolan123.main.draw",err)
- end
- end
- function touched(touch)
- if errored then
- return
- end
- if onmenu then
- Menu:touched(touch)
- end
- if touch.y < buttonsize and touch.x < buttonsize and not fadingin and not paused then
- if touch.state == ENDED then
- leftpress = false
- else
- leftpress = true
- rightpress = false
- end
- elseif touch.y < buttonsize and touch.x < buttonsize*2 and not fadingin and not paused then
- if touch.state == ENDED then
- rightpress = false
- else
- rightpress = true
- leftpress = false
- end
- elseif touch.y < buttonsize and touch.x > WIDTH-(buttonsize*2) and touch.x < WIDTH-buttonsize and not fadingin and not paused then
- if touch.state == ENDED then
- flagpress = false
- else
- flagpress = true
- jumppress = false
- attackpress = false
- end
- elseif touch.y < buttonsize*2 and touch.y > buttonsize and touch.x > WIDTH-buttonsize and not fadingin and not paused then
- if touch.x > WIDTH-buttonsize then
- if touch.state == ENDED then
- attackpress = false
- else
- attackpress = true
- jumppress = false
- flagpress = false
- end
- end
- elseif touch.y < buttonsize and touch.x > WIDTH-buttonsize and not fadingin and not paused then
- if touch.state == ENDED then
- jumppress = false
- elseif character ~= nil and character.onground then
- jumppress = true
- flagpress = false
- attackpress = false
- end
- elseif touch.y > buttonsize and touch.y < buttonsize*2 and touch.x < buttonsize and not fadingin and enableInventory then
- if touch.state == ENDED then
- inventorypress = false
- --inventoryOpen = not inventoryOpen
- if inventoryOpen then
- tween(0.4,inventorytween,{invslotsx*(70)},"backIn",function()
- inventoryOpen = false
- paused = false
- end)
- else
- inventoryOpen = true
- inventorytween = {invslotsx*(70)}
- tween(0.4,inventorytween,{0},"backOut")
- paused = true
- end
- else
- inventorypress = true
- end
- elseif touch.x < WIDTH-buttonsize and touch.x > WIDTH-(buttonsize*2) and not fadingin and enableRecording then
- if touch.state == ENDED and fade ~= nil and fade[1] == 0 then
- save()
- fadingin = true
- paused = true
- tween(0.5,fade,{255},nil,restart)
- else
- closepress = true
- end
- elseif touch.y > HEIGHT-buttonsize and touch.x < WIDTH and touch.x > WIDTH-buttonsize and not fadingin then
- if enableRecording then
- if touch.state == ENDED then
- recordpress = false
- if isRecording() then
- stopRecording()
- else
- startRecording()
- end
- else
- recordpress = true
- end
- else
- if touch.state == ENDED and fade ~= nil and fade[1] == 0 then
- save()
- fadingin = true
- paused = true
- tween(0.5,fade,{255},nil,restart)
- else
- closepress = true
- end
- end
- elseif character ~= nil and character.dead and not fadingin then
- if touch.state == ENDED then
- fadingin = true
- tween(0.35,fade,{255},nil,function()
- character = Character:init(character.spawnx,character.spawny,true)
- respawnpress = false
- save()
- tween(0.35,fade,{0},nil,function()fadingin = false end)
- end)
- else
- respawnpress = true
- end
- elseif character ~= nil and not character.dead and not fadingin and touch then
- if touch.state ~= ENDED then --as a programmer I hate multitouch's complexity
- if touch.state == BEGAN then
- if os.clock() - touchmovelast < 0.4 and not touchmovetweening then
- --print("hi")
- touchmovesingletap = false
- touchmovetweening = true
- pcall(function()--Tweening function woes
- tween(0.4,touchcammove,{0})
- tween(0.4,touchcammovey,{0},function()touchmovetweening = false end)
- end)
- touchmovelast = os.clock()
- touchmovesingletap = true
- Delay(0.4,function()touchmovesingletap = false end)
- return
- end
- lasttouch = {touch.x,touch.y}
- touchmovelast = os.clock()
- end
- touchcammove = {(lasttouch[1] or 0)-touch.x+(touchcammove[1] or 0)}
- touchcammovey = {(lasttouch[2] or 0)-touch.y+(touchcammovey[1] or 0)}
- if touchcammovey[1] < 0 then
- touchcammovey[1] = 0
- end
- lasttouch = {touch.x,touch.y}
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement