Advertisement
Guest User

lua game by me

a guest
Sep 4th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.01 KB | None | 0 0
  1. client.pause()
  2. client.SetGameExtraPadding(0,0,240-client.bufferwidth(),160-client.bufferheight())
  3. client.setwindowsize(2)
  4.  
  5. local gameName="Platformer"
  6. local gameVersion="v0.13"
  7. local debugMode=false
  8. local debugPause=false
  9. local debugWallCollision={}
  10. local debugHitCollision={}
  11. local mode=0
  12. local frames=0
  13. local background=nil
  14. local foreground=nil
  15. local image=nil
  16. local fadeFrames=0
  17. local currentRoom=0
  18. local gamescreen=nil
  19. local reachedIngame=false
  20.  
  21. local tileSize=27
  22. local currentDisplayWidth=10 --tiles
  23. local currentDisplayHeight=7 --tiles
  24.  
  25.  
  26. local Camera = {
  27.     X = 0,
  28.     Y = 0,
  29.     Xcap = 0,
  30.     Ycap = 0
  31. }
  32.  
  33. local MapOffset = {
  34.     X = 0,
  35.     Y = 0
  36. }
  37.  
  38. local Actor = {
  39.     [1] = { HP=0,
  40.             X=0,
  41.             Y=0,
  42.             width=20,
  43.             height=38,
  44.             dir=0,          --   -1: left, 1: right},
  45.             spd=0,          --horizontal speed
  46.             verticalspd=0,  --vertical speed
  47.             moves=false,    --for horizontal movement
  48.             grounded=false, --for vertical movement
  49.             jumps=false
  50.         },
  51.     [2] = { HP=0},
  52.     [3] = { HP=0},
  53.     [4] = { HP=0},
  54.     [5] = { HP=0}, 
  55.     [6] = { HP=0},
  56.     [7] = { HP=0},
  57.     [8] = { HP=0}
  58. }
  59.  
  60. local Map = {
  61.     [1] = { {1,1,1,0,0,1,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
  62.             {1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
  63.             {1,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  64.             {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  65.             {0,1,1,1,1,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  66.             {0,0,0,1,1,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  67.             {1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  68.             {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  69.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  70.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  71.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  72.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  73.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  74.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  75.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  76.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  77.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  78.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  79.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  80.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  81.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  82.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  83.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  84.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  85.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  86.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  87.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  88.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  89.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  90.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  91.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  92.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  93.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  94.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  95.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  96.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  97.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  98.             {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}
  99. }
  100.  
  101. local Mouse = {
  102.     X               = 0,
  103.     Y               = 0,
  104.     XBefore         = 0,
  105.     YBefore         = 0,
  106.     clickedFrames   = 0,
  107.     clicked         = false
  108. }
  109.  
  110. local Color = {
  111.     Selected    = 0xB0A0A0A0,
  112.     Normal      = 0xA0404040,
  113.     Grey        = 0xA0C0C0C0
  114. }
  115.  
  116. local game = {
  117.     [0]= function()
  118.             mode=11 -- remove later
  119.             foreground=0xFFFFFFFF
  120.             fadeFrames=60
  121.             console.clear()
  122.             if frames>20 then mode=1 end
  123.         end,   
  124.     [1]= function()
  125.             image = "files/intro.dat"
  126.             fadeIn()
  127.             if frames>80 then mode=2 end
  128.         end,
  129.     [2]= function()
  130.             fadeFrames=60
  131.             if frames>300 then mode=3 end
  132.         end,
  133.     [3]= function()
  134.             fadeOut()
  135.             if frames>360 then mode=4 end
  136.         end,
  137.     [4]= function()
  138.             fadeFrames=60
  139.             mode=5
  140.         end,
  141.     [5]= function()
  142.             image = "files/title.dat"
  143.             fadeIn()
  144.             if frames>420 then mode=6 end
  145.         end,
  146.     [6]= function()
  147.             image = "files/title.dat"
  148.             getMouse()
  149.             button(80,70,80,16,"     PLAY",Color.Normal,true,function()     mode=11
  150.                                                                     frames=0 end)
  151.             button(80,90,80,16,"BEST TIMES",Color.Normal,true,function() mode=10
  152.                                                                     frames=0 end)
  153.                                                                     --todo:actual highscore list
  154.                                                        
  155.             button(80,110,80,16,"  OPTIONS",Color.Normal,true,function() mode=10
  156.                                                                     frames=0 end)
  157.                                                                     --todo:actual options functionality
  158.             button(80,130,80,16,"      QUIT",Color.Normal,true,function()   mode=7
  159.                                                                     frames=0 end)
  160.             text(206,145,gameVersion,0xFFFFFFFF)                                                       
  161.         end,
  162.     [7]= function()
  163.             fadeFrames=60
  164.             mode=8
  165.         end,
  166.     [8]= function()
  167.             fadeOut()
  168.             if frames>60 then mode=9 end
  169.         end,
  170.     [9]= function()
  171.             foreground=nil
  172.             background=nil
  173.             image=nil
  174.             client.SetGameExtraPadding(0,0,0,0)
  175.             client.unpause()
  176.             --todo:save times to file
  177.         end,
  178.     [10]= function()
  179.             getMouse()
  180.             image = nil
  181.             text(20,20,"TEST SCREEN.\nNOTHING TO SEE HERE YET.\nGO BACK?",0xFFFFFFFF)
  182.             button(20,70,40,16,"YES!",Color.Grey,false,function() mode=6 end)
  183.         end,
  184.     [11]= function()
  185.             if reachedIngame then
  186.                 drawMap()
  187.                 debugMode=false
  188.                 debugPause=false
  189.             end
  190.             fadeFrames=60
  191.             frames=0
  192.             background=nil
  193.             mode=12
  194.         end,
  195.     [12]= function()
  196.             if reachedIngame then
  197.                 drawMap()
  198.             end
  199.             fadeOut()
  200.             if frames>60 then mode=13 end
  201.         end,
  202.     [13]= function()
  203.             prepareRoom()
  204.             gui.clearImageCache()
  205.             fadeFrames=60
  206.             mode=14
  207.         end,
  208.     [14]= function()
  209.             image = "files/sky.dat"
  210.             drawMap()
  211.             fadeIn()
  212.             if frames>120 then
  213.                 mode=15
  214.                 frames=0
  215.                 reachedIngame=true
  216.             end
  217.         end,
  218.     [15]= function()    -- MAIN INGAME LOOP
  219.    
  220.             drawMap()
  221.             drawPlayer()
  222.    
  223.             if not debugPause then
  224.                 if frames%2==0 then
  225.                     assertMapPosition()
  226.                     --enemyHitCollision()
  227.                 else
  228.                     --enemyMove()
  229.                     --enemyWallCollision()
  230.                 end
  231.                        
  232.                 playerWallCollision()
  233.                 getInput()     
  234.                 playerMove()   
  235.                 assertCameraScroll()
  236.             end
  237.  
  238.             --remove this section later
  239.             getMouse()                                 
  240.             if debugMode then debugColor=Color.Selected
  241.             else debugColor=Color.Normal end           
  242.             if debugPause then pauseColor=Color.Selected    
  243.             else pauseColor=Color.Normal end       
  244.             button(170,8,58,16," Debug ",debugColor,false,function() debugMode=not debugMode end)
  245.             button(170,26,58,16," Pause ",pauseColor,false,function() debugPause=not debugPause end)
  246.             button(170,44,58,16," Reset ",Color.Normal,false,function()     mode=11 end)
  247.             --  --  --  --  --  --  --     
  248.         end
  249. }
  250.  
  251. function img(x,y,path)
  252.     gui.drawImage(path,x,y)
  253. end
  254.  
  255. function text(x, y, text, color)
  256.     gui.drawText(x, y, text,color,0x00000000,12,"Arial")
  257. end
  258.  
  259. function button(posx,posy,width,height,label,color,mainmenu,clickedfunction)
  260.     if Mouse.X>posx and Mouse.X<posx+width and Mouse.Y>posy and Mouse.Y<posy+height then
  261.         if Mouse.clicked and Mouse.clickedFrames%8==1 then  
  262.             clickedfunction()
  263.         end
  264.         gui.drawBox(posx,posy,posx+width,posy+height,0xFFFFFFFF,color+0x30303030)
  265.         if mainmenu then
  266.             for a=0,10 do
  267.                 gui.drawLine(posx+1+a,posy+1,posx+1+a,posy+height-1,0xFFFFFFFF-0x18000000*a)
  268.                 gui.drawLine(posx+width-1-a,posy+1,posx+width-1-a,posy+height-1,0xFFFFFFFF-0x18000000*a)
  269.             end
  270.         end
  271.     else
  272.         gui.drawBox(posx,posy,posx+width,posy+height,color-0x00303030,color)
  273.         if mainmenu then
  274.             for a=0,10 do
  275.                 gui.drawLine(posx+1+a,posy+1,posx+1+a,posy+height-1,0xFF303030-0x18000000*a)
  276.                 gui.drawLine(posx+width-1-a,posy+1,posx+width-1-a,posy+height-1,0xFF303030-0x18000000*a)
  277.             end
  278.         end
  279.     end
  280.     text(posx+3,posy+1,label,0xFFFFFFFF)
  281. end
  282.  
  283. function fadeIn()
  284.     if fadeFrames>=0 then
  285.         foreground=string.format("%x",math.floor(255/60*fadeFrames)) .. "FFFFFF"
  286.         foreground=tonumber(foreground,16)
  287.         fadeFrames=fadeFrames-1
  288.     else
  289.         foreground=nil
  290.     end
  291. end
  292.  
  293. function fadeOut()
  294.     if fadeFrames>=0 then
  295.         foreground=string.format("%x",math.floor(255/60*(60-fadeFrames))) .. "FFFFFF"
  296.         foreground=tonumber(foreground,16)
  297.         fadeFrames=fadeFrames-1
  298.     else
  299.         foreground=nil
  300.     end
  301. end
  302.  
  303. function prepareRoom()
  304.     --todo, change this so it's not just for newgame but new rooms in general
  305.     currentRoom=1
  306.     Actor[1].HP=1000
  307.     Actor[1].X=110
  308.     Actor[1].Y=70
  309.     Actor[1].dir=1
  310.     Actor[1].spd=0
  311.     Actor[1].verticalspd=0
  312.     Actor[1].jumps=false
  313.     Actor[1].grounded=true
  314.     Actor[1].moves=false
  315.     Actor[2].HP=nil
  316.     Actor[3].HP=nil
  317.     Actor[4].HP=nil
  318.     Actor[5].HP=nil
  319.     Actor[6].HP=nil
  320.     MapOffset.X=0
  321.     MapOffset.Y=0
  322.     Camera.Xcap = (table.getn(Map[currentRoom][1])-currentDisplayWidth+1)*tileSize
  323.     Camera.Ycap = (table.getn(Map[currentRoom])-currentDisplayHeight)*tileSize
  324.     assertCameraScroll()
  325. end
  326.  
  327. function getInput()
  328.     Actor[1].moves=false
  329.     keys = input.get()
  330.  
  331.     if keys.LeftArrow then  --LEFT
  332.         Actor[1].moves=true
  333.         Actor[1].dir=-1
  334.     elseif keys.RightArrow then     --RIGHT
  335.         Actor[1].moves=true
  336.         Actor[1].dir=1
  337.     end
  338.  
  339.     if keys.UpArrow then    --JUMP
  340.         if Actor[1].grounded then
  341.             Actor[1].grounded=false
  342.             Actor[1].verticalspd=-6
  343.         end
  344.     end
  345. end
  346.  
  347. function getMouse()
  348.     Mouse.X = input.getmouse().X
  349.     Mouse.Y = input.getmouse().Y
  350.     Mouse.clicked = input.getmouse().Left
  351.     if Mouse.clicked then Mouse.clickedFrames = Mouse.clickedFrames + 1
  352.     else Mouse.clickedFrames = 0 end
  353. end
  354.  
  355. function drawMap()
  356.     for a=MapOffset.Y+1,MapOffset.Y+currentDisplayHeight do
  357.         for b=MapOffset.X+1,MapOffset.X+currentDisplayWidth do
  358.        
  359.             tilex=-Camera.X+tileSize*(b-1)
  360.             tiley=-Camera.Y+tileSize*(a-1)
  361.            
  362.             if Map[currentRoom][a][b]==0 then -- nothing
  363.             --  img(tilex,tiley,"files/mapTest0.dat")
  364.             elseif Map[currentRoom][a][b]==1 then --wall
  365.                 img(tilex,tiley,"files/mapTest1.dat")              
  366.             end
  367.            
  368.             if debugMode then
  369.                 text(tilex,tiley,Map[currentRoom][a][b],0xFFFFFFFF)
  370.             end
  371.            
  372.         end
  373.     end
  374.    
  375.     if debugMode then
  376.         for i=1,table.getn(debugWallCollision) do
  377.             if table.getn(debugWallCollision)>0 then
  378.                     img(debugWallCollision[i][1],debugWallCollision[i][2],"files/mapTestC.dat")
  379.             end
  380.         end
  381.         if not debugPause then
  382.             debugWallCollision={}
  383.         end
  384.     end
  385. end
  386.  
  387. function drawPlayer()
  388.     PlayerX =   Actor[1].X-Camera.X
  389.     PlayerY =   Actor[1].Y-Camera.Y
  390.    
  391.     --left
  392.     gui.drawBox(PlayerX,PlayerY,PlayerX+Actor[1].width,PlayerY+Actor[1].height,0xFFFFFF00)
  393.    
  394. end
  395.  
  396. function playerMove()
  397.  
  398.     --apply speed, player gains distance
  399.     Actor[1].X = Actor[1].X+Actor[1].spd
  400.     Actor[1].Y = Actor[1].Y+Actor[1].verticalspd
  401.    
  402.     --moves right or left, runs to 0 if no button held
  403.     if Actor[1].moves then
  404.         Actor[1].spd = Actor[1].spd+Actor[1].dir
  405.     elseif Actor[1].spd ~= 0 then
  406.         if frames%3==0 then
  407.             Actor[1].spd = Actor[1].spd-1*Actor[1].dir
  408.         end
  409.     end
  410.    
  411.     --moves up or down, gains distance
  412.     if not Actor[1].grounded then
  413.         if Actor[1].verticalspd < 5 then
  414.             if frames%2==0 then
  415.                 Actor[1].verticalspd = Actor[1].verticalspd+1
  416.             end
  417.         end
  418.     end
  419.    
  420.     --speed cap
  421.     if Actor[1].spd>2 then Actor[1].spd=2
  422.     elseif Actor[1].spd<-2 then Actor[1].spd=-2 end
  423.    
  424. end
  425.  
  426. function playerWallCollision()
  427.    
  428.     local empty
  429.     local playerX = math.ceil(Actor[1].X/tileSize)  -- this is where the player sits in terms of tile units
  430.     local playerY = math.ceil(Actor[1].Y/tileSize)
  431.     local collisionCheckRangeX = math.ceil((Actor[1].X+Actor[1].width)/tileSize) - playerX  -- amount of tiles your hitbox extends into
  432.     local collisionCheckRangeY = math.ceil((Actor[1].Y+Actor[1].height)/tileSize) - playerY
  433.  
  434.     local function moveLeft()
  435.         Actor[1].X = (playerX+collisionCheckRangeX-1)*tileSize-Actor[1].width-1
  436.         Actor[1].spd=0
  437.     end
  438.    
  439.     local function moveRight()
  440.         Actor[1].X = playerX*tileSize+1
  441.         Actor[1].spd=0
  442.     end
  443.    
  444.     local function moveBottom()
  445.         Actor[1].Y = playerY*tileSize
  446.         Actor[1].verticalspd=1
  447.     end
  448.    
  449.     local function moveTop()
  450.         Actor[1].Y= (playerY+collisionCheckRangeY-1)*tileSize - Actor[1].height-1   -- else, put character to the top
  451.         Actor[1].verticalspd=0
  452.         Actor[1].grounded=true
  453.     end
  454.  
  455.     if Actor[1].spd ~=0 or Actor[1].verticalspd ~=0 then
  456.        
  457.         for a=0,collisionCheckRangeY do
  458.             for b=0,collisionCheckRangeX do
  459.            
  460.                 if debugMode then  
  461.                     table.insert(debugWallCollision, {tileSize*(playerX+b-1)-Camera.X,  (tileSize*(playerY+a-1))-Camera.Y})
  462.                 end
  463.                
  464.                 if Map[currentRoom][playerY+a][playerX+b]==1 then
  465.                 --wall collision detected
  466.                
  467.                     if b==0 and a==0 then   -- TOP LEFT CORNER
  468.  
  469.                         if (tileSize-(Actor[1].X-1)%tileSize) < (tileSize-(Actor[1].Y-1)%tileSize) then -- shortest distance is rightwards
  470.                        
  471.                             empty=true
  472.                            
  473.                             for z=1,math.max(1,collisionCheckRangeX) do -- if right is empty,
  474.                                 if Map[currentRoom][playerY+a][playerX+b+z]==1 then
  475.                                     empty=false
  476.                                     break
  477.                                 end
  478.                             end
  479.                            
  480.                             if empty then
  481.                                 moveRight()
  482.                             else
  483.                                 moveBottom()
  484.                             end
  485.                            
  486.                         else -- shortest distance is bottom
  487.                            
  488.                             empty=true
  489.                            
  490.                             for z=1,math.max(1,collisionCheckRangeY) do -- if bottom is empty,
  491.                                 if Map[currentRoom][playerY+a+z][playerX+b]==1 then
  492.                                     empty=false
  493.                                     break
  494.                                 end
  495.                             end
  496.                            
  497.                             if empty then
  498.                                 moveBottom()
  499.                             else
  500.                                 moveRight()
  501.                             end
  502.                            
  503.                         end
  504.                    
  505.                     elseif b==collisionCheckRangeX and a==0 then    --TOP RIGHT CORNER
  506.                    
  507.                         if ((Actor[1].X+Actor[1].width)%tileSize) < (tileSize-(Actor[1].Y-1)%tileSize) then -- shortest distance is leftwards
  508.                        
  509.                             empty=true
  510.                        
  511.                             for z=1,math.max(1,collisionCheckRangeX) do -- if left is empty,
  512.                                 if Map[currentRoom][playerY+a][playerX+b-z]==1 then
  513.                                     empty=false
  514.                                     break
  515.                                 end
  516.                             end
  517.                        
  518.                             if empty then
  519.                                 moveLeft()
  520.  
  521.                             else
  522.                                 moveBottom()
  523.                             end
  524.                        
  525.                         else -- shortest distance is bottom
  526.                            
  527.                             empty=true
  528.                            
  529.                             for z=1,math.max(1,collisionCheckRangeY) do -- if bottom is empty,
  530.                                 if Map[currentRoom][playerY+a+z][playerX+b]==1 then
  531.                                     empty=false
  532.                                     break
  533.                                 end
  534.                             end
  535.                            
  536.                             if empty then
  537.                                 moveBottom()
  538.                             else
  539.                                 moveLeft()
  540.                             end
  541.                            
  542.                         end
  543.                    
  544.                     elseif b==0 and a==collisionCheckRangeY then    --BOTTOM LEFT CORNER
  545.  
  546.                         if (tileSize-(Actor[1].X-1)%tileSize) < ((Actor[1].Y+Actor[1].height-1)%tileSize) then -- shortest distance is rightwards
  547.  
  548.                             empty=true
  549.                        
  550.                             for z=1,math.max(1,collisionCheckRangeX) do -- if right is empty,
  551.                                 if Map[currentRoom][playerY+a][playerX+b+z]==1 then
  552.                                     empty=false
  553.                                     break
  554.                                 end
  555.                             end
  556.                            
  557.                             if empty then
  558.                                 moveRight()
  559.                             else
  560.                                 moveTop()
  561.                             end
  562.                        
  563.                         else    -- shortest distance is top
  564.                        
  565.                             empty=true
  566.                        
  567.                             for z=1,math.max(1,collisionCheckRangeY) do     -- if top is empty,
  568.                                 if Map[currentRoom][playerY+a-z][playerX+b]==1 then
  569.                                     empty=false
  570.                                     break
  571.                                 end
  572.                             end
  573.                            
  574.                             if empty then
  575.                                 moveTop()
  576.                             else
  577.                                 moveRight()
  578.                             end
  579.                        
  580.                         end
  581.                    
  582.                     elseif b==collisionCheckRangeX and a==collisionCheckRangeY then     --BOTTOM RIGHT CORNER
  583.  
  584.                         if ((Actor[1].X+Actor[1].width)%tileSize) < ((Actor[1].Y+Actor[1].height)%tileSize) then -- shortest distance is leftwards
  585.                        
  586.                             empty=true
  587.                        
  588.                             for z=1,math.max(1,collisionCheckRangeX) do -- if left is empty,
  589.                                 if Map[currentRoom][playerY+a][playerX+b-z]==1 then
  590.                                     empty=false
  591.                                     break
  592.                                 end
  593.                             end
  594.                        
  595.                             if empty then
  596.                                 moveLeft()
  597.                             else
  598.                                 moveTop()
  599.                             end
  600.                        
  601.                         else    -- shortest distance is top
  602.  
  603.                             empty=true
  604.                        
  605.                             for z=1,math.max(1,collisionCheckRangeY) do     -- if top is empty,
  606.                                 if Map[currentRoom][playerY+a-z][playerX+b]==1 then
  607.                                     empty=false
  608.                                     break
  609.                                 end
  610.                             end
  611.  
  612.                             if empty then
  613.                                 moveTop()
  614.                             else
  615.                                 moveLeft()
  616.                             end    
  617.                            
  618.                         end            
  619.                     end
  620.                 end
  621.             end
  622.         end
  623.     end
  624.    
  625.     -- if no wall below, make character fall
  626.     if Map[currentRoom][playerY+collisionCheckRangeY+1][playerX] == 0
  627.     and Map[currentRoom][playerY+collisionCheckRangeY+1][playerX+collisionCheckRangeX] == 0
  628.     and Map[currentRoom][playerY+collisionCheckRangeY][playerX] == 0
  629.     and Map[currentRoom][playerY+collisionCheckRangeY][playerX+collisionCheckRangeX] == 0
  630.     then
  631.         Actor[1].grounded=false
  632.     end
  633.  
  634. end
  635.  
  636. function assertCameraScroll()
  637.     if Actor[1].X<120 then 
  638.         Camera.X=0
  639.     else
  640.         Camera.X=Actor[1].X-120
  641.     end
  642.    
  643.     if Actor[1].Y<80 then
  644.         Camera.Y=0
  645.     else
  646.         Camera.Y=Actor[1].Y-80
  647.     end
  648.    
  649.     if Camera.X>Camera.Xcap then Camera.X=Camera.Xcap end
  650.     if Camera.Y>Camera.Ycap then Camera.Y=Camera.Ycap end
  651. end
  652.  
  653. function assertMapPosition()
  654.     MapOffset.X = math.floor(Camera.X / tileSize)
  655.     MapOffset.Y = math.floor(Camera.Y / tileSize)
  656. end
  657.  
  658. while true do
  659.  
  660.     if not client.ispaused() then client.pause() end
  661.  
  662.     if image~=nil then
  663.         img(0,0,image)
  664.     end
  665.    
  666.     if background~=nil then
  667.         gui.drawBox(-1,-1,240,160,0x00000000,background)
  668.     end
  669.    
  670.     game[mode]()
  671.  
  672.     if foreground~=nil then
  673.         gui.drawBox(-1,-1,240,160,0x00000000,foreground)
  674.     end
  675.    
  676.     if client.ispaused() then
  677.         gui.DrawNew("emu")
  678.         emu.yield()
  679.     else
  680.         emu.frameadvance()
  681.     end
  682.    
  683.     frames=frames+1
  684. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement