Advertisement
Redxone

[CC] Monster diffence test

Apr 30th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.93 KB | None | 0 0
  1. local w,h = term.getSize()
  2. local tick = os.startTimer(0.1)
  3. local bulletfire = os.startTimer(0.2)
  4. local btick = os.startTimer(0.001)
  5. local canfire = false
  6. local blocks = {
  7.     ['1'] = {name='grass',graphic=" ",tcolor="lime",bcolor="green",solid=false},
  8.     ['W'] = {name='wall',graphic=" ",tcolor="gray",bcolor="gray",solid=true},
  9.     ['C'] = {name='coin',graphic="O",tcolor="yellow",bcolor="orange",solid=false},
  10. }
  11.  
  12. local map = {}
  13. local enemies = {}
  14. local bullet = {}
  15.  
  16. ----- make map -----
  17. for y=0,h do
  18.     map[y] = string.rep('1',w)
  19. end
  20.  
  21. function getBlock(x,y)
  22.     return blocks[tostring(map[y]:sub(x,x))]
  23. end
  24.  
  25. function drawBlock(x,y)
  26.     local block = getBlock(x,y)
  27.     term.setTextColor(colors[block.tcolor])
  28.     term.setBackgroundColor(colors[block.bcolor])
  29.     term.setCursorPos(x,y)
  30.     term.write(block.graphic)
  31. end
  32.  
  33. function drawMap()
  34.     for y=1,h do
  35.         for x=1,w do
  36.             drawBlock(x,y)
  37.         end
  38.     end
  39. end
  40.  
  41. function setBlock(x,y,bid)
  42.     map[y] = table.concat{map[y]:sub(1,x-1), tostring(bid), map[y]:sub(x+1)}
  43.     drawBlock(x,y)
  44. end
  45.  
  46. local player = {
  47.     x=5,
  48.     y=5,
  49.     hp=100,
  50.     tcolor = "blue",
  51.     score = 0,
  52.     draw = function(self)
  53.         term.setCursorPos(self.x,self.y)
  54.         term.setTextColor(colors[self.tcolor])
  55.         term.setBackgroundColor(colors[getBlock(self.x,self.y).bcolor])
  56.         write("&")
  57.     end,
  58.  
  59.     translate = function(self,ox,oy)
  60.         if(not self:checkCollision(ox,oy))then
  61.             drawBlock(self.x,self.y)
  62.             self.x = self.x + ox
  63.             self.y = self.y + oy
  64.             self:draw()
  65.         end
  66.     end,
  67.  
  68.     checkCollision = function(self,ox,oy)
  69.         if(getBlock(self.x+ox,self.y+oy).solid)then
  70.             return true
  71.         else
  72.             for i = 1, #enemies do
  73.                 if(enemies[i] ~= nil and enemies[i].isAlive)then
  74.                     if( (self.x+ox == enemies[i].x and self.y == enemies[i].y) or
  75.                         (self.x == enemies[i].x and self.y+oy == enemies[i].y) or
  76.                         (self.x+ox == enemies[i].x and self.y+oy == enemies[i].y)
  77.                         )then
  78.                             return true
  79.                         end
  80.                     end
  81.                 end
  82.             return false
  83.         end
  84.     end,
  85.  
  86.     update = function(self,ev)
  87.         if(getBlock(self.x,self.y).name == "coin")then
  88.             self.score = self.score + 15
  89.             setBlock(self.x,self.y,'1')
  90.             self:draw()
  91.         end
  92.         if(ev[1] == "key")then
  93.             if(ev[2] == keys.w and self.y > 2)then
  94.                 self:translate(0,-1)
  95.             end
  96.             if(ev[2] == keys.s and self.y < h)then
  97.                 self:translate(0,1)
  98.             end
  99.             if(ev[2] == keys.d and self.x < w)then
  100.                 self:translate(1,0)
  101.             end
  102.             if(ev[2] == keys.a and self.x > 1)then
  103.                 self:translate(-1,0)
  104.             end
  105.         end
  106.     end,
  107. }
  108.  
  109. function addEnemy(x,y)
  110.     local nenemy = {
  111.         x = 0,
  112.         y = 0,
  113.         tcolor = "red",
  114.         movechance = 2,
  115.         hp = 100,
  116.         dmg=5,
  117.         isAlive = true,
  118.  
  119.         checkCollision = function(self,ox,oy)
  120.             if(getBlock(self.x+ox,self.y+oy).solid)then
  121.                 return true
  122.             elseif( (self.x+ox == player.x and self.y == player.y) or
  123.                     (self.x == player.x and self.y+oy == player.y) or
  124.                     (self.x+ox == player.x and self.y+oy == player.y)
  125.                     )then
  126.                 player.hp = player.hp - self.dmg
  127.                 return true
  128.             else
  129.                 for i = 1, #enemies do
  130.                     if(i ~= self.id and enemies[i] ~= nil)then
  131.                         if( (self.x+ox == enemies[i].x and self.y == enemies[i].y) or
  132.                             (self.x == enemies[i].x and self.y+oy == enemies[i].y) or
  133.                             (self.x+ox == enemies[i].x and self.y+oy == enemies[i].y)
  134.                             )then
  135.  
  136.                                 return true
  137.                             end
  138.                     end
  139.                 end
  140.  
  141.                 for i = 1, #bullet do
  142.                     if(bullet[i] ~= nil)then
  143.                         if( (self.x+ox == bullet[i].x and self.y == bullet[i].y) or
  144.                             (self.x == bullet[i].x and self.y+oy == bullet[i].y) or
  145.                             (self.x+ox == bullet[i].x and self.y+oy == bullet[i].y)
  146.                             )then
  147.                                 self.hp = self.hp - bullet[i].dmg
  148.                                 bullet[i]:destroy()
  149.                                 return true
  150.                             end
  151.                     end
  152.                 end
  153.  
  154.                 return false
  155.             end
  156.         end,
  157.  
  158.         destroy = function(self)
  159.             self.isAlive = false
  160.             drawBlock(self.x,self.y)
  161.             return true
  162.         end,
  163.  
  164.         draw = function(self)
  165.             term.setCursorPos(self.x,self.y)
  166.             term.setTextColor(colors[self.tcolor])
  167.             term.setBackgroundColor(colors[getBlock(self.x,self.y).bcolor])
  168.             write("&")
  169.         end,
  170.  
  171.         translate = function(self,ox,oy)
  172.             if( not self:checkCollision(ox,oy) )then
  173.                 drawBlock(self.x,self.y)
  174.                 self.x = self.x + ox
  175.                 self.y = self.y + oy
  176.                 self:draw()
  177.             end
  178.         end,
  179.  
  180.         update = function(self)
  181.             if(self.isAlive)then
  182.                 -- move twords player --
  183.                 if(self.hp <= 0)then
  184.                     if(math.random(1,8) == 7)then
  185.                         setBlock(self.x,self.y,'C')
  186.                     end
  187.                     player.score = player.score + 25
  188.                     self:destroy()
  189.                 end
  190.                 local cmove = math.random(-self.movechance,self.movechance)
  191.                 if(cmove == self.movechance)then
  192.                     if(player.x > self.x)then self:translate(1,0) end
  193.                     if(player.x < self.x)then self:translate(-1,0) end
  194.                     if(player.y > self.y)then self:translate(0,1)  end
  195.                     if(player.y < self.y)then self:translate(0,-1)  end
  196.                 end
  197.             end
  198.         end,   
  199.     }
  200.  
  201.     nenemy.isAlive = true
  202.     nenemy.x = x
  203.     nenemy.y = y
  204.     nenemy.id = #enemies+1
  205.     enemies[#enemies+1] = nenemy
  206.     nenemy:draw()
  207. end
  208.  
  209. function updateEnemies()
  210.     for i = 1, #enemies do
  211.         if(enemies[i] ~= nil and enemies[i].isAlive)then
  212.             enemies[i]:update()
  213.         end
  214.     end
  215. end
  216.  
  217. function addBullet(x,y,dirx,diry)
  218.     local nbullet = {
  219.         x = 0,
  220.         y = 0,
  221.         dirx=0,
  222.         diry=0,
  223.         tcolor = "yellow",
  224.         speed=1, -- 2 or more causes problems!
  225.         dmg=25,
  226.         isAlive=true,
  227.  
  228.         checkCollision = function(self,ox,oy)
  229.             if(getBlock(self.x+ox,self.y+oy).solid)then
  230.                 self:destroy()
  231.                 return true
  232.             else
  233.                 for i = 1, #enemies do
  234.                     if(enemies[i] ~= nil and enemies[i].isAlive)then
  235.                         if( (self.x+ox == enemies[i].x and self.y == enemies[i].y) or
  236.                             (self.x == enemies[i].x and self.y+oy == enemies[i].y) or
  237.                             (self.x+ox == enemies[i].x and self.y+oy == enemies[i].y)
  238.                             )then
  239.                                 enemies[i].hp = enemies[i].hp - self.dmg
  240.                                 self:destroy()
  241.                                 return true
  242.                             end
  243.                         end
  244.                     end
  245.                 return false
  246.             end
  247.         end,
  248.  
  249.  
  250.         draw = function(self)
  251.             term.setCursorPos(self.x,self.y)
  252.             term.setTextColor(colors[self.tcolor])
  253.             term.setBackgroundColor(colors[getBlock(self.x,self.y).bcolor])
  254.             write(".")
  255.         end,
  256.  
  257.         destroy = function(self)
  258.             drawBlock(self.x,self.y)
  259.             self.isAlive = false
  260.             drawBlock(self.x,self.y)
  261.             return true
  262.         end,
  263.  
  264.         translate = function(self,ox,oy)
  265.             if( not self:checkCollision(ox,oy) )then
  266.                 drawBlock(self.x,self.y)
  267.                 self.x = self.x + ox
  268.                 self.y = self.y + oy
  269.                 self:draw()
  270.             end
  271.         end,
  272.  
  273.         update = function(self)
  274.             if(self.isAlive)then
  275.                 if(self.x+(self.dirx*self.speed) <= 1)then self:destroy() return false end
  276.                 if(self.y+(self.diry*self.speed) <= 1)then self:destroy() return false end
  277.                 if(self.x+(self.dirx*self.speed) >= w)then self:destroy() return false end
  278.                 if(self.y+(self.diry*self.speed) >= h)then self:destroy() return false end
  279.                 self:translate(self.dirx*self.speed,self.diry*self.speed)
  280.             end
  281.         end,   
  282.     }
  283.     nbullet.x = x
  284.     nbullet.y = y
  285.     nbullet.dirx = dirx
  286.     nbullet.diry = diry
  287.     nbullet.id = #bullet+1
  288.     bullet[#bullet+1] = nbullet
  289.     nbullet:draw()
  290. end
  291.  
  292. function updateBullets()
  293.     for i = 1, #bullet do
  294.         if(bullet[i] ~= nil and bullet[i].isAlive)then
  295.             bullet[i]:update()
  296.         end
  297.     end
  298. end
  299.  
  300. setBlock(10,10,'C')
  301. addEnemy(10,11)
  302. addEnemy(10,15)
  303. addEnemy(10,17)
  304. addEnemy(10,18)
  305. addEnemy(12,18)
  306. addEnemy(11,15)
  307. drawMap()
  308. player:draw()
  309. while true do
  310.     if(#bullet >= 50 and not canfire)then
  311.         for i = 1, #bullet do
  312.             if(bullet[i] ~= nil)then
  313.                 drawBlock(bullet[i].x,bullet[i].y)
  314.             end
  315.         end
  316.         bullet = {}
  317.     end
  318.       local hasAlive = false
  319.         for i = 1, #enemies do
  320.             if(enemies[i].isAlive)then
  321.                 hasAlive = true
  322.             else
  323.                 drawBlock(enemies[i].x,enemies[i].y)
  324.             end
  325.         end
  326.         if(not hasAlive)then
  327.             enemies = {}
  328.         end
  329.    
  330.     local e = {os.pullEvent()}
  331.     player:update(e)
  332.     term.setTextColor(colors.white)
  333.     term.setBackgroundColor(colors.blue)
  334.     term.setCursorPos(1,1)
  335.     term.clearLine()
  336.     term.write("Score: " .. player.score .. "       HP: " .. player.hp)
  337.     if(e[1] == "timer")then
  338.         if(e[2] == tick)then
  339.             tick = os.startTimer(0.1)
  340.             updateEnemies()
  341.         end
  342.         if(e[2] == bulletfire)then
  343.             bulletfire = os.startTimer(0.2)
  344.             if(canfire==false)then canfire = true end
  345.             updateBullets()
  346.         end
  347.         if(e[2] == btick)then
  348.             btick = os.startTimer(0.001)
  349.             updateBullets()
  350.         end
  351.     end
  352.     if(e[1] == "key" and e[2] == keys.p)then addEnemy(math.random(3,50),math.random(3,15)) end
  353.     if( (e[1] == "mouse_click" or e[1] == "mouse_drag") and e[2] == 2)then
  354.         if(getBlock(e[3],e[4]).name == "wall")then
  355.             setBlock(e[3],e[4],"1")
  356.         else
  357.             setBlock(e[3],e[4],"W")
  358.         end
  359.     end
  360.     if( (e[1] == "mouse_click" and e[2] == 1) and canfire)then
  361.         canfire=false
  362.         local dx = 0
  363.         local dy = 0
  364.         if(e[3] < player.x)then
  365.             dx = -1
  366.         elseif(e[3] > player.x)then
  367.             dx = 1
  368.         elseif(e[3] == player.x)then
  369.             dx=0
  370.         end
  371.         if(e[4] < player.y)then
  372.             dy = -1
  373.         elseif(e[4] > player.y)then
  374.             dy = 1
  375.         elseif(e[4] == player.y)then
  376.             dy=0
  377.         end
  378.         addBullet(player.x+dx,player.y+dy,dx,dy)
  379.         updateBullets()
  380.     end
  381. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement