I am working on a small game with a friend.
First, heres the code:
Code: Select all
--------------------------------------------------------------------------------
-------------------------------LOVE.LOAD----------------------------------------
--------------------------------------------------------------------------------
function love.load()
lphys = love.physics
lgraph = love.graphics
----------------------------BASIC PARAMETERS------------------------------------
dt = 0.02
m = 2
ld = 2
i = 0
g = 0
gegnerrate = 25
schussrate = 0
cann = lgraph.newImage("cann.png")
welt = lphys.newWorld(0, 0, true)
lphys.setMeter(20)
------------------------------CREATING TABLES-----------------------------------
player = {}
enemy = {}
bullet = {}
enemy.testenemy = {}
-----------------------ATTACHING SHAPES AND BODYS-------------------------------
player.shape = lphys.newRectangleShape(30, 10)
player.body = lphys.newBody(welt, 200, 200, "dynamic")
player.body:setLinearDamping(ld)
player.fixture = lphys.newFixture(player.body, player.shape, m)
end
--------------------------------------------------------------------------------
-----------------------------LOVE.UPDATE----------------------------------------
--------------------------------------------------------------------------------
function love.update(dt)
welt:update(dt)
fps = love.timer.getFPS()
------------------------PLAYER INTERACTION--------------------------------------
if love.keyboard.isDown("w") then
player.body:applyLinearImpulse(0, -15)
end
if love.keyboard.isDown("s") then
player.body:applyLinearImpulse(0, 15)
end
if love.keyboard.isDown("d") then
player.body:applyLinearImpulse(10, 0)
end
if love.keyboard.isDown("a") then
player.body:applyLinearImpulse(-10, 0)
end
--[[
-----------------------GENERATING ENEMIES----------------------------------------
if gegnerrate == 0 then
table.insert(enemy.testenemy, g)
enemy[g].shape = lphys.newRectangleShape(5, 5)
enemy[g].body = lphys.newBody(welt, 650, math.random(800), "dynamic")
enemy[g].fixture = lphys.newFixture(enemy.testenemy[g].body, enemy.testenemy[g].shape, 1)
--enemy[g].typ = "testenemy"
end
if gegnerrate > 0 then
gegnerrate = gegnerrate - 1
end
]]
-----------------------GENERATING BULLETS---------------------------------------
if love.mouse.isDown("l") and schussrate == 0 then
shoot = true
i = i + 1
x = player.body:getX()
y = player.body:getY()
deltax = x - love.mouse.getX()
deltay = y - love.mouse.getY()
m = deltay/deltax
rad = math.atan(m)
table.insert(bullet, i)
bullet[i] = {}
bullet[i].body = lphys.newBody(welt, x + 20, y, "dynamic")
bullet[i].shape = lphys.newRectangleShape(5, 1)
bullet[i].fixture = lphys.newFixture(bullet[i].body, bullet[i].shape, 1)
bullet[i].ttl = 100
rad = math.atan(m)
bullet[i].body:applyForce(math.cos(rad)*1000, 0)
bullet[i].body:applyForce(0, math.sin(rad)*1000)
bullet[i].body:setAngle(rad)
bullet[i].body:setBullet(true)
schussrate = 5
end
if schussrate > 0 then
schussrate = schussrate - 1
end
-------------------------------STUFF--------------------------------------------
i = #bullet
player.body:setAngle(0)
love.draw()
end
--------------------------------------------------------------------------------
-------------------------------------LOVE.DRAW----------------------------------
--------------------------------------------------------------------------------
function love.draw()
lgraph.polygon("fill", player.body:getWorldPoints(player.shape:getPoints()))
if shoot == true then
for z, b in pairs(bullet) do
--for z = 1, i do
lgraph.polygon("fill", bullet[z].body:getWorldPoints(bullet[z].shape:getPoints()))
if bullet[z].ttl < 0 then
bullet[z].body:destroy()
table.remove(bullet, z)
i = i - 1
else
bullet[z].ttl = bullet[z].ttl - 1
end
end
end
--[[
for z, b in pairs(enemy.testenemy) do
lgraph.polygon("fill", enemy.testenemy[z].body:getWorldPoints(enemy.testenemy[z].shape:getPoints()))
end]]
lgraph.draw(cann, player.body:getX() + 15, player.body:getY() - 1, rad, 2, 2, 0, 1)
lgraph.print(fps, 5, 5)
lgraph.print(schussrate, 5, 30)
lgraph.print("Schuss Nummer:", 5, 45)
lgraph.print(i, 120, 45)
end
--[[
if bullets[i][t] = 0 then
bullets[i]:destroy
bullets[i][t] = ttl
rad = math.atan(m)
dy = math.sin(rad)*10
dx = math.cos(rad)*10
table.insert(bullet, i)
]]
On my computer, everything works as it should: By clicking i get a nice line of fire, and i can move the Rectangel with wasd. On my friends computer, which isnt as powerful as mine, theres a strange lag appearing after some seconds off firing. Its going well for half a minute, but then its lagging, as if the computer is trying to load something big.
Normally, the shots are flying straight towards the mouse pointer. But on his computer, they are spreading crazily after lag or short after loading the game.
We are not able to fix this.
Any suggestions? Shouldnt we use physics? Is it his computer, bad programming or the wrong use of the engine?
Help D: