collision
This commit is contained in:
parent
36f0188f0f
commit
f963cd0aca
1 changed files with 37 additions and 7 deletions
36
main.lua
36
main.lua
|
@ -8,12 +8,13 @@ function love.load()
|
|||
love.window.setMode(1024, 512)
|
||||
|
||||
player = {}
|
||||
player["x"] = 300
|
||||
player["y"] = 300
|
||||
player["x"] = 100
|
||||
player["y"] = 110
|
||||
player["size"] = 8
|
||||
player["angle"] = 0
|
||||
player["angle"] = -math.pi / 2
|
||||
player["dx"] = math.cos(player.angle) * 5
|
||||
player["dy"] = math.sin(player.angle) * 5
|
||||
player["offset"] = 20
|
||||
|
||||
map = {}
|
||||
map["x"] = 8
|
||||
|
@ -45,15 +46,43 @@ function love.update()
|
|||
player.dx = math.cos(player.angle) * 5
|
||||
player.dy = math.sin(player.angle) * 5
|
||||
end
|
||||
|
||||
local xo = 0
|
||||
if player.dx < 0 then xo = -player.offset else xo = player.offset end
|
||||
local yo = 0
|
||||
if player.dy < 0 then yo = -player.offset else yo = player.offset end
|
||||
|
||||
local ipx = math.ceil(player.x / map.size)
|
||||
local ipx_add_xo = math.ceil((player.x + xo) / map.size)
|
||||
local ipx_sub_xo = math.ceil((player.x - xo) / map.size)
|
||||
local ipy = math.ceil(player.y / map.size)
|
||||
local ipy_add_yo = math.ceil((player.y + yo) / map.size)
|
||||
local ipy_sub_yo = math.ceil((player.y - yo) / map.size)
|
||||
|
||||
local pos1 = (ipy - 1)*map.x + ipx_add_xo
|
||||
local pos2 = (ipy_add_yo - 1)*map.x + ipx
|
||||
|
||||
if love.keyboard.isDown("w") then
|
||||
if map.map[pos1] == 0 then
|
||||
player.x = player.x + player.dx
|
||||
end
|
||||
if map.map[pos2] == 0 then
|
||||
player.y = player.y + player.dy
|
||||
end
|
||||
end
|
||||
|
||||
local pos3 = (ipy - 1)*map.x + ipx_sub_xo
|
||||
local pos4 = (ipy_sub_yo - 1)*map.x + ipx
|
||||
|
||||
if love.keyboard.isDown("s") then
|
||||
if map.map[pos3] == 0 then
|
||||
player.x = player.x - player.dx
|
||||
end
|
||||
if map.map[pos4] == 0 then
|
||||
player.y = player.y - player.dy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
drawMap()
|
||||
|
@ -210,6 +239,7 @@ function drawRays3D()
|
|||
love.graphics.setColor(0.7, 0, 0)
|
||||
end
|
||||
|
||||
love.graphics.setLineWidth(1)
|
||||
love.graphics.line(
|
||||
player.x, player.y,
|
||||
ray.x, ray.y
|
||||
|
|
Loading…
Add table
Reference in a new issue