Advertisement
Redxone

RedGame

Jul 25th, 2015
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.62 KB | None | 0 0
  1. --]] RedGame API By: Redxone[[--
  2.  
  3. function init()
  4.     w,h = term.getSize()
  5.     pmap = {}
  6.      for i = 1, h do
  7.         pmap[i] = {}
  8.        for c = 1, w do
  9.             pmap[i][c] = 0
  10.        end    
  11.     end
  12.  
  13.     gmap = {}
  14.     emp={}
  15.     gplayer={}
  16.     gblocks={}
  17.     doors={}
  18.     maps={}
  19.     mapName = ""
  20.  
  21. end
  22.  
  23. function getGMAP()
  24.     return gmap
  25. end
  26.  
  27. function getPMAP()
  28.     return pmap
  29. end
  30.  
  31. function getGBLOCKS()
  32.     return gblocks
  33. end
  34.  
  35. function setPMAP(x,y,id)
  36.     pmap[y][x] = id
  37. end
  38.  
  39. function setGMAP(x,y,id)
  40.     gmap[y][x] = id
  41. end
  42.  
  43. function openTextbox()
  44.     paintutils.drawBox(1,h-5,w,h,colors.gray)
  45.     paintutils.drawFilledBox(2,h-4,w-1,h-1,colors.blue)
  46. end
  47.  
  48. clearTextbox = openTextbox
  49.  
  50. function drawTextbox(text,ln,speed)
  51.     term.setCursorPos(6,h- ( 4 - ( ln-1 ) ) )
  52.     term.setTextColor(colors.white)
  53.     textutils.slowPrint(text,speed)
  54. end
  55.  
  56. function createYesNo(YText,NText,ln)
  57.     local sel = 1
  58.     local waiting = true
  59.     local opt = {
  60.         {name=YText};
  61.         {name=NText};
  62.     }
  63.  
  64.     function waitResult()
  65.         while waiting do
  66.             for i = 1, #opt do
  67.                 term.setCursorPos((w/2) - #opt[i].name/2,h- ( 4 - ( (ln+i)-1 ) ) )
  68.                 if(sel == i )then
  69.                     write("["..opt[i].name.."]")
  70.                 else
  71.                     write(" "..opt[i].name.." ")
  72.                 end
  73.             end
  74.  
  75.             a = {os.pullEvent("key")}
  76.  
  77.             if(a[2] == keys.w and sel > 1)then
  78.                 sel = sel - 1
  79.             end
  80.             if(a[2] == keys.s and sel < #opt)then
  81.                 sel = sel + 1
  82.             end
  83.             if(a[2] == keys.space)then
  84.                 if(sel == 1)then waiting = false  return "ok" end
  85.                 if(sel == 2)then  waiting = false return "no" end
  86.             end
  87.         end
  88.     end
  89.     return waitResult()
  90. end
  91.  
  92.  
  93. function addDoor(fromx,fromy,tox,toy,fromMap,toMap)
  94.     doors[#doors+1] = {fx=fromx,fy=fromy,tx=tox,ty=toy,fMap=fromMap,tMap=toMap}
  95. end
  96.  
  97. function draw()
  98.  
  99.     for i = 1, #gmap do
  100.         for c = 1, #gmap[i] do
  101.             term.setCursorPos(c,i)
  102.             term.setTextColor(colors[ gblocks[ gmap[i][c] ].color ])
  103.             term.setBackgroundColor(colors[ gblocks[ gmap[i][c] ].bcolor ])
  104.             write(gblocks[ gmap[i][c] ].graphic)
  105.         end
  106.     end
  107.  
  108.     for i = 1, #pmap do
  109.         for c = 1, #pmap[i] do
  110.             if(pmap[i][c] ~= 0)then
  111.                 term.setCursorPos(c,i)
  112.                 term.setTextColor(colors[ gblocks[ pmap[i][c] ].color ])
  113.                 term.setBackgroundColor(colors[ gblocks[ pmap[i][c] ].bcolor ])
  114.                 write(gblocks[ pmap[i][c] ].graphic)
  115.             end
  116.         end
  117.     end
  118.  
  119. end
  120.  
  121.  
  122. function closeTextbox()
  123.     os.pullEvent("key")
  124.     draw()
  125.     term.setCursorPos(1,h)
  126.     term.setBackgroundColor(colors[gblocks[0].bcolor])
  127.     term.clearLine()
  128. end
  129.  
  130. function closeTextboxRaw()
  131.     draw()
  132.     term.setCursorPos(1,h)
  133.     term.setBackgroundColor(colors[gblocks[0].bcolor])
  134.     term.clearLine()
  135. end
  136.  
  137.  
  138. function drawMapPixel(x,y)
  139.     term.setCursorPos(y,x)
  140.     term.setTextColor(colors[ gblocks[ gmap[x][y] ].color ])
  141.     term.setBackgroundColor(colors[ gblocks[ gmap[x][y] ].bcolor ])
  142.     write(gblocks[ gmap[x][y] ].graphic)
  143.     if(pmap[x][y] ~= 0)then
  144.         term.setCursorPos(y,x)
  145.         term.setTextColor(colors[ gblocks[ pmap[x][y] ].color ])
  146.         term.setBackgroundColor(colors[ gblocks[ pmap[x][y] ].bcolor ])
  147.         write(gblocks[ pmap[x][y] ].graphic)
  148.     end
  149. end
  150.  
  151. function lookupBlock(id)
  152.     for i = 1, #gmap do
  153.         for c = 1, #gmap[i] do
  154.             if(gmap[i][c] == id)then return true end
  155.         end
  156.     end
  157.     return false
  158. end
  159.  
  160. function findBlockAt(id)
  161.     inf = {}
  162.     for i = 1, #gmap do
  163.         for c = 1, #gmap[i] do
  164.             if(gmap[i][c] == tonumber(id))then
  165.                  return i,c
  166.             end
  167.         end
  168.     end
  169.     return nil
  170. end
  171.  
  172. function getBlockAt(x,y)
  173.     return gmap[y][x]
  174. end
  175.  
  176. function setBlockAt(id,x,y)
  177.     gmap[y][x] = id
  178.     drawMapPixel(y,x)
  179. end
  180.  
  181. function breakBlockAt(x,y)
  182.     gmap[y][x] = 0
  183.     drawMapPixel(y,x)
  184. end
  185.  
  186. function editBlock(id,color,bcolor,graphic,solid)
  187.     gblocks[id].color = color
  188.     gblocks[id].bcolor = bcolor
  189.     gblocks[id].graphic = graphic
  190.     gblocks[id].solid = solid
  191.     draw()
  192. end
  193. function getCurrentMap()
  194.     return mapName
  195. end
  196.  
  197.  
  198. function addMap(name,levmap,blocks)
  199.     if(maps[name] ~= nil)then error("[Redgame] - > AddMap - > Map already added!") end
  200.     maps[name] = {level=levmap,blockmap=blocks}
  201. end
  202.  
  203. function setSolid(id,solid)
  204.     gblocks[id].solid = solid
  205. end
  206.  
  207. function getResource(file)
  208.     f = fs.open(file,"r")
  209.     res = textutils.unserialize(f.readAll())
  210.     f.close()
  211.     if(type(res) ~= "table")then error("[RedGame]: getResource -> file ["..file.."] must contain only a table!") end
  212.     return res
  213. end
  214.  
  215. function setMap(map)
  216.     if(maps[map] == nil)then
  217.         error("[RedGame] -> setMap -> No such map.")
  218.     end
  219.     gmap=maps[map].level
  220.     mapName = map
  221.     gblocks = maps[map].blockmap
  222.     draw()
  223. end
  224.  
  225. function getMap()
  226.     return gmap
  227. end
  228.  
  229.  
  230.  
  231.  
  232. function createPlayer(block,startX,startY)
  233.  
  234.     local player = {
  235.  
  236.         x=startX,
  237.         y=startY,
  238.         tickrate=tonumber(0.1),
  239.         physicsTick = os.startTimer(0.1),
  240.         jump_height=2,
  241.         hspd = 0,
  242.         vspd = 0,
  243.         blockid = block,
  244.         multidir = false,
  245.         physic_control= {
  246.             ["direction_up"] = keys.w,
  247.             ["direction_down"] = keys.s,
  248.             ["direction_left"] = keys.a,
  249.             ["direction_right"] = keys.d,
  250.         },
  251.         lastMove = "player_up",
  252.         events = {},
  253.         interactions ={},
  254.  
  255.         addInteraction = function(self,mydmap,x,y,func)
  256.             self.interactions[#self.interactions+1] = {onmap=mydmap,x=x,y=y,active=true,event=func}
  257.         end,
  258.  
  259.         removeInteraction = function(self,mydmap,x,y)
  260.             for i = 1, #self.interactions do
  261.                 if(self.interactions[i].x == x and self.interactions[i].y == y and self.interactions[i].onmap == mydmap)then
  262.                     self.interactions[i].active=false;
  263.                 end
  264.             end
  265.         end,
  266.  
  267.         setPhysicsTick = function(self,time)
  268.             self.tickrate = tonumber(time)
  269.             self.physicsTick = os.startTimer(time)
  270.         end,
  271.  
  272.         addEvent = function(self,check,evfunc)
  273.             table.insert(self.events,{type=check,run=evfunc})
  274.         end,
  275.  
  276.         getBlockUnder = function(self)
  277.                 if(gmap[self.y][self.x] ~= 0)then
  278.                     return gmap[self.y][self.x]
  279.                 end
  280.                 return 0
  281.         end,
  282.  
  283.  
  284.         checkCollision = function(self,x,y)
  285.             hasColl = false
  286.             if( gblocks[gmap[y][x]].solid or pmap[y][x] ~= 0)then hasColl = true end
  287.             if( gmap[y][x] == 0 and pmap[y][x] == 0)then hasColl = false end
  288.             return hasColl
  289.         end,
  290.  
  291.         jump = function(self,ammount)
  292.             if(self:checkCollision(self.x,self.y+1))then
  293.                 for i = 1, ammount do
  294.                     if(not self:checkCollision(self.x,self.y-1))then
  295.                         pmap[self.y][self.x] = 0
  296.                         drawMapPixel(self.y,self.x)
  297.                         self.y = self.y - 1
  298.                         pmap[self.y][self.x] = self.blockid
  299.                         drawMapPixel(self.y,self.x)
  300.                     end
  301.                 end
  302.             end
  303.         end,
  304.  
  305.         moveUp=function(self,ammount)
  306.             if(not self:checkCollision(self.x,self.y-1) and not self:checkPhysics("gravity"))then
  307.                 pmap[self.y][self.x] = 0
  308.                 drawMapPixel(self.y,self.x)
  309.                 self.y = self.y - ammount
  310.                 pmap[self.y][self.x] = self.blockid
  311.                 drawMapPixel(self.y,self.x)
  312.             end
  313.  
  314.             if(not self:checkCollision(self.x,self.y-1) and self:checkPhysics("gravity"))then
  315.                 self:jump(self.jump_height)
  316.             end
  317.         end,
  318.  
  319.         moveDown=function(self,ammount)
  320.             if(not self:checkCollision(self.x,self.y+1))then
  321.                 pmap[self.y][self.x] = 0
  322.                 drawMapPixel(self.y,self.x)
  323.                 self.y = self.y + ammount
  324.                 pmap[self.y][self.x] = self.blockid
  325.                 drawMapPixel(self.y,self.x)
  326.             end
  327.        
  328.         end,
  329.  
  330.         moveLeft=function(self,ammount)
  331.             --pmap[self.y][self.x-1] == 0
  332.             if(not self:checkCollision(self.x-1,self.y))then
  333.                 pmap[self.y][self.x] = 0
  334.                 drawMapPixel(self.y,self.x)
  335.                 self.x = self.x - ammount
  336.                 pmap[self.y][self.x] = self.blockid
  337.                 drawMapPixel(self.y,self.x)
  338.             end    
  339.  
  340.  
  341.         end,
  342.  
  343.         moveRight=function(self,ammount)
  344.             if(not self:checkCollision(self.x+1,self.y))then
  345.                 pmap[self.y][self.x] = 0
  346.                 drawMapPixel(self.y,self.x)
  347.                 self.x = self.x + ammount
  348.                 pmap[self.y][self.x] = self.blockid
  349.                 drawMapPixel(self.y,self.x)
  350.             end    
  351.         end,
  352.  
  353.  
  354.         physics = {
  355.  
  356.             ["gravity"] = {has=false,func=function(self)
  357.                 if(not self:checkCollision(self.x,self.y+1))then
  358.                         self:moveDown(1)
  359.                         sleep(0.01)
  360.                 end
  361.             end};
  362.  
  363.             ["smooth_experimental"] = {has=false,func=function(self)
  364.                     self.multidir = true
  365.                     self.physics["smooth_experimental"].has = false
  366.             end};
  367.  
  368.         },
  369.  
  370.  
  371.         checkPhysics = function(self,element)
  372.             for k, v in pairs(self.physics) do
  373.                 if(k == element and v.has)then
  374.                     return true
  375.                 end
  376.             end
  377.  
  378.             return false
  379.         end,
  380.  
  381.         checkInteract = function(self)
  382.             for i = 1, #self.interactions do
  383.                 if(    ((self.y == self.interactions[i].y-1)
  384.                     and (self.x == self.interactions[i].x))
  385.                     or ((self.y == self.interactions[i].y+1)
  386.                     and (self.x == self.interactions[i].x))
  387.                     or ((self.y == self.interactions[i].y)
  388.                     and (self.x == self.interactions[i].x+1))
  389.                     or ((self.y == self.interactions[i].y)
  390.                     and (self.x == self.interactions[i].x-1))
  391.                     )then
  392.                     if(self.interactions[i].onmap == getCurrentMap() and self.interactions[i].active)then
  393.                         self.interactions[i].event()
  394.                     end
  395.                 end
  396.             end
  397.         end,
  398.  
  399.         controls = {
  400.             {name = "player_interact",event="key",key=keys.space,func=function(self) self:checkInteract() end};
  401.             {name="player_up",event="key",key=keys.w,func=function(self) self.lastMove = "player_up" self:moveUp(1)  end};
  402.             {name="player_down",event="key",key=keys.s,func=function(self) self.lastMove = "player_down" self:moveDown(1) end};
  403.             {name="player_left",event="key",key=keys.a,func=function(self) self.lastMove = "player_left" self:moveLeft(1) end};
  404.             {name="player_right",event="key",key=keys.d,func=function(self)self.lastMove = "player_right" self:moveRight(1) end};
  405.         },
  406.  
  407.         --]] movement functions
  408.  
  409.         put = function(self)
  410.             pmap[self.y][self.x] = self.blockid
  411.         end,
  412.  
  413.         unput = function(self)
  414.             pmap[self.y][self.x] = 0;
  415.             drawMapPixel(self.y,self.x)
  416.         end,
  417.  
  418.         applyPhysics = function(self,element)
  419.             if(self.physics[element] ~= nil)then
  420.                 if(self.physics[element].has == false)then
  421.                     self.physics[element].has = true
  422.                 end
  423.             else
  424.                 error("[RedGame]: player -> addPhysicsElement -> no such element!")
  425.             end
  426.         end,
  427.  
  428.         setJumpHeight = function(self,h)
  429.             self.jump_height = h
  430.         end,
  431.  
  432.         createPhysics = function(self,element,physic)
  433.                 if(self.physics[element] ~= nil)then error("[RedGame]: player - > createPhysicsElement -> element already exists!") end
  434.                 if(type(physic) ~= "table")then error("[RedGame]: player -> createPhysicsElement -> must be in table format eg {func=function(self) physics stuff end};") end
  435.                  metaindex = {has=false,func=function(self) end};
  436.                 physic = setmetatable(physic, {__index = metaindex})
  437.                 self.physics[element] = physic
  438.         end,
  439.  
  440.         remapPhysicsControl = function(self,name,to)
  441.                 if(self.physic_control[name])then
  442.                     self.physic_control[name] = to
  443.                 else
  444.                     error("[RedGame]: player -> remapPhysicsControl invalid control!")
  445.                 end
  446.         end,
  447.  
  448.         remapControl = function(self,name,to)
  449.             for i = 1, #self.controls do
  450.                 if(self.controls[i].name == name)then
  451.                     self.controls[i].key = to
  452.                 end
  453.             end
  454.         end,
  455.  
  456.         addControl=function(self,controltable)
  457.             if(type(controltable) ~= "table")then
  458.                 error("[RedGame]:addControl -> control must be a table in this format: {name=<control name>,event=<event>,key=<key in string format>,func=function() <actions> end}")
  459.             else
  460.                 table.insert(self.controls,controltable)
  461.             end
  462.         end,
  463.  
  464.         importControls=function(self,controltable)
  465.             if(type(controltable) ~= "table")then
  466.                 error("[RedGame]:importControls -> control must be a table in this format: controls = { {name=<control name>,event=<event>,key=<key in string format>,func=function() <actions> end}, ect... }")
  467.             else
  468.                 self.controls = controltable
  469.             end
  470.         end,
  471.  
  472.  
  473.         setPos = function(self,x,y)
  474.             pmap[self.y][self.x] = 0
  475.             drawMapPixel(self.y,self.x)
  476.             self.x = x
  477.             self.y = y
  478.             self:checkCollision(self.x,self.y)
  479.             pmap[self.y][self.x] = self.blockid
  480.             drawMapPixel(self.y,self.x)
  481.         end,
  482.  
  483.         update=function(self)
  484.  
  485.             for i = 1, #doors do
  486.                 if(getCurrentMap() == doors[i].fMap and self.x == doors[i].fx and self.y == doors[i].fy)then
  487.                     setMap(doors[i].tMap)
  488.                     self:setPos(doors[i].tx,doors[i].ty)
  489.                 end
  490.             end
  491.  
  492.             if(self.multidir)then
  493.                 pmap[self.y][self.x] = 0
  494.                 drawMapPixel(self.y,self.x)
  495.                 if(not self:checkCollision(self.x+self.hspd,self.y))then
  496.                     self.x = self.x + self.hspd
  497.                 end
  498.                 if(not self:checkCollision(self.x,self.y+self.vspd))then
  499.                     self.y = self.y + self.vspd
  500.                 end
  501.                 pmap[self.y][self.x] = self.blockid
  502.                 drawMapPixel(self.y,self.x)
  503.             end
  504.  
  505.             a = {os.pullEvent()}
  506.  
  507.             for k, v in pairs(self.events) do
  508.                 if(v.type == a[1])then
  509.                     v.run(a)
  510.                 end
  511.             end
  512.  
  513.             if(a[1]=="timer")then
  514.                 self.physicsTick=os.startTimer(self.tickrate)
  515.                 for k, v in pairs(self.physics) do
  516.                     if(v.has)then v.func(self) end
  517.                 end
  518.             end
  519.  
  520.             if(not self.multidir)then
  521.                 for i = 1, #self.controls do
  522.                     if(a[1] == self.controls[i].event and a[2] == self.controls[i].key)then
  523.                         self.controls[i].func(self)
  524.                     end
  525.                 end
  526.             end
  527.  
  528.             if(self.multidir)then
  529.                         if(a[2] ~= self.physic_control["direction_up"] and a[2] ~= self.physic_control["direction_down"])then
  530.                             self.vspd = 0
  531.                         end
  532.                         if(a[2] ~= self.physic_control["direction_left"] and a[2] ~= self.physic_control["direction_right"])then
  533.                             self.hspd = 0
  534.                         end
  535.                         if(a[2] == self.physic_control["direction_up"])then
  536.                             if(self:checkPhysics("gravity"))then
  537.                                 if(self:checkCollision(self.x,self.y+1))then
  538.                                     self.vspd = -self.jump_height
  539.                                 end
  540.                             else
  541.                                 self.vspd = -self.jump_height
  542.                             end
  543.                         end
  544.                         if(a[2] == self.physic_control["direction_down"])then
  545.                             self.vspd = 1
  546.                         end
  547.                         if(a[2] == self.physic_control["direction_left"])then
  548.                             self.hspd = -1
  549.                         end
  550.                         if(a[2] == self.physic_control["direction_right"])then
  551.                             self.hspd = 1
  552.                         end
  553.             end
  554.         end,
  555.  
  556.     }
  557.  
  558.     return player
  559. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement