libraryaddict

Untitled

Apr 25th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.33 KB | None | 0 0
  1. --Lets make a game..
  2. local Player = {"<", ">", "^", "V"}
  3. local Laser = {}
  4. local Lasers = {"-", "-", "i", "!"}
  5. local Pulsing = {"o", "O", "0", "O"}
  6. local PulseExplodes = {}
  7. local Pulse = {}
  8. local Cooldown = 0
  9. local CooldownPulse = 0
  10. local Map = {}
  11. local MapView = {}
  12. local Health = 10
  13. local Lives = 3
  14. local Score = 10
  15. local Name = ""
  16. function clear()
  17.   term.clear()
  18.   term.setCursorPos(1,1)
  19. end
  20.  
  21. function LoadFile()
  22.   repeat
  23.     clear()
  24.     write("A map file: ")
  25.     Name = read()
  26.     if not fs.exists("Maps/"..Name) then
  27.       print("That file doesnt exist")
  28.       sleep(3)
  29.     end
  30.   until fs.exists("Maps/"..Name)
  31.   clear()
  32.   File = io.open("Maps/"..Name, "r")
  33.   for line in File:lines() do
  34.     Map[#Map+1] = line.."\n"
  35.   end
  36.   PlayerCords = Map[1]
  37.   PlayerDir = tonumber(string.sub(PlayerCords, 5, 5))
  38.   PlayerX = tonumber(string.sub(PlayerCords, 1, 2))
  39.   PlayerY = tonumber(string.sub(PlayerCords, 3, 4))
  40.   table.remove(Map, 1)
  41.   File:close()
  42.   p = io.open("Debug", "w")
  43.   for n=1,#Map do
  44.     p:write(Map[n])
  45.   end
  46.   for n=1,#Map do
  47.     MapView[n] = Map[n]
  48.   end
  49.   p:close()
  50.   StatusBox()
  51.   reDraw()
  52. end
  53.  
  54. function Load()
  55.   Name = ""
  56.   LoadFile()
  57. end
  58.  
  59. function Tick()
  60.   if Cooldown > 0 then
  61.     Cooldown = Cooldown-1
  62.   end
  63.   if CooldownPulse > 0 then
  64.     CooldownPulse = CooldownPulse-1
  65.   end
  66.   PulseMove()
  67.   LaserMove()
  68.   CheckUnder()
  69.   UpdateExplode()
  70.   reDraw()
  71.   Ticker = os.startTimer(0.1)
  72. end
  73.  
  74. function CheckUnder() -- Check under the player
  75.   if CheckMapView(PlayerX, PlayerY) ~= " " then
  76.     if CheckMapView(PlayerX, PlayerY) == "x" then --Pickup pack!
  77.       StatusBox("Health", 5)
  78.       deleteMapView(PlayerX, PlayerY)
  79.     elseif CheckMapView(PlayerX, PlayerY) == "o" then -- Hit by explosion!
  80.       StatusBox("Health", -3)
  81.       deleteMapView(PlayerX, PlayerY)
  82.     elseif CheckMapView(PlayerX, PlayerY) == "i" or CheckMapView(PlayerX, PlayerY) == "!" or CheckMapView(PlayerX, PlayerY) == "-" then --Laser!
  83.       StatusBox("Health", -5)
  84.       deleteMapView(PlayerX, PlayerY)
  85.       for n=1,#Laser do
  86.         if Laser[n] then
  87.           local LaserX = tonumber(string.sub(Laser[n], 1, 2))
  88.           local LaserY = tonumber(string.sub(Laser[n], 3, 4))
  89.           if LaserX == PlayerX and LaserY == PlayerY then
  90.             table.remove(Laser, n)
  91.             n = n-1
  92.           end
  93.         end
  94.       end
  95.     elseif checkPulse() then --pulse!
  96.       StatusBox("Health", -10)
  97.       deleteMapView(PlayerX, PlayerY)
  98.       PulseExplode(checkPulse())
  99.       for n=1,#Pulse do
  100.         if Pulse[n] then
  101.           local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  102.           local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  103.           if PulseX == PlayerX and PulseY == PlayerY then
  104.             table.remove(Pulse, n)
  105.             n = n-1
  106.           end
  107.         end
  108.       end
  109.     end
  110.   end
  111. end
  112.  
  113. function checkPulse()
  114.   for n=1,#Pulse do
  115.     if Pulse[n] then
  116.       local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  117.       local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  118.       if PulseX == PlayerX and PulseY == PlayerY then
  119.         return PulseX, PulseY
  120.       end
  121.     end
  122.   end
  123.   return false
  124. end
  125.  
  126. function StatusBox(Type, Modifier)
  127.   FlashHP = false
  128.   FlashLives = false
  129.   if Type == "Health" then
  130.     Health = tonumber(Health)+tonumber(Modifier)
  131.     FlashHP = true
  132.     if Health <= 0 then
  133.       if Lives > 1 then
  134.         Lives = Lives-1
  135.         Health = 10
  136.         FlashLives = true
  137.       else
  138.         clear()
  139.         print("You died")
  140.         error()
  141.       end
  142.     end
  143.   end
  144.   DrawStatus()
  145. end
  146.  
  147. function DrawStatus()
  148.   for n=1,3 do
  149.     MapView[n] = string.sub(MapView[n], 14, string.len(MapView[4]))
  150.   end
  151.   local ScoreNum = tonumber("4")-string.len(Score)
  152.   local Spaces = string.rep(" ", ScoreNum)
  153.   MapView[1] = "Score: "..Score..Spaces.." O"..MapView[1]
  154.   if FlashLives == true then
  155.     MapView[2] = "Lives:      O"..MapView[2]
  156.   else
  157.     MapView[2] = "Lives: "..Lives.."    O"..MapView[2]
  158.   end
  159.   local HealthNum = tonumber("4")-string.len(Health)
  160.   local Spaces = string.rep(" ", HealthNum)
  161.   if FlashHP == true then
  162.     MapView[3] = "Health:     O"..MapView[3]
  163.   else
  164.     MapView[3] = "Health: "..Health..Spaces.."O"..MapView[3]
  165.   end
  166.   MapView[4] = string.sub(MapView[4], 14, string.len(MapView[4]))
  167.   MapView[4] = "OOOOOOOOOOOOO"..MapView[4]
  168.   Draw = os.startTimer(0.15)
  169. end
  170.  
  171. function CreateLaser(x, y, dir)
  172.   if CheckMapView(x, y) ~= "O" then
  173.     Laser[#Laser+1] = Num(x)..Num(y)..dir
  174.     EditMapView(x, y, Lasers[dir])
  175.   end
  176.   Cooldown = 5
  177. end
  178.  
  179. function CreatePulse(x, y, dir)
  180.   if CheckMapView(x, y) ~= "O" then
  181.     Pulse[#Pulse+1] = Num(x)..Num(y)..dir.."1".."1"
  182.     EditMapView(x, y, Pulsing[dir])
  183.   end
  184.   CooldownPulse = 30
  185. end
  186.  
  187. function Num(num)
  188.   if string.len(num) == 1 then
  189.     return "0"..num
  190.   else
  191.     return num
  192.   end
  193. end
  194.  
  195. function LaserMove()
  196.   for n=1,#Laser do
  197.     if Laser[n] then
  198.       local LaserX = tonumber(string.sub(Laser[n], 1, 2))
  199.       local LaserY = tonumber(string.sub(Laser[n], 3, 4))
  200.       local LaserDir = tonumber(string.sub(Laser[n], 5, 5))
  201.       deleteMapView(LaserX, LaserY)
  202.       if LaserDir == 1 then
  203.         LaserX = LaserX-1
  204.       elseif LaserDir == 2 then
  205.         LaserX = LaserX+1
  206.       elseif LaserDir == 3 then
  207.         LaserY = LaserY-1
  208.       elseif LaserDir == 4 then
  209.         LaserY = LaserY+1
  210.       else
  211.         error("LaserDir errored")
  212.       end
  213.       if CheckMapView(LaserX, LaserY) == " " then
  214.         EditMapView(LaserX, LaserY, Lasers[LaserDir])
  215.         Laser[n] = Num(LaserX)..Num(LaserY)..LaserDir
  216.       else
  217.         table.remove(Laser, n)
  218.         n = n-1
  219.       end
  220.     end
  221.   end
  222. end
  223.  
  224. function PulseMove()
  225.   for n=1,#Pulse do
  226.     if Pulse[n] then
  227.       local PulseX = tonumber(string.sub(Pulse[n], 1, 2))
  228.       local PulseY = tonumber(string.sub(Pulse[n], 3, 4))
  229.       local PulseDir = tonumber(string.sub(Pulse[n], 5, 5))
  230.       local PulseState = tonumber(string.sub(Pulse[n], 6, 6))
  231.       local Speed = tonumber(string.sub(Pulse[n], 7, 7))
  232.       deleteMapView(PulseX, PulseY)
  233.       local Sparex = PulseX
  234.       local Sparey = PulseY
  235.       if Speed == 1 then
  236.         if PulseDir == 1 then
  237.           PulseX = PulseX-1
  238.         elseif PulseDir == 2 then
  239.           PulseX = PulseX+1
  240.         elseif PulseDir == 3 then
  241.           PulseY = PulseY-1
  242.         elseif PulseDir == 4 then
  243.           PulseY = PulseY+1
  244.         else
  245.           error("LaserDir errored")
  246.         end
  247.         Speed = 0
  248.       else
  249.         Speed = 1
  250.       end
  251.       if PulseState == 4 then
  252.         PulseState = 1
  253.       else
  254.         PulseState = PulseState+1
  255.       end
  256.       if CheckMap(PulseX, PulseY) == " " then
  257.         EditMapView(PulseX, PulseY, Pulsing[PulseState])
  258.         Pulse[n] = Num(PulseX)..Num(PulseY)..PulseDir..PulseState..Speed
  259.       else
  260.         PulseExplode(Sparex, Sparey)
  261.         table.remove(Pulse, n)
  262.         n = n-1
  263.       end
  264.     end
  265.   end
  266. end
  267.  
  268. function PulseExplode(x, y, Spread)
  269.   if not Spread then Spread = "3" end
  270.   local Nope = 5
  271.   for n=1,#PulseExplodes do
  272.     if PulseExplodes[n] then
  273.       local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
  274.       local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
  275.       if PulseX == x and PulseY == y then -- Matches another explosion. Dont make it! Abort abort!
  276.         Nope = 4
  277.       end
  278.     end
  279.   end
  280.   if Nope == 5 then -- Nothing is here.. right?
  281.     if CheckMapView(x, y) ~= "O" then
  282.       PulseExplodes[#PulseExplodes+1] = Num(x)..Num(y).."3"..Spread
  283.       EditMapView(x, y, "o")
  284.     end
  285.   end
  286. end
  287. fs.delete("Log")
  288. function Logger(loggy)
  289.   p = io.open("Log", "a")
  290.   p:write(loggy.."\n")
  291.   p:close()
  292. end
  293.  
  294. function UpdateExplode()
  295.   for n=1,#PulseExplodes do
  296.     if PulseExplodes[n] then
  297.       local PulseX = tonumber(string.sub(PulseExplodes[n], 1, 2))
  298.       local PulseY = tonumber(string.sub(PulseExplodes[n], 3, 4))
  299.       local PulseStatus = tonumber(string.sub(PulseExplodes[n], 5, 5))
  300.       local Spread = tonumber(string.sub(PulseExplodes[n], 6, 6))
  301.       if PulseStatus < 1 then -- How long it has left in the world
  302.         deleteMapView(PulseX, PulseY)
  303.         table.remove(PulseExplodes, n)
  304.       elseif Spread ~= 0 then
  305.         PulseExplode(PulseX+1, PulseY, Spread-1)
  306.         PulseExplode(PulseX-1, PulseY, Spread-1)
  307.         PulseExplode(PulseX, PulseY+1, Spread-1)
  308.         PulseExplode(PulseX, PulseY-1, Spread-1)
  309.       end
  310.       if PulseExplodes[n] then
  311.         if PulseStatus ~= 0 then
  312.         PulseExplodes[n] = Num(PulseX)..Num(PulseY)..(PulseStatus-1)..(Spread-1)
  313.       end
  314.       end
  315.     end
  316.   end
  317. end
  318.  
  319. function deleteMapView(x,y)
  320.   local First = string.sub(MapView[y], 1, x-1)
  321.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  322.   MapView[y] = First.." "..Second
  323. end
  324.  
  325. function deleteMap(x,y)
  326.   local First = string.sub(MapView[y], 1, x-1)
  327.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  328.   MapView[y] = First.." "..Second
  329. end
  330.  
  331. function Direction(xory, dir)
  332.   if xory == "x" then
  333.     if dir == 1 then --Left
  334.       return -1
  335.     elseif dir == 2 then --Right
  336.       return 1
  337.     elseif dir == 3 or dir == 4 then
  338.       return 0
  339.     else
  340.       error("funtion Direction was handled incorrectly")
  341.     end
  342.   elseif xory == "y" then
  343.     if dir == 3 then --Up
  344.       return -1
  345.     elseif dir == 4 then --Down
  346.       return 1
  347.     elseif dir == 1 or dir == 2 then
  348.       return 0
  349.     else
  350.       error("funtion Direction was handled incorrectly")
  351.     end
  352.   else
  353.     error("You need to use x or y!!!! in function direction!")
  354.   end
  355. end
  356.  
  357. function reDraw()
  358.   clear()
  359.   for n=1,#MapView do
  360.     term.setCursorPos(1, n)
  361.     term.write(MapView[n])
  362.   end
  363.   term.setCursorPos(PlayerX, PlayerY)
  364.   term.write(Player[PlayerDir])
  365. end
  366.  
  367. function CheckMap(x, y)
  368.   if Map[y] then
  369.     local MapString = Map[y]
  370.     MapString = string.sub(MapString, x, x)
  371.     return MapString
  372.   else
  373.     return "O"
  374.   end
  375. end
  376.  
  377. function CheckMapView(x, y)
  378.   if MapView[y] then
  379.     local Lens = string.len(MapView[y])
  380.     if x > 0 and x < Lens then
  381.       local MapString = MapView[y]
  382.       MapString = string.sub(MapString, x, x)
  383.       return MapString
  384.     else
  385.       return "O"
  386.     end
  387.   end
  388.   return "O"
  389. end
  390.  
  391. function EditMapView(x, y, Get)
  392.   local First = string.sub(MapView[y], 1, x-1)
  393.   local Second = string.sub(MapView[y], x+1, string.len(MapView[y]))
  394.   MapView[y] = First..Get..Second
  395. end
  396.  
  397. function EditMap(x, y, Get)
  398.   local First = string.sub(Map[y], 1, x-1)
  399.   local Second = string.sub(Map[y], x+1, string.len(Map[y]))
  400.   Map[y] = First..Get..Second
  401. end
  402.  
  403. function MovePlayer()
  404.   Tick()
  405.   while true do
  406.     Events = {os.pullEvent()}
  407.     if Events[1] == "key" then
  408.       if Events[2] == 200 or Events[2] == 17 then -- Up
  409.         PlayerDir = 3
  410.         if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
  411.           PlayerY = PlayerY+Direction("y", PlayerDir)
  412.           CheckUnder()
  413.         end
  414.         reDraw()
  415.       elseif Events[2] == 208 or Events[2] == 31 then -- Down
  416.         PlayerDir = 4
  417.         if CheckMapView(PlayerX, PlayerY+Direction("y", PlayerDir)) ~= "O" then
  418.           PlayerY = PlayerY+Direction("y", PlayerDir)
  419.           CheckUnder()
  420.         end
  421.         reDraw()
  422.       elseif Events[2] == 203 or Events[2] == 30 then -- Left
  423.         PlayerDir = 1
  424.         if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
  425.           PlayerX = PlayerX+Direction("x", PlayerDir)
  426.           CheckUnder()
  427.         end
  428.         reDraw()
  429.       elseif Events[2] == 205 or Events[2] == 32 then -- Right
  430.         PlayerDir = 2
  431.         if CheckMapView(PlayerX+Direction("x", PlayerDir), PlayerY) ~= "O" then
  432.           PlayerX = PlayerX+Direction("x", PlayerDir)
  433.           CheckUnder()
  434.         end
  435.         reDraw()
  436.       elseif Events[2] == 57 then --Spacebar
  437.         if Cooldown == 0 then
  438.           CreateLaser(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
  439.         end
  440.       elseif Events[2] == 45 then -- x button
  441.         if CooldownPulse == 0 then
  442.           CreatePulse(PlayerX+Direction("x", PlayerDir), PlayerY+Direction("y", PlayerDir), PlayerDir)
  443.         end
  444.       end
  445.     elseif Events[1] == "timer" and Events[2] == Ticker then --Tick
  446.       Tick()
  447.     elseif Events[1] == "timer" and Events[2] == Draw then
  448.       StatusBox("Nothing", "Ignore")
  449.     end
  450.   end
  451. end
  452. Load()
  453. MovePlayer()
Advertisement
Add Comment
Please, Sign In to add comment