Re: Wrap images around game window
Posted: Wed Apr 12, 2017 12:21 pm
The green ones are basically missing.
Code: Select all
function love.update(dt)
local wh = love.graphics.getHeight()
local ww = love.graphics.getWidth()
local wrap_coord_offsets =
{
{x = 0, y = -wh}, -- top
{x = ww, y = -wh}, -- top right
{x = ww, y = 0}, -- right
{x = ww, y = wh}, -- bottom right
{x = 0, y = wh}, -- bottom
{x = -ww, y = wh}, -- bottom left
{x = -ww, y = 0}, -- left
{x = -ww, y = -wh} -- top left
}
-- Clear wrapped image coordinates. They are about to be recalculated.
for i, wco in ipairs(p.wrap_coords) do
p.wrap_coords[i] = nil
end
-- Calculate coordinates of all 8 images used for wrapping around borders
-- Only add to p.wrap_coords if the image is visible
for i, wco in ipairs(wrap_coord_offsets) do
local x = p.x + wco.x
local y = p.y + wco.y
if isVisible(x, y, p.ox, p.oy) then
-- Whichever one of these has its coords inside the window becomes the
-- primary image. Don't add it to the secondary wrap images
if x >= 0 and x < ww and y >= 0 and y < wh then
p.x = x
p.y = y
else
table.insert(p.wrap_coords, {x = x, y = y})
end
end
end
end
function love.draw()
-- Rotate player image around its center.
-- This moves the origin to the center of the image
love.graphics.draw(p.image, p.x, p.y, p.a, 1, 1, p.ox, p.oy)
-- Draw any boundary wrapping images
for i, wc in ipairs(p.wrap_coords) do
love.graphics.draw(p.image, wc.x, wc.y, p.a, 1, 1, p.ox, p.oy)
end
end
Code: Select all
local function drawPlayerAt (x, y)
love.graphics.draw(p.image, x, y, p.a, 1, 1, p.ox, p.oy)
end
local function getEdge (pos, rad, max)
if pos - rad < 0 then return pos + max end
if pos + rad > max then return pos - max end
end
function love.draw ()
local x = getEdge(p.x, p.w, WINDOW_WIDTH)
local y = getEdge(p.y, p.h, WINDOW_HEIGHT)
drawPlayerAt(p.x, p.y) -- draw at real position
if x then drawPlayerAt(x, p.y) end -- draw horizontal wrap
if y then drawPlayerAt(p.x, y) end -- draw vertical wrap
if x and y then drawPlayerAt(x, y) end -- draw corner wrap
end
Code: Select all
local function drawPlayerAt (x, y)
love.graphics.draw(p.image, x, y, p.a, 1, 1, p.ox, p.oy)
end
local function getEdge (pos, rad, max)
pos = pos - max
if -pos < rad then return pos end
end
function love.draw ()
-- you need this in update function anyway so better do it in update
p.x=p.x%WINDOW_WIDTH
p.y=p.y%WINDOW_HEIGHT
local x = getEdge(p.x, p.w, WINDOW_WIDTH)
local y = getEdge(p.y, p.h, WINDOW_HEIGHT)
drawPlayerAt(p.x, p.y) -- draw at real position
if x then drawPlayerAt(x, p.y) end -- draw horizontal wrap
if y then drawPlayerAt(p.x, y) end -- draw vertical wrap
if x and y then drawPlayerAt(x, y) end -- draw corner wrap
end
Because when assured that postion is in range [0,size) then there will be no wrap from left to right let me show: