The bad thing comes when I try to select actual objects like the little square I made. I'm not sure if all the if statements I did for checking if the selection box that covers the X, Y, X+Width and Y+Height of the cube are correct but it should on my understanding.
So! This is my code:
Code: Select all
--Hello
--//Functions
function love.load()
print("Hi there! Console talking! Wassup?")
--print(npcs[1].X)
--//Declaring some variables
npcNumber = 1
mouseX = love.mouse.getX()
mouseY = love.mouse.getY()
oldMouseX = 0
oldMouseY = 0
selectionX = 0
selectionY = 0
selection = false
selected = false
--//Making some entities! :D
npc1 = {}
npc1.X = math.random(100,900) --RandomGenerator:random(100,900)
npc1.Y = math.random(100,500) --RandomGenerator:random(100,500)
npc1.Height = 16
npc1.Width = 16
npc1.mode = "fill"
--npc1.image = npcSkinImage
npcs = {npc1}
end
--[[function love.keypressed(key)
end]]--
function love.mousepressed(x,y,button)
if button == "l" then
oldMouseX = love.mouse.getX()
oldMouseY = love.mouse.getY()
print("Left button pressed")
if selection == false then
selection = true
end
if selection == true and npcs[1].X > oldMouseX and npcs[1].X + npcs[1].Width < oldMouseX + selectionX and npcs[1].Y > oldMouseY and npcs[1].Y + npcs[1].Height < oldMouseY + selectionY then
print("One NPC selected")
if selected == false then
selected = true
else
selected = true
end
end
end
end
function love.mousereleased(x,y,button)
if button == "l" then
if selection == true then
selection = false
end
end
end
function love.draw()
love.graphics.print("NPCs Number: " .. npcNumber,0,0)
love.graphics.print("Mouse X: " .. mouseX,0,10)
love.graphics.print("Mouse Y: " .. mouseY,0,20)
love.graphics.print("Old MouseX: " .. oldMouseX,0,30)
love.graphics.print("Old MouseY: " .. oldMouseY,0,40)
love.graphics.print("SquareX: " .. npcs[1].X,0,50)
love.graphics.print("SquareY: " .. npcs[1].Y,0,60)
love.graphics.print("Selection: " .. tostring(selection),0,70)
love.graphics.print("SelectionX : " .. selectionX,0,80)
love.graphics.print("SelectionY : " .. selectionY,0,90)
love.graphics.print("Selected: " .. tostring(selected),0,100)
if selected == true then
love.graphics.setColor(255,0,0,255)
else
love.graphics.setColor(255,255,255,255)
end
love.graphics.rectangle(npcs[1].mode,npcs[1].X,npcs[1].Y,npcs[1].Width,npcs[1].Height)
if selection == true then
love.graphics.setColor(0,0,255,100)
love.graphics.rectangle("fill",oldMouseX,oldMouseY,selectionX,selectionY)
end
love.graphics.setColor(255,255,255,255) -- Back to its normal colors
end
function love.update()
mouseX = love.mouse.getX()
mouseY = love.mouse.getY()
selectionX = mouseX - oldMouseX
selectionY = mouseY - oldMouseY
end
Also: I thought that if the selection square has negative numbers for the width and the height it might couse some problems, any idea? Thanks for spending your time helping me!