The code below works by mapping the objects in the center, giving them a size based of the distance from the player and the object. But i dont want to see them in the canvas if they are out of my sight, any help?
Code: Select all
function vision(playerPosition, playerDirection)
local imageWidth = 16
local imageHeight = 16
local viewRadius = 10
local visibleRange = 128
local size = vec2.new(imageWidth/2, imageHeight/2)
local canvas = love.graphics.newCanvas(imageWidth, imageHeight)
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local mappedPlayerPosition = vec2.new( math.map( playerPosition.x, 0, width, 0, imageWidth, true ), math.map( playerPosition.y, 0, height, 0, imageHeight, true ) )
love.graphics.setCanvas( canvas )
local visibleObjects = {}
for _, object in mob.get():pairs() do
local mappedObjectPosition = vec2.new( math.map( object.position.x, 0, width, 0, imageWidth, true ), math.map( object.position.y, 0, height, 0, imageHeight, true ) )
local displacement = mappedObjectPosition - mappedPlayerPosition
local displacementDistance = displacement:magnitude()
local distance = (object.position - playerPosition):magnitude()
local objectAngle = math.atan2(displacement.y, displacement.x)
local angleDifference = objectAngle - playerDirection
-- Check if the object is within the visible range and angle of the player
if distance <= visibleRange and angleDifference >= viewRadius / 4 then
local size = (1 / distance) * (imageWidth * imageHeight)
local color = 1 / displacementDistance
table.insert( visibleObjects, {
x = (displacement.x + imageWidth / 2) - playerDirection,
y = displacement.y + imageHeight / 2,
size = size,
color = color,
} )
end
end
table.sort( visibleObjects, sorter )
for i=1, #visibleObjects do
local object = visibleObjects[ i ]
love.graphics.setColor( object.color, object.color, object.color, 1 )
love.graphics.circle( "fill", object.x, object.y, object.size )
end
love.graphics.setCanvas()
return canvas
end