Pushok

love2d first steps

Jul 6th, 2014
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. g_imgTable = {
  2.     ["tank"] = nil,
  3.     ['bullet'] = nil
  4. }
  5.  
  6. g_fired = {
  7. }
  8.  
  9. g_enems = {
  10.    
  11. }
  12.  
  13. cords = {['x'] = nil, ['y'] = nil}
  14. selfcords = {['x'] = 0, ['y'] = 0, ['dx'] = 0, ['dy'] = 0}
  15. screen = {['w'] = nil, ['h'] = nil}
  16. moveState = {up = nil, down = nil, left = nil, right = nil}
  17.  
  18.  
  19. step = 100
  20. speed = 300
  21. bulletCount = 0
  22. oldState = false
  23.  
  24. function addEnemy() --todo
  25.    
  26. end
  27.  
  28. function setParent() --todo
  29. end
  30.  
  31. function love.load()
  32.     love.graphics.setBackgroundColor(255, 255, 255)
  33.     screen.w = love.graphics.getWidth()
  34.     screen.h = love.graphics.getHeight()
  35.     selfcords.x = screen.w / 2
  36.     selfcords.y = screen.h / 2
  37.     g_imgTable["tank"] = love.graphics.newImage('tank.png')
  38.     g_imgTable["bullet"] = love.graphics.newImage('bullet.png')
  39. end
  40.  
  41. function love.update(dt)
  42.     cords.x, cords.y = love.mouse.getPosition()
  43.     interceptInput()
  44.     for k, v in pairs(g_fired) do
  45.         v.x = v.x + math.cos(v.ang) * speed * dt;
  46.         v.y = v.y + math.sin(v.ang) * speed * dt;
  47.        
  48.         if v.x > screen.w or v.x < 0 or v.y > screen.h or v.y < 0 then
  49.             releaseBullet(v)
  50.         end
  51.     end
  52.    
  53.     if selfcords.dx ~= 0 or selfcords.dy ~= 0 then
  54.         selfcords.x = selfcords.x + selfcords.dx * dt
  55.         selfcords.y = selfcords.y + selfcords.dy * dt
  56.        
  57.         if selfcords.dx < 0 then
  58.             selfcords.dx = selfcords.dx + step
  59.         elseif selfcords.dx > 0 then
  60.             selfcords.dx = selfcords.dx - step
  61.         end
  62.        
  63.         if selfcords.dy < 0 then
  64.             selfcords.dy = selfcords.dy + step
  65.         elseif selfcords.dy > 0 then
  66.             selfcords.dy = selfcords.dy - step
  67.         end
  68.     end
  69.    
  70.     local curState = love.keyboard.isDown(' ')
  71.    
  72.     if curState and not oldState  then
  73.         createBullet()
  74.     end
  75.     bulletCount = bulletCount > 64 and 0 or bulletCount
  76.     oldState = love.keyboard.isDown(' ')
  77. end
  78.  
  79. function interceptInput()
  80.     moveState.up = love.keyboard.isDown('w')
  81.     moveState.down = love.keyboard.isDown('s')
  82.     moveState.left = love.keyboard.isDown('a')
  83.     moveState.right = love.keyboard.isDown('d')
  84.    
  85.     if moveState.up then
  86.         selfcords.dy = selfcords.dy - step
  87.     end
  88.     if moveState.down then
  89.         selfcords.dy = selfcords.dy + step
  90.     end
  91.     if moveState.left then
  92.         selfcords.dx = selfcords.dx - step
  93.     end
  94.     if moveState.right then
  95.         selfcords.dx = selfcords.dx + step
  96.     end
  97. end
  98.  
  99. function combineBullet(num, x, y, ang)
  100.     return {['num'] = num, ['x'] = x, ['y'] = y, ['ang'] = ang}
  101. end
  102.  
  103. function createBullet()
  104.     local x, y = selfcords.x, selfcords.y
  105.     local w, h = x - cords.x, y - cords.y
  106.     local ang = math.atan2(h, w) + math.pi
  107.     bulletCount = bulletCount + 1
  108.     table.insert(g_fired, combineBullet(bulletCount, x + math.cos(ang) * 16, y + math.sin(ang) * 16, ang))
  109. end
  110.  
  111. function releaseBullet(bullet)
  112.     for k, v in ipairs(g_fired) do
  113.         if v.num == bullet.num then
  114.             table.remove(g_fired, k)
  115.         end
  116.     end
  117. end
  118.  
  119. function love.draw()
  120.     local x, y = selfcords.x, selfcords.y
  121.     local w, h = x - cords.x, y - cords.y
  122.     love.graphics.draw(g_imgTable["tank"], x, y, math.atan2(h, w) - math.pi / 2, 1, 1, 16, 16)
  123.    
  124.     for k, v in pairs(g_fired) do
  125.         love.graphics.draw(g_imgTable["bullet"], v.x, v.y, 0, 1, 1, 8, 8)
  126.     end
  127.    
  128.     love.graphics.setColor(0, 0, 0)
  129.     love.graphics.print(#g_fired, 100, 100)
  130.     love.graphics.setColor(255, 255, 255)
  131. end
Advertisement
Add Comment
Please, Sign In to add comment