Jummit

Computercraft Game engine

Mar 16th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.87 KB | None | 0 0
  1. width, height = term.getSize()
  2.  
  3. -- EVENT HANDLER MODULE
  4. mouse = {x = 0, y = 0}
  5. keyboard = {lastChar = ""}
  6. eventHandler = {
  7.   mouse_click = function(button, x, y)
  8.     mouse.x, mouse.y = x, y
  9.     mouse.isDown = true
  10.   end,
  11.   mouse_up = function(button, x, y)
  12.     mouse.x, mouse.y = x, y
  13.     mouse.isDown = false
  14.     mouse.isDragged = false
  15.   end,
  16.   mouse_drag = function(button, x, y)
  17.     mouse.x, mouse.y = x, y
  18.     mouse.isDown = true
  19.     mouse.dragX, mouse.dragY = x, y
  20.   end,
  21.  
  22.   key = function(key)
  23.     keyboard[keys.getName(key)] = true
  24.   end,
  25.   key_up = function(key)
  26.     keyboard[keys.getName(key)] = false
  27.   end,
  28.   char = function(char)
  29.     keyboard.lastChar = char
  30.     keyboard.newChar = true
  31.   end
  32. }
  33.  
  34. -- DYNAMIC LIGHT MODULE
  35. dynamicLight = {}
  36. dynamicLight.positions = {
  37.   [0] = {0, -1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}
  38. }
  39. dynamicLight.lightPixel = function(self, x, y)
  40.   local degree = math.deg(math.atan2(self.lightSource.x-x, self.lightSource.y-y))+180
  41.   local eightDegree = math.floor(degree/45)
  42.  
  43.   local tileX, tileY = x-dynamicLight.positions[eightDegree][1], y-dynamicLight.positions[eightDegree][2]
  44.   if self[tileX] and self[x] then
  45.     if self.light[tileX][tileY] == true and not self[tileX][tileY].solid then
  46.       self.light[x][y] = true
  47.     else
  48.       self.light[x][y] = false
  49.     end
  50.   end
  51. end
  52.  
  53.  
  54. -- ELEMENT MODULE
  55. isBoxClicked = function(box)
  56.   return mouse.isDown and box.x<=mouse.x and box.y<=mouse.y and box.x+box.w>mouse.x and box.y+box.h>mouse.y
  57. end
  58. newElement = function(elementTab)
  59.   return function(tab)
  60.     return setmetatable(
  61.       tab,
  62.       {
  63.         __index = elementTab
  64.       }
  65.     )
  66.   end
  67. end
  68. elements = {
  69.   tilemap = newElement({
  70.     generated = false,
  71.     getTile = function(self, x, y)
  72.       if self[x] then
  73.         return self[x][y]
  74.       else
  75.         return {}
  76.       end
  77.     end,
  78.     setTile = function(self, x, y, tile)
  79.       if not self[x] then self[x] = {} end
  80.       self[x][y] = self.tileset[tile]
  81.     end,
  82.     setRectangle = function(self, rx, ry, w, h, tile)
  83.       for x = rx, rx+w do
  84.         for y = ry, ry+h do
  85.           self:setTile(x, y, tile)
  86.         end
  87.       end
  88.     end,
  89.     [1] = {},
  90.     light = {},
  91.     DRAW = function(self)
  92.       for y = 1, #self[1] do
  93.         term.setCursorPos(1, y)
  94.         for x = 1, #self do
  95.           local tile = self[x][y]
  96.           if tile then
  97.             if self.dynamicLight and not tile.solid and not self.light[x][y] then
  98.               term.setBackgroundColor(colors.black)
  99.               term.write(" ")
  100.             else
  101.               term.setBackgroundColor(tile.bc)
  102.               term.setTextColor(tile.tc)
  103.               term.write(tile.char)
  104.             end
  105.           else
  106.             term.write(" ")
  107.           end
  108.         end
  109.       end
  110.     end,
  111.     UPDATE = function(self)
  112.       if self.generate and not self.generated then
  113.         self:generate()
  114.         self.generated = true
  115.         if self.dynamicLight then
  116.           for x = 1, #self do
  117.             self.light[x] = {}
  118.           end
  119.         end
  120.       end
  121.       if self.dynamicLight then
  122.         for x = 1, 3 do
  123.           for y = 1, 3 do
  124.             if self.light[self.lightSource.x+x-2] then
  125.               self.light[self.lightSource.x+x-2][self.lightSource.y+y-2] = true
  126.             end
  127.           end
  128.         end
  129.  
  130.         for x = 1, #self do
  131.           for y = 1, #self[1] do
  132.             dynamicLight.lightPixel(self, x, y)
  133.           end
  134.         end
  135.       end
  136.     end
  137.   }),
  138.   button = newElement({
  139.     UPDATE = function(self)
  140.       if isBoxClicked(self) then
  141.         if not self.clicked and self.clickedFunction then self:clickedFunction() end
  142.         self.clicked = true
  143.       else
  144.         self.clicked = false
  145.       end
  146.     end,
  147.     DRAW = function(self)
  148.       if self.clicked then
  149.         term.setBackgroundColor(colors.white)
  150.         term.setTextColor(colors.lightGray)
  151.       else
  152.         term.setBackgroundColor(colors.lightGray)
  153.         term.setTextColor(colors.gray)
  154.       end
  155.       paintutils.drawFilledBox(self.x, self.y, self.w+self.x-1, self.h+self.y-1)
  156.       term.setCursorPos(self.x+self.w/2-#self.label/2, self.y+self.h/2)
  157.       term.write(self.label)
  158.     end
  159.   }),
  160.   sprite = newElement({
  161.     path = "",
  162.     moveTo = function(self, tilemap, xmove, ymove)
  163.       local canMove = true
  164.       for x = 0, self.w-1 do
  165.         for y = 0, self.h-1 do
  166.           local tile = tilemap:getTile(self.x+xmove+x, self.y+ymove+y)
  167.           if not tile or tile.solid then canMove = false end
  168.         end
  169.       end
  170.       if canMove then
  171.         self.x = self.x + xmove
  172.         self.y = self.y + ymove
  173.       end
  174.     end,
  175.     DRAW = function(self)
  176.       if not self.image then
  177.         self.image = paintutils.loadImage(self.path)
  178.         self.w = #self.image[1]
  179.         self.h = #self.image
  180.       end
  181.       paintutils.drawImage(self.image, self.x, self.y)
  182.     end
  183.   }),
  184.   template = newElement({
  185.     UPDATE = function(self)
  186.     end,
  187.     DRAW = function(self)
  188.     end
  189.   }),
  190. }
  191.  
  192.  
  193. -- LIFECYCLE FUNCTIONS
  194. lastUpdate = os.clock()
  195. runProtected = function(func)
  196.   local succ, mess = pcall(func)
  197.   if not succ then
  198.     quit(mess)
  199.   end
  200. end
  201. update = function(elements, wantedDt, func)
  202.   runProtected(function()
  203.   local timer = os.startTimer(wantedDt)
  204.   local event, var1, var2, var3 = os.pullEventRaw()
  205.   if event == "terminate" then quit(true) end
  206.   os.cancelTimer(timer)
  207.  
  208.   if event ~= "mouse_drag" then mouse.isDragged = false end
  209.   if event ~= "char" then keyboard.newChar = false end
  210.   if eventHandler[event] then eventHandler[event](var1, var2, var3) end
  211.  
  212.   if os.clock()-lastUpdate >= wantedDt then
  213.     for elementName, element in pairs(elements) do
  214.       if element.UPDATE then
  215.         element:UPDATE()
  216.       end
  217.       if element.update then
  218.         element:update()
  219.       end
  220.     end
  221.     if func then func() end
  222.     lastUpdate = os.clock()
  223.   end
  224.   end)
  225. end
  226. buffer = window.create(term.current(), 1, 1, width, height)
  227. draw = function(elements, func)
  228.   runProtected(function()
  229.   buffer.setVisible(false)
  230.   local oldterm = term.redirect(buffer)
  231.   term.setBackgroundColor(colors.black)
  232.   term.clear()
  233.   for elementName, element in pairs(elements) do
  234.     if element.DRAW then
  235.       element:DRAW()
  236.     end
  237.   end
  238.   if func then func() end
  239.   buffer.setVisible(true)
  240.   term.redirect(oldterm)
  241.   end)
  242. end
  243. quit = function(hard)
  244.   buffer.setVisible(true)
  245.   term.setBackgroundColor(colors.black)
  246.   term.setTextColor(colors.yellow)
  247.   term.clear()
  248.   term.setCursorPos(1, 1)
  249.   print("Thanks for using the Pinwyn Game Engine by Jummit!")
  250.   if type(hard) == "string" then
  251.     print("The game crashed! Here is the error:")
  252.     term.setTextColor(colors.red)
  253.     print(hard)
  254.   elseif hard then
  255.     print("The game was forcefully closed")
  256.   else
  257.     print("The game was closed without any errors")
  258.   end
  259.   error()
  260. end
Add Comment
Please, Sign In to add comment