Advertisement
Fallstar

Untitled

Oct 19th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. --Gamestates/Game.lua
  2. --by Fallstar
  3.  
  4. local Game = {}
  5. local Gui = require "Quickie"
  6.  
  7. function Game:enter()
  8.     Previous = "Gamestates/Menu"
  9. end
  10.  
  11. local foes = {}
  12. local nbFoes = 5 --Number of foes
  13. local won = false
  14. local turrets = {}
  15. local nbTurrets = 4
  16. local deadFoes = 0
  17.  
  18.  
  19. function Game:reset()
  20.     for i=1, nbFoes do
  21.         table.insert(foes, { x = -i*20, y = 256, hp = 100})
  22.     end
  23. end
  24.  
  25.  
  26. function Game:init()
  27.     Game:reset()
  28. end
  29.  
  30. function Game:mousepressed(x, y, button)
  31.         if #turrets < nbTurrets then
  32.                 table.insert(turrets, { x = x } )
  33.         end
  34. end
  35.  
  36. function Game:update(dt)
  37.    
  38.     if not won and #turrets==nbTurrets then
  39.         for i, foe in ipairs(foes) do
  40.                 for j, turret in ipairs(turrets) do
  41.                     if foe and foe.x > turret.x-50 and foe.x < turret.x+51 and i % #turrets == j-1 then
  42.                         foe.hp =foe.hp-1
  43.                         if foe.hp <=0 then
  44.                             foes[i]=false
  45.                         end
  46.                         end
  47.                 end
  48.                 if foe then        
  49.                     foe.x = foe.x+1    
  50.                 end
  51.         end
  52.     elseif won then
  53.         Gui.group.push{grow = "down", pos = {50,5}}
  54.         Gui.Label{ text = "Bravo"}
  55.         Gui.group.pop{}
  56.     end
  57.    
  58.    
  59.     if Game:tableSize(foes)==0 then
  60.         won=true
  61.     end
  62. end
  63.  
  64. function Game:tableSize(tab)
  65.     local cpt=0
  66.     for i, val in ipairs(tab) do
  67.         if val then
  68.             cpt=cpt+1
  69.         end
  70.     end
  71.     return cpt
  72. end
  73.  
  74.  
  75.  
  76. function Game:draw()
  77.     love.graphics.setBackgroundColor(100, 050, 200)
  78.     for i, turret in ipairs(turrets) do
  79.         if turret then
  80.         love.graphics.setColor(255, 0, 0, 100)
  81.         love.graphics.rectangle("fill", turret.x-50, 0, 100, 600)
  82.         end
  83.     end
  84.     if not won then
  85.         for i, foe in ipairs(foes) do
  86.             if foe then
  87.                 love.graphics.setColor(0,0,0,255)
  88.                 love.graphics.circle("fill", foe.x, 300, 5, 100) --show foe i if it has hp 
  89.                 love.graphics.print(foe.hp, foe.x, 400) --shows hp
  90.             end
  91.             love.graphics.print(Game:tableSize(foes), 300, 500)
  92.         end
  93.     end
  94.     Gui.core.draw() --Draw Gui
  95. end
  96.  
  97. return Game
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement