Advertisement
billysback

Cards

Jul 12th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.36 KB | None | 0 0
  1. local function createTile(x, y)
  2.     local tile = {}
  3.     tile.x = x
  4.     tile.y = y
  5.     tile.entity = nil
  6.     return tile
  7. end
  8.  
  9. local function createLogs
  10.  
  11. local function createBoard(width, height)
  12.     local board = {}
  13.     for x=1,width do
  14.         board[x] = {}
  15.         for y=1,height do
  16.             board[x][y] = createTile(x,y)
  17.         end
  18.     end
  19.     board.getTile = function(self, x, y)
  20.         local tile = nil
  21.         if self[x] ~= nil then
  22.             if self[x][y] ~= nil then
  23.                 tile = self[x][y]
  24.             end
  25.         end
  26.         return tile
  27.     end
  28.     board.log = createLogger()
  29.     return board
  30. end
  31.  
  32. local function copyTable(table)
  33.     local ntable = {}
  34.     for k,v in pairs(table) do
  35.         if type(v) == "table" then
  36.             ntable[k] = copyTable(v)
  37.         else
  38.             ntable[k] = v
  39.         end
  40.     end
  41.     return ntable
  42. end
  43. local function createEntity(card, x, y, board)
  44.     local entity = {}
  45.     local data = {}
  46.     data.range = card.range --the range of their attack (inf)
  47.     data.movement = card.movement --the range of their movement (inf)
  48.     data.health = card.health --the amount of health they have (inf)
  49.     data.damage = card.damage --the amount of damage they deal (inf)
  50.     data.armour = card.armour --the amount of damage they resist (0 to 50)
  51.     data.armour_pen = card.armour_pen --the amount of amount of armour they ignore (0 to 1)
  52.     data.cooldown = card.cooldown --the amount of turns that must pass before they can do something
  53.     data.actions = card.actions --the amount of actions the unit can do per cooldown
  54.     data.revives = card.revives --the amount of revives available to the unit
  55.     data.aura = card.aura --the name of the stat buffed
  56.     data.aura_amount = card.aura_amount --the amount that the stat is changed by
  57.     data.aura_range = card.aura_range --the range of the aura
  58.     local stats = copyTable(data)
  59.     entity.data = data
  60.     entity.stats = stats
  61.     local pos = {}
  62.     pos.x = x
  63.     pos.y = y
  64.     pos.board = board
  65.     pos.tile = board[x][y]
  66.     entity.protect = nil
  67.    
  68.     entity.checkProtect = function(self)
  69.    
  70.     end
  71.    
  72.     entity.takeDamage = function(self, damage, pen)
  73.         local arm = (self.stats.armour/100)*(pen*100)
  74.         local dmg = damage
  75.         if arm > 0 then
  76.             dmg = math.ceil(dmg - ((damage/100)*arm))
  77.         end
  78.         if entity:checkProtect() then
  79.            
  80.            
  81.         else
  82.             self.stats.health = self.stats.health - damage
  83.         end
  84.         return self:check()
  85.     end
  86.    
  87.     entity.move = function(self, x, y)
  88.         local distX = math.abs(self.pos.x-x)
  89.         local distY = math.abs(self.pos.y-y)
  90.         local dist = math.sqrt(distX^2 + distY^2)
  91.         if dist <= self.stats.movement then
  92.             local tile = self.pos.board:getTile(x, y)
  93.             if tile ~= nil then
  94.                 if tile.entity == nil then
  95.                     self.pos.tile.entity = nil
  96.                     tile.entity = self
  97.                     return true
  98.                 end
  99.             end
  100.         end
  101.         return false
  102.     end
  103.    
  104.     entity.attack = function(self, x, y)
  105.         local distX = math.abs(self.pos.x-x)
  106.         local distY = math.abs(self.pos.y-y)
  107.         local dist = math.sqrt(distX^2 + distY^2)
  108.         if dist <= self.stats.range then
  109.             local tile = self.pos.board:getTile(x, y)
  110.             if tile ~= nil then
  111.                 if tile.entity ~= nil then
  112.                     if tile.entity:takeDamage(self.stats.damage, self.stats.armour_pen) then
  113.                         tile.entity = nil
  114.                     end
  115.                     return true
  116.                 end
  117.             end
  118.         end
  119.         return false
  120.     end
  121.    
  122.     entity.check = function(self)
  123.         if self.stats.health <= 0 then
  124.             if self.stats.revives > 0 then
  125.                 self.stats.health = self.data.health
  126.                 self.stats.revives = self.stats.revives - 1
  127.             else
  128.                 return true
  129.             end
  130.         end
  131.         return false
  132.     end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement