Advertisement
Jummit

Computercraft TerrariCCa Beta

Apr 1st, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.00 KB | None | 0 0
  1. local engine = {}
  2. engine.w, engine.h = term.getSize()
  3. engine.logfile = fs.open("terraricca.log", "w")
  4. engine.log = function(log)
  5.   engine.logfile.write(log.."\n")
  6. end
  7.  
  8. engine.ui = {}
  9. engine.ui = {
  10.   isBoxClicked = function(box, x, y)
  11.     return x>=box.x and y>=box.y and x<box.x+box.w and y<box.y+box.h
  12.   end,
  13.   codes = {
  14.     [colors.red] = "e",
  15.     [colors.orange] = 1,
  16.     [colors.yellow] = 4,
  17.     [colors.black] = "f",
  18.     [colors.gray] = 7,
  19.     [colors.lightGray] = 8,
  20.     [colors.white] = 1,
  21.     [colors.blue] = "b",
  22.     [colors.cyan] = 9,
  23.     [colors.lightBlue] = 3,
  24.     [colors.green] = "d",
  25.     [colors.lime] = 5,
  26.     [colors.brown] = "c",
  27.     [colors.magenta] = 2,
  28.     [colors.pink] = 6,
  29.     [colors.purple] = "a",
  30.   },
  31.   getColorOfPaintCode = function(code)
  32.     for color, paintCode in pairs(engine.ui.paintColorCodes) do
  33.       if paintCode == code then return color end
  34.     end
  35.   end
  36. }
  37.  
  38. engine.mouseKeys = {"left", "right", "wheel"}
  39. engine.mouse = {x = 0, y = 0}
  40. engine.keyboard = {}
  41. engine.eventHandler = {
  42.   mouse_click = function(button, x, y)
  43.     engine.mouse.x, engine.mouse.y = x, y
  44.     engine.mouse[engine.mouseKeys[button]] = true
  45.   end,
  46.   mouse_up = function(button, x, y)
  47.     engine.mouse.x, engine.mouse.y = x, y
  48.     engine.mouse[engine.mouseKeys[button]] = false
  49.     engine.mouse.dragged = false
  50.   end,
  51.   mouse_drag = function(button, x, y)
  52.     engine.eventHandler.mouse_click(button, x, y)
  53.     engine.mouse.dragged = true
  54.   end,
  55.  
  56.   key = function(key)
  57.     engine.keyboard[keys.getName(key)] = true
  58.   end,
  59.   key_up = function(key)
  60.     engine.keyboard[keys.getName(key)] = false
  61.   end,
  62.   char = function(char)
  63.     engine.keyboard[char] = true
  64.   end
  65. }
  66.  
  67. engine.math = {
  68.   reduce = function(value)
  69.     local returnValue = value
  70.     if math.abs(value) ~= 0 then
  71.       if value > 0 then
  72.         returnValue = returnValue - 1
  73.       else
  74.         returnValue = returnValue + 1
  75.       end
  76.     end
  77.     return returnValue
  78.   end,
  79.   getOffset = function(w, pos)
  80.     return math.floor(-pos+w/2)
  81.   end,
  82.   getDistance = function(x1, y1, x2, y2)
  83.     return math.sqrt((x1-x2)^2+(y1-y2)^2)
  84.   end
  85. }
  86.  
  87. engine.elements = {}
  88. engine.elements.methods = {}
  89. engine.elements.methods = {
  90.   element = true,
  91.   moveAndCollide = function(self, xmove, ymove, tilemap, tilesMovedUp)
  92.     local canMove = true
  93.     for x = self.x, self.x+self.w-1 do
  94.       for y = self.y, self.y+self.h-1 do
  95.         local tile = tilemap:getTile(x+xmove, y+ymove)
  96.         if (not tile) or tile.solid then
  97.           if y == self.y+self.h-1 and not tilesMovedUp then
  98.             return engine.elements.methods.moveAndCollide(self, xmove, ymove-1, tilemap, 1)
  99.           else
  100.             canMove = false
  101.           end
  102.         end
  103.       end
  104.     end
  105.     if canMove then
  106.       self:move(xmove, ymove)
  107.     end
  108.   end,
  109.   move = function(self, xmove, ymove)
  110.     self.x = self.x + xmove
  111.     self.y = self.y + ymove
  112.   end
  113. }
  114. engine.elements.newElement = function(elementTable)
  115.   return function(argTable)
  116.     return setmetatable(argTable, {__index = setmetatable(elementTable, {__index = engine.elements.methods})})
  117.   end
  118. end
  119. engine.elements.new = {
  120.   template = engine.elements.newElement({
  121.     INIT = function(self)
  122.     end,
  123.     UPDATE = function(self, event, var1, var2, var3)
  124.     end,
  125.     DRAW = function(self)
  126.     end
  127.   }),
  128.   texture = engine.elements.newElement({
  129.     offX = 0,
  130.     offY = 0,
  131.     getDimensions = function(self)
  132.       local w = 1
  133.       for rowNum = 1, #self do
  134.         local row = self[rowNum][1]
  135.         if #row > w then
  136.           w = #row
  137.         end
  138.       end
  139.       return w, #self
  140.     end,
  141.     INIT = function(self)
  142.       self.w, self.h = self:getDimensions()
  143.     end,
  144.     DRAW = function(self)
  145.       if self.x and self.y then
  146.         self:drawTexture(self.x, self.y)
  147.       end
  148.     end,
  149.     drawTexture = function(self, x, y, offX, offY)
  150.       local drawX, drawY = x+(offX or 0)+self.offX, y+(offY or 0)+self.offY
  151.       if not (drawX < 0 or drawX > engine.w or drawY < 0 or drawY > engine.h) then
  152.         for row = 1, #self do
  153.           local texture = self[row]
  154.           term.setCursorPos(drawX, drawY+row-1)
  155.           term.blit(texture[1], texture[2], texture[3])
  156.         end
  157.       end
  158.     end,
  159.     replaceColor = function(self, toReplaceColor, color)
  160.       for row = 1, #self do
  161.         local texture = self[row]
  162.         for i = 2, #texture do
  163.           texture[i] = string.gsub(texture[i], toReplaceColor, color)
  164.         end
  165.       end
  166.     end
  167.   }),
  168.   button = engine.elements.newElement({
  169.     clicked = false,
  170.     INIT = function(self)
  171.       self.mouse_drag = self.mouse_click
  172.     end,
  173.     mouse_click = function(self, button, x, y)
  174.       if engine.ui.isBoxClicked(self, x, y) then
  175.         self.clicked = true
  176.         if self.clickedFunction then
  177.           self:clickedFunction()
  178.         end
  179.       else
  180.         self.clicked = false
  181.       end
  182.     end,
  183.     mouse_up = function(self)
  184.       if self.clicked then
  185.         if self.releasedFunction then
  186.           self:releasedFunction()
  187.         end
  188.         self.clicked = false
  189.       end
  190.     end,
  191.     DRAW = function(self)
  192.       local color = self.color
  193.       if self.clicked then
  194.         color = self.clickedColor
  195.       end
  196.       paintutils.drawFilledBox(self.x, self.y, self.x+self.w, self.y+self.h, color)
  197.     end
  198.   }),
  199.   tileset = engine.elements.newElement({
  200.   }),
  201.   tilemap = engine.elements.newElement({
  202.     INIT = function(self)
  203.       self.w, self.h = 0, 0
  204.     end,
  205.     offX = 0,
  206.     offY = 0,
  207.     DRAW = function(self)
  208.       offX, offY = self.offX, self.offY
  209.       for x = 1, engine.w do
  210.         for y = 1, engine.h do
  211.           if self[x-offX] then
  212.             local tile = self.tileset[self[x-offX][y-offY]]
  213.             if tile then
  214.               tile.texture:drawTexture(x, y)
  215.             end
  216.           end
  217.         end
  218.       end
  219.     end,
  220.     updateDimensions = function(self)
  221.       local h = 0
  222.       for rowNum = 1, #self do
  223.         local row = self[rowNum]
  224.         if #row > h then
  225.           h = #row
  226.         end
  227.       end
  228.       self.w, self.h = #self, h
  229.     end,
  230.     getTile = function(self, x, y)
  231.       if self[x] then
  232.         return self.tileset[self[x][y]]
  233.       end
  234.     end,
  235.     set = {
  236.       tile = function(self, x, y, tile)
  237.         local tileToSet = tile
  238.         if type(tile) == "table" then
  239.           tileToSet = tile[math.random(#tile)]
  240.         end
  241.         if not self[x] then self[x] = {} end
  242.         self[x][y] = tileToSet
  243.         --self:updateDimensions()
  244.       end,
  245.       rectangle = function(self, x, y, w, h, tile)
  246.         for x = x, x+w-1 do
  247.           for y = y, y+h-1 do
  248.             self.set.tile(self, x, y, tile)
  249.           end
  250.         end
  251.       end,
  252.       sphere = function(self, sx, sy, r, tile)
  253.         for x = sx-r*2, sx+r*2 do
  254.           for y = sy-r*2, sy+r*2 do
  255.             if engine.math.getDistance(x, y, sx, sy) <= r then
  256.               self.set.tile(self, x, y, tile)
  257.             end
  258.           end
  259.         end
  260.       end
  261.     }
  262.   }),
  263.   kinematic = engine.elements.newElement({
  264.     jumping = false,
  265.     jumpedHeight = 0,
  266.     w = 1, h = 1,
  267.     moves = {
  268.       left = {-1, 0},
  269.       right = {1, 0}
  270.     },
  271.     INIT = function(self)
  272.       self.w, self.h = self.texture:getDimensions()
  273.     end,
  274.     UPDATE = function(self)
  275.       self.jumping = engine.keyboard.up
  276.       if self.tilemap:getTile(self.x, self.y-1).solid then
  277.         self.jumping = false
  278.       end
  279.     end,
  280.     DRAW = function(self)
  281.       self.texture:drawTexture(self.x, self.y, self.offX, self.offY)
  282.     end,
  283.     PHYSICSUPDATE = function(self)
  284.       for key, move in pairs(self.moves) do
  285.         if engine.keyboard[key] then
  286.           self:moveAndCollide(move[1], move[2], self.tilemap)
  287.         end
  288.       end
  289.       if self.jumping and self.jumpedHeight<self.maxJumpHeight then
  290.         self:moveAndCollide(0, -1, self.tilemap)
  291.         self.jumpedHeight = self.jumpedHeight + 1
  292.       else
  293.         self:moveAndCollide(0, 1, self.tilemap)
  294.       end
  295.       if self.tilemap:getTile(self.x, self.y+self.h).solid then
  296.         self.jumpedHeight = 0
  297.       end
  298.     end
  299.   }),
  300.   particles = engine.elements.newElement({
  301.     offX = 0, offY = 0,
  302.     timeOut = 10,
  303.     INIT = function(self)
  304.     end,
  305.     PHYSICSUPDATE = function(self)
  306.       local toDeleteParticles = {}
  307.       for particleNum = 1, #self do
  308.         local particle = self[particleNum]
  309.         if particle then
  310.           if self:updateParticle(particle) == "delete" then
  311.             table.insert(toDeleteParticles, particleNum)
  312.           end
  313.         end
  314.       end
  315.       for toDeleteParticleNum = 1, #toDeleteParticles do
  316.         self[toDeleteParticleNum] = nil
  317.       end
  318.     end,
  319.     updateParticle = function(self, particle)
  320.       particle.state = particle.state + 1
  321.       local move = self.moves[particle.state]
  322.       if not move then
  323.         particle.state = 1
  324.         move = self.moves[particle.state]
  325.       end
  326.       particle.timeOut = particle.timeOut - 1
  327.       if particle.timeOut == 0 then
  328.         return "delete"
  329.       end
  330.       particle.y = particle.y + move[2]
  331.       particle.x = particle.x + move[1]
  332.     end,
  333.     drawParticle = function(self, particle)
  334.       paintutils.drawPixel(particle.x+self.offX, particle.y+self.offY, colors.green)
  335.     end,
  336.     spawn = function(self, x, y)
  337.       table.insert(self, {x = x, y = y, state = math.random(#self.moves), timeOut = self.timeOut})
  338.     end,
  339.     DRAW = function(self)
  340.       for particleNum = 1, #self do
  341.         local particle = self[particleNum]
  342.         if particle then
  343.           self:drawParticle(particle)
  344.         end
  345.       end
  346.     end
  347.   }),
  348.   item = engine.elements.newElement({
  349.     drawItem = function(self, x, y)
  350.       self.texture:drawTexture(x, y)
  351.     end
  352.   })
  353. }
  354. engine.elements.new.inventory = engine.elements.newElement({
  355.   INIT = function(self)
  356.     for slotNum = 1, self.slotNum do
  357.       self[slotNum] = {}
  358.     end
  359.   end,
  360.   UPDATE = function(self)
  361.   end,
  362.   mouse_click = function(self, button, mx, my)
  363.     for slotNum = 1, #self do
  364.       local slot = self[slotNum]
  365.       local heldItem = self.heldItem
  366.       local slotX, slotY = self:getSlotPosition(slotNum)
  367.       if mx == slotX and my == slotY then
  368.         if not slot.item and heldItem then
  369.           self[slotNum] = heldItem
  370.           self.heldItem = false
  371.         elseif slot.item and not heldItem then
  372.           self.heldItem = slot
  373.           self[slotNum] = {}
  374.         elseif slot.item and heldItem then
  375.           if slot.item == heldItem.item then
  376.             self[slotNum].amount = self[slotNum].amount + self.heldItem.amount
  377.             self.heldItem = false
  378.           else
  379.             self[slotNum] = self.heldItem
  380.             self.heldItem = slot
  381.           end
  382.         end
  383.         return
  384.       end
  385.     end
  386.   end,
  387.   textures = {
  388.     up = engine.elements.new.texture({{"\143", "a", "0"}}),
  389.     left = engine.elements.new.texture({{"\149", "a", "0"}}),
  390.     right = engine.elements.new.texture({{"\149", "0", "a"}}),
  391.     down = engine.elements.new.texture({{"\131", "0", "a"}})
  392.   },
  393.   charPos = {
  394.     up = {0, -1},
  395.     down = {0, 1},
  396.     left = {-1, 0},
  397.     right = {1, 0},
  398.   },
  399.   items = {},
  400.   give = function(self, item, amount, newSlot)
  401.     local gaveItem = false
  402.     for slotNum = 1, #self do
  403.       local slot = self[slotNum]
  404.       if not gaveItem then
  405.         if newSlot and not slot.item then
  406.           gaveItem = true
  407.           self[slotNum] = {
  408.             item = item,
  409.             amount = amount
  410.           }
  411.         elseif slot.item == item then
  412.           gaveItem = true
  413.           slot.amount = slot.amount + 1
  414.         end
  415.       end
  416.     end
  417.     if not gaveItem then return self:give(item, amount, true) end
  418.   end,
  419.   heldItem = false,
  420.   take = function(self, item)
  421.  
  422.   end,
  423.   getSlotPosition = function(self, slotNum)
  424.     return self.x+(slotNum-1)*3, self.y
  425.   end,
  426.   drawSlot = function(self, slotNum)
  427.     local slotX, slotY = self:getSlotPosition(slotNum)
  428.     for direction, pos in pairs(self.charPos) do
  429.       local charX, charY = slotX+pos[1], slotY+pos[2]
  430.       local texture = self.textures[direction]
  431.       local tileX, tileY = charX-self.tilemap.offX, charY-self.tilemap.offY
  432.       local colorOfTile = self.tilemap:getTile(tileX, tileY).texture[1][3]
  433.       texture:replaceColor("a", colorOfTile)
  434.       texture:drawTexture(charX, charY)
  435.       texture:replaceColor(colorOfTile, "a")
  436.     end
  437.     local slot = self[slotNum]
  438.     if slot.item then
  439.       self.items[slot.item]:drawItem(slotX, slotY)
  440.       term.setCursorPos(slotX-1, slotY+1)
  441.       term.setBackgroundColor(colors.white)
  442.       term.setTextColor(colors.gray)
  443.       term.write(slot.amount)
  444.     end
  445.   end,
  446.   DRAW = function(self)
  447.     for slotNum = 1, #self do
  448.       self:drawSlot(slotNum)
  449.     end
  450.   end
  451. })
  452.  
  453. engine.elements.runFunction = function(elements, func, ...)
  454.   local prioritys = {}
  455.   for elementName, element in pairs(elements) do
  456.     local priority = element.priority
  457.     if priority then
  458.       if priority == true then
  459.         table.insert(prioritys, element)
  460.       else
  461.         prioritys[priority] = element
  462.       end
  463.     elseif element.element then
  464.         if element[func] then
  465.           element[func](element, ...)
  466.         end
  467.     else
  468.       engine.elements.runFunction(element, func, ...)
  469.     end
  470.   end
  471.   for priorityNum = 1, #prioritys do
  472.     local element = prioritys[priorityNum]
  473.     if element[func] then
  474.       element[func](element, ...)
  475.     end
  476.   end
  477. end
  478. engine.elements.init = function(elements)
  479.   engine.elements.runFunction(elements, "INIT")
  480.   engine.elements.runFunction(elements, "init")
  481. end
  482. engine.elements.update = function(elements, event, ...)
  483.   engine.elements.runFunction(elements, "UPDATE", event, ...)
  484.   engine.elements.runFunction(elements, "update", event, ...)
  485.   engine.elements.runFunction(elements, event, ...)
  486. end
  487. engine.elements.physicsUpdate = function(elements)
  488.   engine.elements.runFunction(elements, "PHYSICSUPDATE")
  489.   engine.elements.runFunction(elements, "physicsUpdate")
  490. end
  491. engine.elements.draw = function(elements)
  492.   engine.elements.runFunction(elements, "DRAW")
  493.   engine.elements.runFunction(elements, "draw")
  494. end
  495.  
  496. engine.run = function(elements, dt, physicsUpdateTime)
  497.   local physicsUpdateTime = physicsUpdateTime or 0.06
  498.   local dt = dt or 0.01
  499.   engine.elements.init(elements)
  500.  
  501.   local dt = dt or 0.01
  502.   local buffer = window.create(term.current(), 1, 1, engine.w, engine.h)
  503.   local oldTerm = term.redirect(buffer)
  504.  
  505.   local succ, mess = pcall(function()
  506.   local gameRunning = true
  507.   local physicsUpdateTimer = os.startTimer(physicsUpdateTime)
  508.   while gameRunning do
  509.     buffer.setVisible(false)
  510.     term.setBackgroundColor(colors.black)
  511.     term.clear()
  512.     engine.elements.draw(elements)
  513.     buffer.setVisible(true)
  514.  
  515.     local event, var1, var2, var3 = os.pullEventRaw()
  516.     if engine.eventHandler[event] then
  517.       engine.eventHandler[event](var1, var2, var3)
  518.     end
  519.     if event == "timer" then
  520.       engine.elements.physicsUpdate(elements)
  521.       os.cancelTimer(physicsUpdateTimer)
  522.       physicsUpdateTimer = os.startTimer(physicsUpdateTime)
  523.     elseif event == "char" and var1 == "q" then
  524.       gameRunning = false
  525.     end
  526.     engine.elements.update(elements, event, var1, var2, var3)
  527.   end
  528.   engine.logfile.close()
  529.   end)
  530.  
  531.   buffer.setVisible(true)
  532.   term.setBackgroundColor(colors.black)
  533.   term.clear()
  534.   term.setCursorPos(1, 1)
  535.   term.setTextColor(colors.red)
  536.   if not succ and mess then
  537.     print("The game crashed!")
  538.     print("Error: "..tostring(mess))
  539.   end
  540. end
  541.  
  542. local elements = {}
  543. elements.leaveParticles = engine.elements.new.particles({
  544.   maxStates = 5,
  545.   moves = {
  546.     {2, 2},
  547.     {1, -1},
  548.     {1, 0},
  549.     {-1, 0},
  550.     {-2, 2},
  551.     {-1, 0},
  552.     {-1, -1},
  553.   },
  554.   priority = true
  555. })
  556. elements.tilemap = engine.elements.new.tilemap({
  557.   offset = {0, 0},
  558.   tileset = engine.elements.new.tileset({
  559.     air = {texture = engine.elements.new.texture({{" ", "3", "3"}})},
  560.     grass = {texture = engine.elements.new.texture({{" ", "7", "d"}}),solid = true, behind = "dirt"},
  561.     plant = {texture = engine.elements.new.texture({{"p", "d", "3"}}), behind = "air"},
  562.     dirt = {texture = engine.elements.new.texture({{" ", "7", "c"}}), solid = true, behind = "wall"},
  563.     stone = {texture = engine.elements.new.texture({{" ", "f", "8"}}), solid = true, behind = "wall"},
  564.     leaves = {texture = engine.elements.new.texture({{"b", "5", "d"}})},
  565.     log = {texture = engine.elements.new.texture({{"B", "7", "c"}}), behind = "air", onBreak = function(tilemap, tx, ty)
  566.       for x = 1, 3 do
  567.         for y = 1, 2 do
  568.           local tileX, tileY = tx+x-2, ty+y-2
  569.           local tile = tilemap[tileX][tileY]
  570.           if tile == "leaves" or tile == "log" then
  571.             if math.random(1, 10) == 1 then
  572.               elements.leaveParticles:spawn(tileX, tileY)
  573.             end
  574.             tilemap.set.tile(tilemap, tileX, tileY, "air")
  575.             tilemap.tileset.log.onBreak(tilemap, tileX, tileY)
  576.           end
  577.         end
  578.       end
  579.     end},
  580.   }),
  581.   createWall = function(self, tileName)
  582.     local tile = self.tileset[tileName]
  583.     self.tileset[tileName.."wall"] = {
  584.       texture = engine.elements.new.texture({{"\127", tile.texture[1][3], "7"}})
  585.     }
  586.   end,
  587.   changeMove = function(oldMove)
  588.     local move = oldMove
  589.     if math.random(1, 10) == 1 then
  590.       if math.random(2) == 1 then
  591.         move = 0.6
  592.       else
  593.         move = -0.6
  594.       end
  595.       if math.random(1, 10) == 1 then
  596.         move = move * 2
  597.       end
  598.     elseif math.random(1, 10) == 1 then
  599.       move = engine.math.reduce(oldMove)
  600.     elseif math.random(1, 10) == 1 then
  601.       move = move + 0.5
  602.     elseif math.random(1, 10) == 1 then
  603.       move = move - 0.5
  604.     end
  605.     if math.abs(move) >= 3 then
  606.       engine.math.reduce(move)
  607.     end
  608.     return move
  609.   end,
  610.   generateLine = function(self, x, surfaceHeight, stoneHeight, height)
  611.     self.set.rectangle(self, x, surfaceHeight, 1, height-surfaceHeight, "dirt")
  612.     self.set.rectangle(self, x, stoneHeight, 1, height-stoneHeight, "stone")
  613.     self.set.tile(self, x, surfaceHeight, "grass")
  614.     if math.random(1, 5) == 1 then
  615.       self.set.tile(self, x, surfaceHeight+1, "grass")
  616.     end
  617.     if math.random(1, 2) == 1 then
  618.       self.set.tile(self, x, surfaceHeight-1, "plant")
  619.     end
  620.   end,
  621.   addTree = function(self, x, trees, surfaceHeight, height)
  622.     local height = math.random(4, 12)
  623.     table.insert(trees, {
  624.       x = x, y = surfaceHeight-height-1,
  625.       height = height,
  626.     })
  627.     return trees
  628.   end,
  629.   generateStump = function(self, tree)
  630.     for y = tree.y, tree.y+tree.height do
  631.       self.set.tile(self, tree.x, y, "log")
  632.     end
  633.   end,
  634.   generateLeaves = function(self, tree)
  635.     self.set.sphere(self, tree.x, tree.y, tree.height/2, "leaves")
  636.   end,
  637.   generateWalls = function(self)
  638.     for tileName, tile in pairs(self.tileset) do
  639.       if tile.texture then
  640.         self:createWall(tileName)
  641.       end
  642.     end
  643.   end,
  644.   generateTrees = function(self, trees)
  645.     local generatedTrees = {}
  646.     for treeNum = 1, #trees do
  647.       local tree = trees[treeNum]
  648.       if not generatedTrees[tree.x-1] and not generatedTrees[tree.x+1] then
  649.         self:generateStump(tree)
  650.         generatedTrees[tree.x] = true
  651.       end
  652.     end
  653.     for treeNum = 1, #trees do
  654.       local tree = trees[treeNum]
  655.       if not generatedTrees[tree.x-1] and not generatedTrees[tree.x+1] then
  656.         self:generateLeaves(tree)
  657.         generatedTrees[tree.x] = true
  658.       end
  659.     end
  660.   end,
  661.   spawnPlayer = function(self)
  662.     local y = 1
  663.     while true do
  664.       if self[elements.player.x][y] ~= "air" then
  665.         elements.player.y = y-1
  666.         return
  667.       end
  668.       y = y + 1
  669.     end
  670.   end,
  671.   init = function(self)
  672.     local trees = {}
  673.     local width = engine.w*50
  674.     local height = engine.h*20
  675.     local surfaceY = math.floor(height/2)
  676.     local surfaceHeight = surfaceY
  677.     local stoneHeight = surfaceY+10
  678.     local move = 0
  679.  
  680.     self:generateWalls()
  681.     self.set.rectangle(self, 1, 1, width, height, "air")
  682.  
  683.     for x = 1, width do
  684.       move = self.changeMove(move)
  685.       stoneHeight = stoneHeight + move+math.random(3)-2
  686.       surfaceHeight = surfaceHeight + move
  687.       local tiledSurfaceHeight = math.floor(surfaceHeight)
  688.       local tiledStoneHeight = math.floor(stoneHeight)
  689.       if stoneHeight-surfaceHeight < 5 then stoneHeight = stoneHeight + 2 end
  690.  
  691.       self:generateLine(x, tiledSurfaceHeight, tiledStoneHeight, height)
  692.       if math.floor(move) == 0 and math.random(1, 5) == 1 then
  693.         trees = self:addTree(x, trees, tiledSurfaceHeight, height)
  694.       end
  695.     end
  696.  
  697.     self:spawnPlayer()
  698.     self:generateTrees(trees)
  699.   end
  700. })
  701. elements.player = engine.elements.new.kinematic({
  702.   priority = true,
  703.   tilemap = elements.tilemap,
  704.   texture = engine.elements.new.texture({
  705.     {" ", "4", "4"},
  706.     {" ", "9", "9"}
  707.   }),
  708.   x = math.floor(engine.w*3/2), y = 3,
  709.   maxJumpHeight = 6,
  710.   breakTile = function(self, tileX, tileY)
  711.     local tileToSetOn = self.tilemap:getTile(tileX, tileY)
  712.     if tileToSetOn then
  713.       local tileToSet = tileToSetOn.behind
  714.       local tileName = self.tilemap[tileX][tileY]
  715.       if tileToSet == "wall" then
  716.         tileToSet = tileName.."wall"
  717.       end
  718.       if tileToSet then
  719.         if not self.tilemap.tileset[tileToSet] then
  720.           self.tilemap:createWall(tileName)
  721.         end
  722.         self.tilemap.set.tile(self.tilemap, tileX, tileY, tileToSet)
  723.         if tileToSetOn.onBreak then
  724.           tileToSetOn.onBreak(self.tilemap, tileX, tileY)
  725.         end
  726.         elements.inventory:createItemFromTile(tileName)
  727.         elements.inventory:give(tileName, 1)
  728.       end
  729.     end
  730.   end,
  731.   placeTile = function(self, tileX, tileY)
  732.     local tileToPlace = elements.inventory.heldItem.item
  733.     local tileToPlaceOn = self.tilemap[tileX][tileY]
  734.     if tileToPlaceOn and tileToPlaceOn~=tileToPlace then
  735.       self.tilemap.set.tile(self.tilemap, tileX, tileY, tileToPlace)
  736.       elements.inventory.heldItem.amount = elements.inventory.heldItem.amount-1
  737.       if elements.inventory.heldItem.amount == 0 then
  738.         elements.inventory.heldItem = false
  739.       end
  740.     end
  741.   end,
  742.   update = function(self)
  743.     local offX, offY = engine.math.getOffset(engine.w, self.x), engine.math.getOffset(engine.h, self.y)
  744.     self.offX, self.offY = offX, offY
  745.     self.tilemap.offX, self.tilemap.offY = offX, offY
  746.     elements.leaveParticles.offX, elements.leaveParticles.offY = offX, offY
  747.     local clickedX, clickedY = engine.mouse.x-offX, engine.mouse.y-offY
  748.     if engine.mouse.left then
  749.       self:breakTile(clickedX, clickedY)
  750.     end
  751.     if engine.mouse.right and elements.inventory.heldItem then
  752.       self:placeTile(clickedX, clickedY)
  753.     end
  754.   end
  755. })
  756. elements.inventory = engine.elements.new.inventory({
  757.   x = 3, y = 3, slotNum = 5, priority = true, tilemap = elements.tilemap,
  758.   createItemFromTile = function(self, tile)
  759.     if not self.items[tile] then
  760.       self.items[tile] = engine.elements.new.item({
  761.         texture = elements.tilemap.tileset[tile].texture
  762.       })
  763.     end
  764.   end
  765. })
  766.  
  767. engine.run(elements, 0.01, 0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement