2019-08-14 16:26:23 +02:00
|
|
|
local Cursor = Object:extend()
|
|
|
|
|
|
|
|
local maputils = require "scenes.battlesystem.utils"
|
2019-08-16 23:04:30 +02:00
|
|
|
local TweenManager = require "game.modules.tweenmanager"
|
2019-08-14 16:26:23 +02:00
|
|
|
|
|
|
|
function Cursor:new(world)
|
|
|
|
self.world = world
|
|
|
|
self.scene = world.scene
|
|
|
|
self.assets = world.assets
|
|
|
|
|
2019-08-16 23:04:30 +02:00
|
|
|
self.tweens = TweenManager(self)
|
|
|
|
|
2019-08-14 16:26:23 +02:00
|
|
|
self.x = 1
|
|
|
|
self.y = 1
|
|
|
|
self.isActive = false
|
|
|
|
self.tx = 1
|
|
|
|
self.ty = 1
|
|
|
|
|
|
|
|
self.grid = maputils.newEmptyMap()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:set(x, y)
|
|
|
|
self.x = math.max(math.min(x, 12), 1)
|
|
|
|
self.y = math.max(math.min(y, 07), 1)
|
|
|
|
self.tx = self.x
|
|
|
|
self.ty = self.y
|
|
|
|
|
|
|
|
self.isActive = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:setGrid(ox, oy, shape, size, direction, whitelistedEntity)
|
|
|
|
self.grid = maputils.newEmptyMap()
|
|
|
|
|
|
|
|
for y, line in ipairs(self.grid) do
|
|
|
|
for x, case in ipairs(line) do
|
|
|
|
if not maputils.isInMask(x, y, ox, oy, shape, size, direction) then
|
|
|
|
self.grid[y][x] = 0
|
|
|
|
else
|
|
|
|
if (self:testPoint(x, y, whitelistedEntity)) then
|
|
|
|
self.grid[y][x] = 1
|
|
|
|
else
|
|
|
|
self.grid[y][x] = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:testPoint(x, y, whitelistedActor)
|
|
|
|
if ((self.world:getActorInCase(x, y) == nil) or
|
|
|
|
(self.world:getActorInCase(x, y) == whitelistedActor) and (whitelistedActor ~= nil)) and
|
|
|
|
(self.world.map:getTerrain(x, y) ~= 3) then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:gridIsActive(x, y)
|
|
|
|
if self.grid[y] ~= nil then
|
|
|
|
return (self.grid[y][x] == 1)
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:getGrid()
|
|
|
|
if (self.isActive) then
|
|
|
|
return self.grid
|
|
|
|
else
|
|
|
|
return maputils.newFullMap()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:resetGrid()
|
|
|
|
self.grid = EmptyGrid
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:unset()
|
|
|
|
self.isActive = false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:update(dt)
|
|
|
|
if (self.isActive) then
|
|
|
|
local keys = self.scene:getKeys(1)
|
|
|
|
|
2019-08-16 23:04:30 +02:00
|
|
|
if (keys["up"].isDown and self.y == self.ty) then
|
2019-08-14 16:26:23 +02:00
|
|
|
dy = math.max(self.y - 1, 1)
|
|
|
|
if (self.grid[dy][self.x] == 1) then
|
|
|
|
self.y = dy
|
2019-08-16 23:04:30 +02:00
|
|
|
self:addTween()
|
2019-08-14 16:26:23 +02:00
|
|
|
end
|
|
|
|
end
|
2019-08-16 23:04:30 +02:00
|
|
|
if (keys["down"].isDown and self.y == self.ty) then
|
2019-08-14 16:26:23 +02:00
|
|
|
dy = math.min(self.y + 1, 7)
|
|
|
|
if (self.grid[dy][self.x] == 1) then
|
|
|
|
self.y = dy
|
2019-08-16 23:04:30 +02:00
|
|
|
self:addTween()
|
2019-08-14 16:26:23 +02:00
|
|
|
end
|
|
|
|
end
|
2019-08-16 23:04:30 +02:00
|
|
|
if (keys["left"].isDown and self.x == self.tx) then
|
2019-08-14 16:26:23 +02:00
|
|
|
dx = math.max(self.x - 1, 1)
|
|
|
|
if (self.grid[self.y][dx] == 1) then
|
|
|
|
self.x = dx
|
2019-08-16 23:04:30 +02:00
|
|
|
self:addTween()
|
2019-08-14 16:26:23 +02:00
|
|
|
end
|
|
|
|
end
|
2019-08-16 23:04:30 +02:00
|
|
|
if (keys["right"].isDown and self.x == self.tx) then
|
2019-08-14 16:26:23 +02:00
|
|
|
dx = math.min(self.x + 1, 12)
|
|
|
|
if (self.grid[self.y][dx] == 1) then
|
|
|
|
self.x = dx
|
2019-08-16 23:04:30 +02:00
|
|
|
self:addTween()
|
2019-08-14 16:26:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if (keys["A"].isPressed) then
|
|
|
|
self.world:sendSignalToCurrentBattler()
|
|
|
|
end
|
|
|
|
|
2019-08-16 23:04:30 +02:00
|
|
|
self.tweens:update(dt)
|
2019-08-14 16:26:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-16 23:04:30 +02:00
|
|
|
function Cursor:addTween()
|
|
|
|
self.tweens:newTween(0, 0.2, {tx = self.x, ty = self.y}, 'inCubic')
|
|
|
|
end
|
|
|
|
|
2019-08-14 16:26:23 +02:00
|
|
|
function Cursor:drawBottom()
|
|
|
|
|
|
|
|
if (self.isActive) then
|
|
|
|
local x, y, frame
|
|
|
|
x, y = maputils.gridToPixel(self.tx, self.ty, true)
|
|
|
|
|
|
|
|
self.assets.sprites["cursorground"]:drawAnimation(x, y, 0, 1, 1, 14, 6)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function Cursor:drawTop()
|
|
|
|
|
|
|
|
if (self.isActive) then
|
|
|
|
local x, y
|
|
|
|
x, y = maputils.gridToPixel(self.tx, self.ty, true)
|
|
|
|
|
|
|
|
self.assets.images["cursorpeak"]:draw(x, y - 24, 0, 1, 1, 7, 26)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return Cursor
|