sonic-radiance/sonic-radiance.love/scenes/battlesystem/cursor.lua

143 lines
2.9 KiB
Lua
Raw Normal View History

local Cursor = Object:extend()
local maputils = require "scenes.battlesystem.utils"
function Cursor:new(world)
self.world = world
self.scene = world.scene
self.assets = world.assets
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)
if (keys["up"].isPressed) then
dy = math.max(self.y - 1, 1)
if (self.grid[dy][self.x] == 1) then
self.y = dy
end
end
if (keys["down"].isPressed) then
dy = math.min(self.y + 1, 7)
if (self.grid[dy][self.x] == 1) then
self.y = dy
end
end
if (keys["left"].isPressed) then
dx = math.max(self.x - 1, 1)
if (self.grid[self.y][dx] == 1) then
self.x = dx
end
end
if (keys["right"].isPressed) then
dx = math.min(self.x + 1, 12)
if (self.grid[self.y][dx] == 1) then
self.x = dx
end
end
if (keys["A"].isPressed) then
self.world:sendSignalToCurrentBattler()
end
self.tx = (self.tx) + ((self.x) - (self.tx)) * dt*30
self.ty = (self.ty) + ((self.y) - (self.ty)) * dt*30
-- test
--game
end
end
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