Löve may lack a simple manipulatable rectangle, which I'd like to use to highlight the selected tiles, so I created it myself.
I've previously coded this functionality in C++/SFML (working), so it's more or less copy & paste (what i thought atleast).
But unfortunately the created rectangle does not move accordingly/does not get rendered correctly based on the submitted coordinates.
My rectangle class:
Code: Select all
Rectangle = {}
Rectangle.__index = Rectangle
function Rectangle:create(size, color, thickness)
--rectangle class to use as Rectangle
local rctngl = {}
setmetatable(rctngl, Rectangle)
rctngl.size = size
rctngl.color = color
rctngl.thickness = thickness
rctngl.x = 0
rctngl.y = 0
rctngl.vertex1 = {rctngl.x, rctngl.y, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--TopLeftOutside
rctngl.vertex2 = {rctngl.x + rctngl.size.x, rctngl.y, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--TopRightOutside
rctngl.vertex3 = {rctngl.x + rctngl.size.x, rctngl.y + rctngl.size.y, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--BottomRightOutside
rctngl.vertex4 = {rctngl.x, rctngl.y + rctngl.size.y, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--BottomLeftOutside
rctngl.vertex5 = {rctngl.x + rctngl.thickness, rctngl.y + rctngl.thickness, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--TopLeftInside
rctngl.vertex6 = {rctngl.x + rctngl.size.x - rctngl.thickness, rctngl.y + rctngl.thickness, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--TopRightInside
rctngl.vertex7 = {rctngl.x + rctngl.size.x - rctngl.thickness, rctngl.y + rctngl.size.y - rctngl.thickness, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--BottomRightInside
rctngl.vertex8 = {rctngl.x + rctngl.thickness, rctngl.y + rctngl.size.y - rctngl.thickness, 0, 0, rctngl.color.red, rctngl.color.green, rctngl.color.blue, rctngl.color.alpha}--BottomLeftInside
rctngl.verticies = {rctngl.vertex1, rctngl.vertex2, rctngl.vertex5,
rctngl.vertex2, rctngl.vertex5, rctngl.vertex6,
rctngl.vertex2, rctngl.vertex3, rctngl.vertex6,
rctngl.vertex3, rctngl.vertex6, rctngl.vertex7,
rctngl.vertex3, rctngl.vertex4, rctngl.vertex7,
rctngl.vertex4, rctngl.vertex7, rctngl.vertex8,
rctngl.vertex4, rctngl.vertex5, rctngl.vertex8,
rctngl.vertex4, rctngl.vertex5, rctngl.vertex1
}
rctngl.mesh = love.graphics.newMesh(rctngl.verticies, "triangles", "dynamic")
return rctngl
end
Code: Select all
function Rectangle:moveTo(x,y)
self.x = x
self.y = y
self.vertex1 = {self.x, self.y, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--TopLeftOutside
self.vertex2 = {self.x + self.size.x, self.y, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--TopRightOutside
self.vertex3 = {self.x + self.size.x, self.y + self.size.y, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--BottomRightOutside
self.vertex4 = {self.x, self.y + self.size.y, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--BottomLeftOutside
self.vertex5 = {self.x + self.thickness, self.y + self.thickness, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--TopLeftInside
self.vertex6 = {self.x + self.size.x - self.thickness, self.y + self.thickness, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--TopRightInside
self.vertex7 = {self.x + self.size.x - self.thickness, self.y + self.size.y - self.thickness, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--BottomRightInside
self.vertex8 = {self.x + self.thickness, self.y + self.size.y - self.thickness, 0, 0, self.color.red, self.color.green, self.color.blue, self.color.alpha}--BottomLeftInside
self.verticies = {self.vertex1, self.vertex2, self.vertex5,
self.vertex2, self.vertex5, self.vertex6,
self.vertex2, self.vertex3, self.vertex6,
self.vertex3, self.vertex6, self.vertex7,
self.vertex3, self.vertex4, self.vertex7,
self.vertex4, self.vertex7, self.vertex8,
self.vertex4, self.vertex5, self.vertex8,
self.vertex4, self.vertex5, self.vertex1
}
self.mesh:setVertices(self.verticies, 1)
print("UppaLeft")
print("Rectangle X:\t", self.x, "\tRectangle Y:\t", self.y)
print("LowwaRight")
print("Rectangle X:\t", self.x + self.size.x, "\tRectangle Y:\t", self.y + self.size.y)
self:draw()
end
Code: Select all
function Player:moveIndicator()
x, y = love.mouse.getPosition()
print("Mouse X:\t", x, "\tMouse Y:\t", y)
self.indicator:moveTo(levelValue(x, self.indicator.size.x), levelValue(y, self.indicator.size.y))
end
The levelValue function is supposed to perfectly allign the rectangle with the tiles:
Code: Select all
function levelValue(x, y)
local a = math.floor( x )
local b = math.floor( y )
if a % b ~= 0 then
local fac = a % b
print("LevelValue:\t", a-fac)
return a - fac
else
print("LevelValue:\t", a)
return a
end
end
I've checked my code several times and I've come to the conclusion that I'm either blind or dumb.
It might also be a misconfiguration or a bug.
If someone could have a look at this I'd be grateful.
The current mouse position is represented by the white dot The Issue
(the "gap" increases the further you go) Cheers,
DerSchwarzePeter