g_imgTable = {
["tank"] = nil,
['bullet'] = nil
}
g_fired = {
}
g_enems = {
}
cords = {['x'] = nil, ['y'] = nil}
selfcords = {['x'] = 0, ['y'] = 0, ['dx'] = 0, ['dy'] = 0}
screen = {['w'] = nil, ['h'] = nil}
moveState = {up = nil, down = nil, left = nil, right = nil}
step = 100
speed = 300
bulletCount = 0
oldState = false
function addEnemy() --todo
end
function setParent() --todo
end
function love.load()
love.graphics.setBackgroundColor(255, 255, 255)
screen.w = love.graphics.getWidth()
screen.h = love.graphics.getHeight()
selfcords.x = screen.w / 2
selfcords.y = screen.h / 2
g_imgTable["tank"] = love.graphics.newImage('tank.png')
g_imgTable["bullet"] = love.graphics.newImage('bullet.png')
end
function love.update(dt)
cords.x, cords.y = love.mouse.getPosition()
interceptInput()
for k, v in pairs(g_fired) do
v.x = v.x + math.cos(v.ang) * speed * dt;
v.y = v.y + math.sin(v.ang) * speed * dt;
if v.x > screen.w or v.x < 0 or v.y > screen.h or v.y < 0 then
releaseBullet(v)
end
end
if selfcords.dx ~= 0 or selfcords.dy ~= 0 then
selfcords.x = selfcords.x + selfcords.dx * dt
selfcords.y = selfcords.y + selfcords.dy * dt
if selfcords.dx < 0 then
selfcords.dx = selfcords.dx + step
elseif selfcords.dx > 0 then
selfcords.dx = selfcords.dx - step
end
if selfcords.dy < 0 then
selfcords.dy = selfcords.dy + step
elseif selfcords.dy > 0 then
selfcords.dy = selfcords.dy - step
end
end
local curState = love.keyboard.isDown(' ')
if curState and not oldState then
createBullet()
end
bulletCount = bulletCount > 64 and 0 or bulletCount
oldState = love.keyboard.isDown(' ')
end
function interceptInput()
moveState.up = love.keyboard.isDown('w')
moveState.down = love.keyboard.isDown('s')
moveState.left = love.keyboard.isDown('a')
moveState.right = love.keyboard.isDown('d')
if moveState.up then
selfcords.dy = selfcords.dy - step
end
if moveState.down then
selfcords.dy = selfcords.dy + step
end
if moveState.left then
selfcords.dx = selfcords.dx - step
end
if moveState.right then
selfcords.dx = selfcords.dx + step
end
end
function combineBullet(num, x, y, ang)
return {['num'] = num, ['x'] = x, ['y'] = y, ['ang'] = ang}
end
function createBullet()
local x, y = selfcords.x, selfcords.y
local w, h = x - cords.x, y - cords.y
local ang = math.atan2(h, w) + math.pi
bulletCount = bulletCount + 1
table.insert(g_fired, combineBullet(bulletCount, x + math.cos(ang) * 16, y + math.sin(ang) * 16, ang))
end
function releaseBullet(bullet)
for k, v in ipairs(g_fired) do
if v.num == bullet.num then
table.remove(g_fired, k)
end
end
end
function love.draw()
local x, y = selfcords.x, selfcords.y
local w, h = x - cords.x, y - cords.y
love.graphics.draw(g_imgTable["tank"], x, y, math.atan2(h, w) - math.pi / 2, 1, 1, 16, 16)
for k, v in pairs(g_fired) do
love.graphics.draw(g_imgTable["bullet"], v.x, v.y, 0, 1, 1, 8, 8)
end
love.graphics.setColor(0, 0, 0)
love.graphics.print(#g_fired, 100, 100)
love.graphics.setColor(255, 255, 255)
end
Comments