sonic-radiance/sonic-radiance.love/scenes/battlesystem/controller/cursor.lua
2019-08-12 12:37:00 +02:00

166 lines
4 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.frame = 1
self.tx = 1
self.ty = 1
self.grid = EmptyGrid
self:loadRessources()
end
function Cursor:loadRessources()
self.texture = love.graphics.newImage("assets/gui/cursor.png")
self.texture2 = love.graphics.newImage("assets/gui/cursor2.png")
local w, h = self.texture:getDimensions()
self.sprite = {}
self.sprite[1] = love.graphics.newQuad(0, 0, 28, 11, w, h)
self.sprite[2] = love.graphics.newQuad(28, 0, 28, 11, w, h)
self.sprite[3] = love.graphics.newQuad(56, 0, 28, 11, w, h)
self.sprite[4] = love.graphics.newQuad(28, 0, 28, 11, w, h)
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
self.frame = self.frame + dt*4
if (self.frame >= 4) then
self.frame = 1
end
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)
frame = math.floor(self.frame)
love.graphics.draw(self.texture, self.sprite[frame], 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)
love.graphics.draw(self.texture2, x, y - 24, 0, 1, 1, 7, 26)
end
end
return Cursor