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

145 lines
3.3 KiB
Lua

local Cursor = Object:extend()
local EmptyGrid = {
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00}
}
function Cursor:new(controller)
self.controller = controller
self.x = 1
self.y = 1
self.isActive = false
self.tx = 1
self.ty = 1
self.grid = EmptyGrid
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(type, x, y, value, whitelistedEntity)
self.grid = {
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00},
{00,00,00,00,00,00,00,00,00,00,00,00}
}
if type == "square" then
local x = x - value
local y = y - value
local value = (value * 2) + 1
for i=1, value do
for j=1, value do
local dx, dy
dx = x + i - 1
dy = y + j - 1
if (dy >= 1) and (dy <= 7) and (dx >= 1) and (dx <= 12) then
if ((self.controller.battlearena:getObjectInCase(dx, dy) == nil) or
(self.controller.battlearena:getObjectInCase(dx, dy) == whitelistedEntity) and (whitelistedEntity ~= nil)) and
(self.controller.battlearena:getTerrain(dx, dy) ~= 3) then
self.grid[dy][dx] = 1
end
end
end
end
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:resetGrid()
self.grid = EmptyGrid
end
function Cursor:unset()
self.isActive = false
end
function Cursor:update(dt)
if (self.isActive) then
local keys = self.controller.sources[1].keys
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.controller.actormanager:sendSignalToCurrentEntity()
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 = self.controller.battlearena:gridToPixel(self.tx, self.ty, true)
self.controller.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 = self.controller.battlearena:gridToPixel(self.tx, self.ty, true)
self.controller.assets.images["cursorpeak"]:draw(x, y - 24, 0, 1, 1, 7, 26)
end
end
return Cursor