Redxone

CC - Zelda-Style Game [WIP]

Feb 24th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.63 KB | None | 0 0
  1. os.loadAPI("grid")
  2. local blocks = {
  3.   [-1]={name='void',char=" ",tc=colors.black,bc=colors.black,solid=true},
  4.   [0]={name='player',char="&",tc=colors.blue,solid=true},
  5.   {name='grass',char=' ',tc=colors.lime,bc=colors.green,solid=false},
  6.   {name='wall',char='L',tc=colors.gray,bc=colors.lightGray,solid=true},
  7.   {name='sand',char='.',tc=colors.white,bc=colors.yellow,solid=false},
  8.   {name='rose',char='*',tc=colors.red,bc=colors.green,solid=false},
  9.   {name='dandilion',char='*',tc=colors.yellow,bc=colors.green,solid=false},
  10.   {name='water',char='~',tc=colors.lightBlue,bc=colors.blue,solid=false},
  11.   {name='treetrunk',char='Y',tc=colors.brown,bc=colors.green,solid=true},
  12.   {name='treetop',char='@',tc=colors.green,bc=colors.lime,solid=true},
  13.   {name='hillleft',char='\\',tc=colors.gray,bc=colors.brown,solid=true},
  14.   {name='hillright',char='/',tc=colors.gray,bc=colors.brown,solid=true},
  15.   {name='hill',char='|',tc=colors.gray,bc=colors.brown,solid=true},
  16.   {name='road',char=' ',tc=colors.lightGray,bc=colors.lightGray,solid=false},
  17.   {name='rupee',char='!',tc=colors.blue,bc=colors.green,solid=false},
  18.   {name='shadow',char=" ",tc=colors.gray,bc=colors.gray,solid=false},
  19.   {name='flowerbed',char="o",tc=colors.red,bc=colors.orange,solid=false},
  20.   {name='flowerbedgreen',char="o",tc=colors.green,bc=colors.lime,solid=false},
  21.   --{name='enemy',char='&',tc=colors.red,solid=true},
  22.   {name='chest',char='\169',tc=colors.yellow,bc=colors.brown,solid=true},
  23.   --{name='grass',char='"',tc=colors.lime,bc=colors.green,solid=false},
  24. }
  25. local map = grid.create(150,150)
  26. map:fill(1)
  27. local playerx = 10
  28. local playery = 10
  29. local w,h = term.getSize()
  30. local overx = 0
  31. local overy = 0
  32. local selblock = 2
  33. local states = {inMenu=false,inGame=false,scrollX=false,scrollY=false}
  34. function drawMap(x,y)
  35.   -- Draw Map --
  36.   if(type(map:get(x,y)) == "number")then
  37.       term.setCursorPos(x-overx,y-overy)
  38.       term.setTextColor(blocks[map:get(x,y)].tc)
  39.       term.setBackgroundColor(blocks[map:get(x,y)].bc)
  40.       term.write(blocks[map:get(x,y)].char)
  41.   else
  42.      term.setCursorPos(x-overx,y-overy)
  43.      term.setBackgroundColor(colors.blue)
  44.      term.setTextColor(colors.lightBlue)
  45.      term.write(" ")
  46.   end
  47. end
  48.  
  49. function drawPlayer()
  50.   -- Draw Player Map (Pmap) --
  51.   if(not states.inGame)then
  52.       term.current().setVisible(false)
  53.       term.setCursorPos(1,1)
  54.       term.setBackgroundColor(colors.gray)
  55.       term.setTextColor(colors.yellow)
  56.       print("PlayerX: " .. playerx .. " PlayerY: " .. playery .. " ")
  57.       print("ScrollX: " .. overx .. "  ScrollY: " .. overy .. "  ")
  58.       term.current().setVisible(true)
  59.   end
  60.   term.setCursorPos(playerx-overx,playery-overy)
  61.   term.setTextColor(blocks[0].tc)
  62.   term.setBackgroundColor(blocks[map:get(playerx,playery)].bc)
  63.   if(blocks[map:get(playerx,playery)].bc == blocks[0].tc)then
  64.       term.setTextColor(colors.lightBlue)  
  65.   end
  66.   term.write(blocks[0].char)
  67. end
  68. function draw()
  69.   map:loop(drawMap,overx,overy,overx+w,overy+h)
  70.   drawPlayer()
  71. end
  72. function collides(cx,cy)
  73.  if(type(map:get(playerx+cx,playery+cy)) == 'number')then
  74.   return blocks[map:get(playerx+cx,playery+cy)].solid
  75.  else
  76.   return true
  77.  end
  78. end
  79. function panTo(onx,ony)
  80.   local xmx = w-5
  81.   local ymx = h-5
  82.   if(xmx < 1)then xmx = 1 end
  83.   if(ymx < 1)then ymx = 1 end
  84.   if(onx > 1)then onx = 1 end
  85.   if(onx < -1)then onx = -1 end
  86.   if(ony > 1)then ony = 1 end
  87.   if(ony < -1)then ony = -1 end
  88.  
  89.   if(onx ~= 0)then
  90.      for i = 1, xmx do
  91.        overx = overx + onx
  92.        sleep(0.1)
  93.        draw()
  94.        if(not inGame)then drawSelected() end
  95.      end
  96.   end
  97.   if(ony ~= 0)then
  98.     for i = 1, ymx do
  99.       overy= overy + ony
  100.       sleep(0.1)
  101.       draw()
  102.       if(not inGame)then drawSelected() end
  103.     end
  104.   end
  105. end
  106. function calcOver()
  107.     overx = playerx-5
  108.     overy = playery-5
  109.     if(overx < 0)then overx = playerx-1 end
  110.     if(overy < 0)then overy = playery-1 end
  111.     draw()
  112. end
  113. function panOver(time)
  114.   local xmx = w-5
  115.   local ymx = h-5
  116.   local lerptime = 0.1
  117.   if(time ~= nil)then lerptime = time end
  118.   if(xmx < 1)then xmx = 1 end
  119.   if(ymx < 1)then ymx = 1 end
  120.   if(playerx > overx+w)then
  121.      for i = 1, xmx do
  122.        overx = overx + 1
  123.        sleep(lerptime)
  124.        draw()
  125.      end
  126.   end
  127.   if(playery > overy+h)then
  128.     for i = 1, ymx do
  129.       overy = overy + 1
  130.       sleep(lerptime)
  131.       draw()
  132.     end
  133.   end
  134.   if(playerx <= overx)then
  135.     for i = 1, xmx do
  136.       if(overx > 0)then overx = overx - 1 else break end
  137.       sleep(lerptime)
  138.       draw()
  139.     end
  140.   end
  141.   if(playery <= overy)then
  142.     for i = 1, ymx do
  143.       if(overy > 0)then overy = overy - 1 else break end
  144.       sleep(lerptime)
  145.       draw()
  146.     end
  147.   end
  148. end
  149. function drawEditor()
  150.     term.setCursorPos(1,h)
  151.     term.setBackgroundColor(colors.gray)
  152.     term.clearLine()
  153.     local overd = selblock - 8
  154.     if(overd < 0)then overd = 0 end
  155.     for i = 0+overd, 8+overd do
  156.       if(blocks[i] ~= nil)then
  157.          ix = ( (i-overd) + 1 ) * 5
  158.          term.setCursorPos(ix, h)
  159.          term.setTextColor(blocks[i].tc)
  160.          local bcolor = colors.gray
  161.          if(blocks[i].bc ~= nil)then bcolor = blocks[i].bc end
  162.          term.setBackgroundColor(bcolor)
  163.          write(blocks[i].char)
  164.          if(selblock == i)then
  165.              term.setTextColor(colors.yellow)
  166.              term.setBackgroundColor(colors.gray)
  167.              term.setCursorPos(ix-1, h)
  168.              write(">")
  169.              term.setCursorPos(ix+#blocks[i].char, h)
  170.              write("<")
  171.          end
  172.        end
  173.     end
  174.  
  175.     term.current().setVisible(false)
  176.     -- scroll bars --
  177.     paintutils.drawLine(w, 1, w, h-1, colors.lightGray)
  178.     term.setTextColor(colors.white)
  179.     term.setCursorPos(w,1)
  180.     write("^")
  181.     term.setCursorPos(w,h-1)
  182.     write("v")
  183.     local hscale = overy+2
  184.     local tscale = math.floor((overy+2)/h)
  185.     if(hscale >= h-1)then hscale = math.floor((overy+2)/h)+2 end
  186.     if(tscale <= 0)then tscale = "=" end
  187.     term.setCursorPos(w,hscale)
  188.     term.setBackgroundColor(colors.gray)
  189.     write(tscale)
  190.     -- X Scale --
  191.     paintutils.drawLine(1, h-1, w-1, h-1, colors.lightGray)
  192.     term.setTextColor(colors.white)
  193.     term.setCursorPos(1,h-1)
  194.     write("<")
  195.     term.setCursorPos(w-1,h-1)
  196.     write(">")
  197.     local wscale = overx+2
  198.     local tscale = math.floor((overx+2)/w)
  199.     if(wscale >= w-1)then wscale = math.floor((overx+2)/w)+2 end
  200.     if(tscale <= 0)then tscale = "=" end
  201.     term.setCursorPos(wscale,h-1)
  202.     term.setBackgroundColor(colors.gray)
  203.     write(tscale)
  204.     term.current().setVisible(true)
  205. end
  206. draw()
  207. drawEditor()
  208. while true do
  209.   e = {os.pullEvent()}
  210.   if(states.inGame)then
  211.       if(e[1]=="key")then
  212.           drawMap(playerx,playery)
  213.         if(e[2] == keys.w and playery > 1 and not collides(0,-1)) then
  214.             playery = playery - 1
  215.         end
  216.         if(e[2] == keys.s and playery < map:getHeight() and not collides(0,1)) then
  217.            playery = playery + 1
  218.         end
  219.         if(e[2] == keys.a and playerx > 1 and not collides(-1,0)) then
  220.           playerx = playerx - 1
  221.         end
  222.         if(e[2] == keys.d and playerx < map:getWidth() and not collides(1,0)) then
  223.           playerx = playerx + 1
  224.         end
  225.           drawPlayer()
  226.           panOver()
  227.       end
  228.     end
  229.  
  230.     if(states.inMenu and not states.inGame)then
  231.  
  232.     end
  233.  
  234.     if(not states.inMenu and not states.inGame)then
  235.         if(e[1] == "mouse_scroll")then
  236.           if(e[3] ~= w and e[4] ~= h-1)then
  237.            if(e[2] == -1 and selblock > 0)then selblock = selblock - 1 end
  238.            if(e[2] == 1 and selblock < #blocks)then selblock = selblock + 1 end
  239.           end
  240.           if(e[3] == w and e[4] ~= h-1)then overy = overy + e[2] draw() end
  241.           if(e[4] == h-1 and e[3] ~= w)then overx = overx + e[2] draw() end
  242.           if(overx > (map:getWidth()-w)+1)then overx = (map:getWidth()-w)+1 draw() end
  243.           if(overx < 0)then overx = 0 draw() end
  244.           if(overy < 0)then overy = 0 draw() end
  245.           if(overy > (map:getHeight()-h)+1)then overy = (map:getHeight()-h)+1 draw() end
  246.            drawEditor()
  247.         end
  248.         if(e[1] == "key")then
  249.            pk = e[2]
  250.            if(pk == keys.g)then states.inGame = true calcOver() end
  251.            if(pk == keys.w and overy > 0)then overy = overy - 1 draw() end
  252.            if(pk == keys.s and overy < (map:getHeight()-h)+2)then overy = overy + 1 draw() end
  253.            if(pk == keys.a and overx > 0)then overx = overx - 1 draw() end
  254.            if(pk == keys.d and overx < (map:getWidth()-w)+1)then  overx = overx + 1 draw() end
  255.            if(pk == keys.left and selblock > 0)then selblock = selblock - 1 end
  256.            if(pk == keys.right and selblock < #blocks)then selblock = selblock + 1 end
  257.            if(not states.inGame and not states.inMenu)then drawEditor() end
  258.         end
  259.         -- Placing blocks in edit mode --
  260.         if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
  261.             if(e[2] == 1)then
  262.               if(selblock == 0)then
  263.                   drawMap(playerx,playery)
  264.                   playerx = e[3]+overx
  265.                   playery = e[4]+overy
  266.                   drawPlayer()
  267.               elseif(e[3]+overx <= map:getWidth() and e[4]+overy <= map:getHeight() and e[3]+overx >= 0 and e[3]+overy >= 0)then
  268.                   map:set(e[3]+overx,e[4]+overy,selblock)
  269.                   drawMap(e[3]+overx,e[4]+overy)
  270.                   if(e[3]+overx == playerx and e[4]+overy == playery)then
  271.                       drawPlayer()
  272.                   end
  273.               end
  274.             elseif(e[2] == 2)then
  275.                map:set(e[3]+overx,e[4]+overy,-1)
  276.                drawMap(e[3]+overx,e[4]+overy)
  277.             end
  278.         end
  279.     end
  280. end
Add Comment
Please, Sign In to add comment