scenes/map: add a basic cursor

This commit is contained in:
Kazhnuz 2019-03-06 22:33:13 +01:00
parent 4fb216c471
commit d1f110f11b
1 changed files with 57 additions and 0 deletions

View File

@ -5,17 +5,74 @@ function WorldMap:new()
WorldMap.super.new(self)
self.assets:addImage("background", "assets/backgrounds/worldmap.png")
self.banner = game.gui.newBanner("banner.png", "Choose your level")
self.cursor = {}
self.cursor.x, self.cursor.y = 0, 0
self.cursor.drawx, self.cursor.drawy = 0, 0
self.cursor.canMove = true
self:register()
end
function WorldMap:update(dt)
if (self.keys["up"].isDown) and (self.cursor.canMove) then
self.cursor.y = math.max(0, self.cursor.y - 1)
self.cursor.canMove = false
end
if (self.keys["down"].isDown) and (self.cursor.canMove) then
self.cursor.y = math.min(12, self.cursor.y + 1)
self.cursor.canMove = false
end
if (self.keys["right"].isDown) and (self.cursor.canMove) then
self.cursor.x = math.min(27, self.cursor.x + 1)
self.cursor.canMove = false
end
if (self.keys["left"].isDown) and (self.cursor.canMove) then
self.cursor.x = math.max(0, self.cursor.x - 1)
self.cursor.canMove = false
end
local dx = self.cursor.x - self.cursor.drawx
local dy = self.cursor.y - self.cursor.drawy
local dist = (dt*6)
if math.abs(dx) >= (dist) then
self.cursor.drawx = self.cursor.drawx + (dist * utils.math.sign(dx))
else
self.cursor.drawx = self.cursor.x
end
if math.abs(dy) >= (dist) then
self.cursor.drawy = self.cursor.drawy + (dist * utils.math.sign(dy))
else
self.cursor.drawy = self.cursor.y
end
if (self.cursor.drawy == self.cursor.y) and (self.cursor.drawx == self.cursor.x) then
self.cursor.canMove = true
end
end
function WorldMap:mousemoved(x, y)
if x >= 16 and y >= 48 then
local xx = math.floor((x - 16) / 16)
local yy = math.floor((y - 48) / 16)
xx = math.max(0, math.min(xx, 27))
yy = math.max(0, math.min(yy, 12))
self.cursor.x, self.cursor.y = xx, yy
self.cursor.drawx, self.cursor.drawy = xx, yy
end
end
function WorldMap:draw()
self.assets:drawImage("background", 0, 0)
love.graphics.draw(self.banner, 0, 8)
local x, y = 16 + self.cursor.drawx * 16, 48 + self.cursor.drawy * 16
love.graphics.rectangle("fill", x, y, 16, 16)
end
return WorldMap